Skip to content

Commit ca69d4d

Browse files
bedhangergitster
authored andcommitted
checkout: describe_detached_head: remove ellipsis after committish
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a2cd709 commit ca69d4d

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

Documentation/user-manual.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ do so (now or later) by using -b with the checkout command again. Example:
319319

320320
git checkout -b new_branch_name
321321

322-
HEAD is now at 427abfa... Linux v2.6.17
322+
HEAD is now at 427abfa Linux v2.6.17
323323
------------------------------------------------
324324

325325
The HEAD then refers to the SHA-1 of the commit instead of to a branch,

builtin/checkout.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,16 @@ static void show_local_changes(struct object *head,
400400
static void describe_detached_head(const char *msg, struct commit *commit)
401401
{
402402
struct strbuf sb = STRBUF_INIT;
403+
403404
if (!parse_commit(commit))
404405
pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
405-
fprintf(stderr, "%s %s... %s\n", msg,
406-
find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
406+
if (print_sha1_ellipsis()) {
407+
fprintf(stderr, "%s %s... %s\n", msg,
408+
find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
409+
} else {
410+
fprintf(stderr, "%s %s %s\n", msg,
411+
find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV), sb.buf);
412+
}
407413
strbuf_release(&sb);
408414
}
409415

t/t2020-checkout-detach.sh

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,127 @@ test_expect_success 'no advice given for explicit detached head state' '
186186
test_cmp expect.no-advice actual
187187
'
188188

189+
# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
190+
test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
191+
192+
# The first detach operation is more chatty than the following ones.
193+
cat >1st_detach <<-'EOF' &&
194+
Note: checking out 'HEAD^'.
195+
196+
You are in 'detached HEAD' state. You can look around, make experimental
197+
changes and commit them, and you can discard any commits you make in this
198+
state without impacting any branches by performing another checkout.
199+
200+
If you want to create a new branch to retain commits you create, you may
201+
do so (now or later) by using -b with the checkout command again. Example:
202+
203+
git checkout -b <new-branch-name>
204+
205+
HEAD is now at 7c7cd714e262 three
206+
EOF
207+
208+
# The remaining ones just show info about previous and current HEADs.
209+
cat >2nd_detach <<-'EOF' &&
210+
Previous HEAD position was 7c7cd714e262 three
211+
HEAD is now at 139b20d8e6c5 two
212+
EOF
213+
214+
cat >3rd_detach <<-'EOF' &&
215+
Previous HEAD position was 139b20d8e6c5 two
216+
HEAD is now at d79ce1670bdc one
217+
EOF
218+
219+
reset &&
220+
check_not_detached &&
221+
222+
# Various ways of *not* asking for ellipses
223+
224+
sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
225+
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
226+
check_detached &&
227+
test_i18ncmp 1st_detach actual &&
228+
229+
GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
230+
check_detached &&
231+
test_i18ncmp 2nd_detach actual &&
232+
233+
GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
234+
check_detached &&
235+
test_i18ncmp 3rd_detach actual &&
236+
237+
sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
238+
239+
# We only have four commits, but we can re-use them
240+
reset &&
241+
check_not_detached &&
242+
243+
# Make no mention of the env var at all
244+
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
245+
check_detached &&
246+
test_i18ncmp 1st_detach actual &&
247+
248+
GIT_PRINT_SHA1_ELLIPSIS='nope' &&
249+
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
250+
check_detached &&
251+
test_i18ncmp 2nd_detach actual &&
252+
253+
GIT_PRINT_SHA1_ELLIPSIS=nein &&
254+
git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
255+
check_detached &&
256+
test_i18ncmp 3rd_detach actual &&
257+
258+
true
259+
"
260+
261+
# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
262+
test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked to' "
263+
264+
# The first detach operation is more chatty than the following ones.
265+
cat >1st_detach <<-'EOF' &&
266+
Note: checking out 'HEAD^'.
267+
268+
You are in 'detached HEAD' state. You can look around, make experimental
269+
changes and commit them, and you can discard any commits you make in this
270+
state without impacting any branches by performing another checkout.
271+
272+
If you want to create a new branch to retain commits you create, you may
273+
do so (now or later) by using -b with the checkout command again. Example:
274+
275+
git checkout -b <new-branch-name>
276+
277+
HEAD is now at 7c7cd714e262... three
278+
EOF
279+
280+
# The remaining ones just show info about previous and current HEADs.
281+
cat >2nd_detach <<-'EOF' &&
282+
Previous HEAD position was 7c7cd714e262... three
283+
HEAD is now at 139b20d8e6c5... two
284+
EOF
285+
286+
cat >3rd_detach <<-'EOF' &&
287+
Previous HEAD position was 139b20d8e6c5... two
288+
HEAD is now at d79ce1670bdc... one
289+
EOF
290+
291+
reset &&
292+
check_not_detached &&
293+
294+
# Various ways of asking for ellipses...
295+
# The user can just use any kind of quoting (including none).
296+
297+
GIT_PRINT_SHA1_ELLIPSIS="yes" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
298+
check_detached &&
299+
test_i18ncmp 1st_detach actual &&
300+
301+
GIT_PRINT_SHA1_ELLIPSIS='yes' git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
302+
check_detached &&
303+
test_i18ncmp 2nd_detach actual &&
304+
305+
GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
306+
check_detached &&
307+
test_i18ncmp 3rd_detach actual &&
308+
309+
true
310+
"
311+
189312
test_done

0 commit comments

Comments
 (0)