remote 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 edge cases, if HEAD is changed from a previous symbolic reference that was not a remote branch, explicitly call attention to this fact, and also notify the user if the previous reference was not a symbolic reference. Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bence Ferdinandy committed Nov 22, 2024 at 13:28 UTC dfe86fa06bdc46f1e5bf1f55e8571bb9a088d20c
2 files changed +109 -13
builtin/remote.c
+47 -12
@@ -1399,10 +1399,38 @@ static int show(int argc, const char **argv, const char *prefix)
1399 return result;
1400 }
1401
1402 +static void report_set_head_auto(const char *remote, const char *head_name,
1403 + struct strbuf *b_local_head, int was_detached) {
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(b_local_head->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 (!b_local_head->len)
1417 + printf(_("'%s/HEAD' is now created and points to '%s'\n"),
1418 + remote, head_name);
1419 + else if (was_detached && b_local_head->len)
1420 + printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"),
1421 + remote, b_local_head->buf, head_name);
1422 + else
1423 + printf(_("'%s/HEAD' used to point to '%s' "
1424 + "(which is not a remote branch), but now points to '%s'\n"),
1425 + remote, b_local_head->buf, head_name);
1426 + strbuf_release(&buf_prefix);
1427 +}
1428 +
1429 static int set_head(int argc, const char **argv, const char *prefix)
1430 {
1404 - int i, opt_a = 0, opt_d = 0, result = 0;
1405 - struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
1431 + int i, opt_a = 0, opt_d = 0, result = 0, was_detached;
1432 + struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
1433 + b_local_head = STRBUF_INIT;
1434 char *head_name = NULL;
1435 struct ref_store *refs = get_main_ref_store(the_repository);
1436
@@ -1440,20 +1468,27 @@ static int set_head(int argc, const char **argv, const char *prefix)
1468 } else
1469 usage_with_options(builtin_remote_sethead_usage, options);
1470
1443 - if (head_name) {
1444 - strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
1445 - /* make sure it's valid */
1446 - if (!refs_ref_exists(refs, b_remote_head.buf))
1447 - result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
1448 - else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
1449 - result |= error(_("Could not set up %s"), b_head.buf);
1450 - else if (opt_a)
1451 - printf("%s/HEAD set to %s\n", argv[0], head_name);
1452 - free(head_name);
1471 + if (!head_name)
1472 + goto cleanup;
1473 + strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
1474 + if (!refs_ref_exists(refs, b_remote_head.buf)) {
1475 + result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
1476 + goto cleanup;
1477 + }
1478 + was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
1479 + "remote set-head", &b_local_head);
1480 + if (was_detached == -1) {
1481 + result |= error(_("Could not set up %s"), b_head.buf);
1482 + goto cleanup;
1483 }
1484 + if (opt_a)
1485 + report_set_head_auto(argv[0], head_name, &b_local_head, was_detached);
1486
1487 +cleanup:
1488 + free(head_name);
1489 strbuf_release(&b_head);
1490 strbuf_release(&b_remote_head);
1491 + strbuf_release(&b_local_head);
1492 return result;
1493 }
1494
t/t5505-remote.sh
+62 -1
@@ -444,12 +444,63 @@ test_expect_success REFFILES 'set-head --auto failure' '
444 )
445 '
446
447 +test_expect_success 'set-head --auto detects creation' '
448 + (
449 + cd test &&
450 + git update-ref --no-deref -d refs/remotes/origin/HEAD &&
451 + git remote set-head --auto origin >output &&
452 + echo "${SQ}origin/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
453 + test_cmp expect output
454 + )
455 +'
456 +
457 +test_expect_success 'set-head --auto to update a non symbolic ref' '
458 + (
459 + cd test &&
460 + git update-ref --no-deref -d refs/remotes/origin/HEAD &&
461 + git update-ref refs/remotes/origin/HEAD HEAD &&
462 + HEAD=$(git log --pretty="%H") &&
463 + git remote set-head --auto origin >output &&
464 + echo "${SQ}origin/HEAD${SQ} was detached at ${SQ}${HEAD}${SQ} and now points to ${SQ}main${SQ}" >expect &&
465 + test_cmp expect output
466 + )
467 +'
468 +
469 +test_expect_success 'set-head --auto detects no change' '
470 + (
471 + cd test &&
472 + git remote set-head --auto origin >output &&
473 + echo "${SQ}origin/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
474 + test_cmp expect output
475 + )
476 +'
477 +
478 +test_expect_success 'set-head --auto detects change' '
479 + (
480 + cd test &&
481 + git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/ahead &&
482 + git remote set-head --auto origin >output &&
483 + echo "${SQ}origin/HEAD${SQ} has changed from ${SQ}ahead${SQ} and now points to ${SQ}main${SQ}" >expect &&
484 + test_cmp expect output
485 + )
486 +'
487 +
488 +test_expect_success 'set-head --auto detects strange ref' '
489 + (
490 + cd test &&
491 + git symbolic-ref refs/remotes/origin/HEAD refs/heads/main &&
492 + git remote set-head --auto origin >output &&
493 + echo "${SQ}origin/HEAD${SQ} used to point to ${SQ}refs/heads/main${SQ} (which is not a remote branch), but now points to ${SQ}main${SQ}" >expect &&
494 + test_cmp expect output
495 + )
496 +'
497 +
498 test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
499 (
500 cd test &&
501 git fetch two "refs/heads/*:refs/remotes/two/*" &&
502 git remote set-head --auto two >output 2>&1 &&
452 - echo "two/HEAD set to main" >expect &&
503 + echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
504 test_cmp expect output
505 )
506 '
@@ -468,6 +519,16 @@ test_expect_success 'set-head explicit' '
519 )
520 '
521
522 +test_expect_success 'set-head --auto reports change' '
523 + (
524 + cd test &&
525 + git remote set-head origin side2 &&
526 + git remote set-head --auto origin >output 2>&1 &&
527 + echo "${SQ}origin/HEAD${SQ} has changed from ${SQ}side2${SQ} and now points to ${SQ}main${SQ}" >expect &&
528 + test_cmp expect output
529 + )
530 +'
531 +
532 cat >test/expect <<EOF
533 Pruning origin
534 URL: $(pwd)/one