@matheusbsbs / matheus-bagliano / commits / 3631bf77f7
Fork 0

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>

Erick Mattos committed May 21, 2010 at 21:28 UTC 3631bf77f75bc34e810216f3ec6abc8203e2d16a
2 files changed +85 -24
builtin/checkout.c
+28 -3
@@ -493,7 +493,24 @@ static void update_refs_for_switch(struct checkout_opts *opts,
493 struct strbuf msg = STRBUF_INIT;
494 const char *old_desc;
495 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
514 create_branch(old->name, opts->new_branch, new->name, 0,
515 opts->new_branch_log, opts->track);
516 new->name = opts->new_branch;
@@ -517,6 +534,14 @@ static void update_refs_for_switch(struct checkout_opts *opts,
534 opts->new_branch ? " a new" : "",
535 new->name);
536 }
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 + }
545 } else if (strcmp(new->name, "HEAD")) {
546 update_ref(msg.buf, "HEAD", new->commit->object.sha1, NULL,
547 REF_NODEREF, DIE_ON_ERR);
@@ -684,8 +709,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
709 if (opts.new_orphan_branch) {
710 if (opts.new_branch)
711 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");
714 opts.new_branch = opts.new_orphan_branch;
715 }
716
t/t2017-checkout-orphan.sh
+57 -21
@@ -49,6 +49,62 @@ test_expect_success '--orphan must be rejected with -b' '
49 test refs/heads/master = "$(git symbolic-ref HEAD)"
50 '
51
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 +
108 test_expect_success '--orphan is rejected with an existing name' '
109 git checkout master &&
110 test_must_fail git checkout --orphan master &&
@@ -60,31 +116,11 @@ test_expect_success '--orphan refuses to switch if a merge is needed' '
116 git reset --hard &&
117 echo local >>"$TEST_FILE" &&
118 cat "$TEST_FILE" >"$TEST_FILE.saved" &&
63 - test_must_fail git checkout --orphan gamma master^ &&
119 + test_must_fail git checkout --orphan new master^ &&
120 test refs/heads/master = "$(git symbolic-ref HEAD)" &&
121 test_cmp "$TEST_FILE" "$TEST_FILE.saved" &&
122 git diff-index --quiet --cached HEAD &&
123 git reset --hard
124 '
125
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 -
126 test_done