remote set-head: set followRemoteHEAD to "warn" if "always"

When running "remote set-head" manually it is unlikely, that the user would actually like to have "fetch" always update the remote/HEAD. On the contrary, it is more likely, that the user would expect remote/HEAD to stay the way they manually set it, and just forgot about having "followRemoteHEAD" set to "always". When "followRemoteHEAD" is set to "always" make running "remote set-head" change the config to "warn". Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bence Ferdinandy committed Dec 5, 2024 at 13:16 UTC 012bc566bad79876f4809d1e730a348b419772d0
2 files changed +22 -1
builtin/remote.c
+11 -1
@@ -1433,6 +1433,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
1433 b_local_head = STRBUF_INIT;
1434 char *head_name = NULL;
1435 struct ref_store *refs = get_main_ref_store(the_repository);
1436 + struct remote *remote;
1437
1438 struct option options[] = {
1439 OPT_BOOL('a', "auto", &opt_a,
@@ -1443,8 +1444,10 @@ static int set_head(int argc, const char **argv, const char *prefix)
1444 };
1445 argc = parse_options(argc, argv, prefix, options,
1446 builtin_remote_sethead_usage, 0);
1446 - if (argc)
1447 + if (argc) {
1448 strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
1449 + remote = remote_get(argv[0]);
1450 + }
1451
1452 if (!opt_a && !opt_d && argc == 2) {
1453 head_name = xstrdup(argv[1]);
@@ -1483,6 +1486,13 @@ static int set_head(int argc, const char **argv, const char *prefix)
1486 }
1487 if (opt_a)
1488 report_set_head_auto(argv[0], head_name, &b_local_head, was_detached);
1489 + if (remote->follow_remote_head == FOLLOW_REMOTE_ALWAYS) {
1490 + struct strbuf config_name = STRBUF_INIT;
1491 + strbuf_addf(&config_name,
1492 + "remote.%s.followremotehead", remote->name);
1493 + git_config_set(config_name.buf, "warn");
1494 + strbuf_release(&config_name);
1495 + }
1496
1497 cleanup:
1498 free(head_name);
t/t5505-remote.sh
+11
@@ -505,6 +505,17 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
505 )
506 '
507
508 +test_expect_success 'set-head changes followRemoteHEAD always to warn' '
509 + (
510 + cd test &&
511 + git config set remote.origin.followRemoteHEAD "always" &&
512 + git remote set-head --auto origin &&
513 + git config get remote.origin.followRemoteHEAD >actual &&
514 + echo "warn" >expect &&
515 + test_cmp expect actual
516 + )
517 +'
518 +
519 cat >test/expect <<\EOF
520 refs/remotes/origin/side2
521 EOF