diff: --abbrev option
When I show transcripts to explain how something works, I often find myself hand-editing the diff-raw output to shorten various object names in the output. This adds --abbrev option to the diff family, which shortens diff-raw output and diff-tree commit id headers. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 13, 2005 at 17:21 UTC
47dd0d595d04ee5283dfd8a0b4cbd6e6de8ad57f
5 files changed
+93
-23
Documentation/diff-options.txt
+7
@@ -18,6 +18,13 @@
18
object name of pre- and post-image blob on the "index"
19
line when generating a patch format output.
20
21
+--abbrev::
22
+ Instead of showing the full 40-byte hexadecimal object
23
+ name in diff-raw format output and diff-tree header
24
+ lines, show only handful prefix. This is independent of
25
+ --full-index option above, which controls the diff-patch
26
+ output format.
27
+
28
-B::
29
Break complete rewrite changes into pairs of delete and create.
30
diff-tree.c
+24
-16
@@ -14,11 +14,6 @@ static enum cmit_fmt commit_format = CMIT_FMT_RAW;
14
15
static struct diff_options diff_options;
16
17
-static void call_diff_setup_done(void)
18
-{
19
- diff_setup_done(&diff_options);
20
-}
21
-
17
static int call_diff_flush(void)
18
{
19
diffcore_std(&diff_options);
@@ -43,7 +38,6 @@ static int diff_tree_sha1_top(const unsigned char *old,
38
{
39
int ret;
40
46
- call_diff_setup_done();
41
ret = diff_tree_sha1(old, new, base, &diff_options);
42
call_diff_flush();
43
return ret;
@@ -55,7 +49,6 @@ static int diff_root_tree(const unsigned char *new, const char *base)
49
void *tree;
50
struct tree_desc empty, real;
51
58
- call_diff_setup_done();
52
tree = read_object_with_reference(new, "tree", &real.size, NULL);
53
if (!tree)
54
die("unable to read root tree (%s)", sha1_to_hex(new));
@@ -69,18 +62,29 @@ static int diff_root_tree(const unsigned char *new, const char *base)
62
return retval;
63
}
64
72
-static const char *generate_header(const char *commit, const char *parent, const char *msg)
65
+static const char *generate_header(const unsigned char *commit_sha1,
66
+ const unsigned char *parent_sha1,
67
+ const char *msg)
68
{
69
static char this_header[16384];
70
int offset;
71
unsigned long len;
72
+ int abbrev = diff_options.abbrev;
73
74
if (!verbose_header)
79
- return commit;
75
+ return sha1_to_hex(commit_sha1);
76
77
len = strlen(msg);
82
- offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
83
- offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
78
+
79
+ offset = sprintf(this_header, "%s%s ",
80
+ header_prefix,
81
+ diff_unique_abbrev(commit_sha1, abbrev));
82
+ offset += sprintf(this_header + offset, "(from %s)\n",
83
+ parent_sha1 ?
84
+ diff_unique_abbrev(parent_sha1, abbrev) : "root");
85
+ offset += pretty_print_commit(commit_format, msg, len,
86
+ this_header + offset,
87
+ sizeof(this_header) - offset);
88
return this_header;
89
}
90
@@ -99,18 +103,18 @@ static int diff_tree_commit(const unsigned char *commit_sha1)
103
104
/* Root commit? */
105
if (show_root_diff && !commit->parents) {
102
- header = generate_header(name, "root", commit->buffer);
106
+ header = generate_header(sha1, NULL, commit->buffer);
107
diff_root_tree(commit_sha1, "");
108
}
109
110
/* More than one parent? */
111
if (ignore_merges && commit->parents && commit->parents->next)
108
- return 0;
112
+ return 0;
113
114
for (parents = commit->parents; parents; parents = parents->next) {
115
struct commit *parent = parents->item;
112
- header = generate_header(name,
113
- sha1_to_hex(parent->object.sha1),
116
+ header = generate_header(sha1,
117
+ parent->object.sha1,
118
commit->buffer);
119
diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
120
if (!header && verbose_header) {
@@ -129,6 +133,7 @@ static int diff_tree_stdin(char *line)
133
int len = strlen(line);
134
unsigned char commit[20], parent[20];
135
static char this_header[1000];
136
+ int abbrev = diff_options.abbrev;
137
138
if (!len || line[len-1] != '\n')
139
return -1;
@@ -138,7 +143,9 @@ static int diff_tree_stdin(char *line)
143
if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
144
line[40] = 0;
145
line[81] = 0;
141
- sprintf(this_header, "%s (from %s)\n", line, line+41);
146
+ sprintf(this_header, "%s (from %s)\n",
147
+ diff_unique_abbrev(commit, abbrev),
148
+ diff_unique_abbrev(parent, abbrev));
149
header = this_header;
150
return diff_tree_sha1_top(parent, commit, "");
151
}
@@ -239,6 +246,7 @@ int main(int argc, const char **argv)
246
diff_options.recursive = 1;
247
248
diff_tree_setup_paths(get_pathspec(prefix, argv));
249
+ diff_setup_done(&diff_options);
250
251
switch (nr_sha1) {
252
case 0:
diff.c
+51
-6
@@ -723,11 +723,13 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
723
724
if (memcmp(one->sha1, two->sha1, 20)) {
725
char one_sha1[41];
726
- const char *index_fmt = o->full_index ? "index %s..%s" : "index %.7s..%.7s";
726
+ int abbrev = o->full_index ? 40 : DIFF_DEFAULT_INDEX_ABBREV;
727
memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
728
729
len += snprintf(msg + len, sizeof(msg) - len,
730
- index_fmt, one_sha1, sha1_to_hex(two->sha1));
730
+ "index %.*s..%.*s",
731
+ abbrev, one_sha1, abbrev,
732
+ sha1_to_hex(two->sha1));
733
if (one->mode == two->mode)
734
len += snprintf(msg + len, sizeof(msg) - len,
735
" %06o", one->mode);
@@ -791,6 +793,8 @@ int diff_setup_done(struct diff_options *options)
793
}
794
if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
795
use_size_cache = 1;
796
+ if (options->abbrev <= 0 || 40 < options->abbrev)
797
+ options->abbrev = 40; /* full */
798
799
return 0;
800
}
@@ -841,6 +845,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
845
}
846
else if (!strcmp(arg, "--find-copies-harder"))
847
options->find_copies_harder = 1;
848
+ else if (!strcmp(arg, "--abbrev"))
849
+ options->abbrev = DIFF_DEFAULT_ABBREV;
850
+ else if (!strncmp(arg, "--abbrev=", 9))
851
+ options->abbrev = strtoul(arg + 9, NULL, 10);
852
else
853
return 0;
854
return 1;
@@ -947,14 +955,49 @@ void diff_free_filepair(struct diff_filepair *p)
955
free(p);
956
}
957
958
+/* This is different from find_unique_abbrev() in that
959
+ * it needs to deal with 0{40} SHA1.
960
+ */
961
+const char *diff_unique_abbrev(const unsigned char *sha1, int len)
962
+{
963
+ int abblen;
964
+ const char *abbrev;
965
+ if (len == 40)
966
+ return sha1_to_hex(sha1);
967
+
968
+ abbrev = find_unique_abbrev(sha1, len);
969
+ if (!abbrev) {
970
+ if (!memcmp(sha1, null_sha1, 20)) {
971
+ char *buf = sha1_to_hex(null_sha1);
972
+ if (len < 37)
973
+ strcpy(buf + len, "...");
974
+ return buf;
975
+ }
976
+ else
977
+ return sha1_to_hex(sha1);
978
+ }
979
+ abblen = strlen(abbrev);
980
+ if (abblen < 37) {
981
+ static char hex[41];
982
+ if (len < abblen && abblen <= len + 2)
983
+ sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
984
+ else
985
+ sprintf(hex, "%s...", abbrev);
986
+ return hex;
987
+ }
988
+ return sha1_to_hex(sha1);
989
+}
990
+
991
static void diff_flush_raw(struct diff_filepair *p,
992
int line_termination,
993
int inter_name_termination,
953
- int output_format)
994
+ struct diff_options *options)
995
{
996
int two_paths;
997
char status[10];
998
+ int abbrev = options->abbrev;
999
const char *path_one, *path_two;
1000
+ int output_format = options->output_format;
1001
1002
path_one = p->one->path;
1003
path_two = p->two->path;
@@ -985,8 +1028,10 @@ static void diff_flush_raw(struct diff_filepair *p,
1028
}
1029
if (output_format != DIFF_FORMAT_NAME_STATUS) {
1030
printf(":%06o %06o %s ",
988
- p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
989
- printf("%s ", sha1_to_hex(p->two->sha1));
1031
+ p->one->mode, p->two->mode,
1032
+ diff_unique_abbrev(p->one->sha1, abbrev));
1033
+ printf("%s ",
1034
+ diff_unique_abbrev(p->two->sha1, abbrev));
1035
}
1036
printf("%s%c%s", status, inter_name_termination, path_one);
1037
if (two_paths)
@@ -1194,7 +1239,7 @@ void diff_flush(struct diff_options *options)
1239
case DIFF_FORMAT_NAME_STATUS:
1240
diff_flush_raw(p, line_termination,
1241
inter_name_termination,
1197
- diff_output_format);
1242
+ options);
1243
break;
1244
case DIFF_FORMAT_NAME:
1245
diff_flush_name(p,
diff.h
+8
-1
@@ -44,6 +44,7 @@ struct diff_options {
44
int reverse_diff;
45
int rename_limit;
46
int setup;
47
+ int abbrev;
48
49
change_fn_t change;
50
add_remove_fn_t add_remove;
@@ -87,6 +88,9 @@ extern int diff_setup_done(struct diff_options *);
88
89
#define DIFF_PICKAXE_ALL 1
90
91
+#define DIFF_DEFAULT_INDEX_ABBREV 7 /* hex digits */
92
+#define DIFF_DEFAULT_ABBREV 7 /* hex digits */
93
+
94
extern void diffcore_std(struct diff_options *);
95
96
extern void diffcore_std_no_resolve(struct diff_options *);
@@ -98,7 +102,8 @@ extern void diffcore_std_no_resolve(struct diff_options *);
102
" -u synonym for -p.\n" \
103
" --name-only show only names of changed files.\n" \
104
" --name-status show names and status of changed files.\n" \
101
-" --full-index show full object name on index ines.\n" \
105
+" --full-index show full object name on index lines.\n" \
106
+" --abbrev abbreviate object names in diff-tree header and diff-raw.\n" \
107
" -R swap input file pairs.\n" \
108
" -B detect complete rewrites.\n" \
109
" -M detect renames.\n" \
@@ -137,4 +142,6 @@ extern void diff_flush(struct diff_options*);
142
#define DIFF_STATUS_FILTER_AON '*'
143
#define DIFF_STATUS_FILTER_BROKEN 'B'
144
145
+extern const char *diff_unique_abbrev(const unsigned char *, int);
146
+
147
#endif /* DIFF_H */
sha1_name.c
+3
@@ -188,7 +188,10 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)
188
{
189
int status;
190
static char hex[41];
191
+
192
memcpy(hex, sha1_to_hex(sha1), 40);
193
+ if (len == 40)
194
+ return hex;
195
while (len < 40) {
196
unsigned char sha1_ret[20];
197
status = get_short_sha1(hex, len, sha1_ret, 1);