checkout: describe_detached_head: remove ellipsis after committish
authorAnn T Ropea <bedhanger@gmx.de>
Wed, 6 Dec 2017 00:20:42 +0000 (01:20 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 6 Dec 2017 15:32:40 +0000 (07:32 -0800)
We do not want an ellipsis displayed following an (abbreviated) SHA-1
value.

The days when this was necessary to indicate the truncation to
lower-level Git commands and/or the user are bygone.

However, to ease the transition, the ellipsis will still be printed if
the user sets the environment variable GIT_PRINT_SHA1_ELLIPSIS to "yes".

Correct documentation with respect to what describe_detached_head prints
when GIT_PRINT_SHA1_ELLIPSIS is not set as indicated above.

Add tests for the old and new behaviour.

Signed-off-by: Ann T Ropea <bedhanger@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/user-manual.txt
builtin/checkout.c
t/t2020-checkout-detach.sh
index 497e82e88dd0f97734ac37771bf0a0fd32bd1168..eff78902742ab0c4c9855eed11b67cf576c0e4d4 100644 (file)
@@ -319,7 +319,7 @@ do so (now or later) by using -b with the checkout command again. Example:
 
   git checkout -b new_branch_name
 
-HEAD is now at 427abfa... Linux v2.6.17
+HEAD is now at 427abfa Linux v2.6.17
 ------------------------------------------------
 
 The HEAD then refers to the SHA-1 of the commit instead of to a branch,
index 6c2b4cd419a4a588e73a3f12a9ebdd6c1365ce78..6a6f5bcb2af2c7abe644bddeeb752e5955d878d7 100644 (file)
@@ -400,10 +400,16 @@ static void show_local_changes(struct object *head,
 static void describe_detached_head(const char *msg, struct commit *commit)
 {
        struct strbuf sb = STRBUF_INIT;
+
        if (!parse_commit(commit))
                pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
-       fprintf(stderr, "%s %s... %s\n", msg,
-               find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
+       if (print_sha1_ellipsis()) {
+               fprintf(stderr, "%s %s... %s\n", msg,
+                       find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
+       } else {
+               fprintf(stderr, "%s %s %s\n", msg,
+                       find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
+       }
        strbuf_release(&sb);
 }
 
index fbb4ee9bb42dbcfdfcdb79511523a8fa2ac8b5d9..9464069d15053a386399f852f6e5b1cb1814840e 100755 (executable)
@@ -186,4 +186,127 @@ test_expect_success 'no advice given for explicit detached head state' '
        test_cmp expect.no-advice actual
 '
 
+# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
+test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
+
+       # The first detach operation is more chatty than the following ones.
+       cat >1st_detach <<-'EOF' &&
+       Note: checking out 'HEAD^'.
+
+       You are in 'detached HEAD' state. You can look around, make experimental
+       changes and commit them, and you can discard any commits you make in this
+       state without impacting any branches by performing another checkout.
+
+       If you want to create a new branch to retain commits you create, you may
+       do so (now or later) by using -b with the checkout command again. Example:
+
+         git checkout -b <new-branch-name>
+
+       HEAD is now at 7c7cd714e262 three
+       EOF
+
+       # The remaining ones just show info about previous and current HEADs.
+       cat >2nd_detach <<-'EOF' &&
+       Previous HEAD position was 7c7cd714e262 three
+       HEAD is now at 139b20d8e6c5 two
+       EOF
+
+       cat >3rd_detach <<-'EOF' &&
+       Previous HEAD position was 139b20d8e6c5 two
+       HEAD is now at d79ce1670bdc one
+       EOF
+
+       reset &&
+       check_not_detached &&
+
+       # Various ways of *not* asking for ellipses
+
+       sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
+       git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 1st_detach actual &&
+
+       GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 2nd_detach actual &&
+
+       GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 3rd_detach actual &&
+
+       sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
+
+       # We only have four commits, but we can re-use them
+       reset &&
+       check_not_detached &&
+
+       # Make no mention of the env var at all
+       git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 1st_detach actual &&
+
+       GIT_PRINT_SHA1_ELLIPSIS='nope' &&
+       git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 2nd_detach actual &&
+
+       GIT_PRINT_SHA1_ELLIPSIS=nein &&
+       git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 3rd_detach actual &&
+
+       true
+"
+
+# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
+test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked to' "
+
+       # The first detach operation is more chatty than the following ones.
+       cat >1st_detach <<-'EOF' &&
+       Note: checking out 'HEAD^'.
+
+       You are in 'detached HEAD' state. You can look around, make experimental
+       changes and commit them, and you can discard any commits you make in this
+       state without impacting any branches by performing another checkout.
+
+       If you want to create a new branch to retain commits you create, you may
+       do so (now or later) by using -b with the checkout command again. Example:
+
+         git checkout -b <new-branch-name>
+
+       HEAD is now at 7c7cd714e262... three
+       EOF
+
+       # The remaining ones just show info about previous and current HEADs.
+       cat >2nd_detach <<-'EOF' &&
+       Previous HEAD position was 7c7cd714e262... three
+       HEAD is now at 139b20d8e6c5... two
+       EOF
+
+       cat >3rd_detach <<-'EOF' &&
+       Previous HEAD position was 139b20d8e6c5... two
+       HEAD is now at d79ce1670bdc... one
+       EOF
+
+       reset &&
+       check_not_detached &&
+
+       # Various ways of asking for ellipses...
+       # The user can just use any kind of quoting (including none).
+
+       GIT_PRINT_SHA1_ELLIPSIS="yes" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 1st_detach actual &&
+
+       GIT_PRINT_SHA1_ELLIPSIS='yes' git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 2nd_detach actual &&
+
+       GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+       check_detached &&
+       test_i18ncmp 3rd_detach actual &&
+
+       true
+"
+
 test_done