Skip to content

Commit 6c0110f

Browse files
committed
Merge branch 'hn/sort-ls-remote'
"git ls-remote" learned an option to allow sorting its output based on the refnames being shown. * hn/sort-ls-remote: ls-remote: create '--sort' option
2 parents a500a9c + 1fb20df commit 6c0110f

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
#include "refs.h"
67

@@ -45,10 +46,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
4546
const char *uploadpack = NULL;
4647
const char **pattern = NULL;
4748
struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
49+
int i;
4850

4951
struct remote *remote;
5052
struct transport *transport;
5153
const struct ref *ref;
54+
struct ref_array ref_array;
55+
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
5256

5357
struct option options[] = {
5458
OPT__QUIET(&quiet, N_("do not print remote URL")),
@@ -62,6 +66,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
6266
OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
6367
OPT_BOOL(0, "get-url", &get_url,
6468
N_("take url.<base>.insteadOf into account")),
69+
OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
70+
N_("field name to sort on"), &parse_opt_ref_sorting),
6571
OPT_SET_INT_F(0, "exit-code", &status,
6672
N_("exit with exit code 2 if no matching refs are found"),
6773
2, PARSE_OPT_NOCOMPLETE),
@@ -70,6 +76,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
7076
OPT_END()
7177
};
7278

79+
memset(&ref_array, 0, sizeof(ref_array));
80+
7381
argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
7482
PARSE_OPT_STOP_AT_NON_OPTION);
7583
dest = argv[0];
@@ -101,6 +109,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
101109

102110
if (get_url) {
103111
printf("%s\n", *remote->url);
112+
UNLEAK(sorting);
104113
return 0;
105114
}
106115

@@ -109,20 +118,35 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
109118
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
110119

111120
ref = transport_get_remote_refs(transport, &ref_prefixes);
112-
if (transport_disconnect(transport))
121+
if (transport_disconnect(transport)) {
122+
UNLEAK(sorting);
113123
return 1;
124+
}
114125

115126
if (!dest && !quiet)
116127
fprintf(stderr, "From %s\n", *remote->url);
117128
for ( ; ref; ref = ref->next) {
129+
struct ref_array_item *item;
118130
if (!check_ref_type(ref, flags))
119131
continue;
120132
if (!tail_match(pattern, ref->name))
121133
continue;
134+
item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
135+
item->symref = xstrdup_or_null(ref->symref);
136+
}
137+
138+
if (sorting)
139+
ref_array_sort(sorting, &ref_array);
140+
141+
for (i = 0; i < ref_array.nr; i++) {
142+
const struct ref_array_item *ref = ref_array.items[i];
122143
if (show_symref_target && ref->symref)
123-
printf("ref: %s\t%s\n", ref->symref, ref->name);
124-
printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
144+
printf("ref: %s\t%s\n", ref->symref, ref->refname);
145+
printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
125146
status = 0; /* we found something */
126147
}
148+
149+
UNLEAK(sorting);
150+
UNLEAK(ref_array);
127151
return status;
128152
}

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)