show-branch: allow glob pattern to name branches to show.
With this, you can say "git-show-branch topic/* master" to show all the topic branches you have under .git/refs/heads/topic/ and your master branch. Another example is "git-show-branch --list v1.0*" to show all the v1.0 tags. You can disambiguate by saying "heads/topic/*" to show only topic branches if you have tags under .git/refs/tags/topic/ as well. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 4, 2005 at 15:58 UTC
287f860054345505e4f737466279efa6c3ff04b5
2 files changed
+84
-15
Documentation/git-show-branch.txt
+17
-6
@@ -7,18 +7,29 @@ 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] [--no-name | --sha1-name] <reference>...'
10
+'git-show-branch [--all] [--heads] [--tags] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] [<rev> | <glob>]...'
11
12
DESCRIPTION
13
-----------
14
-Shows the head commits from the named <reference> (or all refs under
15
-$GIT_DIR/refs/heads), and displays concise list of commit logs
16
-to show their relationship semi-visually.
14
+
15
+Shows the commit ancestry graph starting from the commits named
16
+with <rev>s or <globs>s (or all refs under $GIT_DIR/refs/heads
17
+and/or $GIT_DIR/refs/tags) semi-visually.
18
+
19
+It cannot show more than 29 branches and commits at a time.
20
+
21
22
OPTIONS
23
-------
20
-<reference>::
21
- Name of the reference under $GIT_DIR/refs/.
24
+<rev>::
25
+ Arbitrary extended SHA1 expression (see `git-rev-parse`)
26
+ that typically names a branch HEAD or a tag.
27
+
28
+<glob>::
29
+ A glob pattern that matches branch or tag names under
30
+ $GIT_DIR/refs. For example, if you have many topic
31
+ branches under $GIT_DIR/refs/heads/topic, giving
32
+ `topic/*` would show all of them.
33
34
--all --heads --tags::
35
Show all refs under $GIT_DIR/refs, $GIT_DIR/refs/heads,
show-branch.c
+67
-9
@@ -1,4 +1,5 @@
1
#include <stdlib.h>
2
+#include <fnmatch.h>
3
#include "cache.h"
4
#include "commit.h"
5
#include "refs.h"
@@ -332,6 +333,39 @@ static int append_tag_ref(const char *refname, const unsigned char *sha1)
333
return append_ref(refname + 5, sha1);
334
}
335
336
+static const char *match_ref_pattern = NULL;
337
+static int match_ref_slash = 0;
338
+static int count_slash(const char *s)
339
+{
340
+ int cnt = 0;
341
+ while (*s)
342
+ if (*s++ == '/')
343
+ cnt++;
344
+ return cnt;
345
+}
346
+
347
+static int append_matching_ref(const char *refname, const unsigned char *sha1)
348
+{
349
+ /* we want to allow pattern hold/<asterisk> to show all
350
+ * branches under refs/heads/hold/, and v0.99.9? to show
351
+ * refs/tags/v0.99.9a and friends.
352
+ */
353
+ const char *tail;
354
+ int slash = count_slash(refname);
355
+ for (tail = refname; *tail && match_ref_slash < slash; )
356
+ if (*tail++ == '/')
357
+ slash--;
358
+ if (!*tail)
359
+ return 0;
360
+ if (fnmatch(match_ref_pattern, tail, 0))
361
+ return 0;
362
+ if (!strncmp("refs/heads/", refname, 11))
363
+ return append_head_ref(refname, sha1);
364
+ if (!strncmp("refs/tags/", refname, 10))
365
+ return append_tag_ref(refname, sha1);
366
+ return append_ref(refname, sha1);
367
+}
368
+
369
static void snarf_refs(int head, int tag)
370
{
371
if (head) {
@@ -400,6 +434,27 @@ static int show_independent(struct commit **rev,
434
return 0;
435
}
436
437
+static void append_one_rev(const char *av)
438
+{
439
+ unsigned char revkey[20];
440
+ if (!get_sha1(av, revkey)) {
441
+ append_ref(av, revkey);
442
+ return;
443
+ }
444
+ if (strchr(av, '*') || strchr(av, '?')) {
445
+ /* glob style match */
446
+ int saved_matches = ref_name_cnt;
447
+ match_ref_pattern = av;
448
+ match_ref_slash = count_slash(av);
449
+ for_each_ref(append_matching_ref);
450
+ if (saved_matches == ref_name_cnt &&
451
+ ref_name_cnt < MAX_REVS)
452
+ error("no matching refs with %s", av);
453
+ return;
454
+ }
455
+ die("bad sha1 reference %s", av);
456
+}
457
+
458
int main(int ac, char **av)
459
{
460
struct commit *rev[MAX_REVS], *commit;
@@ -458,17 +513,20 @@ int main(int ac, char **av)
513
if (all_heads + all_tags)
514
snarf_refs(all_heads, all_tags);
515
461
- while (0 < ac) {
462
- unsigned char revkey[20];
463
- if (get_sha1(*av, revkey))
464
- die("bad sha1 reference %s", *av);
465
- append_ref(*av, revkey);
466
- ac--; av++;
516
+ if (ac) {
517
+ while (0 < ac) {
518
+ append_one_rev(*av);
519
+ ac--; av++;
520
+ }
521
}
468
-
469
- /* If still no revs, then add heads */
470
- if (!ref_name_cnt)
522
+ else {
523
+ /* If no revs given, then add heads */
524
snarf_refs(1, 0);
525
+ }
526
+ if (!ref_name_cnt) {
527
+ fprintf(stderr, "No revs to be shown.\n");
528
+ exit(0);
529
+ }
530
531
for (num_rev = 0; ref_name[num_rev]; num_rev++) {
532
unsigned char revkey[20];