show-branch: optionally use unique prefix as name.
git-show-branch acquires two new options. --sha1-name to name commits using the unique prefix of their object names, and --no-name to not to show names at all. This was outlined in <7vk6gpyuyr.fsf@assigned-by-dhcp.cox.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 11, 2005 at 15:22 UTC
013f276eb78967f9742654ebde303c2fbe7a6cd6
4 files changed
+82
-44
Documentation/git-show-branch.txt
+10
-16
@@ -7,7 +7,7 @@ git-show-branch - Show branches and their commits.
7
8
SYNOPSIS
9
--------
10
-'git-show-branch [--all] [--heads] [--tags] [--more=<n> | --list | --independent | --merge-base] <reference>...'
10
+'git-show-branch [--all] [--heads] [--tags] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] <reference>...'
11
12
DESCRIPTION
13
-----------
@@ -44,6 +44,15 @@ OPTIONS
44
Among the <reference>s given, display only the ones that
45
cannot be reached from any other <reference>.
46
47
+--no-name::
48
+ Do not show naming strings for each commit.
49
+
50
+--sha1-name::
51
+ Instead of naming the commits using the path to reach
52
+ them from heads (e.g. "master~2" to mean the grandparent
53
+ of "master"), name them with the unique prefix of their
54
+ object names.
55
+
56
Note that --more, --list, --independent and --merge-base options
57
are mutually exclusive.
58
@@ -88,21 +97,6 @@ whose commit message is "Add 'git show-branch'. "fixes" branch
97
adds one commit 'Introduce "reset type"'. "mhf" branch has many
98
other commits.
99
91
-When only one head is given, the output format changes slightly
92
-to conserve space. The '+' sign to show which commit is
93
-reachable from which head and the first N lines to show the list
94
-of heads being displayed are both meaningless so they are
95
-omitted. Also the label given to each commit does not repeat
96
-the name of the branch because it is obvious.
97
-
98
-------------------------------------------------
99
-$ git show-branch --more=4 master
100
-[master] Add 'git show-branch'.
101
-[~1] Add a new extended SHA1 syntax <name>~<num>
102
-[~2] Fix "git-diff A B"
103
-[~3] git-ls-files: generalized pathspecs
104
-[~4] Make "git-ls-files" work in subdirectories
105
-------------------------------------------------
100
101
Author
102
------
cache.h
+1
@@ -194,6 +194,7 @@ extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)
194
extern char *sha1_file_name(const unsigned char *sha1);
195
extern char *sha1_pack_name(const unsigned char *sha1);
196
extern char *sha1_pack_index_name(const unsigned char *sha1);
197
+extern const char *find_unique_abbrev(const unsigned char *sha1, int);
198
extern const unsigned char null_sha1[20];
199
200
int git_mkstemp(char *path, size_t n, const char *template);
sha1_name.c
+33
-7
@@ -119,6 +119,9 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
119
return found;
120
}
121
122
+#define SHORT_NAME_NOT_FOUND (-1)
123
+#define SHORT_NAME_AMBIGUOUS (-2)
124
+
125
static int find_unique_short_object(int len, char *canonical,
126
unsigned char *res, unsigned char *sha1)
127
{
@@ -128,23 +131,24 @@ static int find_unique_short_object(int len, char *canonical,
131
has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
132
has_packed = find_short_packed_object(len, res, packed_sha1);
133
if (!has_unpacked && !has_packed)
131
- return -1;
134
+ return SHORT_NAME_NOT_FOUND;
135
if (1 < has_unpacked || 1 < has_packed)
133
- return error("short SHA1 %.*s is ambiguous.", len, canonical);
136
+ return SHORT_NAME_AMBIGUOUS;
137
if (has_unpacked != has_packed) {
138
memcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1), 20);
139
return 0;
140
}
141
/* Both have unique ones -- do they match? */
142
if (memcmp(packed_sha1, unpacked_sha1, 20))
140
- return error("short SHA1 %.*s is ambiguous.", len, canonical);
143
+ return -2;
144
memcpy(sha1, packed_sha1, 20);
145
return 0;
146
}
147
145
-static int get_short_sha1(const char *name, int len, unsigned char *sha1)
148
+static int get_short_sha1(const char *name, int len, unsigned char *sha1,
149
+ int quietly)
150
{
147
- int i;
151
+ int i, status;
152
char canonical[40];
153
unsigned char res[20];
154
@@ -171,7 +175,29 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1)
175
res[i >> 1] |= val;
176
}
177
174
- return find_unique_short_object(i, canonical, res, sha1);
178
+ status = find_unique_short_object(i, canonical, res, sha1);
179
+ if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
180
+ return error("short SHA1 %.*s is ambiguous.", len, canonical);
181
+ return status;
182
+}
183
+
184
+const char *find_unique_abbrev(const unsigned char *sha1, int len)
185
+{
186
+ int status;
187
+ static char hex[41];
188
+ memcpy(hex, sha1_to_hex(sha1), 40);
189
+ while (len < 40) {
190
+ unsigned char sha1_ret[20];
191
+ status = get_short_sha1(hex, len, sha1_ret, 1);
192
+ if (!status) {
193
+ hex[len] = 0;
194
+ return hex;
195
+ }
196
+ if (status != SHORT_NAME_AMBIGUOUS)
197
+ return NULL;
198
+ len++;
199
+ }
200
+ return NULL;
201
}
202
203
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
@@ -292,7 +318,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
318
ret = get_sha1_basic(name, len, sha1);
319
if (!ret)
320
return 0;
295
- return get_short_sha1(name, len, sha1);
321
+ return get_short_sha1(name, len, sha1, 0);
322
}
323
324
/*
show-branch.c
+38
-21
@@ -133,25 +133,28 @@ static void name_commits(struct commit_list *list,
133
nth = 0;
134
while (parents) {
135
struct commit *p = parents->item;
136
- char newname[1000];
136
+ char newname[1000], *en;
137
parents = parents->next;
138
nth++;
139
if (p->object.util)
140
continue;
141
+ en = newname;
142
switch (n->generation) {
143
case 0:
143
- sprintf(newname, "%s^%d",
144
- n->head_name, nth);
144
+ en += sprintf(en, "%s", n->head_name);
145
break;
146
case 1:
147
- sprintf(newname, "%s^^%d",
148
- n->head_name, nth);
147
+ en += sprintf(en, "%s^", n->head_name);
148
break;
149
default:
151
- sprintf(newname, "%s~%d^%d",
152
- n->head_name, n->generation,
153
- nth);
150
+ en += sprintf(en, "%s~%d",
151
+ n->head_name, n->generation);
152
+ break;
153
}
154
+ if (nth == 1)
155
+ en += sprintf(en, "^");
156
+ else
157
+ en += sprintf(en, "^%d", nth);
158
name_commit(p, strdup(newname), 0);
159
i++;
160
name_first_parent_chain(p);
@@ -205,7 +208,7 @@ static void join_revs(struct commit_list **list_p,
208
}
209
}
210
208
-static void show_one_commit(struct commit *commit)
211
+static void show_one_commit(struct commit *commit, int no_name)
212
{
213
char pretty[128], *cp;
214
struct commit_name *name = commit->object.util;
@@ -218,11 +221,21 @@ static void show_one_commit(struct commit *commit)
221
cp = pretty + 8;
222
else
223
cp = pretty;
221
- if (name && name->head_name) {
222
- printf("[%s", name->head_name);
223
- if (name->generation)
224
- printf("~%d", name->generation);
225
- printf("] ");
224
+
225
+ if (!no_name) {
226
+ if (name && name->head_name) {
227
+ printf("[%s", name->head_name);
228
+ if (name->generation) {
229
+ if (name->generation == 1)
230
+ printf("^");
231
+ else
232
+ printf("~%d", name->generation);
233
+ }
234
+ printf("] ");
235
+ }
236
+ else
237
+ printf("[%s] ",
238
+ find_unique_abbrev(commit->object.sha1, 7));
239
}
240
puts(cp);
241
}
@@ -354,7 +367,8 @@ int main(int ac, char **av)
367
unsigned char head_sha1[20];
368
int merge_base = 0;
369
int independent = 0;
357
- char **label;
370
+ int no_name = 0;
371
+ int sha1_name = 0;
372
373
setup_git_directory();
374
@@ -370,6 +384,10 @@ int main(int ac, char **av)
384
extra = 1;
385
else if (!strcmp(arg, "--list"))
386
extra = -1;
387
+ else if (!strcmp(arg, "--no-name"))
388
+ no_name = 1;
389
+ else if (!strcmp(arg, "--sha1-name"))
390
+ sha1_name = 1;
391
else if (!strncmp(arg, "--more=", 7))
392
extra = atoi(arg + 7);
393
else if (!strcmp(arg, "--merge-base"))
@@ -465,7 +483,8 @@ int main(int ac, char **av)
483
printf("%c [%s] ",
484
is_head ? '*' : '!', ref_name[i]);
485
}
468
- show_one_commit(rev[i]);
486
+ /* header lines never need name */
487
+ show_one_commit(rev[i], 1);
488
}
489
if (0 <= extra) {
490
for (i = 0; i < num_rev; i++)
@@ -480,7 +499,8 @@ int main(int ac, char **av)
499
sort_in_topological_order(&seen);
500
501
/* Give names to commits */
483
- name_commits(seen, rev, ref_name, num_rev);
502
+ if (!sha1_name && !no_name)
503
+ name_commits(seen, rev, ref_name, num_rev);
504
505
all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
506
all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
@@ -490,7 +510,6 @@ int main(int ac, char **av)
510
struct commit *commit = pop_one_commit(&seen);
511
int this_flag = commit->object.flags;
512
int is_merge_point = (this_flag & all_revs) == all_revs;
493
- static char *obvious[] = { "" };
513
514
if (is_merge_point)
515
shown_merge_point = 1;
@@ -501,9 +520,7 @@ int main(int ac, char **av)
520
? '+' : ' ');
521
putchar(' ');
522
}
504
- show_one_commit(commit);
505
- if (num_rev == 1)
506
- label = obvious;
523
+ show_one_commit(commit, no_name);
524
if (shown_merge_point && is_merge_point)
525
if (--extra < 0)
526
break;