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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 27, 2020 at 10:41 UTC 9144ba4cf52bb0e891d7c10a331fc32c1d3e8f64
3 files changed +47 -22
Documentation/git-remote.txt
+11
@@ -203,6 +203,17 @@ The remote configuration is achieved using the `remote.origin.url` and
203 `remote.origin.fetch` configuration variables. (See
204 linkgit:git-config[1]).
205
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 +
217 EXAMPLES
218 --------
219
builtin/remote.c
+28 -14
@@ -191,8 +191,10 @@ static int add(int argc, const char **argv)
191 url = argv[1];
192
193 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 + }
198
199 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
200 if (!valid_fetch_refspec(buf2.buf))
@@ -686,15 +688,19 @@ static int mv(int argc, const char **argv)
688 rename.remote_branches = &remote_branches;
689
690 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 + }
695
696 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
697 return migrate_file(oldremote);
698
699 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 + }
704
705 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
706 if (!valid_fetch_refspec(buf.buf))
@@ -829,8 +835,10 @@ static int rm(int argc, const char **argv)
835 usage_with_options(builtin_remote_rm_usage, options);
836
837 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 + }
842
843 known_remotes.to_delete = remote;
844 for_each_remote(add_known_remote, &known_remotes);
@@ -1511,8 +1519,10 @@ static int set_remote_branches(const char *remotename, const char **branches,
1519 strbuf_addf(&key, "remote.%s.fetch", remotename);
1520
1521 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 + }
1526
1527 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1528 strbuf_release(&key);
@@ -1565,8 +1575,10 @@ static int get_url(int argc, const char **argv)
1575 remotename = argv[0];
1576
1577 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 + }
1582
1583 url_nr = 0;
1584 if (push_mode) {
@@ -1633,8 +1645,10 @@ static int set_url(int argc, const char **argv)
1645 oldurl = newurl;
1646
1647 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 + }
1652
1653 if (push_mode) {
1654 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
t/t5505-remote.sh
+8 -8
@@ -145,8 +145,8 @@ test_expect_success 'remove remote protects local branches' '
145 test_expect_success 'remove errors out early when deleting non-existent branch' '
146 (
147 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 &&
150 test_i18ncmp expect actual
151 )
152 '
@@ -173,24 +173,24 @@ test_expect_success 'remove remote with a branch without configured merge' '
173 test_expect_success 'rename errors out early when deleting non-existent branch' '
174 (
175 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 &&
178 test_i18ncmp expect actual
179 )
180 '
181
182 test_expect_success 'add existing foreign_vcs remote' '
183 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 &&
186 test_i18ncmp expect actual
187 '
188
189 test_expect_success 'add existing foreign_vcs remote' '
190 test_config remote.foo.vcs bar &&
191 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 &&
194 test_i18ncmp expect actual
195 '
196