"checkout A...B" switches to the merge base between A and B

When flipping commits around on topic branches, I often end up doing
this sequence:

 * Run "log --oneline next..jc/frotz" to find out the first commit
   on 'jc/frotz' branch not yet merged to 'next';

 * Run "checkout $that_commit^" to detach HEAD to the parent of it;

 * Rebuild the series on top of that commit; and

 * "show-branch jc/frotz HEAD" and "diff jc/frotz HEAD" to verify.

Introduce a new syntax to "git checkout" to name the commit to switch to,
to make the first two steps easier.  When the branch to switch to is
specified as A...B (you can omit either A or B but not both, and HEAD
is used instead of the omitted side), the merge base between these two
commits are computed, and if there is one unique one, we detach the HEAD
at that commit.

With this, I can say "checkout next...jc/frotz".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2009-10-18 12:34:56 -07:00
Родитель 46148dd7ea
Коммит 619a644d6d
4 изменённых файлов: 74 добавлений и 3 удалений

Просмотреть файл

@ -689,7 +689,10 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
* case 3: git checkout <something> [<paths>]
*
* With no paths, if <something> is a commit, that is to
* switch to the branch or detach HEAD at it.
* switch to the branch or detach HEAD at it. As a special case,
* if <something> is A...B (missing A or B means HEAD but you can
* omit at most one side), and if there is a unique merge base
* between A and B, A...B names that merge base.
*
* With no paths, if <something> is _not_ a commit, no -t nor -b
* was given, and there is a tracking branch whose name is
@ -715,7 +718,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
if (!strcmp(arg, "-"))
arg = "@{-1}";
if (get_sha1(arg, rev)) {
if (get_sha1_mb(arg, rev)) {
if (has_dash_dash) /* case (1) */
die("invalid reference: %s", arg);
if (!patch_mode &&

Просмотреть файл

@ -703,6 +703,7 @@ extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
extern int dwim_log(const char *str, int len, unsigned char *sha1, char **ref);
extern int interpret_branch_name(const char *str, struct strbuf *);
extern int get_sha1_mb(const char *str, unsigned char *sha1);
extern int refname_match(const char *abbrev_name, const char *full_name, const char **rules);
extern const char *ref_rev_parse_rules[];

Просмотреть файл

@ -794,6 +794,48 @@ release_return:
return retval;
}
int get_sha1_mb(const char *name, unsigned char *sha1)
{
struct commit *one, *two;
struct commit_list *mbs;
unsigned char sha1_tmp[20];
const char *dots;
int st;
dots = strstr(name, "...");
if (!dots)
return get_sha1(name, sha1);
if (dots == name)
st = get_sha1("HEAD", sha1_tmp);
else {
struct strbuf sb;
strbuf_init(&sb, dots - name);
strbuf_add(&sb, name, dots - name);
st = get_sha1(sb.buf, sha1_tmp);
strbuf_release(&sb);
}
if (st)
return st;
one = lookup_commit_reference_gently(sha1_tmp, 0);
if (!one)
return -1;
if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
return -1;
two = lookup_commit_reference_gently(sha1_tmp, 0);
if (!two)
return -1;
mbs = get_merge_bases(one, two, 1);
if (!mbs || mbs->next)
st = -1;
else {
st = 0;
hashcpy(sha1, mbs->item->object.sha1);
}
free_commit_list(mbs);
return st;
}
/*
* This is like "get_sha1_basic()", except it allows "sha1 expressions",
* notably "xyz^" for "parent of xyz"

Просмотреть файл

@ -1,6 +1,6 @@
#!/bin/sh
test_description='checkout can switch to last branch'
test_description='checkout can switch to last branch and merge base'
. ./test-lib.sh
@ -91,4 +91,29 @@ test_expect_success 'switch to twelfth from the last' '
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/branch13"
'
test_expect_success 'merge base test setup' '
git checkout -b another other &&
echo "hello again" >>world &&
git add world &&
git commit -m third
'
test_expect_success 'another...master' '
git checkout another &&
git checkout another...master &&
test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify master^)"
'
test_expect_success '...master' '
git checkout another &&
git checkout ...master &&
test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify master^)"
'
test_expect_success 'master...' '
git checkout another &&
git checkout master... &&
test "z$(git rev-parse --verify HEAD)" = "z$(git rev-parse --verify master^)"
'
test_done