]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fetch: refactor do_fetch handling of followRemoteHEAD
authorMatt Hunter <m@lfurio.us>
Fri, 19 Jun 2026 09:44:25 +0000 (05:44 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Jun 2026 16:19:36 +0000 (09:19 -0700)
Update enum follow_remote_head_settings to include the value
FOLLOW_REMOTE_UNCONFIGURED as the new zero-initialized value for
followRemoteHEAD.  This will allow us to distinguish between the
variable being unset vs. explicitly set to 'create', which is ultimately
the system default.  The unnecessary indentation is removed.

The do_fetch function is likewise updated to perform its own decision
making to determine the effective followRemoteHEAD mode, falling back to
the system default if necessary.  This will enable the next patch to
introduce a user-configurable default.

Function set_head now accepts the mode as an argument rather than only
considering the value defined by the remote.

The use of the 'warn-if-not-$branch' value is awkward in the context of
a global default, since the branches will differ between individual
remotes.  For this reason, it's left out of this scheme and handling of
the no_warn_branch variable is untouched.  Since a remote-specific
value for followRemoteHEAD takes priority, we can assume that if
remote->no_warn_branch is set, then the remote is also asserting
FOLLOW_REMOTE_WARN as the effective operating mode, and it will be
honored by do_fetch.

Signed-off-by: Matt Hunter <m@lfurio.us>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fetch.c
remote.h

index 1036e8edbc59ae710fe415c641279e54952f50a7..ad63ca943c33d7a5d4e5d689bac16d2af6515b10 100644 (file)
@@ -1730,12 +1730,12 @@ static void warn_set_head(const char *remote, const char *head_name,
        strbuf_release(&buf_prefix);
 }
 
-static int set_head(const struct ref *remote_refs, struct remote *remote)
+static int set_head(const struct ref *remote_refs, struct remote *remote,
+                       int follow_remote_head)
 {
        int result = 0, create_only, baremirror, was_detached;
        struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
                      b_local_head = STRBUF_INIT;
-       int follow_remote_head = remote->follow_remote_head;
        const char *no_warn_branch = remote->no_warn_branch;
        char *head_name = NULL;
        struct ref *ref, *matches;
@@ -1902,6 +1902,7 @@ static int do_fetch(struct transport *transport,
        struct ref_update_display_info_array display_array = { 0 };
        struct strmap rejected_refs = STRMAP_INIT;
        int summary_width = 0;
+       int follow_remote_head;
 
        if (tags == TAGS_DEFAULT) {
                if (transport->remote->fetch_tags == 2)
@@ -1917,6 +1918,11 @@ static int do_fetch(struct transport *transport,
                        goto cleanup;
        }
 
+       if (transport->remote->follow_remote_head)
+               follow_remote_head = transport->remote->follow_remote_head;
+       else
+               follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
+
        if (rs->nr) {
                refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
        } else {
@@ -1925,7 +1931,7 @@ static int do_fetch(struct transport *transport,
                if (transport->remote->fetch.nr) {
                        refspec_ref_prefixes(&transport->remote->fetch,
                                             &transport_ls_refs_options.ref_prefixes);
-                       if (transport->remote->follow_remote_head != FOLLOW_REMOTE_NEVER)
+                       if (follow_remote_head != FOLLOW_REMOTE_NEVER)
                                do_set_head = 1;
                }
                if (branch && branch_has_merge_config(branch) &&
@@ -2132,7 +2138,7 @@ static int do_fetch(struct transport *transport,
                 * Way too many cases where this can go wrong so let's just
                 * ignore errors and fail silently for now.
                 */
-               set_head(remote_refs, transport->remote);
+               set_head(remote_refs, transport->remote, follow_remote_head);
        }
 
 cleanup:
index 54b17e4b028b5baa0845fdc1b948376165237719..72a54d84ad511d8cf820307b53e662b973f735db 100644 (file)
--- a/remote.h
+++ b/remote.h
@@ -62,12 +62,14 @@ struct remote_state {
 void remote_state_clear(struct remote_state *remote_state);
 struct remote_state *remote_state_new(void);
 
-       enum follow_remote_head_settings {
-               FOLLOW_REMOTE_NEVER = -1,
-               FOLLOW_REMOTE_CREATE = 0,
-               FOLLOW_REMOTE_WARN = 1,
-               FOLLOW_REMOTE_ALWAYS = 2,
-       };
+#define BUILTIN_FOLLOW_REMOTE_HEAD_DFLT FOLLOW_REMOTE_CREATE
+enum follow_remote_head_settings {
+       FOLLOW_REMOTE_UNCONFIGURED = 0,
+       FOLLOW_REMOTE_NEVER,
+       FOLLOW_REMOTE_CREATE,
+       FOLLOW_REMOTE_WARN,
+       FOLLOW_REMOTE_ALWAYS,
+};
 
 struct remote {
        struct hashmap_entry ent;