chiark / gitweb /
stunt is abolished
[version-charset-test.git] / 0005-check-ref-format-New-stdin-option.patch
1 From f22917e0bd9347ed547e124738083d5b6889da0a Mon Sep 17 00:00:00 2001
2 From: Ian Jackson <ijackson@chiark.greenend.org.uk>
3 Date: Fri, 4 Nov 2016 17:41:15 +0000
4 Subject: [PATCH 5/5] check-ref-format: New --stdin option
5
6 Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
7 ---
8  Documentation/git-check-ref-format.txt | 10 ++++++++--
9  builtin/check-ref-format.c             | 34 +++++++++++++++++++++++++++++++---
10  2 files changed, 39 insertions(+), 5 deletions(-)
11
12 diff --git a/Documentation/git-check-ref-format.txt b/Documentation/git-check-ref-format.txt
13 index e9a2657..5a213ce 100644
14 --- a/Documentation/git-check-ref-format.txt
15 +++ b/Documentation/git-check-ref-format.txt
16 @@ -10,8 +10,9 @@ SYNOPSIS
17  [verse]
18  'git check-ref-format' [--report-errors] [--normalize]
19         [--[no-]allow-onelevel] [--refspec-pattern]
20 -       <refname>
21 -'git check-ref-format' [--report-errors] --branch <branchname-shorthand>
22 +       <refname> | --stdin
23 +'git check-ref-format' [--report-errors] --branch
24 +       <branchname-shorthand> | --stdin
25  
26  DESCRIPTION
27  -----------
28 @@ -109,6 +110,11 @@ OPTIONS
29         If any ref does not check OK, print a message to stderr.
30          (By default, git check-ref-format is silent.)
31  
32 +--stdin::
33 +       Instead of checking on ref supplied on the command line,
34 +       read refs, one per line, from stdin.  The exit status is
35 +       0 if all the refs were OK.
36 +
37  
38  EXAMPLES
39  --------
40 diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
41 index 559d5c2..87f52fa 100644
42 --- a/builtin/check-ref-format.c
43 +++ b/builtin/check-ref-format.c
44 @@ -76,6 +76,7 @@ static int check_one_ref_format(const char *refname)
45  int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
46  {
47         int i;
48 +       int use_stdin = 0;
49  
50         if (argc == 2 && !strcmp(argv[1], "-h"))
51                 usage(builtin_check_ref_format_usage);
52 @@ -93,6 +94,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
53                         check_branch = 1;
54                 else if (!strcmp(argv[i], "--report-errors"))
55                         report_errors = 1;
56 +               else if (!strcmp(argv[i], "--stdin"))
57 +                       use_stdin = 1;
58                 else
59                         usage(builtin_check_ref_format_usage);
60         }
61 @@ -100,8 +103,33 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
62         if (check_branch && (flags || normalize))
63                 usage(builtin_check_ref_format_usage);
64  
65 -       if (! (i == argc - 1))
66 -               usage(builtin_check_ref_format_usage);
67 +       if (!use_stdin) {
68 +               if (! (i == argc - 1))
69 +                       usage(builtin_check_ref_format_usage);
70 +
71 +               return check_one_ref_format(argv[i]);
72 +       } else {
73 +               char buffer[2048];
74 +               int worst = 0;
75  
76 -       return check_one_ref_format(argv[i]);
77 +               if (! (i == argc))
78 +                       usage(builtin_check_ref_format_usage);
79 +
80 +               while (fgets(buffer, sizeof(buffer), stdin)) {
81 +                       char *newline = strchr(buffer, '\n');
82 +                       if (!newline) {
83 +                               fprintf(stderr, "%s --stdin: missing final newline or line too long\n", *argv);
84 +                               exit(127);
85 +                       }
86 +                       *newline = 0;
87 +                       int got = check_one_ref_format(buffer);
88 +                       if (got > worst)
89 +                               worst = got;
90 +               }
91 +               if (!feof(stdin)) {
92 +                       perror("reading from stdin");
93 +                       exit(127);
94 +               }
95 +               return worst;
96 +       }
97  }
98 -- 
99 2.10.1
100