@matheusbsbs / matheus-bagliano / commits / f604652e05
Fork 0

git-diff --numstat -z: make it machine readable

The "-z" format is all about machine parsability, but showing renamed paths as "common/{a => b}/suffix" makes it impossible. The scripts would never have successfully parsed "--numstat -z -M" in the old format. This fixes the output format in a (hopefully minimally) backward incompatible way. * The output without -z is not changed. This has given a good way for humans to view added and deleted lines separately, and showing the path in combined, shorter way would preserve readability. * The output with -z is unchanged for paths that do not involve renames. Existing scripts that do not pass -M/-C are not affected at all. * The output with -z for a renamed path is shown in a format that can easily be distinguished from an unrenamed path. This is based on Jakub Narebski's patch. Bugs and documentation typos are mine. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 11, 2007 at 23:46 UTC f604652e05073aaef6d83e83b5d6499b55bb6dfd
2 files changed +129 -32
Documentation/diff-format.txt
+61
@@ -84,3 +84,64 @@ all parents.
84
85
86 include::diff-generate-patch.txt[]
87 +
88 +
89 +other diff formats
90 +------------------
91 +
92 +The `--summary` option describes newly added, deleted, renamed and
93 +copied files. The `--stat` option adds diffstat(1) graph to the
94 +output. These options can be combined with other options, such as
95 +`-p`, and are meant for human consumption.
96 +
97 +When showing a change that involves a rename or a copy, `--stat` output
98 +formats the pathnames compactly by combining common prefix and suffix of
99 +the pathnames. For example, a change that moves `arch/i386/Makefile` to
100 +`arch/x86/Makefile` while modifying 4 lines will be shown like this:
101 +
102 +------------------------------------
103 +arch/{i386 => x86}/Makefile | 4 +--
104 +------------------------------------
105 +
106 +The `--numstat` option gives the diffstat(1) information but is designed
107 +for easier machine consumption. An entry in `--numstat` output looks
108 +like this:
109 +
110 +----------------------------------------
111 +1 2 README
112 +3 1 arch/{i386 => x86}/Makefile
113 +----------------------------------------
114 +
115 +That is, from left to right:
116 +
117 +. the number of added lines;
118 +. a tab;
119 +. the number of deleted lines;
120 +. a tab;
121 +. pathname (possibly with rename/copy information);
122 +. a newline.
123 +
124 +When `-z` output option is in effect, the output is formatted this way:
125 +
126 +----------------------------------------
127 +1 2 README NUL
128 +3 1 NUL arch/i386/Makefile NUL arch/x86/Makefile NUL
129 +----------------------------------------
130 +
131 +That is:
132 +
133 +. the number of added lines;
134 +. a tab;
135 +. the number of deleted lines;
136 +. a tab;
137 +. a NUL (only exists if renamed/copied);
138 +. pathname in preimage;
139 +. a NUL (only exists if renamed/copied);
140 +. pathname in postimage (only exists if renamed/copied);
141 +. a NUL.
142 +
143 +The extra `NUL` before the preimage path in renamed case is to allow
144 +scripts that read the output to tell if the current record being read is
145 +a single-path record or a rename/copy record without reading ahead.
146 +After reading added and deleted lines, reading up to `NUL` would yield
147 +the pathname, but if that is `NUL`, the record will show two paths.
diff.c
+68 -32
@@ -734,7 +734,9 @@ struct diffstat_t {
734 int nr;
735 int alloc;
736 struct diffstat_file {
737 + char *from_name;
738 char *name;
739 + char *print_name;
740 unsigned is_unmerged:1;
741 unsigned is_binary:1;
742 unsigned is_renamed:1;
@@ -755,11 +757,14 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
757 }
758 diffstat->files[diffstat->nr++] = x;
759 if (name_b) {
758 - x->name = pprint_rename(name_a, name_b);
760 + x->from_name = xstrdup(name_a);
761 + x->name = xstrdup(name_b);
762 x->is_renamed = 1;
763 }
761 - else
764 + else {
765 + x->from_name = NULL;
766 x->name = xstrdup(name_a);
767 + }
768 return x;
769 }
770
@@ -803,6 +808,28 @@ static void show_graph(char ch, int cnt, const char *set, const char *reset)
808 printf("%s", reset);
809 }
810
811 +static void fill_print_name(struct diffstat_file *file)
812 +{
813 + char *pname;
814 +
815 + if (file->print_name)
816 + return;
817 +
818 + if (!file->is_renamed) {
819 + struct strbuf buf;
820 + strbuf_init(&buf, 0);
821 + if (quote_c_style(file->name, &buf, NULL, 0)) {
822 + pname = strbuf_detach(&buf, NULL);
823 + } else {
824 + pname = file->name;
825 + strbuf_release(&buf);
826 + }
827 + } else {
828 + pname = pprint_rename(file->from_name, file->name);
829 + }
830 + file->print_name = pname;
831 +}
832 +
833 static void show_stats(struct diffstat_t* data, struct diff_options *options)
834 {
835 int i, len, add, del, total, adds = 0, dels = 0;
@@ -836,19 +863,8 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
863 for (i = 0; i < data->nr; i++) {
864 struct diffstat_file *file = data->files[i];
865 int change = file->added + file->deleted;
839 -
840 - if (!file->is_renamed) { /* renames are already quoted by pprint_rename */
841 - struct strbuf buf;
842 - strbuf_init(&buf, 0);
843 - if (quote_c_style(file->name, &buf, NULL, 0)) {
844 - free(file->name);
845 - file->name = strbuf_detach(&buf, NULL);
846 - } else {
847 - strbuf_release(&buf);
848 - }
849 - }
850 -
851 - len = strlen(file->name);
866 + fill_print_name(file);
867 + len = strlen(file->print_name);
868 if (max_len < len)
869 max_len = len;
870
@@ -873,7 +889,7 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
889
890 for (i = 0; i < data->nr; i++) {
891 const char *prefix = "";
876 - char *name = data->files[i]->name;
892 + char *name = data->files[i]->print_name;
893 int added = data->files[i]->added;
894 int deleted = data->files[i]->deleted;
895 int name_len;
@@ -901,17 +917,17 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
917 printf("%s%d%s", add_c, added, reset);
918 printf(" bytes");
919 printf("\n");
904 - goto free_diffstat_file;
920 + continue;
921 }
922 else if (data->files[i]->is_unmerged) {
923 show_name(prefix, name, len, reset, set);
924 printf(" Unmerged\n");
909 - goto free_diffstat_file;
925 + continue;
926 }
927 else if (!data->files[i]->is_renamed &&
928 (added + deleted == 0)) {
929 total_files--;
914 - goto free_diffstat_file;
930 + continue;
931 }
932
933 /*
@@ -933,11 +949,7 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
949 show_graph('+', add, add_c, reset);
950 show_graph('-', del, del_c, reset);
951 putchar('\n');
936 - free_diffstat_file:
937 - free(data->files[i]->name);
938 - free(data->files[i]);
952 }
940 - free(data->files);
953 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
954 set, total_files, adds, dels, reset);
955 }
@@ -962,11 +974,7 @@ static void show_shortstats(struct diffstat_t* data)
974 dels += deleted;
975 }
976 }
965 - free(data->files[i]->name);
966 - free(data->files[i]);
977 }
968 - free(data->files);
969 -
978 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
979 total_files, adds, dels);
980 }
@@ -975,6 +983,9 @@ static void show_numstat(struct diffstat_t* data, struct diff_options *options)
983 {
984 int i;
985
986 + if (data->nr == 0)
987 + return;
988 +
989 for (i = 0; i < data->nr; i++) {
990 struct diffstat_file *file = data->files[i];
991
@@ -982,15 +993,39 @@ static void show_numstat(struct diffstat_t* data, struct diff_options *options)
993 printf("-\t-\t");
994 else
995 printf("%d\t%d\t", file->added, file->deleted);
985 - if (!file->is_renamed) {
986 - write_name_quoted(file->name, stdout, options->line_termination);
996 + if (options->line_termination) {
997 + fill_print_name(file);
998 + if (!file->is_renamed)
999 + write_name_quoted(file->name, stdout,
1000 + options->line_termination);
1001 + else {
1002 + fputs(file->print_name, stdout);
1003 + putchar(options->line_termination);
1004 + }
1005 } else {
988 - fputs(file->name, stdout);
989 - putchar(options->line_termination);
1006 + if (file->is_renamed) {
1007 + putchar('\0');
1008 + write_name_quoted(file->from_name, stdout, '\0');
1009 + }
1010 + write_name_quoted(file->name, stdout, '\0');
1011 }
1012 }
1013 }
1014
1015 +static void free_diffstat_info(struct diffstat_t *diffstat)
1016 +{
1017 + int i;
1018 + for (i = 0; i < diffstat->nr; i++) {
1019 + struct diffstat_file *f = diffstat->files[i];
1020 + if (f->name != f->print_name)
1021 + free(f->print_name);
1022 + free(f->name);
1023 + free(f->from_name);
1024 + free(f);
1025 + }
1026 + free(diffstat->files);
1027 +}
1028 +
1029 struct checkdiff_t {
1030 struct xdiff_emit_state xm;
1031 const char *filename;
@@ -2943,8 +2978,9 @@ void diff_flush(struct diff_options *options)
2978 show_numstat(&diffstat, options);
2979 if (output_format & DIFF_FORMAT_DIFFSTAT)
2980 show_stats(&diffstat, options);
2946 - else if (output_format & DIFF_FORMAT_SHORTSTAT)
2981 + if (output_format & DIFF_FORMAT_SHORTSTAT)
2982 show_shortstats(&diffstat);
2983 + free_diffstat_info(&diffstat);
2984 separator++;
2985 }
2986