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 <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Harald Nordgren committed
Apr 9, 2018 at 03:42 UTC
1fb20dfd8ed70b4459312918a71444bc79ea6f0b
3 files changed
+89
-11
Documentation/git-ls-remote.txt
+15
-1
@@ -10,7 +10,7 @@ SYNOPSIS
10
--------
11
[verse]
12
'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>]
14
[--symref] [<repository> [<refs>...]]
15
16
DESCRIPTION
@@ -60,6 +60,16 @@ OPTIONS
60
upload-pack only shows the symref HEAD, so it will be the only
61
one shown by ls-remote.
62
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
+
73
<repository>::
74
The "remote" repository to query. This parameter can be
75
either a URL or the name of a remote (see the GIT URLS and
@@ -90,6 +100,10 @@ EXAMPLES
100
c5db5456ae3b0873fc659c19fafdde22313cc441 refs/tags/v0.99.2
101
7ceca275d047c90c0c7d5afb13ab97efdf51bd6e refs/tags/v0.99.3
102
103
+SEE ALSO
104
+--------
105
+linkgit:git-check-ref-format[1].
106
+
107
GIT
108
---
109
Part of the linkgit:git[1] suite
builtin/ls-remote.c
+27
-3
@@ -1,6 +1,7 @@
1
#include "builtin.h"
2
#include "cache.h"
3
#include "transport.h"
4
+#include "ref-filter.h"
5
#include "remote.h"
6
7
static const char * const ls_remote_usage[] = {
@@ -43,10 +44,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
44
int show_symref_target = 0;
45
const char *uploadpack = NULL;
46
const char **pattern = NULL;
47
+ int i;
48
49
struct remote *remote;
50
struct transport *transport;
51
const struct ref *ref;
52
+ struct ref_array ref_array;
53
+ static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
54
55
struct option options[] = {
56
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)
64
OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
65
OPT_BOOL(0, "get-url", &get_url,
66
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),
69
OPT_SET_INT_F(0, "exit-code", &status,
70
N_("exit with exit code 2 if no matching refs are found"),
71
2, PARSE_OPT_NOCOMPLETE),
@@ -68,6 +74,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
74
OPT_END()
75
};
76
77
+ memset(&ref_array, 0, sizeof(ref_array));
78
+
79
argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
80
PARSE_OPT_STOP_AT_NON_OPTION);
81
dest = argv[0];
@@ -90,6 +98,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
98
99
if (get_url) {
100
printf("%s\n", *remote->url);
101
+ UNLEAK(sorting);
102
return 0;
103
}
104
@@ -98,20 +107,35 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
107
transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
108
109
ref = transport_get_remote_refs(transport);
101
- if (transport_disconnect(transport))
110
+ if (transport_disconnect(transport)) {
111
+ UNLEAK(sorting);
112
return 1;
113
+ }
114
115
if (!dest && !quiet)
116
fprintf(stderr, "From %s\n", *remote->url);
117
for ( ; ref; ref = ref->next) {
118
+ struct ref_array_item *item;
119
if (!check_ref_type(ref, flags))
120
continue;
121
if (!tail_match(pattern, ref->name))
122
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];
132
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);
135
status = 0; /* we found something */
136
}
137
+
138
+ UNLEAK(sorting);
139
+ UNLEAK(ref_array);
140
return status;
141
}
t/t5512-ls-remote.sh
+47
-7
@@ -10,6 +10,9 @@ test_expect_success setup '
10
test_tick &&
11
git commit -m initial &&
12
git tag mark &&
13
+ git tag mark1.1 &&
14
+ git tag mark1.2 &&
15
+ git tag mark1.10 &&
16
git show-ref --tags -d | sed -e "s/ / /" >expected.tag &&
17
(
18
echo "$(git rev-parse HEAD) HEAD"
@@ -39,6 +42,39 @@ test_expect_success 'ls-remote self' '
42
test_cmp expected.all actual
43
'
44
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
+
78
test_expect_success 'dies when no remote specified and no default remotes found' '
79
test_must_fail git ls-remote
80
'
@@ -131,7 +167,7 @@ test_expect_success 'Report no-match with --exit-code' '
167
168
test_expect_success 'Report match with --exit-code' '
169
git ls-remote --exit-code other.git "refs/tags/*" >actual &&
134
- git ls-remote . tags/mark >expect &&
170
+ git ls-remote . tags/mark* >expect &&
171
test_cmp expect actual
172
'
173
@@ -170,14 +206,18 @@ test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs'
206
grep refs/tags/magic actual
207
'
208
209
+git fetch origin
210
test_expect_success 'ls-remote --symref' '
174
- cat >expect <<-\EOF &&
211
+ cat >expect <<-EOF &&
212
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
221
EOF
222
git ls-remote --symref >actual &&
223
test_cmp expect actual