On 11/04/2016 08:13 PM, Ian Jackson wrote:
> Signed-off-by: Ian Jackson <ijack...@chiark.greenend.org.uk>
> ---
>  Documentation/git-check-ref-format.txt | 10 ++++++++--
>  builtin/check-ref-format.c             | 34 
> +++++++++++++++++++++++++++++++---
>  2 files changed, 39 insertions(+), 5 deletions(-)
> 
> [...]
> diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
> index 559d5c2..87f52fa 100644
> --- a/builtin/check-ref-format.c
> +++ b/builtin/check-ref-format.c
> @@ -76,6 +76,7 @@ static int check_one_ref_format(const char *refname)
>  int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
>  {
>       int i;
> +     int use_stdin = 0;
>  
>       if (argc == 2 && !strcmp(argv[1], "-h"))
>               usage(builtin_check_ref_format_usage);
> @@ -93,6 +94,8 @@ int cmd_check_ref_format(int argc, const char **argv, const 
> char *prefix)
>                       check_branch = 1;
>               else if (!strcmp(argv[i], "--report-errors"))
>                       report_errors = 1;
> +             else if (!strcmp(argv[i], "--stdin"))
> +                     use_stdin = 1;
>               else
>                       usage(builtin_check_ref_format_usage);
>       }
> @@ -100,8 +103,33 @@ int cmd_check_ref_format(int argc, const char **argv, 
> const char *prefix)
>       if (check_branch && (flags || normalize))
>               usage(builtin_check_ref_format_usage);
>  
> -     if (! (i == argc - 1))
> -             usage(builtin_check_ref_format_usage);
> +     if (!use_stdin) {
> +             if (! (i == argc - 1))
> +                     usage(builtin_check_ref_format_usage);
> +
> +             return check_one_ref_format(argv[i]);
> +     } else {
> +             char buffer[2048];
> +             int worst = 0;
>  
> -     return check_one_ref_format(argv[i]);
> +             if (! (i == argc))
> +                     usage(builtin_check_ref_format_usage);
> +
> +             while (fgets(buffer, sizeof(buffer), stdin)) {
> +                     char *newline = strchr(buffer, '\n');
> +                     if (!newline) {
> +                             fprintf(stderr, "%s --stdin: missing final 
> newline or line too long\n", *argv);
> +                             exit(127);
> +                     }
> +                     *newline = 0;
> +                     int got = check_one_ref_format(buffer);

Another minor point: project policy is not to mix declarations and code.
Please declare `got` at the top of the block.

> +                     if (got > worst)
> +                             worst = got;
> +             }
> +             if (!feof(stdin)) {
> +                     perror("reading from stdin");
> +                     exit(127);
> +             }
> +             return worst;
> +     }
>  }
> 

Michael

Reply via email to