1 #define USE_THE_REPOSITORY_VARIABLE
8 #include "parse-options.h"
11 static void flush_current_id(int patchlen
, struct object_id
*id
, struct object_id
*result
)
14 printf("%s %s\n", oid_to_hex(result
), oid_to_hex(id
));
17 static int remove_space(char *line
)
23 while ((c
= *src
++) != '\0') {
30 static int scan_hunk_header(const char *p
, int *p_before
, int *p_after
)
32 static const char digits
[] = "0123456789";
37 n
= strspn(q
, digits
);
41 n
= strspn(q
, digits
);
46 if (n
== 0 || q
[n
] != ' ' || q
[n
+1] != '+')
50 n
= strspn(r
, digits
);
54 n
= strspn(r
, digits
);
64 static int get_one_patchid(struct object_id
*next_oid
, struct object_id
*result
,
65 struct strbuf
*line_buf
, int stable
, int verbatim
)
67 int patchlen
= 0, found_next
= 0;
68 int before
= -1, after
= -1;
69 int diff_is_binary
= 0;
70 char pre_oid_str
[GIT_MAX_HEXSZ
+ 1], post_oid_str
[GIT_MAX_HEXSZ
+ 1];
73 the_hash_algo
->init_fn(&ctx
);
74 oidclr(result
, the_repository
->hash_algo
);
76 while (strbuf_getwholeline(line_buf
, stdin
, '\n') != EOF
) {
77 char *line
= line_buf
->buf
;
81 /* Possibly skip over the prefix added by "log" or "format-patch" */
82 if (!skip_prefix(line
, "commit ", &p
) &&
83 !skip_prefix(line
, "From ", &p
) &&
84 starts_with(line
, "\\ ") && 12 < strlen(line
)) {
86 the_hash_algo
->update_fn(&ctx
, line
, strlen(line
));
90 if (!get_oid_hex(p
, next_oid
)) {
95 /* Ignore commit comments */
96 if (!patchlen
&& !starts_with(line
, "diff "))
99 /* Parsing diff header? */
101 if (starts_with(line
, "GIT binary patch") ||
102 starts_with(line
, "Binary files")) {
105 the_hash_algo
->update_fn(&ctx
, pre_oid_str
,
106 strlen(pre_oid_str
));
107 the_hash_algo
->update_fn(&ctx
, post_oid_str
,
108 strlen(post_oid_str
));
110 flush_one_hunk(result
, &ctx
);
112 } else if (skip_prefix(line
, "index ", &p
)) {
113 char *oid1_end
= strstr(line
, "..");
114 char *oid2_end
= NULL
;
116 oid2_end
= strstr(oid1_end
, " ");
118 oid2_end
= line
+ strlen(line
) - 1;
119 if (oid1_end
!= NULL
&& oid2_end
!= NULL
) {
120 *oid1_end
= *oid2_end
= '\0';
121 strlcpy(pre_oid_str
, p
, GIT_MAX_HEXSZ
+ 1);
122 strlcpy(post_oid_str
, oid1_end
+ 2, GIT_MAX_HEXSZ
+ 1);
125 } else if (starts_with(line
, "--- "))
127 else if (!isalpha(line
[0]))
131 if (diff_is_binary
) {
132 if (starts_with(line
, "diff ")) {
139 /* Looking for a valid hunk header? */
140 if (before
== 0 && after
== 0) {
141 if (starts_with(line
, "@@ -")) {
142 /* Parse next hunk, but ignore line numbers. */
143 scan_hunk_header(line
, &before
, &after
);
147 /* Split at the end of the patch. */
148 if (!starts_with(line
, "diff "))
151 /* Else we're parsing another header. */
153 flush_one_hunk(result
, &ctx
);
157 /* If we get here, we're inside a hunk. */
158 if (line
[0] == '-' || line
[0] == ' ')
160 if (line
[0] == '+' || line
[0] == ' ')
163 /* Add line to hash algo (possibly removing whitespace) */
164 len
= verbatim
? strlen(line
) : remove_space(line
);
166 the_hash_algo
->update_fn(&ctx
, line
, len
);
170 oidclr(next_oid
, the_repository
->hash_algo
);
172 flush_one_hunk(result
, &ctx
);
177 static void generate_id_list(int stable
, int verbatim
)
179 struct object_id oid
, n
, result
;
181 struct strbuf line_buf
= STRBUF_INIT
;
183 oidclr(&oid
, the_repository
->hash_algo
);
184 while (!feof(stdin
)) {
185 patchlen
= get_one_patchid(&n
, &result
, &line_buf
, stable
, verbatim
);
186 flush_current_id(patchlen
, &oid
, &result
);
189 strbuf_release(&line_buf
);
192 static const char *const patch_id_usage
[] = {
193 N_("git patch-id [--stable | --unstable | --verbatim]"), NULL
196 struct patch_id_opts
{
201 static int git_patch_id_config(const char *var
, const char *value
,
202 const struct config_context
*ctx
, void *cb
)
204 struct patch_id_opts
*opts
= cb
;
206 if (!strcmp(var
, "patchid.stable")) {
207 opts
->stable
= git_config_bool(var
, value
);
210 if (!strcmp(var
, "patchid.verbatim")) {
211 opts
->verbatim
= git_config_bool(var
, value
);
215 return git_default_config(var
, value
, ctx
, cb
);
218 int cmd_patch_id(int argc
,
221 struct repository
*repo UNUSED
)
223 /* if nothing is set, default to unstable */
224 struct patch_id_opts config
= {0, 0};
226 struct option builtin_patch_id_options
[] = {
227 OPT_CMDMODE(0, "unstable", &opts
,
228 N_("use the unstable patch-id algorithm"), 1),
229 OPT_CMDMODE(0, "stable", &opts
,
230 N_("use the stable patch-id algorithm"), 2),
231 OPT_CMDMODE(0, "verbatim", &opts
,
232 N_("don't strip whitespace from the patch"), 3),
236 git_config(git_patch_id_config
, &config
);
238 /* verbatim implies stable */
242 argc
= parse_options(argc
, argv
, prefix
, builtin_patch_id_options
,
246 * We rely on `the_hash_algo` to compute patch IDs. This is dubious as
247 * it means that the hash algorithm now depends on the object hash of
248 * the repository, even though git-patch-id(1) clearly defines that
249 * patch IDs always use SHA1.
251 * NEEDSWORK: This hack should be removed in favor of converting
252 * the code that computes patch IDs to always use SHA1.
255 repo_set_hash_algo(the_repository
, GIT_HASH_SHA1
);
257 generate_id_list(opts
? opts
> 1 : config
.stable
,
258 opts
? opts
== 3 : config
.verbatim
);