6 #include "parse-options.h"
8 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
11 printf("%s %s\n", oid_to_hex(result
), oid_to_hex(id
));
14 static int remove_space(char *line
)
20 while ((c
= *src
++) != '\0') {
27 static int scan_hunk_header(const char *p
, int *p_before
, int *p_after
)
29 static const char digits
[] = "0123456789";
34 n
= strspn(q
, digits
);
38 n
= strspn(q
, digits
);
43 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
47 n
= strspn(r
, digits
);
51 n
= strspn(r
, digits
);
61 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
62 struct strbuf
*line_buf
, int stable
, int verbatim
)
64 int patchlen
= 0, found_next
= 0;
65 int before
= -1, after
= -1;
66 int diff_is_binary
= 0;
67 char pre_oid_str
[GIT_MAX_HEXSZ
+ 1], post_oid_str
[GIT_MAX_HEXSZ
+ 1];
70 the_hash_algo
->init_fn(&ctx
);
73 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
74 char *line
= line_buf
->buf
;
78 /* Possibly skip over the prefix added by "log" or "format-patch" */
79 if (!skip_prefix(line
, "commit ", &p
) &&
80 !skip_prefix(line
, "From ", &p
) &&
81 starts_with(line
, "\\ ") && 12 < strlen(line
)) {
83 the_hash_algo
->update_fn(&ctx
, line
, strlen(line
));
87 if (!get_oid_hex(p
, next_oid
)) {
92 /* Ignore commit comments */
93 if (!patchlen
&& !starts_with(line
, "diff "))
96 /* Parsing diff header? */
98 if (starts_with(line
, "GIT binary patch") ||
99 starts_with(line
, "Binary files")) {
102 the_hash_algo
->update_fn(&ctx
, pre_oid_str
,
103 strlen(pre_oid_str
));
104 the_hash_algo
->update_fn(&ctx
, post_oid_str
,
105 strlen(post_oid_str
));
107 flush_one_hunk(result
, &ctx
);
109 } else if (skip_prefix(line
, "index ", &p
)) {
110 char *oid1_end
= strstr(line
, "..");
111 char *oid2_end
= NULL
;
113 oid2_end
= strstr(oid1_end
, " ");
115 oid2_end
= line
+ strlen(line
) - 1;
116 if (oid1_end
!= NULL
&& oid2_end
!= NULL
) {
117 *oid1_end
= *oid2_end
= '\0';
118 strlcpy(pre_oid_str
, p
, GIT_MAX_HEXSZ
+ 1);
119 strlcpy(post_oid_str
, oid1_end
+ 2, GIT_MAX_HEXSZ
+ 1);
122 } else if (starts_with(line
, "--- "))
124 else if (!isalpha(line
[0]))
128 if (diff_is_binary
) {
129 if (starts_with(line
, "diff ")) {
136 /* Looking for a valid hunk header? */
137 if (before
== 0 && after
== 0) {
138 if (starts_with(line
, "@@ -")) {
139 /* Parse next hunk, but ignore line numbers. */
140 scan_hunk_header(line
, &before
, &after
);
144 /* Split at the end of the patch. */
145 if (!starts_with(line
, "diff "))
148 /* Else we're parsing another header. */
150 flush_one_hunk(result
, &ctx
);
154 /* If we get here, we're inside a hunk. */
155 if (line
[0] == '-' || line
[0] == ' ')
157 if (line
[0] == '+' || line
[0] == ' ')
160 /* Add line to hash algo (possibly removing whitespace) */
161 len
= verbatim
? strlen(line
) : remove_space(line
);
163 the_hash_algo
->update_fn(&ctx
, line
, len
);
169 flush_one_hunk(result
, &ctx
);
174 static void generate_id_list(int stable
, int verbatim
)
176 struct object_id oid
, n
, result
;
178 struct strbuf line_buf
= STRBUF_INIT
;
181 while (!feof(stdin
)) {
182 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
, verbatim
);
183 flush_current_id(patchlen
, &oid
, &result
);
186 strbuf_release(&line_buf
);
189 static const char *const patch_id_usage
[] = {
190 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
193 struct patch_id_opts
{
198 static int git_patch_id_config(const char *var
, const char *value
, void *cb
)
200 struct patch_id_opts
*opts
= cb
;
202 if (!strcmp(var
, "patchid.stable")) {
203 opts
->stable
= git_config_bool(var
, value
);
206 if (!strcmp(var
, "patchid.verbatim")) {
207 opts
->verbatim
= git_config_bool(var
, value
);
211 return git_default_config(var
, value
, cb
);
214 int cmd_patch_id(int argc
, const char **argv
, const char *prefix
)
216 /* if nothing is set, default to unstable */
217 struct patch_id_opts config
= {0, 0};
219 struct option builtin_patch_id_options
[] = {
220 OPT_CMDMODE(0, "unstable", &opts
,
221 N_("use the unstable patch-id algorithm"), 1),
222 OPT_CMDMODE(0, "stable", &opts
,
223 N_("use the stable patch-id algorithm"), 2),
224 OPT_CMDMODE(0, "verbatim", &opts
,
225 N_("don't strip whitespace from the patch"), 3),
229 git_config(git_patch_id_config
, &config
);
231 /* verbatim implies stable */
235 argc
= parse_options(argc
, argv
, prefix
, builtin_patch_id_options
,
238 generate_id_list(opts
? opts
> 1 : config
.stable
,
239 opts
? opts
== 3 : config
.verbatim
);