Skip to content

Commit cc8e538

Browse files
tgummerergitster
authored andcommitted
remote: actually check if remote exits
When converting the git remote command to a builtin in 211c89 ("Make git-remote a builtin"), a few calls to check if a remote exists were converted from: if (!exists $remote->{$name}) { [...] to: remote = remote_get(argv[1]); if (!remote) [...] The new check is not quite correct, because remote_get() never returns NULL if a name is given. This leaves us with the somewhat cryptic error message "error: Could not remove config section 'remote.test'", if we are trying to remove a remote that does not exist, or a similar error if we try to rename a remote. Use the remote_is_configured() function to check whether the remote actually exists, and die with a more sensible error message ("No such remote: $remotename") instead if it doesn't. Signed-off-by: Thomas Gummerer <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 674468b commit cc8e538

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

builtin/remote.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static int mv(int argc, const char **argv)
634634
rename.remote_branches = &remote_branches;
635635

636636
oldremote = remote_get(rename.old);
637-
if (!oldremote)
637+
if (!remote_is_configured(oldremote))
638638
die(_("No such remote: %s"), rename.old);
639639

640640
if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
@@ -773,7 +773,7 @@ static int rm(int argc, const char **argv)
773773
usage_with_options(builtin_remote_rm_usage, options);
774774

775775
remote = remote_get(argv[1]);
776-
if (!remote)
776+
if (!remote_is_configured(remote))
777777
die(_("No such remote: %s"), argv[1]);
778778

779779
known_remotes.to_delete = remote;

t/t5505-remote.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ test_expect_success 'remove remote protects local branches' '
139139
)
140140
'
141141

142+
test_expect_success 'remove errors out early when deleting non-existent branch' '
143+
(
144+
cd test &&
145+
echo "fatal: No such remote: foo" >expect &&
146+
test_must_fail git remote rm foo 2>actual &&
147+
test_i18ncmp expect actual
148+
)
149+
'
150+
151+
test_expect_success 'rename errors out early when deleting non-existent branch' '
152+
(
153+
cd test &&
154+
echo "fatal: No such remote: foo" >expect &&
155+
test_must_fail git remote rename foo bar 2>actual &&
156+
test_i18ncmp expect actual
157+
)
158+
'
159+
142160
cat >test/expect <<EOF
143161
* remote origin
144162
Fetch URL: $(pwd)/one

0 commit comments

Comments
 (0)