Skip to content

Commit 9144ba4

Browse files
avargitster
authored andcommitted
remote: add meaningful exit code on missing/existing
Change the exit code for the likes of "git remote add/rename" to exit with 2 if the remote in question doesn't exist, and 3 if it does. Before we'd just die() and exit with the general 128 exit code. This changes the output message from e.g.: fatal: remote origin already exists. To: error: remote origin already exists. Which I believe is a feature, since we generally use "fatal" for the generic errors, and "error" for the more specific ones with a custom exit code, but this part of the change may break code that already relies on stderr parsing (not that we ever supported that...). The motivation for this is a discussion around some code in GitLab's gitaly which wanted to check this, and had to parse stderr to do so: https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2695 It's worth noting as an aside that a method of checking this that doesn't rely on that is to check with "git config" whether the value in question does or doesn't exist. That introduces a TOCTOU race condition, but on the other hand this code (e.g. "git remote add") already has a TOCTOU race. We go through the config.lock for the actual setting of the config, but the pseudocode logic is: read_config(); check_config_and_arg_sanity(); save_config(); So e.g. if a sleep() is added right after the remote_is_configured() check in add() we'll clobber remote.NAME.url, and add another (usually duplicate) remote.NAME.fetch entry (and other values, depending on invocation). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e67335 commit 9144ba4

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
strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
198200
if (!valid_fetch_refspec(buf2.buf))
@@ -686,15 +688,19 @@ static int mv(int argc, const char **argv)
686688
rename.remote_branches = &remote_branches;
687689

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

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

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

699705
strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
700706
if (!valid_fetch_refspec(buf.buf))
@@ -829,8 +835,10 @@ static int rm(int argc, const char **argv)
829835
usage_with_options(builtin_remote_rm_usage, options);
830836

831837
remote = remote_get(argv[1]);
832-
if (!remote_is_configured(remote, 1))
833-
die(_("No such remote: '%s'"), argv[1]);
838+
if (!remote_is_configured(remote, 1)) {
839+
error(_("No such remote: '%s'"), argv[1]);
840+
exit(2);
841+
}
834842

835843
known_remotes.to_delete = remote;
836844
for_each_remote(add_known_remote, &known_remotes);
@@ -1511,8 +1519,10 @@ static int set_remote_branches(const char *remotename, const char **branches,
15111519
strbuf_addf(&key, "remote.%s.fetch", remotename);
15121520

15131521
remote = remote_get(remotename);
1514-
if (!remote_is_configured(remote, 1))
1515-
die(_("No such remote '%s'"), remotename);
1522+
if (!remote_is_configured(remote, 1)) {
1523+
error(_("No such remote '%s'"), remotename);
1524+
exit(2);
1525+
}
15161526

15171527
if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
15181528
strbuf_release(&key);
@@ -1565,8 +1575,10 @@ static int get_url(int argc, const char **argv)
15651575
remotename = argv[0];
15661576

15671577
remote = remote_get(remotename);
1568-
if (!remote_is_configured(remote, 1))
1569-
die(_("No such remote '%s'"), remotename);
1578+
if (!remote_is_configured(remote, 1)) {
1579+
error(_("No such remote '%s'"), remotename);
1580+
exit(2);
1581+
}
15701582

15711583
url_nr = 0;
15721584
if (push_mode) {
@@ -1633,8 +1645,10 @@ static int set_url(int argc, const char **argv)
16331645
oldurl = newurl;
16341646

16351647
remote = remote_get(remotename);
1636-
if (!remote_is_configured(remote, 1))
1637-
die(_("No such remote '%s'"), remotename);
1648+
if (!remote_is_configured(remote, 1)) {
1649+
error(_("No such remote '%s'"), remotename);
1650+
exit(2);
1651+
}
16381652

16391653
if (push_mode) {
16401654
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,24 +173,24 @@ 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
'
181181

182182
test_expect_success 'add existing foreign_vcs remote' '
183183
test_config remote.foo.vcs bar &&
184-
echo "fatal: remote foo already exists." >expect &&
185-
test_must_fail git remote add foo bar 2>actual &&
184+
echo "error: remote foo already exists." >expect &&
185+
test_expect_code 3 git remote add foo bar 2>actual &&
186186
test_i18ncmp expect actual
187187
'
188188

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

0 commit comments

Comments
 (0)