describe: Add --first-parent option

Only consider the first parent commit when walking the commit history. This is useful if you only wish to match tags on your branch after a merge. Signed-off-by: Mike Crowe <mac@mcrowe.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mike Crowe committed May 17, 2013 at 21:56 UTC e00dd1e9485c50f202cc97dfae19d510e108b565
3 files changed +16 -1
Documentation/git-describe.txt
+8 -1
@@ -88,6 +88,11 @@ OPTIONS
88 --always::
89 Show uniquely abbreviated commit object as fallback.
90
91 +--first-parent::
92 + Follow only the first parent commit upon seeing a merge commit.
93 + This is useful when you wish to not match tags on branches merged
94 + in the history of the target commit.
95 +
96 EXAMPLES
97 --------
98
@@ -149,7 +154,9 @@ is found, its name will be output and searching will stop.
154 If an exact match was not found, 'git describe' will walk back
155 through the commit history to locate an ancestor commit which
156 has been tagged. The ancestor's tag will be output along with an
152 -abbreviation of the input committish's SHA-1.
157 +abbreviation of the input committish's SHA-1. If '--first-parent' was
158 +specified then the walk will only consider the first parent of each
159 +commit.
160
161 If multiple tags were found during the walk then the tag which
162 has the fewest commits different from the input committish will be
builtin/describe.c
+5
@@ -21,6 +21,7 @@ static int debug; /* Display lots of verbose info */
21 static int all; /* Any valid ref can be used */
22 static int tags; /* Allow lightweight tags */
23 static int longformat;
24 +static int first_parent;
25 static int abbrev = -1; /* unspecified */
26 static int max_candidates = 10;
27 static struct hash_table names;
@@ -336,6 +337,9 @@ static void describe(const char *arg, int last_one)
337 commit_list_insert_by_date(p, &list);
338 p->object.flags |= c->object.flags;
339 parents = parents->next;
340 +
341 + if (first_parent)
342 + break;
343 }
344 }
345
@@ -404,6 +408,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
408 OPT_BOOLEAN(0, "all", &all, N_("use any ref")),
409 OPT_BOOLEAN(0, "tags", &tags, N_("use any tag, even unannotated")),
410 OPT_BOOLEAN(0, "long", &longformat, N_("always use long format")),
411 + OPT_BOOLEAN(0, "first-parent", &first_parent, N_("only follow first parent")),
412 OPT__ABBREV(&abbrev),
413 OPT_SET_INT(0, "exact-match", &max_candidates,
414 N_("only output exact matches"), 0),
t/t6120-describe.sh
+3
@@ -110,6 +110,9 @@ check_describe tags/e --all HEAD^^^
110 check_describe B-0-* --long HEAD^^2^
111 check_describe A-3-* --long HEAD^^2
112
113 +check_describe c-7-* --tags
114 +check_describe e-3-* --first-parent --tags
115 +
116 : >err.expect
117 check_describe A --all A^0
118 test_expect_success 'no warning was displayed for A' '