set remote/HEAD with fetch
PATCH 1/1 fetch: fix erroneous set_head advice message
In reply to Teng Long9e2b7005be (fetch set_head: add warn-if-not-$branch option, 2024-12-05) tried to expand the advice message for set_head with the new option, but unfortunately did not manage to add the right incantation. Fix the advice message with the correct usage of warn-if-not-$branch.
--- builtin/fetch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
builtin/fetch.c +2 -2
@@ -1588,8 +1588,8 @@ static void set_head_advice_msg(const char *remote, const char *head_name)N_ ("Run 'git remote set-head %s %s' to follow the change, or set\n" "'remote.%s.followRemoteHEAD' configuration option to a different value\n" "if you do not want to see this message. Specifically running\n" -"'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n" -"until the remote changes HEAD to something else." ); +"'git config set remote.%s.followRemoteHEAD warn-if-not-branch-%s'\n" +"will disable the warning until the remote changes HEAD to something else." );advise_if_enabled (ADVICE_FETCH_SET_HEAD_WARN ,_ (message_advice_set_head), remote, head_name, remote, remote, head_name);
base-commit: 1b4e9a5f8b5f048972c21fe8acafe0404096f694
Signature · 1 line
2.48.0.rc0.146.g306ab352f4
Re: [PATCH] fetch: fix erroneous set_head advice message
Quoted 16 lines · Bence Ferdinandy
9e2b7005be(fetchset_head: add warn-if-not-$branch option, 2024-12-05) tried to expand the advice message forset_headwith the new option, but unfortunately did not manage to add the right incantation. Fix the advice message with the correct usage of warn-if-not-$branch.Reported-by Teng Long <[email protected]>Signed-off-by Bence Ferdinandy <[email protected]>--- builtin/fetch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)builtin/fetch.c +0
-0
builtin/fetch.c static void set_head_advice_msg(const char *remote, const char *head_name) · line 1595
Quoted 10 lines
builtin/fetch.c +2
-2@@ -1588,8 +1588,8 @@ static void set_head_advice_msg(const char *remote, const char *head_name)N_ ("Run 'git remote set-head %s %s' to follow the change, or set\n" "'remote.%s.followRemoteHEAD' configuration option to a different value\n" "if you do not want to see this message. Specifically running\n" -"'git config set remote.%s.followRemoteHEAD %s' will disable the warning\n" -"until the remote changes HEAD to something else." ); +"'git config set remote.%s.followRemoteHEAD warn-if-not-branch-%s'\n" +"will disable the warning until the remote changes HEAD to something else." );advise_if_enabled (ADVICE_FETCH_SET_HEAD_WARN ,_ (message_advice_set_head), remote, head_name, remote, remote, head_name);
Thanks, both. Will queue.
PATCH v3 3/3 remote set-head: set followRemoteHEAD to "warn" if "always"
In reply to Bence FerdinandyWhen 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".
---
Notes:
v3: s/output/actual
builtin/remote.c | 12 +++++++++++- t/t5505-remote.sh | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-)
builtin/remote.c +11 -1
@@ -1438,6 +1438,7 @@ static int set_head(int argc, const char **argv, const char *prefix, b_local_head =STRBUF_INIT ;char *head_name =NULL ;struct ref_store *refs =get_main_ref_store (the_repository); +struct remote *remote;struct option options[] = { OPT_BOOL('a', "auto", &opt_a,
@@ -1448,8 +1449,10 @@ static int set_head(int argc, const char **argv, const char *prefix, }; argc =parse_options (argc, argv, prefix, options, builtin_remote_sethead_usage,0 ); -if (argc) +if (argc) {strbuf_addf (&b_head,"refs/remotes/%s/HEAD" , argv[0 ]); + remote =remote_get (argv[0 ]); + }if (!opt_a && !opt_d && argc ==2 ) { head_name =xstrdup (argv[1 ]);
@@ -1488,6 +1491,13 @@ static int set_head(int argc, const char **argv, const char *prefix, }if (opt_a)report_set_head_auto (argv[0 ], head_name, &b_local_head, was_detached); +if (remote->follow_remote_head ==FOLLOW_REMOTE_ALWAYS ) { +struct strbuf config_name =STRBUF_INIT ; +strbuf_addf (&config_name, +"remote.%s.followremotehead" , remote->name); +git_config_set (config_name.buf,"warn" ); +strbuf_release (&config_name); + } cleanup:free (head_name);
t/t5505-remote.sh +11 -0
@@ -504,6 +504,17 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' ' ) ' +test_expect_success 'set-head changes followRemoteHEAD always to warn' ' + ( + cd test && + git config set remote.origin.followRemoteHEAD "always" && + git remote set-head --auto origin && + git config get remote.origin.followRemoteHEAD >actual && + echo "warn" >expect && + test_cmp expect actual + ) +' + cat >test/expect <<\EOF refs/remotes/origin/side2 EOF
Signature · 1 line
2.47.1.398.g18e7475ebe
Re: [PATCH v2 1/3] fetch set_head: move warn advice into advise_if_enabled
In reply to Bence FerdinandyQuoted 11 lines · Bence Ferdinandy
builtin/fetch.c +10
-0+static void set_head_advice_msg (const char *remote,const char *head_name) +{ +const char message_advice_set_head[] = +N_ ("Run 'git remote set-head %s %s' to follow the change, or set\n" +"'remote.%s.followRemoteHEAD' configuration option to a different value\n" +"if you do not want to see this message." ); + +advise_if_enabled (ADVICE_FETCH_SET_HEAD_WARN ,_ (message_advice_set_head), + remote, head_name, remote); +}
builtin/fetch.c static const char *strip_refshead(const char *name){
Quoted 10 lines
builtin/fetch.c +1
-2else if (updateres && buf_prev->len) {printf ("'HEAD' at '%s' is '%s', " "but we have a detached HEAD pointing to '%s' locally.\n" , remote, head_name, buf_prev->buf); -printf ("Run 'git remote set-head %s %s' to follow the change.\n" , - remote, head_name); +set_head_advice_msg (remote, head_name); }strbuf_release (&buf_prefix); }
Looking good.
t/t5510-fetch.sh test_expect_success "fetch test followRemoteHEAD warn no change" ' · line 126
Quoted 10 lines
t/t5510-fetch.sh +0
-1@@ -123,7 +123,6 @@ test_expect_success "fetch test followRemoteHEAD warn no change" ' git fetch >output && echo "${SQ}HEAD${SQ} at ${SQ}origin${SQ} is ${SQ}main${SQ}," \ "but we have ${SQ}other${SQ} locally." >expect && - echo "Run ${SQ}git remote set-head origin main${SQ} to follow the change." >>expect && test_cmp expect output &&
OK. We just lose this part of the test, not because the advice messages are squelched but because they go to the standard error stream. An obvious alternative is to capture it in a separate file and grep, i.e.
git fetch >output 2>error &&
echo ... >expect &&
test_grep "Run ${SQ}git remote set-head" error &&
test_cmp expect outputIf we were testing that the advice mechanism is used, do that with and without the ADVICE_FETCH_SET_HEAD_WARN squelched, but probably it is a bit too much, in the sense that we are not in the business of testing that the advice mechanism works correctly in this series.
PATCH v2 1/1 fetch: add configuration for set_head behaviour
In reply to Junio C HamanoIn the current implementation, if refs/remotes/$remote/HEAD does not exist, running fetch will create it, but if it does exist it will not do anything, which is a somewhat safe and minimal approach. Unfortunately, for users who wish to NOT have refs/remotes/$remote/HEAD set for any reason (e.g. so that git rev-parse origin doesn't accidentally point them somewhere they do not want to), there is no way to remove this behaviour. On the other side of the spectrum, users may want fetch to automatically update HEAD or at least give them a warning if something changed on the remote.
Introduce a new setting, remote.$remote.followRemoteHEAD with four options:
- "never": do not ever do anything, not even create
- "create": the current behaviour, now the default behaviour
- "warn": print a message if remote and local HEAD is different
- "always": silently update HEAD on every change
---
Notes:
This patch is built off v15 of the set-head/fetch updates series:
https://lore.kernel.org/git/[email protected]/T/#m13a0c3e8919872188ef07fd9fd984c78e8aa35ba
v2:
- magic numbers converted to enum
- since the enum is descriptive (well, along with the documentation) I felt there is no need for the comment explaining the values
Documentation/config/remote.txt | 11 ++++ builtin/fetch.c | 46 ++++++++++++-- remote.c | 9 +++ remote.h | 9 +++ t/t5510-fetch.sh | 102 ++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 6 deletions(-)
Documentation/config/remote.txt +11 -0
@@ -101,6 +101,17 @@ remote.<name>.serverOption:: The default set of server options used when fetching from this remote. These server options can be overridden by the `--server-option=` command line arguments. + +remote.<name>.followRemoteHEAD:: + How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`. + The default value is "create", which will create `remotes/<name>/HEAD` + if it exists on the remote, but not locally, but will not touch an + already existing local reference. Setting to "warn" will print + a message if the remote has a different value, than the local one and + in case there is no local reference, it behaves like "create". Setting + to "always" will silently update it to the value on the remote. + Finally, setting it to "never" will never change or create the local + reference. + This is a multi-valued variable, and an empty value can be used in a higher priority configuration file (e.g. `.git/config` in a repository) to clear
builtin/fetch.c +40 -6
84 lines not on this page · raw message
remote.c +9 -0
@@ -514,6 +514,15 @@ static int handle_config(const char *key, const char *value, }else if (!strcmp (subkey,"serveroption" )) {return parse_transport_option (key, value, &remote->server_options); + }else if (!strcmp (subkey,"followremotehead" )) { +if (!strcmp (value,"never" )) + remote->follow_remote_head =FOLLOW_REMOTE_NEVER ; +else if (!strcmp (value,"create" )) + remote->follow_remote_head =FOLLOW_REMOTE_CREATE ; +else if (!strcmp (value,"warn" )) + remote->follow_remote_head =FOLLOW_REMOTE_WARN ; +else if (!strcmp (value,"always" )) + remote->follow_remote_head =FOLLOW_REMOTE_ALWAYS ; }return 0 ; }
remote.h +9 -0
@@ -59,6 +59,13 @@ 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 , + }; +struct remote {struct hashmap_entry ent;
@@ -107,6 +114,8 @@ struct remote {char *http_proxy_authmethod;struct string_list server_options; + +enum follow_remote_head_settings follow_remote_head; };/**
t/t5510-fetch.sh +102 -0
108 lines not on this page · raw message
Signature · 1 line
2.47.1.295.g12e80f91dc.dirty
PATCH v3 1/1 fetch: add configuration for set_head behaviour
In the current implementation, if refs/remotes/$remote/HEAD does not exist, running fetch will create it, but if it does exist it will not do anything, which is a somewhat safe and minimal approach. Unfortunately, for users who wish to NOT have refs/remotes/$remote/HEAD set for any reason (e.g. so that git rev-parse origin doesn't accidentally point them somewhere they do not want to), there is no way to remove this behaviour. On the other side of the spectrum, users may want fetch to automatically update HEAD or at least give them a warning if something changed on the remote.
Introduce a new setting, remote.$remote.followRemoteHEAD with four options:
- "never": do not ever do anything, not even create
- "create": the current behaviour, now the default behaviour
- "warn": print a message if remote and local HEAD is different
- "always": silently update HEAD on every change
---
Notes:
This patch is built off v15 of the set-head/fetch updates series:
https://lore.kernel.org/git/[email protected]/T/#m13a0c3e8919872188ef07fd9fd984c78e8aa35ba
v2:
- magic numbers converted to enum
- since the enum is descriptive (well, along with the documentation) I felt there is no need for the comment explaining the values
v3: missed a magic number in v2
Documentation/config/remote.txt | 11 ++++ builtin/fetch.c | 46 ++++++++++++-- remote.c | 9 +++ remote.h | 9 +++ t/t5510-fetch.sh | 102 ++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 6 deletions(-)
Documentation/config/remote.txt +11 -0
@@ -101,6 +101,17 @@ remote.<name>.serverOption:: The default set of server options used when fetching from this remote. These server options can be overridden by the `--server-option=` command line arguments. + +remote.<name>.followRemoteHEAD:: + How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`. + The default value is "create", which will create `remotes/<name>/HEAD` + if it exists on the remote, but not locally, but will not touch an + already existing local reference. Setting to "warn" will print + a message if the remote has a different value, than the local one and + in case there is no local reference, it behaves like "create". Setting + to "always" will silently update it to the value on the remote. + Finally, setting it to "never" will never change or create the local + reference. + This is a multi-valued variable, and an empty value can be used in a higher priority configuration file (e.g. `.git/config` in a repository) to clear
builtin/fetch.c +40 -6
84 lines not on this page · raw message
remote.c +9 -0
@@ -514,6 +514,15 @@ static int handle_config(const char *key, const char *value, }else if (!strcmp (subkey,"serveroption" )) {return parse_transport_option (key, value, &remote->server_options); + }else if (!strcmp (subkey,"followremotehead" )) { +if (!strcmp (value,"never" )) + remote->follow_remote_head =FOLLOW_REMOTE_NEVER ; +else if (!strcmp (value,"create" )) + remote->follow_remote_head =FOLLOW_REMOTE_CREATE ; +else if (!strcmp (value,"warn" )) + remote->follow_remote_head =FOLLOW_REMOTE_WARN ; +else if (!strcmp (value,"always" )) + remote->follow_remote_head =FOLLOW_REMOTE_ALWAYS ; }return 0 ; }
remote.h +9 -0
@@ -59,6 +59,13 @@ 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 , + }; +struct remote {struct hashmap_entry ent;
@@ -107,6 +114,8 @@ struct remote {char *http_proxy_authmethod;struct string_list server_options; + +enum follow_remote_head_settings follow_remote_head; };/**
t/t5510-fetch.sh +102 -0
108 lines not on this page · raw message
Range-diff against v2:
1: 60986e890e ! 1: 4e9ac5524a fetch: add configuration for set_head behaviour
@@ Notes
- since the enum is descriptive (well, along with the documentation)
I felt there is no need for the comment explaining the values
+ v3: missed a magic number in v2
+
## Documentation/config/remote.txt ##
@@ Documentation/config/remote.txt: remote.<name>.serverOption::
The default set of server options used when fetching from this remote.
@@ builtin/fetch.c: static int set_head(const struct ref *remote_refs)
if (!head_name)
goto cleanup;
is_bare = is_bare_repository();
-+ create_only = follow_remote_head == 2 ? 0 : !is_bare;
++ create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !is_bare;
if (is_bare) {
strbuf_addstr(&b_head, "HEAD");
strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);Signature · 1 line
2.47.1.295.g66bad26ef4
Re: [PATCH v3] fetch: add configuration for set_head behaviour
Quoted 6 lines · Bence Ferdinandy
v2:
- magic numbers converted to enum
- since the enum is descriptive (well, along with the documentation) I felt there is no need for the comment explaining the valuesv3: missed a magic number in v2
Both changes look quite good. Will queue.
Let me mark the topic for 'next'.
Thanks.
PATCH v3 1/1 fetch: add configuration for set_head behaviour
In reply to Bence FerdinandyIn the current implementation, if refs/remotes/$remote/HEAD does not exist, running fetch will create it, but if it does exist it will not do anything, which is a somewhat safe and minimal approach. Unfortunately, for users who wish to NOT have refs/remotes/$remote/HEAD set for any reason (e.g. so that git rev-parse origin doesn't accidentally point them somewhere they do not want to), there is no way to remove this behaviour. On the other side of the spectrum, users may want fetch to automatically update HEAD or at least give them a warning if something changed on the remote.
Introduce a new setting, remote.$remote.followRemoteHEAD with four options:
- "never": do not ever do anything, not even create
- "create": the current behaviour, now the default behaviour
- "warn": print a message if remote and local HEAD is different
- "always": silently update HEAD on every change
---
Notes:
This patch is built off v15 of the set-head/fetch updates series:
https://lore.kernel.org/git/[email protected]/T/#m13a0c3e8919872188ef07fd9fd984c78e8aa35ba
v2:
- magic numbers converted to enum
- since the enum is descriptive (well, along with the documentation) I felt there is no need for the comment explaining the values
v3: missed a magic number in v2
Documentation/config/remote.txt | 11 ++++ builtin/fetch.c | 46 ++++++++++++-- remote.c | 9 +++ remote.h | 9 +++ t/t5510-fetch.sh | 102 ++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 6 deletions(-)
Documentation/config/remote.txt +11 -0
@@ -101,6 +101,17 @@ remote.<name>.serverOption:: The default set of server options used when fetching from this remote. These server options can be overridden by the `--server-option=` command line arguments. + +remote.<name>.followRemoteHEAD:: + How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`. + The default value is "create", which will create `remotes/<name>/HEAD` + if it exists on the remote, but not locally, but will not touch an + already existing local reference. Setting to "warn" will print + a message if the remote has a different value, than the local one and + in case there is no local reference, it behaves like "create". Setting + to "always" will silently update it to the value on the remote. + Finally, setting it to "never" will never change or create the local + reference. + This is a multi-valued variable, and an empty value can be used in a higher priority configuration file (e.g. `.git/config` in a repository) to clear
builtin/fetch.c +40 -6
84 lines not on this page · raw message
remote.c +9 -0
@@ -514,6 +514,15 @@ static int handle_config(const char *key, const char *value, }else if (!strcmp (subkey,"serveroption" )) {return parse_transport_option (key, value, &remote->server_options); + }else if (!strcmp (subkey,"followremotehead" )) { +if (!strcmp (value,"never" )) + remote->follow_remote_head =FOLLOW_REMOTE_NEVER ; +else if (!strcmp (value,"create" )) + remote->follow_remote_head =FOLLOW_REMOTE_CREATE ; +else if (!strcmp (value,"warn" )) + remote->follow_remote_head =FOLLOW_REMOTE_WARN ; +else if (!strcmp (value,"always" )) + remote->follow_remote_head =FOLLOW_REMOTE_ALWAYS ; }return 0 ; }
remote.h +9 -0
@@ -59,6 +59,13 @@ 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 , + }; +struct remote {struct hashmap_entry ent;
@@ -107,6 +114,8 @@ struct remote {char *http_proxy_authmethod;struct string_list server_options; + +enum follow_remote_head_settings follow_remote_head; };/**
t/t5510-fetch.sh +102 -0
108 lines not on this page · raw message
Range-diff against v2:
1: 60986e890e ! 1: 4e9ac5524a fetch: add configuration for set_head behaviour
@@ Notes
- since the enum is descriptive (well, along with the documentation)
I felt there is no need for the comment explaining the values
+ v3: missed a magic number in v2
+
## Documentation/config/remote.txt ##
@@ Documentation/config/remote.txt: remote.<name>.serverOption::
The default set of server options used when fetching from this remote.
@@ builtin/fetch.c: static int set_head(const struct ref *remote_refs)
if (!head_name)
goto cleanup;
is_bare = is_bare_repository();
-+ create_only = follow_remote_head == 2 ? 0 : !is_bare;
++ create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !is_bare;
if (is_bare) {
strbuf_addstr(&b_head, "HEAD");
strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);Signature · 1 line
2.47.1.295.g66bad26ef4
PATCH v3 2/2 fetch: set remote/HEAD if it does not exist
In reply to Bence FerdinandyIf the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can.
---
Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the new REF_CREATE_ONLY to atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning
builtin/fetch.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)
builtin/fetch.c +71 -0
@@ -1577,6 +1577,70 @@ static int backfill_tags(struct display_state *display_state,return retcode; } +static const char *abbrev_ref (const char *name,const char *prefix) +{ +skip_prefix (name, prefix, &name); +return name; +} +#define abbrev_branch (name) abbrev_ref((name), "refs/heads/") + +static inline int set_head (const struct ref *remote_refs) +{ +int result, ref_changed =0 ; +struct strbuf b_head =STRBUF_INIT , b_remote_head =STRBUF_INIT , b_local_head =STRBUF_INIT , b_prefix =STRBUF_INIT ; +const char *remote = gtransport->remote->name; +char * head_name =NULL ; +struct ref *ref, *matches; +struct ref *fetch_map =NULL , **fetch_map_tail = &fetch_map; +struct refspec_item refspec = { + .force =0 , + .pattern =1 , + .src = (char *)"refs/heads/*" , + .dst = (char *)"refs/heads/*" , + }; +struct string_list heads =STRING_LIST_INIT_DUP ; + +get_fetch_map (remote_refs, &refspec, &fetch_map_tail,0 ); + matches =guess_remote_head (find_ref_by_name (remote_refs,"HEAD" ), + fetch_map,1 ); +for (ref = matches; ref; ref = ref->next) +string_list_append (&heads,abbrev_branch (ref->name)); + + +if (!heads.nr) + result =1 ; +else if (heads.nr >1 ) { + result =1 ; + }else + head_name =xstrdup (heads.items[0 ].string); +if (head_name) { +strbuf_addf (&b_head,"refs/remotes/%s/HEAD" , remote); +strbuf_addf (&b_remote_head,"refs/remotes/%s/%s" , remote, head_name); +strbuf_addf (&b_prefix,"refs/remotes/%s/" , remote); +if (!refs_read_symbolic_ref (get_main_ref_store (the_repository),b_head.buf,&b_local_head)) { + ref_changed =strcmp (b_remote_head.buf,b_local_head.buf); +if (heads.nr ==1 && ref_changed) { +printf ("The ref \'%s/HEAD\' has changed from the locally recorded " +"\'%s\' to \'%s\'.\n" ,remote,abbrev_ref (b_local_head.buf,b_prefix.buf), head_name); +printf ("Run \'git remote set-head -a %s\' to set it automatically.\n" , remote); + } + } +/* make sure it's valid */ +if (!refs_ref_exists (get_main_ref_store (the_repository), b_remote_head.buf)) + result =1 ; +else if (refs_update_symref (get_main_ref_store (the_repository), b_head.buf, + b_remote_head.buf,REF_CREATE_ONLY ,"fetch" )) + result =1 ; +free (head_name); + } + +strbuf_release (&b_head); +strbuf_release (&b_local_head); +strbuf_release (&b_remote_head); +strbuf_release (&b_prefix); +return result; +} +static int do_fetch (struct transport *transport,struct refspec *rs,const struct fetch_config *config)
@@ -1646,6 +1710,8 @@ static int do_fetch(struct transport *transport, "refs/tags/"); } +strvec_push (&transport_ls_refs_options.ref_prefixes,"HEAD" ); +if (must_list_refs) {trace2_region_enter ("fetch" ,"remote_refs" , the_repository); remote_refs = transport_get_remote_refs(transport,
@@ -1790,6 +1856,10 @@ static int do_fetch(struct transport *transport, "you need to specify exactly one branch with the --set-upstream option")); } } +if (!set_head (remote_refs)) +printf ("Ran into issues with \'%s/HEAD\',\n" +"use \'git remote set-head -a %s\' to investigate" , + gtransport->remote->name,gtransport->remote->name); cleanup:if (retcode) {
@@ -2020,6 +2090,7 @@ static int fetch_multiple(struct string_list *list, int max_children,return !!result; } +/* * Fetching from the promisor remote should use the given filter-spec * or inherit the default filter-spec from the config.
Signature · 1 line
2.46.1.546.gaa0cd55cd3.dirty
Re: [PATCH v3 2/2] fetch: set remote/HEAD if it does not exist
Quoted 4 lines · Bence Ferdinandy
If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can.
Signed-off-by Bence Ferdinandy <[email protected]>
Quoted 8 lines
---
Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the newREF_CREATE_ONLYto atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warning
Except for one thing, I can tell from the above that the design is sensible without looking at the actual patch. Nicely described.
The "one thing" is what to do if you normally do *not* keep track of the remote-tracking refs for this particular remote.
If I am only fetching refs (or HEAD) in FETCH_HEAD for immediate consumtion by doing "git pull bence HEAD" with something like
[remote "bence"]
URL = http://github.com/bence/git
(with no other configuration under remote.bence.* hierarchy), I do not think I want the code to create refs/remotes/bence/HEAD, with no other remote-tracking refs in the same hierarchy, even if it finds no existing refs/remotes/bence/HEAD there. For that, I suspect that you'd need to see if the pointee of refs/remotes/bence/HEAD either already exists or this fetch is about to create it, or something like that, before deciding to create a ref with the REF_CREATE_ONLY flag.
If you are doing so already in the code (I haven't started reading it yet at this moment), you should mention it in your proposed log message, so discount my earlier "Nicely described" a bit ;-).
Let's see what actually happens in the code now.
builtin/fetch.c static int backfill_tags(struct display_state *display_state, · line 1585
Quoted 6 lines
builtin/fetch.c +6
-0+static const char *abbrev_ref (const char *name,const char *prefix) +{ +skip_prefix (name, prefix, &name); +return name; +} +#define abbrev_branch (name) abbrev_ref((name), "refs/heads/")
We do not call the act of optionally skipping prefix "abbreviate" in this project (object names are abbreviated but that is done by chomping the later bytes of a long name).
I suspect that a caller of either of these two functions are inherently buggy in that they _optionally_ skip the prefix so they do the same thing for "refs/heads/main" and "main" but not "heads/main". The callsites may need to be inspected to see how they should deal with cases where skip_prefix() did *not* see anything to skip.
Quoted 1 line
+static inline int
set_head(const struct ref *remote_refs)
Drop "inline". You do not want to inline this much code if there are many callers, and if you have only one caller, the compiler would inline it into the sole caller if it is the more efficient thing to do.
Quoted 23 lines
builtin/fetch.c +23
-0+{ +int result, ref_changed =0 ; +struct strbuf b_head =STRBUF_INIT , b_remote_head =STRBUF_INIT , b_local_head =STRBUF_INIT , b_prefix =STRBUF_INIT ; +const char *remote = gtransport->remote->name; +char * head_name =NULL ; +struct ref *ref, *matches; +struct ref *fetch_map =NULL , **fetch_map_tail = &fetch_map; +struct refspec_item refspec = { + .force =0 , + .pattern =1 , + .src = (char *)"refs/heads/*" , + .dst = (char *)"refs/heads/*" , + }; +struct string_list heads =STRING_LIST_INIT_DUP ; + +get_fetch_map (remote_refs, &refspec, &fetch_map_tail,0 ); + matches =guess_remote_head (find_ref_by_name (remote_refs,"HEAD" ), + fetch_map,1 ); +for (ref = matches; ref; ref = ref->next) +string_list_append (&heads,abbrev_branch (ref->name)); + + +if (!heads.nr)
builtin/fetch.c static int backfill_tags(struct display_state *display_state, · line 1620
Quoted 10 lines
builtin/fetch.c +10
-0+ result =1 ; + else if (heads.nr > 1) { + result =1 ; + } else + head_name =xstrdup (heads.items[0 ].string); +if (head_name) { +strbuf_addf (&b_head,"refs/remotes/%s/HEAD" , remote); +strbuf_addf (&b_remote_head,"refs/remotes/%s/%s" , remote, head_name); +strbuf_addf (&b_prefix,"refs/remotes/%s/" , remote); +if (!refs_read_symbolic_ref (get_main_ref_store (the_repository),b_head.buf,&b_local_head)) {
Overly long lines around here cannot be sanely reviewed on my terminal, so I'll stop here for now.
Do not omit SP after ",", as that is the least effective way to shorten overly long lines. Instead, consider introducing a one-time-use temporary variables with meaningful name, e.g.
struct ref_store *store = get_main_ref_store(the_repository);
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
...
if (!refs_read_symbolic_ref(store, b_head.buf, ¤t)) {
/* it's current value is in "current" */
...Re: [PATCH v3 2/2] fetch: set remote/HEAD if it does not exist
Quoted 27 lines · Junio C Hamano
Quoted 4 lines · Bence Ferdinandy
If the user has remote/HEAD set already and it looks like it has changed on the server, then print a message, otherwise set it if we can.
Signed-off-by Bence Ferdinandy <[email protected]>Quoted 8 lines
---
Notes:
v3: - does not rely on remote set-head anymore so it only authenticates
once
- uses the newREF_CREATE_ONLYto atomically check if the ref exists and only write it if it doesn't
- in all other cases the maximum it does is print a warningExcept for one thing, I can tell from the above that the design is sensible without looking at the actual patch. Nicely described.
The "one thing" is what to do if you normally do *not* keep track of the remote-tracking refs for this particular remote.
If I am only fetching refs (or HEAD) in
FETCH_HEADfor immediate consumtion by doing "git pull bence HEAD" with something like[remote "bence"]
URL = http://github.com/bence/git
Quoted 8 lines
(with no other configuration under remote.bence.* hierarchy), I do not think I want the code to create refs/remotes/bence/HEAD, with no other remote-tracking refs in the same hierarchy, even if it finds no existing refs/remotes/bence/HEAD there. For that, I suspect that you'd need to see if the pointee of refs/remotes/bence/HEAD either already exists or this fetch is about to create it, or something like that, before deciding to create a ref with the
REF_CREATE_ONLYflag.
I'll take another look at this.
Quoted 4 lines
If you are doing so already in the code (I haven't started reading it yet at this moment), you should mention it in your proposed log message, so discount my earlier "Nicely described" a bit ;-).
:D
Quoted 14 lines
Let's see what actually happens in the code now.
builtin/fetch.c static int backfill_tags(struct display_state *display_state, · line 1585
Quoted 6 lines
builtin/fetch.c +6
-0+static const char *abbrev_ref (const char *name,const char *prefix) +{ +skip_prefix (name, prefix, &name); +return name; +} +#define abbrev_branch (name) abbrev_ref((name), "refs/heads/")We do not call the act of optionally skipping prefix "abbreviate" in this project (object names are abbreviated but that is done by chomping the later bytes of a long name).
Quoted 6 lines
I suspect that a caller of either of these two functions are inherently buggy in that they _optionally_ skip the prefix so they do the same thing for "refs/heads/main" and "main" but not "heads/main". The callsites may need to be inspected to see how they should deal with cases where
skip_prefix()did *not* see anything to skip.
I copied this verbatim from builtin/remote.c
https://github.com/git/git/blob/master/builtin/remote.c#L267 so it looked like a safe bet T_T I'm not 100% sure, but I think there's no use case where you pass a ref directly to remote except for set_head, but there it's not used, so it looks safe?
That being said, I can change the name(s) to not use abbrev here, especially if you have a good suggestion. Maybe strip_prefix_ref/branch? Again in fetch, we (should) know what type of refs we're calling it on, so should be safe?
Quoted 7 lines
Quoted 2 lines
+static inline int
set_head(const struct ref *remote_refs)Drop "inline". You do not want to inline this much code if there are many callers, and if you have only one caller, the compiler would inline it into the sole caller if it is the more efficient thing to do.
Ack.
Quoted 42 lines
Quoted 24 lines
builtin/fetch.c +23
-0+{ +int result, ref_changed =0 ; +struct strbuf b_head =STRBUF_INIT , b_remote_head =STRBUF_INIT , b_local_head =STRBUF_INIT , b_prefix =STRBUF_INIT ; +const char *remote = gtransport->remote->name; +char * head_name =NULL ; +struct ref *ref, *matches; +struct ref *fetch_map =NULL , **fetch_map_tail = &fetch_map; +struct refspec_item refspec = { + .force =0 , + .pattern =1 , + .src = (char *)"refs/heads/*" , + .dst = (char *)"refs/heads/*" , + }; +struct string_list heads =STRING_LIST_INIT_DUP ; + +get_fetch_map (remote_refs, &refspec, &fetch_map_tail,0 ); + matches =guess_remote_head (find_ref_by_name (remote_refs,"HEAD" ), + fetch_map,1 ); +for (ref = matches; ref; ref = ref->next) +string_list_append (&heads,abbrev_branch (ref->name)); + + +if (!heads.nr)builtin/fetch.c static int backfill_tags(struct display_state *display_state, · line 1620
Quoted 10 lines
builtin/fetch.c +10
-0+ result =1 ; + else if (heads.nr > 1) { + result =1 ; + } else + head_name =xstrdup (heads.items[0 ].string); +if (head_name) { +strbuf_addf (&b_head,"refs/remotes/%s/HEAD" , remote); +strbuf_addf (&b_remote_head,"refs/remotes/%s/%s" , remote, head_name); +strbuf_addf (&b_prefix,"refs/remotes/%s/" , remote); +if (!refs_read_symbolic_ref (get_main_ref_store (the_repository),b_head.buf,&b_local_head)) {Overly long lines around here cannot be sanely reviewed on my terminal, so I'll stop here for now.
Do not omit SP after ",", as that is the least effective way to shorten overly long lines. Instead, consider introducing a one-time-use temporary variables with meaningful name, e.g.
Quoted 7 lines
struct ref_store *store = get_main_ref_store(the_repository);
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
...
if (!refs_read_symbolic_ref(store, b_head.buf, ¤t)) {
/* it's current value is in "current" */
...Ah, yeah terribly sorry about this, I've gotten so used to automatic formatters, that I just didn't take a second look. I'll be more careful in the next round.
Signature · 1 line
bence.ferdinandy.com
RFC PATCH v2 2/2 set-head: do not update if there is no change
In reply to Bence FerdinandyCurrently, even if there is no actual change to remote/HEAD calling remote set-head will overwrite the appropriate file and if set to --auto will also print a message saying "remote/HEAD set to branch", which implies something was changed. In contrast, on a nil operation e.g. pull will clearly state that nothing was done, while fetch will not output anything.
Change the behaviour of remote set-head so that the reference is only updated if it actually needs to change. Since set-head --auto is essentially a fetch-like operation, align it's behaviour with fetch and only print output if something was actually done.
--- builtin/remote.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
builtin/remote.c +6 -4
@@ -1400,8 +1400,8 @@ static int show(int argc, const char **argv, const char *prefix)static int set_head (int argc,const char **argv,const char *prefix) { -int i, opt_a =0 , opt_d =0 , result =0 ; -struct strbuf buf =STRBUF_INIT , buf2 =STRBUF_INIT ; +int i, opt_a =0 , opt_d =0 , is_ref_changed =0 , result =0 ; +struct strbuf buf =STRBUF_INIT , buf2 =STRBUF_INIT , buf3 =STRBUF_INIT ;char *head_name =NULL ;struct option options[] = {
@@ -1440,12 +1440,14 @@ static int set_head(int argc, const char **argv, const char *prefix)if (head_name) {strbuf_addf (&buf2,"refs/remotes/%s/%s" , argv[0 ], head_name); +refs_read_symbolic_ref (get_main_ref_store (the_repository),buf.buf,&buf3); + is_ref_changed =strcmp (buf2.buf,buf3.buf);/* make sure it's valid */ if (!refs_ref_exists (get_main_ref_store (the_repository), buf2.buf)) result |=error (_ ("Not a valid ref: %s" ), buf2.buf); -else if (refs_update_symref (get_main_ref_store (the_repository), buf.buf, buf2.buf,"remote set-head" )) +else if (is_ref_changed &&refs_update_symref (get_main_ref_store (the_repository), buf.buf, buf2.buf,"remote set-head" )) result |=error (_ ("Could not setup %s" ), buf.buf); -else if (opt_a) +else if (opt_a && is_ref_changed)printf ("%s/HEAD set to %s\n" , argv[0 ], head_name);free (head_name); }
Signature · 1 line
2.46.0
Quoted 3 lines · Bence Ferdinandy
What is missing for sure is:
- documentation
- tests (if needed)
What change does not need tests?
Quoted 8 lines
- settings
For settings, my idea would be a fetch/remote.set_head that could take three values:
* never
* missing: run it only if the ref is missing, this setting would basically allow replicating the result of a clone
* always (with the other patch, this would still be a no-op if it didn't change)
Quoted 4 lines
This would probably also require a --no-set-head flag, to disable an always/missing setting. A --missing-set-head or something of the like also may or may not make sense. Alternatively, only two behaviours might be enough (missing and always) since clone already sort of does this.
If we were to assume "always" is needed, then the tristate like the above may be a reasonable way to go.
But as I outlined in my response to [1/2], I suspect that an approach without configuration or command line option would give the users the most smooth experience. They are used to seeing "clone" to give them a remote tracking HEAD and nobody complained that we lack the option to "clone" to prevent that. If "fetch" notices we do not have remote tracking HEAD for a remote [*] and stores what it observed at their HEAD in remote tracking HEAD, that should not bother anybody. No matter what mechanism gave you the initial remote tracking HEAD, if you want to update it to something else, we already have the "remote set-head" command.
[Footnote]
* One thing we MUST be careful about is that some remotes may not have ANY remote tracking branches (i.e. you only want to use the remote mechanism to give you a shorthand for URL, but you do not have fetch refspec at all). Even if refs/remotes/$repo/HEAD is missing for such a remote, we should *not* attempt to create it, as we are not populating refs/remotes/$repo/master and friends at all in such a case.
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Bence Ferdinandy
What is missing for sure is:
- documentation
- tests (if needed)What change does not need tests?
Fair enough, for the next iteration I'll look into tests as well!
Quoted 17 lines
Quoted 9 lines
- settings
For settings, my idea would be a fetch/remote.set_head that could take three values:
* never
* missing: run it only if the ref is missing, this setting would basically allow replicating the result of a clone
* always (with the other patch, this would still be a no-op if it didn't change)Quoted 4 lines
This would probably also require a --no-set-head flag, to disable an always/missing setting. A --missing-set-head or something of the like also may or may not make sense. Alternatively, only two behaviours might be enough (missing and always) since clone already sort of does this.
If we were to assume "always" is needed, then the tristate like the above may be a reasonable way to go.
Quoted 10 lines
But as I outlined in my response to [1/2], I suspect that an approach without configuration or command line option would give the users the most smooth experience. They are used to seeing "clone" to give them a remote tracking HEAD and nobody complained that we lack the option to "clone" to prevent that. If "fetch" notices we do not have remote tracking HEAD for a remote [*] and stores what it observed at their HEAD in remote tracking HEAD, that should not bother anybody. No matter what mechanism gave you the initial remote tracking HEAD, if you want to update it to something else, we already have the "remote set-head" command.
So I guess we did conclude that no settings are actually needed.
Quoted 11 lines
[Footnote]
* One thing we MUST be careful about is that some remotes may not have ANY remote tracking branches (i.e. you only want to use the remote mechanism to give you a shorthand for URL, but you do not have fetch refspec at all). Even if refs/remotes/$repo/HEAD is missing for such a remote, we should *not* attempt to create it, as we are not populating refs/remotes/$repo/master and friends at all in such a case.
You mean that somebody does git init && git remote add origin $remote, but never does calls fetch? Currently remote set-head -a origin will fail with
error: Not a valid ref: refs/remotes/origin/master
Although if everything is done _after_ a fetch, unless I misunderstood, we shouldn't be able to be in this situation at all.
Best,
Bence
Quoted 10 lines · Bence Ferdinandy
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Bence Ferdinandy
What is missing for sure is:
- documentation
- tests (if needed)What change does not need tests?
Fair enough, for the next iteration I'll look into tests as well!
In this project, tests are written not because we want to demonstrate the shiny new feature we just invented. We write tests so that the invention we are shipping to our end users will keep behaveing in the way we originall intended without getting broken by future careless developers. The tests serve as watching eyes while the original author of the feature is not watching ;-)
And that is why we also write tests that a feature does not trigger when it should not, as well as it triggers when it should.
Quoted 10 lines
Quoted 7 lines
* One thing we MUST be careful about is that some remotes may not have ANY remote tracking branches (i.e. you only want to use the remote mechanism to give you a shorthand for URL, but you do not have fetch refspec at all). Even if refs/remotes/$repo/HEAD is missing for such a remote, we should *not* attempt to create it, as we are not populating refs/remotes/$repo/master and friends at all in such a case.
You mean that somebody does git init && git remote add origin $remote, but never does calls fetch?
No. If the remote HEAD does not exist, we may still not want to create it. Imagine
[remote "his"]
url = https://git.kernel.org/pub/scm/git/git
push = refs/heads/maint
push = refs/heads/master
push = refs/heads/next
push = +refs/heads/seen
without any refspec for the fetching side. "git fetch his master" may learn where the remote HEAD is, and it may even be pointing at their 'master' branch, but because we do not maintain any remote tracking information for their 'master' (in other words, refs/remotes/his/master is not updated by this 'fetch' and there is no configuration to make future 'fetch' to do so).
Thanks.