@matheusbsbs / matheus-bagliano / commits / 2cf67f1e35
Fork 0

git-apply: parse index information

Add an new option --show-index-info to git-apply command to summarize the index information new git-diff outputs. The command shows something similar to git-ls-files --stage output for the pre-change image: 100644 7be5041... apply.c 100644 ec2a161... cache.h ... Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 7, 2005 at 03:42 UTC 2cf67f1e35d3a2175e426b4b2841374e68105c2c
1 file changed +76 -1
apply.c
+76 -1
@@ -14,6 +14,7 @@
14 // files that are being modified, but doesn't apply the patch
15 // --stat does just a diffstat, and doesn't actually apply
16 // --show-files shows the directory changes
17 +// --show-index-info shows the old and new index info for paths if available.
18 //
19 static int check_index = 0;
20 static int write_index = 0;
@@ -22,8 +23,9 @@ static int summary = 0;
23 static int check = 0;
24 static int apply = 1;
25 static int show_files = 0;
26 +static int show_index_info = 0;
27 static const char apply_usage[] =
26 -"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] <patch>...";
28 +"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] <patch>...";
29
30 /*
31 * For "diff-stat" like behaviour, we keep track of the biggest change
@@ -56,6 +58,8 @@ struct patch {
58 struct fragment *fragments;
59 char *result;
60 unsigned long resultsize;
61 + char old_sha1_prefix[41];
62 + char new_sha1_prefix[41];
63 struct patch *next;
64 };
65
@@ -334,6 +338,38 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch)
338 return 0;
339 }
340
341 +static int gitdiff_index(const char *line, struct patch *patch)
342 +{
343 + /* index line is N hexadecimal, "..", N hexadecimal,
344 + * and optional space with octal mode.
345 + */
346 + const char *ptr, *eol;
347 + int len;
348 +
349 + ptr = strchr(line, '.');
350 + if (!ptr || ptr[1] != '.' || 40 <= ptr - line)
351 + return 0;
352 + len = ptr - line;
353 + memcpy(patch->old_sha1_prefix, line, len);
354 + patch->old_sha1_prefix[len] = 0;
355 +
356 + line = ptr + 2;
357 + ptr = strchr(line, ' ');
358 + eol = strchr(line, '\n');
359 +
360 + if (!ptr || eol < ptr)
361 + ptr = eol;
362 + len = ptr - line;
363 +
364 + if (40 <= len)
365 + return 0;
366 + memcpy(patch->new_sha1_prefix, line, len);
367 + patch->new_sha1_prefix[len] = 0;
368 + if (*ptr == ' ')
369 + patch->new_mode = patch->old_mode = strtoul(ptr+1, NULL, 8);
370 + return 0;
371 +}
372 +
373 /*
374 * This is normal for a diff that doesn't change anything: we'll fall through
375 * into the next diff. Tell the parser to break out.
@@ -438,6 +474,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch
474 { "rename to ", gitdiff_renamedst },
475 { "similarity index ", gitdiff_similarity },
476 { "dissimilarity index ", gitdiff_dissimilarity },
477 + { "index ", gitdiff_index },
478 { "", gitdiff_unrecognized },
479 };
480 int i;
@@ -1136,6 +1173,36 @@ static void show_file_list(struct patch *patch)
1173 }
1174 }
1175
1176 +static inline int is_null_sha1(const unsigned char *sha1)
1177 +{
1178 + return !memcmp(sha1, null_sha1, 20);
1179 +}
1180 +
1181 +static void show_index_list(struct patch *list)
1182 +{
1183 + struct patch *patch;
1184 +
1185 + /* Once we start supporting the reverse patch, it may be
1186 + * worth showing the new sha1 prefix, but until then...
1187 + */
1188 + for (patch = list; patch; patch = patch->next) {
1189 + const unsigned char *sha1_ptr;
1190 + unsigned char sha1[20];
1191 + const char *name;
1192 +
1193 + name = patch->old_name ? patch->old_name : patch->new_name;
1194 + if (patch->is_new)
1195 + sha1_ptr = null_sha1;
1196 + else if (get_sha1(patch->old_sha1_prefix, sha1))
1197 + die("sha1 information is lacking or useless (%s).",
1198 + name);
1199 + else
1200 + sha1_ptr = sha1;
1201 + printf("%06o %s %s\n",patch->old_mode,
1202 + sha1_to_hex(sha1_ptr), name);
1203 + }
1204 +}
1205 +
1206 static void stat_patch_list(struct patch *patch)
1207 {
1208 int files, adds, dels;
@@ -1476,6 +1543,9 @@ static int apply_patch(int fd)
1543 if (show_files)
1544 show_file_list(list);
1545
1546 + if (show_index_info)
1547 + show_index_list(list);
1548 +
1549 if (diffstat)
1550 stat_patch_list(list);
1551
@@ -1534,6 +1604,11 @@ int main(int argc, char **argv)
1604 show_files = 1;
1605 continue;
1606 }
1607 + if (!strcmp(arg, "--show-index-info")) {
1608 + apply = 0;
1609 + show_index_info = 1;
1610 + continue;
1611 + }
1612 fd = open(arg, O_RDONLY);
1613 if (fd < 0)
1614 usage(apply_usage);