Skip to content

Commit 3631bf7

Browse files
erickmattosgitster
authored andcommitted
checkout --orphan: respect -l option always
Added changes to satisfy a corner case: creating reflogs by using -l when core.logAllRefUpdates is set to false. Signed-off-by: Erick Mattos <erick.mattos@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 859c301 commit 3631bf7

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

builtin/checkout.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,24 @@ static void update_refs_for_switch(struct checkout_opts *opts,
493493
struct strbuf msg = STRBUF_INIT;
494494
const char *old_desc;
495495
if (opts->new_branch) {
496-
if (!opts->new_orphan_branch)
496+
if (opts->new_orphan_branch) {
497+
if (opts->new_branch_log && !log_all_ref_updates) {
498+
int temp;
499+
char *log_file;
500+
char *ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
501+
502+
temp = log_all_ref_updates;
503+
log_all_ref_updates = 1;
504+
if (log_ref_setup(ref_name, &log_file)) {
505+
fprintf(stderr, "Can not do reflog for '%s'\n",
506+
opts->new_orphan_branch);
507+
log_all_ref_updates = temp;
508+
return;
509+
}
510+
log_all_ref_updates = temp;
511+
}
512+
}
513+
else
497514
create_branch(old->name, opts->new_branch, new->name, 0,
498515
opts->new_branch_log, opts->track);
499516
new->name = opts->new_branch;
@@ -517,6 +534,14 @@ static void update_refs_for_switch(struct checkout_opts *opts,
517534
opts->new_branch ? " a new" : "",
518535
new->name);
519536
}
537+
if (old->path && old->name) {
538+
char log_file[PATH_MAX], ref_file[PATH_MAX];
539+
540+
git_snpath(log_file, sizeof(log_file), "logs/%s", old->path);
541+
git_snpath(ref_file, sizeof(ref_file), "%s", old->path);
542+
if (!file_exists(ref_file) && file_exists(log_file))
543+
remove_path(log_file);
544+
}
520545
} else if (strcmp(new->name, "HEAD")) {
521546
update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
522547
REF_NODEREF, DIE_ON_ERR);
@@ -684,8 +709,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
684709
if (opts.new_orphan_branch) {
685710
if (opts.new_branch)
686711
die("--orphan and -b are mutually exclusive");
687-
if (opts.track > 0 || opts.new_branch_log)
688-
die("--orphan cannot be used with -t or -l");
712+
if (opts.track > 0)
713+
die("--orphan cannot be used with -t");
689714
opts.new_branch = opts.new_orphan_branch;
690715
}
691716

t/t2017-checkout-orphan.sh

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,62 @@ test_expect_success '--orphan must be rejected with -b' '
4949
test refs/heads/master = "$(git symbolic-ref HEAD)"
5050
'
5151

52+
test_expect_success '--orphan must be rejected with -t' '
53+
git checkout master &&
54+
test_must_fail git checkout --orphan new -t master &&
55+
test refs/heads/master = "$(git symbolic-ref HEAD)"
56+
'
57+
58+
test_expect_success '--orphan ignores branch.autosetupmerge' '
59+
git checkout master &&
60+
git config branch.autosetupmerge always &&
61+
git checkout --orphan gamma &&
62+
test -z "$(git config branch.gamma.merge)" &&
63+
test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
64+
test_must_fail git rev-parse --verify HEAD^
65+
'
66+
67+
test_expect_success '--orphan makes reflog by default' '
68+
git checkout master &&
69+
git config --unset core.logAllRefUpdates &&
70+
git checkout --orphan delta &&
71+
! test -f .git/logs/refs/heads/delta &&
72+
test_must_fail PAGER= git reflog show delta &&
73+
git commit -m Delta &&
74+
test -f .git/logs/refs/heads/delta &&
75+
PAGER= git reflog show delta
76+
'
77+
78+
test_expect_success '--orphan does not make reflog when core.logAllRefUpdates = false' '
79+
git checkout master &&
80+
git config core.logAllRefUpdates false &&
81+
git checkout --orphan epsilon &&
82+
! test -f .git/logs/refs/heads/epsilon &&
83+
test_must_fail PAGER= git reflog show epsilon &&
84+
git commit -m Epsilon &&
85+
! test -f .git/logs/refs/heads/epsilon &&
86+
test_must_fail PAGER= git reflog show epsilon
87+
'
88+
89+
test_expect_success '--orphan with -l makes reflog when core.logAllRefUpdates = false' '
90+
git checkout master &&
91+
git checkout -l --orphan zeta &&
92+
test -f .git/logs/refs/heads/zeta &&
93+
test_must_fail PAGER= git reflog show zeta &&
94+
git commit -m Zeta &&
95+
PAGER= git reflog show zeta
96+
'
97+
98+
test_expect_success 'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' '
99+
git checkout master &&
100+
git checkout -l --orphan eta &&
101+
test -f .git/logs/refs/heads/eta &&
102+
test_must_fail PAGER= git reflog show eta &&
103+
git checkout master &&
104+
! test -f .git/logs/refs/heads/eta &&
105+
test_must_fail PAGER= git reflog show eta
106+
'
107+
52108
test_expect_success '--orphan is rejected with an existing name' '
53109
git checkout master &&
54110
test_must_fail git checkout --orphan master &&
@@ -60,31 +116,11 @@ test_expect_success '--orphan refuses to switch if a merge is needed' '
60116
git reset --hard &&
61117
echo local >>"$TEST_FILE" &&
62118
cat "$TEST_FILE" >"$TEST_FILE.saved" &&
63-
test_must_fail git checkout --orphan gamma master^ &&
119+
test_must_fail git checkout --orphan new master^ &&
64120
test refs/heads/master = "$(git symbolic-ref HEAD)" &&
65121
test_cmp "$TEST_FILE" "$TEST_FILE.saved" &&
66122
git diff-index --quiet --cached HEAD &&
67123
git reset --hard
68124
'
69125

70-
test_expect_success '--orphan does not mix well with -t' '
71-
git checkout master &&
72-
test_must_fail git checkout -t master --orphan gamma &&
73-
test refs/heads/master = "$(git symbolic-ref HEAD)"
74-
'
75-
76-
test_expect_success '--orphan ignores branch.autosetupmerge' '
77-
git checkout -f master &&
78-
git config branch.autosetupmerge always &&
79-
git checkout --orphan delta &&
80-
test -z "$(git config branch.delta.merge)" &&
81-
test refs/heads/delta = "$(git symbolic-ref HEAD)" &&
82-
test_must_fail git rev-parse --verify HEAD^
83-
'
84-
85-
test_expect_success '--orphan does not mix well with -l' '
86-
git checkout -f master &&
87-
test_must_fail git checkout -l --orphan gamma
88-
'
89-
90126
test_done

0 commit comments

Comments
 (0)