4 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
11 memcpy(name
, oid_to_hex(id
), GIT_SHA1_HEXSZ
+ 1);
12 printf("%s %s\n", oid_to_hex(result
), name
);
15 static int remove_space(char *line
)
21 while ((c
= *src
++) != '\0') {
28 static int scan_hunk_header(const char *p
, int *p_before
, int *p_after
)
30 static const char digits
[] = "0123456789";
35 n
= strspn(q
, digits
);
38 n
= strspn(q
, digits
);
40 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
44 n
= strspn(r
, digits
);
47 n
= strspn(r
, digits
);
57 static void flush_one_hunk(struct object_id
*result
, git_SHA_CTX
*ctx
)
59 unsigned char hash
[GIT_MAX_RAWSZ
];
60 unsigned short carry
= 0;
63 git_SHA1_Final(hash
, ctx
);
65 /* 20-byte sum, with carry */
66 for (i
= 0; i
< GIT_SHA1_RAWSZ
; ++i
) {
67 carry
+= result
->hash
[i
] + hash
[i
];
68 result
->hash
[i
] = carry
;
73 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
74 struct strbuf
*line_buf
, int stable
)
76 int patchlen
= 0, found_next
= 0;
77 int before
= -1, after
= -1;
83 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
84 char *line
= line_buf
->buf
;
88 if (!skip_prefix(line
, "diff-tree ", &p
) &&
89 !skip_prefix(line
, "commit ", &p
) &&
90 !skip_prefix(line
, "From ", &p
) &&
91 starts_with(line
, "\\ ") && 12 < strlen(line
))
94 if (!get_oid_hex(p
, next_oid
)) {
99 /* Ignore commit comments */
100 if (!patchlen
&& !starts_with(line
, "diff "))
103 /* Parsing diff header? */
105 if (starts_with(line
, "index "))
107 else if (starts_with(line
, "--- "))
109 else if (!isalpha(line
[0]))
113 /* Looking for a valid hunk header? */
114 if (before
== 0 && after
== 0) {
115 if (starts_with(line
, "@@ -")) {
116 /* Parse next hunk, but ignore line numbers. */
117 scan_hunk_header(line
, &before
, &after
);
121 /* Split at the end of the patch. */
122 if (!starts_with(line
, "diff "))
125 /* Else we're parsing another header. */
127 flush_one_hunk(result
, &ctx
);
131 /* If we get here, we're inside a hunk. */
132 if (line
[0] == '-' || line
[0] == ' ')
134 if (line
[0] == '+' || line
[0] == ' ')
137 /* Compute the sha without whitespace */
138 len
= remove_space(line
);
140 git_SHA1_Update(&ctx
, line
, len
);
146 flush_one_hunk(result
, &ctx
);
151 static void generate_id_list(int stable
)
153 struct object_id oid
, n
, result
;
155 struct strbuf line_buf
= STRBUF_INIT
;
158 while (!feof(stdin
)) {
159 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
);
160 flush_current_id(patchlen
, &oid
, &result
);
163 strbuf_release(&line_buf
);
166 static const char patch_id_usage
[] = "git patch-id [--stable | --unstable]";
168 static int git_patch_id_config(const char *var
, const char *value
, void *cb
)
172 if (!strcmp(var
, "patchid.stable")) {
173 *stable
= git_config_bool(var
, value
);
177 return git_default_config(var
, value
, cb
);
180 int cmd_patch_id(int argc
, const char **argv
, const char *prefix
)
184 git_config(git_patch_id_config
, &stable
);
186 /* If nothing is set, default to unstable. */
190 if (argc
== 2 && !strcmp(argv
[1], "--stable"))
192 else if (argc
== 2 && !strcmp(argv
[1], "--unstable"))
195 usage(patch_id_usage
);
197 generate_id_list(stable
);