5 #include "parse-options.h"
7 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
10 printf("%s %s\n", oid_to_hex(result
), oid_to_hex(id
));
13 static int remove_space(char *line
)
19 while ((c
= *src
++) != '\0') {
26 static int scan_hunk_header(const char *p
, int *p_before
, int *p_after
)
28 static const char digits
[] = "0123456789";
33 n
= strspn(q
, digits
);
37 n
= strspn(q
, digits
);
42 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
46 n
= strspn(r
, digits
);
50 n
= strspn(r
, digits
);
60 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
61 struct strbuf
*line_buf
, int stable
, int verbatim
)
63 int patchlen
= 0, found_next
= 0;
64 int before
= -1, after
= -1;
65 int diff_is_binary
= 0;
66 char pre_oid_str
[GIT_MAX_HEXSZ
+ 1], post_oid_str
[GIT_MAX_HEXSZ
+ 1];
69 the_hash_algo
->init_fn(&ctx
);
72 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
73 char *line
= line_buf
->buf
;
77 /* Possibly skip over the prefix added by "log" or "format-patch" */
78 if (!skip_prefix(line
, "commit ", &p
) &&
79 !skip_prefix(line
, "From ", &p
) &&
80 starts_with(line
, "\\ ") && 12 < strlen(line
)) {
82 the_hash_algo
->update_fn(&ctx
, line
, strlen(line
));
86 if (!get_oid_hex(p
, next_oid
)) {
91 /* Ignore commit comments */
92 if (!patchlen
&& !starts_with(line
, "diff "))
95 /* Parsing diff header? */
97 if (starts_with(line
, "GIT binary patch") ||
98 starts_with(line
, "Binary files")) {
101 the_hash_algo
->update_fn(&ctx
, pre_oid_str
,
102 strlen(pre_oid_str
));
103 the_hash_algo
->update_fn(&ctx
, post_oid_str
,
104 strlen(post_oid_str
));
106 flush_one_hunk(result
, &ctx
);
108 } else if (skip_prefix(line
, "index ", &p
)) {
109 char *oid1_end
= strstr(line
, "..");
110 char *oid2_end
= NULL
;
112 oid2_end
= strstr(oid1_end
, " ");
114 oid2_end
= line
+ strlen(line
) - 1;
115 if (oid1_end
!= NULL
&& oid2_end
!= NULL
) {
116 *oid1_end
= *oid2_end
= '\0';
117 strlcpy(pre_oid_str
, p
, GIT_MAX_HEXSZ
+ 1);
118 strlcpy(post_oid_str
, oid1_end
+ 2, GIT_MAX_HEXSZ
+ 1);
121 } else if (starts_with(line
, "--- "))
123 else if (!isalpha(line
[0]))
127 if (diff_is_binary
) {
128 if (starts_with(line
, "diff ")) {
135 /* Looking for a valid hunk header? */
136 if (before
== 0 && after
== 0) {
137 if (starts_with(line
, "@@ -")) {
138 /* Parse next hunk, but ignore line numbers. */
139 scan_hunk_header(line
, &before
, &after
);
143 /* Split at the end of the patch. */
144 if (!starts_with(line
, "diff "))
147 /* Else we're parsing another header. */
149 flush_one_hunk(result
, &ctx
);
153 /* If we get here, we're inside a hunk. */
154 if (line
[0] == '-' || line
[0] == ' ')
156 if (line
[0] == '+' || line
[0] == ' ')
159 /* Add line to hash algo (possibly removing whitespace) */
160 len
= verbatim
? strlen(line
) : remove_space(line
);
162 the_hash_algo
->update_fn(&ctx
, line
, len
);
168 flush_one_hunk(result
, &ctx
);
173 static void generate_id_list(int stable
, int verbatim
)
175 struct object_id oid
, n
, result
;
177 struct strbuf line_buf
= STRBUF_INIT
;
180 while (!feof(stdin
)) {
181 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
, verbatim
);
182 flush_current_id(patchlen
, &oid
, &result
);
185 strbuf_release(&line_buf
);
188 static const char *const patch_id_usage
[] = {
189 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
192 struct patch_id_opts
{
197 static int git_patch_id_config(const char *var
, const char *value
, void *cb
)
199 struct patch_id_opts
*opts
= cb
;
201 if (!strcmp(var
, "patchid.stable")) {
202 opts
->stable
= git_config_bool(var
, value
);
205 if (!strcmp(var
, "patchid.verbatim")) {
206 opts
->verbatim
= git_config_bool(var
, value
);
210 return git_default_config(var
, value
, cb
);
213 int cmd_patch_id(int argc
, const char **argv
, const char *prefix
)
215 /* if nothing is set, default to unstable */
216 struct patch_id_opts config
= {0, 0};
218 struct option builtin_patch_id_options
[] = {
219 OPT_CMDMODE(0, "unstable", &opts
,
220 N_("use the unstable patch-id algorithm"), 1),
221 OPT_CMDMODE(0, "stable", &opts
,
222 N_("use the stable patch-id algorithm"), 2),
223 OPT_CMDMODE(0, "verbatim", &opts
,
224 N_("don't strip whitespace from the patch"), 3),
228 git_config(git_patch_id_config
, &config
);
230 /* verbatim implies stable */
234 argc
= parse_options(argc
, argv
, prefix
, builtin_patch_id_options
,
237 generate_id_list(opts
? opts
> 1 : config
.stable
,
238 opts
? opts
== 3 : config
.verbatim
);