@matheusbsbs / matheus-bagliano / commits / 9c00de5a31
Fork 0

ls-remote: fall-back to default remotes when no remote specified

Instead of breaking execution when no remote (as specified in the variable dest) is specified when git-ls-remote is invoked, continue on and let remote_get() handle it. This way, we are able to use the default remotes (eg. "origin", branch.<name>.remote), as git-fetch, git-push, and other users of remote_get(), do. If no suitable remote is found, exit with a message describing the issue, instead of just the usage text, as we do previously. Add several tests to check that git-ls-remote handles the no-remote-specified situation. Also add a test that "git ls-remote <pattern>" does not work; we are unable to guess the remote in that situation, as are git-fetch and git-push. In that test, we are testing for messages coming from two separate processes, but we should be OK, because the second message is triggered by closing the fd which must happen after the first message is printed. (analysis by Jeff King.) Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tay Ray Chuan committed Apr 9, 2010 at 01:21 UTC 9c00de5a3135c8f7273668d4013c225d48d47861
2 files changed +65 -4
builtin/ls-remote.c
+7 -4
@@ -4,7 +4,8 @@
4 #include "remote.h"
5
6 static const char ls_remote_usage[] =
7 -"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...";
7 +"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
8 +" [<repository> [<refs>...]]";
9
10 /*
11 * Is there one among the list of patterns that match the tail part
@@ -73,9 +74,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
74 break;
75 }
76
76 - if (!dest)
77 - usage(ls_remote_usage);
78 -
77 if (argv[i]) {
78 int j;
79 pattern = xcalloc(sizeof(const char *), argc - i + 1);
@@ -87,6 +85,11 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
85 }
86 }
87 remote = remote_get(dest);
88 + if (!remote) {
89 + if (dest)
90 + die("bad repository '%s'", dest);
91 + die("No remote configured to list refs from.");
92 + }
93 if (!remote->url_nr)
94 die("remote %s has no configured URL", dest);
95 transport = transport_get(remote, NULL);
t/t5512-ls-remote.sh
+58
@@ -49,4 +49,62 @@ test_expect_success 'ls-remote self' '
49
50 '
51
52 +test_expect_success 'dies when no remote specified and no default remotes found' '
53 +
54 + test_must_fail git ls-remote
55 +
56 +'
57 +
58 +test_expect_success 'use "origin" when no remote specified' '
59 +
60 + git remote add origin "$(pwd)/.git" &&
61 + git ls-remote >actual &&
62 + test_cmp expected.all actual
63 +
64 +'
65 +
66 +test_expect_success 'use branch.<name>.remote if possible' '
67 +
68 + #
69 + # Test that we are indeed using branch.<name>.remote, not "origin", even
70 + # though the "origin" remote has been set.
71 + #
72 +
73 + # setup a new remote to differentiate from "origin"
74 + git clone . other.git &&
75 + (
76 + cd other.git &&
77 + echo "$(git rev-parse HEAD) HEAD"
78 + git show-ref | sed -e "s/ / /"
79 + ) >exp &&
80 +
81 + git remote add other other.git &&
82 + git config branch.master.remote other &&
83 +
84 + git ls-remote >actual &&
85 + test_cmp exp actual
86 +
87 +'
88 +
89 +cat >exp <<EOF
90 +fatal: 'refs*master' does not appear to be a git repository
91 +fatal: The remote end hung up unexpectedly
92 +EOF
93 +test_expect_success 'confuses pattern as remote when no remote specified' '
94 + #
95 + # Do not expect "git ls-remote <pattern>" to work; ls-remote, correctly,
96 + # confuses <pattern> for <remote>. Although ugly, this behaviour is akin
97 + # to the confusion of refspecs for remotes by git-fetch and git-push,
98 + # eg:
99 + #
100 + # $ git fetch branch
101 + #
102 +
103 + # We could just as easily have used "master"; the "*" emphasizes its
104 + # role as a pattern.
105 + test_must_fail git ls-remote refs*master >actual 2>&1 &&
106 + test_cmp exp actual
107 +
108 +'
109 +
110 test_done