Skip to content

Commit 3c0ae61

Browse files
committed
Merge branch 'ms/ls-remote-exit-with-status'
* ms/ls-remote-exit-with-status: ls-remote: the --exit-code option reports "no matching refs"
2 parents 6bb696c + a872477 commit 3c0ae61

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Documentation/git-ls-remote.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git ls-remote' [--heads] [--tags] [-u <exec> | --upload-pack <exec>]
13-
<repository> [<refs>...]
13+
[--exit-code] <repository> [<refs>...]
1414

1515
DESCRIPTION
1616
-----------
@@ -36,6 +36,12 @@ OPTIONS
3636
SSH and where the SSH daemon does not use the PATH configured by the
3737
user.
3838

39+
--exit-code::
40+
Exit with status "2" when no matching refs are found in the remote
41+
repository. Usually the command exits with status "0" to indicate
42+
it successfully talked with the remote repository, whether it
43+
found any matching refs.
44+
3945
<repository>::
4046
Location of the repository. The shorthand defined in
4147
$GIT_DIR/branches/ can be used. Use "." (dot) to list references in

builtin/ls-remote.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
static const char ls_remote_usage[] =
77
"git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>]\n"
8-
" [-q|--quiet] [<repository> [<refs>...]]";
8+
" [-q|--quiet] [--exit-code] [<repository> [<refs>...]]";
99

1010
/*
1111
* Is there one among the list of patterns that match the tail part
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
3535
unsigned flags = 0;
3636
int get_url = 0;
3737
int quiet = 0;
38+
int status = 0;
3839
const char *uploadpack = NULL;
3940
const char **pattern = NULL;
4041

@@ -74,6 +75,11 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
7475
get_url = 1;
7576
continue;
7677
}
78+
if (!strcmp("--exit-code", arg)) {
79+
/* return this code if no refs are reported */
80+
status = 2;
81+
continue;
82+
}
7783
usage(ls_remote_usage);
7884
}
7985
dest = arg;
@@ -121,6 +127,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
121127
if (!tail_match(pattern, ref->name))
122128
continue;
123129
printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name);
130+
status = 0; /* we found something */
124131
}
125-
return 0;
132+
return status;
126133
}

t/t5512-ls-remote.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,28 @@ test_expect_success 'confuses pattern as remote when no remote specified' '
123123
124124
'
125125

126+
test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
127+
git ls-remote --exit-code ./no-such-repository ;# not &&
128+
status=$? &&
129+
test $status != 2 && test $status != 0
130+
'
131+
132+
test_expect_success 'Report success even when nothing matches' '
133+
git ls-remote other.git "refs/nsn/*" >actual &&
134+
>expect &&
135+
test_cmp expect actual
136+
'
137+
138+
test_expect_success 'Report no-match with --exit-code' '
139+
test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
140+
>expect &&
141+
test_cmp expect actual
142+
'
143+
144+
test_expect_success 'Report match with --exit-code' '
145+
git ls-remote --exit-code other.git "refs/tags/*" >actual &&
146+
git ls-remote . tags/mark >expect &&
147+
test_cmp expect actual
148+
'
149+
126150
test_done

0 commit comments

Comments
 (0)