When a user asked for a detached HEAD specifically with `--detach`,
we do not need to give advice on what a detached HEAD state entails as
we can assume they know what they're getting into as they asked for it.

Signed-off-by: Stefan Beller <sbel...@google.com>
---

 Junio writes:
 > It might be controversial how the second from the last case should
 > behave, though.
 
 I agree. I think if the advice is configured explicitly we can still give it.
 That makes the code a bit more complicated though.
 
 Also note I added stderr to stdout redirections as suggested by Peff.
 
 Thanks,
 Stefan
 
 builtin/checkout.c         |  4 +++-
 t/t2020-checkout-detach.sh | 28 ++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 4866111..6196b40 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -658,7 +658,9 @@ static void update_refs_for_switch(const struct 
checkout_opts *opts,
                update_ref(msg.buf, "HEAD", new->commit->object.oid.hash, NULL,
                           REF_NODEREF, UPDATE_REFS_DIE_ON_ERR);
                if (!opts->quiet) {
-                       if (old->path && advice_detached_head)
+                       if (old->path &&
+                           (advice_detached_head == 1 ||
+                            (advice_detached_head == -1 && 
!opts->force_detach)))
                                detach_advice(new->name);
                        describe_detached_head(_("HEAD is now at"), 
new->commit);
                }
diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
index 5d68729..fe311a1 100755
--- a/t/t2020-checkout-detach.sh
+++ b/t/t2020-checkout-detach.sh
@@ -163,4 +163,32 @@ test_expect_success 'tracking count is accurate after 
orphan check' '
        test_i18ncmp expect stdout
 '
 
+test_expect_success 'no advice given for explicit detached head state' '
+       # baseline
+       test_config advice.detachedHead true &&
+       git checkout child && git checkout HEAD^0 >expect.advice 2>&1 &&
+       test_config advice.detachedHead false &&
+       git checkout child && git checkout HEAD^0 >expect.no-advice 2>&1 &&
+       test_unconfig advice.detachedHead &&
+       # without configuration, the advice.* variables default to true
+       git checkout child && git checkout HEAD^0 >actual 2>&1 &&
+       test_cmp expect.advice actual &&
+
+       # with explicit --detach
+       # no configuration
+       test_unconfig advice.detachedHead &&
+       git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
+       test_cmp expect.no-advice actual &&
+
+       # explicitly ask advice
+       test_config advice.detachedHead true &&
+       git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
+       test_cmp expect.advice actual &&
+
+       # explicitly decline advice
+       test_config advice.detachedHead false &&
+       git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
+       test_cmp expect.no-advice actual
+'
+
 test_done
-- 
2.9.2.730.g525ad04.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to