Skip to content

Commit 074b0aa

Browse files
ferdinandybgitster
authored andcommitted
set-head: better output for --auto
Currently, set-head --auto will print a message saying "remote/HEAD set to branch", which implies something was changed. Change the output of --auto, so the output actually reflects what was done: a) set a previously unset HEAD, b) change HEAD because remote changed or c) no updates. As a fourth output, if HEAD is changed from a previous value that was not a remote branch, explicitly call attention to this fact. Signed-off-by: Bence Ferdinandy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be5c3a3 commit 074b0aa

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

builtin/remote.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,10 +1399,34 @@ static int show(int argc, const char **argv, const char *prefix)
13991399
return result;
14001400
}
14011401

1402+
static void report_auto(const char *remote, const char *head_name,
1403+
struct strbuf *buf_prev) {
1404+
struct strbuf buf_prefix = STRBUF_INIT;
1405+
const char *prev_head = NULL;
1406+
1407+
strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
1408+
skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head);
1409+
1410+
if (prev_head && !strcmp(prev_head, head_name))
1411+
printf("'%s/HEAD' is unchanged and points to '%s'\n",
1412+
remote, head_name);
1413+
else if (prev_head)
1414+
printf("'%s/HEAD' has changed from '%s' and now points to '%s'\n",
1415+
remote, prev_head, head_name);
1416+
else if (buf_prev->len == 0)
1417+
printf("'%s/HEAD' is now created and points to '%s'\n",
1418+
remote, head_name);
1419+
else
1420+
printf("'%s/HEAD' used to point to '%s' "
1421+
"(which is unusual), but now points to '%s'\n",
1422+
remote, buf_prev->buf, head_name);
1423+
}
1424+
14021425
static int set_head(int argc, const char **argv, const char *prefix)
14031426
{
14041427
int i, opt_a = 0, opt_d = 0, result = 0;
1405-
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1428+
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT,
1429+
buf_prev = STRBUF_INIT;
14061430
char *head_name = NULL;
14071431
struct ref_store *refs = get_main_ref_store(the_repository);
14081432

@@ -1445,15 +1469,17 @@ static int set_head(int argc, const char **argv, const char *prefix)
14451469
/* make sure it's valid */
14461470
if (!refs_ref_exists(refs, buf2.buf))
14471471
result |= error(_("Not a valid ref: %s"), buf2.buf);
1448-
else if (refs_update_symref(refs, buf.buf, buf2.buf, "remote set-head", NULL))
1472+
else if (refs_update_symref(refs, buf.buf, buf2.buf, "remote set-head", &buf_prev))
14491473
result |= error(_("Could not setup %s"), buf.buf);
1450-
else if (opt_a)
1451-
printf("%s/HEAD set to %s\n", argv[0], head_name);
1474+
else if (opt_a) {
1475+
report_auto(argv[0], head_name, &buf_prev);
1476+
}
14521477
free(head_name);
14531478
}
14541479

14551480
strbuf_release(&buf);
14561481
strbuf_release(&buf2);
1482+
strbuf_release(&buf_prev);
14571483
return result;
14581484
}
14591485

t/t5505-remote.sh

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,51 @@ test_expect_success 'set-head --auto' '
429429
)
430430
'
431431

432+
test_expect_success 'set-head --auto detects creation' '
433+
(
434+
cd test &&
435+
rm .git/refs/remotes/origin/HEAD &&
436+
git remote set-head --auto origin >output &&
437+
echo "'\''origin/HEAD'\'' is now created and points to '\''main'\''" >expect &&
438+
test_cmp expect output
439+
)
440+
'
441+
442+
test_expect_success 'set-head --auto detects no change' '
443+
(
444+
cd test &&
445+
git remote set-head --auto origin >output &&
446+
echo "'\''origin/HEAD'\'' is unchanged and points to '\''main'\''" >expect &&
447+
test_cmp expect output
448+
)
449+
'
450+
451+
test_expect_success 'set-head --auto detects change' '
452+
(
453+
cd test &&
454+
echo "ref: refs/remotes/origin/ahead" >.git/refs/remotes/origin/HEAD &&
455+
git remote set-head --auto origin >output &&
456+
echo "'\''origin/HEAD'\'' has changed from '\''ahead'\'' and now points to '\''main'\''" >expect &&
457+
test_cmp expect output
458+
)
459+
'
460+
461+
test_expect_success 'set-head --auto detects strange ref' '
462+
(
463+
cd test &&
464+
echo "ref: refs/heads/main" >.git/refs/remotes/origin/HEAD &&
465+
git remote set-head --auto origin >output &&
466+
echo "'\''origin/HEAD'\'' used to point to '\''refs/heads/main'\'' (which is unusual), but now points to '\''main'\''" >expect &&
467+
test_cmp expect output
468+
)
469+
'
470+
432471
test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
433472
(
434473
cd test &&
435474
git fetch two "refs/heads/*:refs/remotes/two/*" &&
436475
git remote set-head --auto two >output 2>&1 &&
437-
echo "two/HEAD set to main" >expect &&
476+
echo "'\''two/HEAD'\'' is now created and points to '\''main'\''" >expect &&
438477
test_cmp expect output
439478
)
440479
'
@@ -453,6 +492,17 @@ test_expect_success 'set-head explicit' '
453492
)
454493
'
455494

495+
496+
test_expect_success 'set-head --auto reports change' '
497+
(
498+
cd test &&
499+
git remote set-head origin side2 &&
500+
git remote set-head --auto origin >output 2>&1 &&
501+
echo "'\''origin/HEAD'\'' has changed from '\''side2'\'' and now points to '\''main'\''" >expect &&
502+
test_cmp expect output
503+
)
504+
'
505+
456506
cat >test/expect <<EOF
457507
Pruning origin
458508
URL: $(pwd)/one

0 commit comments

Comments
 (0)