Skip to contentSkip to conversation

Add 'git show-branch'.

PATCH New Junio C Hamano 1 participant 1 message Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add 'git show-branch'.

The 'git show-branches' command turns out to be reasonably useful, but painfully slow. So rewrite it in C, using ideas from merge-base while enhancing it a bit more.

- Unlike show-branches, it can take --heads (show me all my heads), --tags (show me all my tags), or --all (both).

- It can take --more=<number> to show beyond the merge-base.

- It shows the short name for each commit in the extended SHA1 syntax.

- It can find merge-base for more than two heads.

Examples:

    $ git show-branch --more=6 HEAD

is almost the same as "git log --pretty=oneline --max-count=6".

$ git show-branch --merge-base master mhf misc

finds the merge base of the three given heads.

$ git show-branch --all --more=10

is poor-man's gitk, showing all the tags and heads, and going back 10 commits beyond the merge base of those refs.

---

 Makefile      |    2 
 commit.c      |   21 +++-
 commit.h      |    2 
 show-branch.c |  325 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 343 insertions(+), 7 deletions(-)
 create mode 100644 show-branch.c

660c5d6332b541e2186c93374eef637e30894c62

Makefile +1 -1
@@ -85,7 +85,7 @@ PROG=   git-update-cache git-diff-files 
 	git-diff-stages git-rev-parse git-patch-id git-pack-objects \
 	git-unpack-objects git-verify-pack git-receive-pack git-send-pack \
 	git-prune-packed git-fetch-pack git-upload-pack git-clone-pack \
-	git-show-index git-daemon git-var git-peek-remote \
+	git-show-index git-daemon git-var git-peek-remote git-show-branch \
 	git-update-server-info git-show-rev-cache git-build-rev-cache
 
 ifndef NO_CURL
commit.c +15 -6
@@ -38,23 +38,32 @@ enum cmit_fmt get_commit_format(const ch
 	die("invalid --pretty format");
 }
 
-static struct commit *check_commit(struct object *obj, const unsigned char *sha1)
+static struct commit *check_commit(struct object *obj,
+				   const unsigned char *sha1,
+				   int quiet)
 {
 	if (obj->type != commit_type) {
-		error("Object %s is a %s, not a commit", 
-		      sha1_to_hex(sha1), obj->type);
+		if (!quiet)
+			error("Object %s is a %s, not a commit",
+			      sha1_to_hex(sha1), obj->type);
 		return NULL;
 	}
 	return (struct commit *) obj;
 }
 
-struct commit *lookup_commit_reference(const unsigned char *sha1)
+struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
+					      int quiet)
 {
 	struct object *obj = deref_tag(parse_object(sha1));
 
 	if (!obj)
 		return NULL;
-	return check_commit(obj, sha1);
+	return check_commit(obj, sha1, quiet);
+}
+
+struct commit *lookup_commit_reference(const unsigned char *sha1)
+{
+	return lookup_commit_reference_gently(sha1, 0);
 }
 
 struct commit *lookup_commit(const unsigned char *sha1)
@@ -69,7 +78,7 @@ struct commit *lookup_commit(const unsig
 	}
 	if (!obj->type)
 		obj->type = commit_type;
-	return check_commit(obj, sha1);
+	return check_commit(obj, sha1, 0);
 }
 
 static unsigned long parse_commit_date(const char *buf)
commit.h +2 -0
@@ -21,6 +21,8 @@ extern const char *commit_type;
 
 struct commit *lookup_commit(const unsigned char *sha1);
 struct commit *lookup_commit_reference(const unsigned char *sha1);
+struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
+					      int quiet);
 
 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
 
show-branch.c new file +325 -0

325 lines not on this page · raw message