Skip to content

Commit 17b7e84

Browse files
bertwesarggitster
authored andcommitted
remote rename/remove: gently handle remote.pushDefault config
When renaming a remote with git remote rename X Y git remote remove X Git already renames or removes any branch.<name>.remote and branch.<name>.pushRemote configurations if their value is X. However remote.pushDefault needs a more gentle approach, as this may be set in a non-repo configuration file. In such a case only a warning is printed, such as: warning: The global configuration remote.pushDefault in: $HOME/.gitconfig:35 now names the non-existent remote origin It is changed to remote.pushDefault = Y or removed when set in a repo configuration though. Signed-off-by: Bert Wesarg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fbb5468 commit 17b7e84

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

builtin/remote.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,55 @@ static int migrate_file(struct remote *remote)
615615
return 0;
616616
}
617617

618+
struct push_default_info
619+
{
620+
const char *old_name;
621+
enum config_scope scope;
622+
struct strbuf origin;
623+
int linenr;
624+
};
625+
626+
static int config_read_push_default(const char *key, const char *value,
627+
void *cb)
628+
{
629+
struct push_default_info* info = cb;
630+
if (strcmp(key, "remote.pushdefault") || strcmp(value, info->old_name))
631+
return 0;
632+
633+
info->scope = current_config_scope();
634+
strbuf_reset(&info->origin);
635+
strbuf_addstr(&info->origin, current_config_name());
636+
info->linenr = current_config_line();
637+
638+
return 0;
639+
}
640+
641+
static void handle_push_default(const char* old_name, const char* new_name)
642+
{
643+
struct push_default_info push_default = {
644+
old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
645+
git_config(config_read_push_default, &push_default);
646+
if (push_default.scope >= CONFIG_SCOPE_COMMAND)
647+
; /* pass */
648+
else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
649+
int result = git_config_set_gently("remote.pushDefault",
650+
new_name);
651+
if (new_name && result && result != CONFIG_NOTHING_SET)
652+
die(_("could not set '%s'"), "remote.pushDefault");
653+
else if (!new_name && result && result != CONFIG_NOTHING_SET)
654+
die(_("could not unset '%s'"), "remote.pushDefault");
655+
} else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
656+
/* warn */
657+
warning(_("The %s configuration remote.pushDefault in:\n"
658+
"\t%s:%d\n"
659+
"now names the non-existent remote '%s'"),
660+
config_scope_name(push_default.scope),
661+
push_default.origin.buf, push_default.linenr,
662+
old_name);
663+
}
664+
}
665+
666+
618667
static int mv(int argc, const char **argv)
619668
{
620669
struct option options[] = {
@@ -750,6 +799,9 @@ static int mv(int argc, const char **argv)
750799
die(_("creating '%s' failed"), buf.buf);
751800
}
752801
string_list_clear(&remote_branches, 1);
802+
803+
handle_push_default(rename.old_name, rename.new_name);
804+
753805
return 0;
754806
}
755807

@@ -835,6 +887,8 @@ static int rm(int argc, const char **argv)
835887
strbuf_addf(&buf, "remote.%s", remote->name);
836888
if (git_config_rename_section(buf.buf, NULL) < 1)
837889
return error(_("Could not remove config section '%s'"), buf.buf);
890+
891+
handle_push_default(remote->name, NULL);
838892
}
839893

840894
return result;

t/t5505-remote.sh

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,14 +737,50 @@ test_expect_success 'rename a remote' '
737737
git clone one four &&
738738
(
739739
cd four &&
740+
test_config_global remote.pushDefault origin &&
740741
git config branch.master.pushRemote origin &&
741742
git remote rename origin upstream &&
742743
test -z "$(git for-each-ref refs/remotes/origin)" &&
743744
test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/master" &&
744745
test "$(git rev-parse upstream/master)" = "$(git rev-parse master)" &&
745746
test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
746747
test "$(git config branch.master.remote)" = "upstream" &&
747-
test "$(git config branch.master.pushRemote)" = "upstream"
748+
test "$(git config branch.master.pushRemote)" = "upstream" &&
749+
test "$(git config --global remote.pushDefault)" = "origin"
750+
)
751+
'
752+
753+
test_expect_success 'rename a remote renames repo remote.pushDefault' '
754+
git clone one four.1 &&
755+
(
756+
cd four.1 &&
757+
git config remote.pushDefault origin &&
758+
git remote rename origin upstream &&
759+
test "$(git config --local remote.pushDefault)" = "upstream"
760+
)
761+
'
762+
763+
test_expect_success 'rename a remote renames repo remote.pushDefault but ignores global' '
764+
git clone one four.2 &&
765+
(
766+
cd four.2 &&
767+
test_config_global remote.pushDefault other &&
768+
git config remote.pushDefault origin &&
769+
git remote rename origin upstream &&
770+
test "$(git config --global remote.pushDefault)" = "other" &&
771+
test "$(git config --local remote.pushDefault)" = "upstream"
772+
)
773+
'
774+
775+
test_expect_success 'rename a remote renames repo remote.pushDefault but keeps global' '
776+
git clone one four.3 &&
777+
(
778+
cd four.3 &&
779+
test_config_global remote.pushDefault origin &&
780+
git config remote.pushDefault origin &&
781+
git remote rename origin upstream &&
782+
test "$(git config --global remote.pushDefault)" = "origin" &&
783+
test "$(git config --local remote.pushDefault)" = "upstream"
748784
)
749785
'
750786

@@ -790,11 +826,47 @@ test_expect_success 'remove a remote' '
790826
git clone one four.five &&
791827
(
792828
cd four.five &&
829+
test_config_global remote.pushDefault origin &&
793830
git config branch.master.pushRemote origin &&
794831
git remote remove origin &&
795832
test -z "$(git for-each-ref refs/remotes/origin)" &&
796833
test_must_fail git config branch.master.remote &&
797-
test_must_fail git config branch.master.pushRemote
834+
test_must_fail git config branch.master.pushRemote &&
835+
test "$(git config --global remote.pushDefault)" = "origin"
836+
)
837+
'
838+
839+
test_expect_success 'remove a remote removes repo remote.pushDefault' '
840+
git clone one four.five.1 &&
841+
(
842+
cd four.five.1 &&
843+
git config remote.pushDefault origin &&
844+
git remote remove origin &&
845+
test_must_fail git config --local remote.pushDefault
846+
)
847+
'
848+
849+
test_expect_success 'remove a remote removes repo remote.pushDefault but ignores global' '
850+
git clone one four.five.2 &&
851+
(
852+
cd four.five.2 &&
853+
test_config_global remote.pushDefault other &&
854+
git config remote.pushDefault origin &&
855+
git remote remove origin &&
856+
test "$(git config --global remote.pushDefault)" = "other" &&
857+
test_must_fail git config --local remote.pushDefault
858+
)
859+
'
860+
861+
test_expect_success 'remove a remote removes repo remote.pushDefault but keeps global' '
862+
git clone one four.five.3 &&
863+
(
864+
cd four.five.3 &&
865+
test_config_global remote.pushDefault origin &&
866+
git config remote.pushDefault origin &&
867+
git remote remove origin &&
868+
test "$(git config --global remote.pushDefault)" = "origin" &&
869+
test_must_fail git config --local remote.pushDefault
798870
)
799871
'
800872

0 commit comments

Comments
 (0)