@matheusbsbs / matheus-bagliano / commits / 75e6e21320
Fork 0

Add -v and --abbrev options to git-branch

The new -v option makes git-branch show the abbreviated sha1 + subjectline for each branch. Additionally, minimum abbreviation length can be specified with --abbrev=<length> Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Lars Hjemli committed Nov 24, 2006 at 14:45 UTC 75e6e2132006770156d9df1881b4114862919c94
2 files changed +51 -6
Documentation/git-branch.txt
+8 -1
@@ -8,7 +8,7 @@ git-branch - List, create, or delete branches.
8 SYNOPSIS
9 --------
10 [verse]
11 -'git-branch' [-r] [-a]
11 +'git-branch' [-r] [-a] [-v] [--abbrev=<length>]
12 'git-branch' [-l] [-f] <branchname> [<start-point>]
13 'git-branch' (-d | -D) <branchname>...
14
@@ -52,6 +52,13 @@ OPTIONS
52 -a::
53 List both remote-tracking branches and local branches.
54
55 +-v::
56 + Show sha1 and subject message for each head.
57 +
58 +--abbrev=<length>::
59 + Alter minimum display length for sha1 in output listing,
60 + default value is 7.
61 +
62 <branchname>::
63 The name of the branch to create or delete.
64 The new branch name must pass all checks defined by
builtin-branch.c
+43 -5
@@ -11,7 +11,7 @@
11 #include "builtin.h"
12
13 static const char builtin_branch_usage[] =
14 -"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r] | [-a]";
14 +"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r | -a] [-v] [--abbrev=<length>] ";
15
16
17 static const char *head;
@@ -87,10 +87,11 @@ static void delete_branches(int argc, const char **argv, int force)
87 struct ref_item {
88 char *name;
89 unsigned int kind;
90 + unsigned char sha1[20];
91 };
92
93 struct ref_list {
93 - int index, alloc;
94 + int index, alloc, maxwidth;
95 struct ref_item *list;
96 int kinds;
97 };
@@ -100,6 +101,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
101 struct ref_list *ref_list = (struct ref_list*)(cb_data);
102 struct ref_item *newitem;
103 int kind = REF_UNKNOWN_TYPE;
104 + int len;
105
106 /* Detect kind */
107 if (!strncmp(refname, "refs/heads/", 11)) {
@@ -128,6 +130,10 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
130 newitem = &(ref_list->list[ref_list->index++]);
131 newitem->name = xstrdup(refname);
132 newitem->kind = kind;
133 + hashcpy(newitem->sha1, sha1);
134 + len = strlen(newitem->name);
135 + if (len > ref_list->maxwidth)
136 + ref_list->maxwidth = len;
137
138 return 0;
139 }
@@ -151,7 +157,24 @@ static int ref_cmp(const void *r1, const void *r2)
157 return strcmp(c1->name, c2->name);
158 }
159
154 -static void print_ref_list(int kinds)
160 +static void print_ref_info(const unsigned char *sha1, int abbrev)
161 +{
162 + struct commit *commit;
163 + char subject[256];
164 +
165 +
166 + commit = lookup_commit(sha1);
167 + if (commit && !parse_commit(commit))
168 + pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
169 + subject, sizeof(subject), 0,
170 + NULL, NULL, 0);
171 + else
172 + strcpy(subject, " **** invalid ref ****");
173 +
174 + printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
175 +}
176 +
177 +static void print_ref_list(int kinds, int verbose, int abbrev)
178 {
179 int i;
180 char c;
@@ -169,7 +192,13 @@ static void print_ref_list(int kinds)
192 !strcmp(ref_list.list[i].name, head))
193 c = '*';
194
172 - printf("%c %s\n", c, ref_list.list[i].name);
195 + if (verbose) {
196 + printf("%c %-*s", c, ref_list.maxwidth,
197 + ref_list.list[i].name);
198 + print_ref_info(ref_list.list[i].sha1, abbrev);
199 + }
200 + else
201 + printf("%c %s\n", c, ref_list.list[i].name);
202 }
203
204 free_ref_list(&ref_list);
@@ -215,6 +244,7 @@ static void create_branch(const char *name, const char *start,
244 int cmd_branch(int argc, const char **argv, const char *prefix)
245 {
246 int delete = 0, force_delete = 0, force_create = 0;
247 + int verbose = 0, abbrev = DEFAULT_ABBREV;
248 int reflog = 0;
249 int kinds = REF_LOCAL_BRANCH;
250 int i;
@@ -255,6 +285,14 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
285 reflog = 1;
286 continue;
287 }
288 + if (!strncmp(arg, "--abbrev=", 9)) {
289 + abbrev = atoi(arg+9);
290 + continue;
291 + }
292 + if (!strcmp(arg, "-v")) {
293 + verbose = 1;
294 + continue;
295 + }
296 usage(builtin_branch_usage);
297 }
298
@@ -268,7 +306,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
306 if (delete)
307 delete_branches(argc - i, argv + i, force_delete);
308 else if (i == argc)
271 - print_ref_list(kinds);
309 + print_ref_list(kinds, verbose, abbrev);
310 else if (i == argc - 1)
311 create_branch(argv[i], head, force_create, reflog);
312 else if (i == argc - 2)