Skip to content

Commit ecf95d9

Browse files
committed
Merge branch 'ab/git-remote-exit-code'
Exit codes from "git remote add" etc. were not usable by scripted callers. * ab/git-remote-exit-code: remote: add meaningful exit code on missing/existing
2 parents 4c7eb63 + 9144ba4 commit ecf95d9

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

Documentation/git-remote.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ The remote configuration is achieved using the `remote.origin.url` and
203203
`remote.origin.fetch` configuration variables. (See
204204
linkgit:git-config[1]).
205205

206+
EXIT STATUS
207+
-----------
208+
209+
On success, the exit status is `0`.
210+
211+
When subcommands such as 'add', 'rename', and 'remove' can't find the
212+
remote in question, the exit status is `2`. When the remote already
213+
exists, the exit status is `3`.
214+
215+
On any other error, the exit status may be any other non-zero value.
216+
206217
EXAMPLES
207218
--------
208219

builtin/remote.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ static int add(int argc, const char **argv)
191191
url = argv[1];
192192

193193
remote = remote_get(name);
194-
if (remote_is_configured(remote, 1))
195-
die(_("remote %s already exists."), name);
194+
if (remote_is_configured(remote, 1)) {
195+
error(_("remote %s already exists."), name);
196+
exit(3);
197+
}
196198

197199
if (!valid_remote_name(name))
198200
die(_("'%s' is not a valid remote name"), name);
@@ -685,15 +687,19 @@ static int mv(int argc, const char **argv)
685687
rename.remote_branches = &remote_branches;
686688

687689
oldremote = remote_get(rename.old_name);
688-
if (!remote_is_configured(oldremote, 1))
689-
die(_("No such remote: '%s'"), rename.old_name);
690+
if (!remote_is_configured(oldremote, 1)) {
691+
error(_("No such remote: '%s'"), rename.old_name);
692+
exit(2);
693+
}
690694

691695
if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
692696
return migrate_file(oldremote);
693697

694698
newremote = remote_get(rename.new_name);
695-
if (remote_is_configured(newremote, 1))
696-
die(_("remote %s already exists."), rename.new_name);
699+
if (remote_is_configured(newremote, 1)) {
700+
error(_("remote %s already exists."), rename.new_name);
701+
exit(3);
702+
}
697703

698704
if (!valid_remote_name(rename.new_name))
699705
die(_("'%s' is not a valid remote name"), rename.new_name);
@@ -826,8 +832,10 @@ static int rm(int argc, const char **argv)
826832
usage_with_options(builtin_remote_rm_usage, options);
827833

828834
remote = remote_get(argv[1]);
829-
if (!remote_is_configured(remote, 1))
830-
die(_("No such remote: '%s'"), argv[1]);
835+
if (!remote_is_configured(remote, 1)) {
836+
error(_("No such remote: '%s'"), argv[1]);
837+
exit(2);
838+
}
831839

832840
known_remotes.to_delete = remote;
833841
for_each_remote(add_known_remote, &known_remotes);
@@ -1508,8 +1516,10 @@ static int set_remote_branches(const char *remotename, const char **branches,
15081516
strbuf_addf(&key, "remote.%s.fetch", remotename);
15091517

15101518
remote = remote_get(remotename);
1511-
if (!remote_is_configured(remote, 1))
1512-
die(_("No such remote '%s'"), remotename);
1519+
if (!remote_is_configured(remote, 1)) {
1520+
error(_("No such remote '%s'"), remotename);
1521+
exit(2);
1522+
}
15131523

15141524
if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
15151525
strbuf_release(&key);
@@ -1562,8 +1572,10 @@ static int get_url(int argc, const char **argv)
15621572
remotename = argv[0];
15631573

15641574
remote = remote_get(remotename);
1565-
if (!remote_is_configured(remote, 1))
1566-
die(_("No such remote '%s'"), remotename);
1575+
if (!remote_is_configured(remote, 1)) {
1576+
error(_("No such remote '%s'"), remotename);
1577+
exit(2);
1578+
}
15671579

15681580
url_nr = 0;
15691581
if (push_mode) {
@@ -1630,8 +1642,10 @@ static int set_url(int argc, const char **argv)
16301642
oldurl = newurl;
16311643

16321644
remote = remote_get(remotename);
1633-
if (!remote_is_configured(remote, 1))
1634-
die(_("No such remote '%s'"), remotename);
1645+
if (!remote_is_configured(remote, 1)) {
1646+
error(_("No such remote '%s'"), remotename);
1647+
exit(2);
1648+
}
16351649

16361650
if (push_mode) {
16371651
strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);

t/t5505-remote.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ test_expect_success 'remove remote protects local branches' '
145145
test_expect_success 'remove errors out early when deleting non-existent branch' '
146146
(
147147
cd test &&
148-
echo "fatal: No such remote: '\''foo'\''" >expect &&
149-
test_must_fail git remote rm foo 2>actual &&
148+
echo "error: No such remote: '\''foo'\''" >expect &&
149+
test_expect_code 2 git remote rm foo 2>actual &&
150150
test_i18ncmp expect actual
151151
)
152152
'
@@ -173,8 +173,8 @@ test_expect_success 'remove remote with a branch without configured merge' '
173173
test_expect_success 'rename errors out early when deleting non-existent branch' '
174174
(
175175
cd test &&
176-
echo "fatal: No such remote: '\''foo'\''" >expect &&
177-
test_must_fail git remote rename foo bar 2>actual &&
176+
echo "error: No such remote: '\''foo'\''" >expect &&
177+
test_expect_code 2 git remote rename foo bar 2>actual &&
178178
test_i18ncmp expect actual
179179
)
180180
'
@@ -188,16 +188,16 @@ test_expect_success 'rename errors out early when when new name is invalid' '
188188

189189
test_expect_success 'add existing foreign_vcs remote' '
190190
test_config remote.foo.vcs bar &&
191-
echo "fatal: remote foo already exists." >expect &&
192-
test_must_fail git remote add foo bar 2>actual &&
191+
echo "error: remote foo already exists." >expect &&
192+
test_expect_code 3 git remote add foo bar 2>actual &&
193193
test_i18ncmp expect actual
194194
'
195195

196196
test_expect_success 'add existing foreign_vcs remote' '
197197
test_config remote.foo.vcs bar &&
198198
test_config remote.bar.vcs bar &&
199-
echo "fatal: remote bar already exists." >expect &&
200-
test_must_fail git remote rename foo bar 2>actual &&
199+
echo "error: remote bar already exists." >expect &&
200+
test_expect_code 3 git remote rename foo bar 2>actual &&
201201
test_i18ncmp expect actual
202202
'
203203

0 commit comments

Comments
 (0)