Skip to content

Commit 1fb20df

Browse files
HaraldNordgrengitster
authored andcommitted
ls-remote: create '--sort' option
Create a '--sort' option for ls-remote, based on the one from for-each-ref. This e.g. allows ref names to be sorted by version semantics, so that v1.2 is sorted before v1.10. Signed-off-by: Harald Nordgren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 427cbc9 commit 1fb20df

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

Documentation/git-ls-remote.txt

Lines changed: 15 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] [--refs] [--upload-pack=<exec>]
13-
[-q | --quiet] [--exit-code] [--get-url]
13+
[-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]
1414
[--symref] [<repository> [<refs>...]]
1515

1616
DESCRIPTION
@@ -60,6 +60,16 @@ OPTIONS
6060
upload-pack only shows the symref HEAD, so it will be the only
6161
one shown by ls-remote.
6262

63+
--sort=<key>::
64+
Sort based on the key given. Prefix `-` to sort in descending order
65+
of the value. Supports "version:refname" or "v:refname" (tag names
66+
are treated as versions). The "version:refname" sort order can also
67+
be affected by the "versionsort.suffix" configuration variable.
68+
See linkgit:git-for-each-ref[1] for more sort options, but be aware
69+
keys like `committerdate` that require access to the objects
70+
themselves will not work for refs whose objects have not yet been
71+
fetched from the remote, and will give a `missing object` error.
72+
6373
<repository>::
6474
The "remote" repository to query. This parameter can be
6575
either a URL or the name of a remote (see the GIT URLS and
@@ -90,6 +100,10 @@ EXAMPLES
90100
c5db5456ae3b0873fc659c19fafdde22313cc441 refs/tags/v0.99.2
91101
7ceca275d047c90c0c7d5afb13ab97efdf51bd6e refs/tags/v0.99.3
92102

103+
SEE ALSO
104+
--------
105+
linkgit:git-check-ref-format[1].
106+
93107
GIT
94108
---
95109
Part of the linkgit:git[1] suite

builtin/ls-remote.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "builtin.h"
22
#include "cache.h"
33
#include "transport.h"
4+
#include "ref-filter.h"
45
#include "remote.h"
56

67
static const char * const ls_remote_usage[] = {
@@ -43,10 +44,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
4344
int show_symref_target = 0;
4445
const char *uploadpack = NULL;
4546
const char **pattern = NULL;
47+
int i;
4648

4749
struct remote *remote;
4850
struct transport *transport;
4951
const struct ref *ref;
52+
struct ref_array ref_array;
53+
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
5054

5155
struct option options[] = {
5256
OPT__QUIET(&quiet, N_("do not print remote URL")),
@@ -60,6 +64,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
6064
OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
6165
OPT_BOOL(0, "get-url", &get_url,
6266
N_("take url.<base>.insteadOf into account")),
67+
OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
68+
N_("field name to sort on"), &parse_opt_ref_sorting),
6369
OPT_SET_INT_F(0, "exit-code", &status,
6470
N_("exit with exit code 2 if no matching refs are found"),
6571
2, PARSE_OPT_NOCOMPLETE),
@@ -68,6 +74,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
6874
OPT_END()
6975
};
7076

77+
memset(&ref_array, 0, sizeof(ref_array));
78+
7179
argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
7280
PARSE_OPT_STOP_AT_NON_OPTION);
7381
dest = argv[0];
@@ -90,6 +98,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
9098

9199
if (get_url) {
92100
printf("%s\n", *remote->url);
101+
UNLEAK(sorting);
93102
return 0;
94103
}
95104

@@ -98,20 +107,35 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
98107
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
99108

100109
ref = transport_get_remote_refs(transport);
101-
if (transport_disconnect(transport))
110+
if (transport_disconnect(transport)) {
111+
UNLEAK(sorting);
102112
return 1;
113+
}
103114

104115
if (!dest && !quiet)
105116
fprintf(stderr, "From %s\n", *remote->url);
106117
for ( ; ref; ref = ref->next) {
118+
struct ref_array_item *item;
107119
if (!check_ref_type(ref, flags))
108120
continue;
109121
if (!tail_match(pattern, ref->name))
110122
continue;
123+
item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
124+
item->symref = xstrdup_or_null(ref->symref);
125+
}
126+
127+
if (sorting)
128+
ref_array_sort(sorting, &ref_array);
129+
130+
for (i = 0; i < ref_array.nr; i++) {
131+
const struct ref_array_item *ref = ref_array.items[i];
111132
if (show_symref_target && ref->symref)
112-
printf("ref: %s\t%s\n", ref->symref, ref->name);
113-
printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
133+
printf("ref: %s\t%s\n", ref->symref, ref->refname);
134+
printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
114135
status = 0; /* we found something */
115136
}
137+
138+
UNLEAK(sorting);
139+
UNLEAK(ref_array);
116140
return status;
117141
}

t/t5512-ls-remote.sh

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test_expect_success setup '
1010
test_tick &&
1111
git commit -m initial &&
1212
git tag mark &&
13+
git tag mark1.1 &&
14+
git tag mark1.2 &&
15+
git tag mark1.10 &&
1316
git show-ref --tags -d | sed -e "s/ / /" >expected.tag &&
1417
(
1518
echo "$(git rev-parse HEAD) HEAD"
@@ -39,6 +42,39 @@ test_expect_success 'ls-remote self' '
3942
test_cmp expected.all actual
4043
'
4144

45+
test_expect_success 'ls-remote --sort="version:refname" --tags self' '
46+
cat >expect <<-EOF &&
47+
$(git rev-parse mark) refs/tags/mark
48+
$(git rev-parse mark1.1) refs/tags/mark1.1
49+
$(git rev-parse mark1.2) refs/tags/mark1.2
50+
$(git rev-parse mark1.10) refs/tags/mark1.10
51+
EOF
52+
git ls-remote --sort="version:refname" --tags self >actual &&
53+
test_cmp expect actual
54+
'
55+
56+
test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
57+
cat >expect <<-EOF &&
58+
$(git rev-parse mark1.10) refs/tags/mark1.10
59+
$(git rev-parse mark1.2) refs/tags/mark1.2
60+
$(git rev-parse mark1.1) refs/tags/mark1.1
61+
$(git rev-parse mark) refs/tags/mark
62+
EOF
63+
git ls-remote --sort="-version:refname" --tags self >actual &&
64+
test_cmp expect actual
65+
'
66+
67+
test_expect_success 'ls-remote --sort="-refname" --tags self' '
68+
cat >expect <<-EOF &&
69+
$(git rev-parse mark1.2) refs/tags/mark1.2
70+
$(git rev-parse mark1.10) refs/tags/mark1.10
71+
$(git rev-parse mark1.1) refs/tags/mark1.1
72+
$(git rev-parse mark) refs/tags/mark
73+
EOF
74+
git ls-remote --sort="-refname" --tags self >actual &&
75+
test_cmp expect actual
76+
'
77+
4278
test_expect_success 'dies when no remote specified and no default remotes found' '
4379
test_must_fail git ls-remote
4480
'
@@ -131,7 +167,7 @@ test_expect_success 'Report no-match with --exit-code' '
131167

132168
test_expect_success 'Report match with --exit-code' '
133169
git ls-remote --exit-code other.git "refs/tags/*" >actual &&
134-
git ls-remote . tags/mark >expect &&
170+
git ls-remote . tags/mark* >expect &&
135171
test_cmp expect actual
136172
'
137173

@@ -170,14 +206,18 @@ test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs'
170206
grep refs/tags/magic actual
171207
'
172208

209+
git fetch origin
173210
test_expect_success 'ls-remote --symref' '
174-
cat >expect <<-\EOF &&
211+
cat >expect <<-EOF &&
175212
ref: refs/heads/master HEAD
176-
1bd44cb9d13204b0fe1958db0082f5028a16eb3a HEAD
177-
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/heads/master
178-
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/remotes/origin/HEAD
179-
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/remotes/origin/master
180-
1bd44cb9d13204b0fe1958db0082f5028a16eb3a refs/tags/mark
213+
$(git rev-parse HEAD) HEAD
214+
$(git rev-parse refs/heads/master) refs/heads/master
215+
$(git rev-parse HEAD) refs/remotes/origin/HEAD
216+
$(git rev-parse refs/remotes/origin/master) refs/remotes/origin/master
217+
$(git rev-parse refs/tags/mark) refs/tags/mark
218+
$(git rev-parse refs/tags/mark1.1) refs/tags/mark1.1
219+
$(git rev-parse refs/tags/mark1.10) refs/tags/mark1.10
220+
$(git rev-parse refs/tags/mark1.2) refs/tags/mark1.2
181221
EOF
182222
git ls-remote --symref >actual &&
183223
test_cmp expect actual

0 commit comments

Comments
 (0)