aboutsummaryrefslogtreecommitdiff
path: root/sha1_name.c
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2005-08-21 02:43:54 -0700
committerJunio C Hamano <junkio@cox.net>2005-08-22 18:34:09 -0700
commit4f7599ac2543e1d59f2b2b865ecff272ae5f4ea5 (patch)
treefc1c72ca91c6d4606abe8f0bd18c58f4e9a35706 /sha1_name.c
parent792fe559d02e55c12d2d544fd6d6f202cbaab6f4 (diff)
downloadgit-4f7599ac2543e1d59f2b2b865ecff272ae5f4ea5.tar.gz
git-4f7599ac2543e1d59f2b2b865ecff272ae5f4ea5.tar.xz
[PATCH] Add a new extended SHA1 syntax <name>~<num>
The new notation is a short-hand for <name> followed by <num> caret ('^') characters. E.g. "master~4" is the fourth generation ancestor of the current "master" branch head, following the first parents; same as "master^^^^" but a bit more readable. This will be used in the updated "git show-branch" command. Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'sha1_name.c')
-rw-r--r--sha1_name.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/sha1_name.c b/sha1_name.c
index 6744bbbef..133af3b6c 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -191,9 +191,29 @@ static int get_parent(const char *name, int len,
return -1;
}
+static int get_nth_ancestor(const char *name, int len,
+ unsigned char *result, int generation)
+{
+ unsigned char sha1[20];
+ int ret = get_sha1_1(name, len, sha1);
+ if (ret)
+ return ret;
+
+ while (generation--) {
+ struct commit *commit = lookup_commit_reference(sha1);
+
+ if (!commit || parse_commit(commit) || !commit->parents)
+ return -1;
+ memcpy(sha1, commit->parents->item->object.sha1, 20);
+ }
+ memcpy(result, sha1, 20);
+ return 0;
+}
+
static int get_sha1_1(const char *name, int len, unsigned char *sha1)
{
int parent, ret;
+ const char *cp;
/* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
if (len > 2 && name[len-2] == '^' &&
@@ -210,6 +230,27 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
if (parent >= 0)
return get_parent(name, len, sha1, parent);
+ /* "name~3" is "name^^^",
+ * "name~12" is "name^^^^^^^^^^^^", and
+ * "name~" and "name~0" are name -- not "name^0"!
+ */
+ parent = 0;
+ for (cp = name + len - 1; name <= cp; cp--) {
+ int ch = *cp;
+ if ('0' <= ch && ch <= '9')
+ continue;
+ if (ch != '~')
+ parent = -1;
+ break;
+ }
+ if (!parent && *cp == '~') {
+ int len1 = cp - name;
+ cp++;
+ while (cp < name + len)
+ parent = parent * 10 + *cp++ - '0';
+ return get_nth_ancestor(name, len1, sha1, parent);
+ }
+
ret = get_sha1_basic(name, len, sha1);
if (!ret)
return 0;