Commit b2070a3f25a75399baa9a402542a60326197a053

Stefan Sperling 2020-03-22T14:21:07

allow for limiting output of 'got ref -l' to a single ref or a namespace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
diff --git a/got/got.1 b/got/got.1
index 59cc0bc..99bf3e8 100644
--- a/got/got.1
+++ b/got/got.1
@@ -843,7 +843,15 @@ If this directory is a
 .Nm
 work tree, use the repository path associated with this work tree.
 .It Fl l
-List all existing references in the repository.
+List references in the repository.
+If no
+.Ar name
+is specified, list all existing references in the repository.
+If
+.Ar name
+is a reference namespace, list all references in this namespace.
+Otherwise, show only the reference with the given
+.Ar name .
 Cannot be used together with any other options except
 .Fl r .
 .It Fl c Ar object
diff --git a/got/got.c b/got/got.c
index b27ba04..a10c0e2 100644
--- a/got/got.c
+++ b/got/got.c
@@ -4182,14 +4182,14 @@ usage_ref(void)
 }
 
 static const struct got_error *
-list_refs(struct got_repository *repo)
+list_refs(struct got_repository *repo, const char *refname)
 {
 	static const struct got_error *err = NULL;
 	struct got_reflist_head refs;
 	struct got_reflist_entry *re;
 
 	SIMPLEQ_INIT(&refs);
-	err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
+	err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
 	if (err)
 		return err;
 
@@ -4304,7 +4304,8 @@ cmd_ref(int argc, char *argv[])
 	struct got_worktree *worktree = NULL;
 	char *cwd = NULL, *repo_path = NULL;
 	int ch, do_list = 0, do_delete = 0;
-	const char *refname = NULL, *obj_arg = NULL, *symref_target= NULL;
+	const char *obj_arg = NULL, *symref_target= NULL;
+	char *refname = NULL;
 
 	while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
 		switch (ch) {
@@ -4350,14 +4351,28 @@ cmd_ref(int argc, char *argv[])
 	argv += optind;
 
 	if (do_list) {
-		if (argc > 0)
+		if (argc != 0 && argc != 1)
 			usage_ref();
+		if (argc == 1) {
+			refname = strdup(argv[0]);
+			if (refname == NULL) {
+				error = got_error_from_errno("strdup");
+				goto done;
+			}
+		}
 	} else {
 		if (argc != 1)
 			usage_ref();
-		refname = argv[0];
+		refname = strdup(argv[0]);
+		if (refname == NULL) {
+			error = got_error_from_errno("strdup");
+			goto done;
+		}
 	}
 
+	if (refname)
+		got_path_strip_trailing_slashes(refname);
+
 #ifndef PROFILE
 	if (do_list) {
 		if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
@@ -4407,7 +4422,7 @@ cmd_ref(int argc, char *argv[])
 		goto done;
 
 	if (do_list)
-		error = list_refs(repo);
+		error = list_refs(repo, refname);
 	else if (do_delete)
 		error = delete_ref(repo, refname);
 	else if (symref_target)
@@ -4418,6 +4433,7 @@ cmd_ref(int argc, char *argv[])
 		error = add_ref(repo, refname, obj_arg);
 	}
 done:
+	free(refname);
 	if (repo)
 		got_repo_close(repo);
 	if (worktree)
diff --git a/lib/reference.c b/lib/reference.c
index 86409d0..f74cc9e 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -162,7 +162,7 @@ parse_ref_line(struct got_reference **ref, const char *name, const char *line)
 
 static const struct got_error *
 parse_ref_file(struct got_reference **ref, const char *name,
-    const char *abspath, int lock)
+    const char *absname, const char *abspath, int lock)
 {
 	const struct got_error *err = NULL;
 	FILE *f;
@@ -175,24 +175,34 @@ parse_ref_file(struct got_reference **ref, const char *name,
 		err = got_lockfile_lock(&lf, abspath);
 		if (err) {
 			if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
-				err = got_error(GOT_ERR_NOT_REF);
+				err = got_error_not_ref(name);
 			return err;
 		}
 	}
 
 	f = fopen(abspath, "rb");
 	if (f == NULL) {
+		if (errno != ENOTDIR && errno != ENOENT)
+			err = got_error_from_errno2("fopen", abspath);
+		else
+			err = got_error_not_ref(name);
 		if (lock)
 			got_lockfile_unlock(lf);
-		return NULL;
+		return err;
 	}
 
 	linelen = getline(&line, &linesize, f);
 	if (linelen == -1) {
 		if (feof(f))
 			err = NULL; /* ignore empty files (could be locks) */
-		else
-			err = got_error_from_errno2("getline", abspath);
+		else {
+			if (errno == EISDIR)
+				err = got_error(GOT_ERR_NOT_REF);
+			else if (ferror(f))
+				err = got_ferror(f, GOT_ERR_IO);
+			else
+				err = got_error_from_errno2("getline", abspath);
+		}
 		if (lock)
 			got_lockfile_unlock(lf);
 		goto done;
@@ -202,7 +212,7 @@ parse_ref_file(struct got_reference **ref, const char *name,
 		linelen--;
 	}
 
-	err = parse_ref_line(ref, name, line);
+	err = parse_ref_line(ref, absname, line);
 	if (lock) {
 		if (err)
 			got_lockfile_unlock(lf);
@@ -411,7 +421,7 @@ open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
 		}
 	}
 
-	err = parse_ref_file(ref, absname, path, lock);
+	err = parse_ref_file(ref, name, absname, path, lock);
 done:
 	if (!ref_is_absolute && !ref_is_well_known)
 		free(absname);
@@ -449,7 +459,7 @@ got_ref_open(struct got_reference **ref, struct got_repository *repo,
 		for (i = 0; i < nitems(subdirs); i++) {
 			err = open_ref(ref, path_refs, subdirs[i], refname,
 			    lock);
-			if (err || *ref)
+			if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
 				goto done;
 		}
 
@@ -802,6 +812,9 @@ gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
 	DIR *d = NULL;
 	char *path_subdir;
 
+	while (subdir[0] == '/')
+		subdir++;
+
 	if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
 		return got_error_from_errno("asprintf");
 
@@ -865,7 +878,8 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 {
 	const struct got_error *err;
 	char *packed_refs_path, *path_refs = NULL;
-	const char *ondisk_ref_namespace = NULL;
+	char *abs_namespace = NULL;
+	char *buf = NULL, *ondisk_ref_namespace = NULL;
 	FILE *f = NULL;
 	struct got_reference *ref;
 	struct got_reflist_entry *new;
@@ -885,11 +899,52 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 			got_ref_close(ref);
 		if (err && err->code != GOT_ERR_NOT_REF)
 			goto done;
+	} else {
+		/* Try listing a single reference. */
+		const char *refname = ref_namespace;
+		path_refs = get_refs_dir_path(repo, refname);
+		if (path_refs == NULL) {
+			err = got_error_from_errno("get_refs_dir_path");
+			goto done;
+		}
+		err = open_ref(&ref, path_refs, "", refname, 0);
+		if (err) {
+			if (err->code != GOT_ERR_NOT_REF)
+				goto done;
+			/* Try to look up references in a given namespace. */
+		} else {
+			err = insert_ref(&new, refs, ref, repo,
+			    cmp_cb, cmp_arg);
+			if (err || new == NULL /* duplicate */)
+				got_ref_close(ref);
+			return err;
+		}
 	}
 
-	ondisk_ref_namespace = ref_namespace;
-	if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
-		ondisk_ref_namespace += 5;
+	if (ref_namespace) {
+		size_t len;
+		/* Canonicalize the path to eliminate double-slashes if any. */
+		if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
+			err = got_error_from_errno("asprintf");
+			goto done;
+		}
+		len = strlen(abs_namespace) + 1;
+		buf = malloc(len);
+		if (buf == NULL) {
+			err = got_error_from_errno("malloc");
+			goto done;
+		}
+		err = got_canonpath(abs_namespace, buf, len);
+		if (err)
+			goto done;
+		ondisk_ref_namespace = buf;
+		while (ondisk_ref_namespace[0] == '/')
+			ondisk_ref_namespace++;
+		if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
+			ondisk_ref_namespace += 5;
+		else if (strcmp(ondisk_ref_namespace, "refs") == 0)
+			ondisk_ref_namespace = "";
+	}
 
 	/* Gather on-disk refs before parsing packed-refs. */
 	free(path_refs);
@@ -936,8 +991,9 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 				if (ref_namespace) {
 					const char *name;
 					name = got_ref_get_name(ref);
-					if (strncmp(name, ref_namespace,
-					    strlen(ref_namespace)) != 0) {
+					if (!got_path_is_child(name,
+					    ref_namespace,
+					    strlen(ref_namespace))) {
 						got_ref_close(ref);
 						continue;
 					}
@@ -952,6 +1008,8 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 		}
 	}
 done:
+	free(abs_namespace);
+	free(buf);
 	free(path_refs);
 	if (f && fclose(f) != 0 && err == NULL)
 		err = got_error_from_errno("fclose");
diff --git a/regress/cmdline/ref.sh b/regress/cmdline/ref.sh
index 9473644..8457642 100755
--- a/regress/cmdline/ref.sh
+++ b/regress/cmdline/ref.sh
@@ -242,5 +242,135 @@ function test_ref_delete {
 	test_done "$testroot" "$ret"
 }
 
+function test_ref_list {
+	local testroot=`test_init ref_list`
+	local commit_id=`git_show_head $testroot/repo`
+
+	# Create a tag pointing at a commit ID
+	got tag -r $testroot/repo -c $commit_id -m "1.0" "1.0" >/dev/null
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got tag command failed unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	local tag_id=`got ref -r $testroot/repo -l \
+		| grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
+
+	# Create a ref based on repository's HEAD reference
+	got ref -r $testroot/repo -c HEAD refs/foo/zoo
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		echo "got ref command failed unexpectedly"
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# Create a head ref based on another specific ref
+	(cd $testroot/repo && got ref -c refs/heads/master refs/foo/bar/baz)
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -r $testroot/repo -l > $testroot/stdout
+
+	echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+	echo "refs/foo/bar/baz: $commit_id" >> $testroot/stdout.expected
+	echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -r $testroot/repo -l refs > $testroot/stdout
+
+	echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
+	echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
+	echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+	echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	got ref -r $testroot/repo -l refs/tags > $testroot/stdout
+
+	echo "refs/tags/1.0: $tag_id" > $testroot/stdout.expected
+	cmp -s $testroot/stdout $testroot/stdout.expected
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	for r in refs/foo/bar/baz refs/foo/bar/baz foo/bar/baz foo/bar; do
+		got ref -r $testroot/repo -l $r > $testroot/stdout
+
+		echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
+		cmp -s $testroot/stdout $testroot/stdout.expected
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			diff -u $testroot/stdout.expected $testroot/stdout
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
+	for r in refs/foo foo; do
+		got ref -r $testroot/repo -l $r > $testroot/stdout
+
+		echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
+		echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
+		cmp -s $testroot/stdout $testroot/stdout.expected
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			diff -u $testroot/stdout.expected $testroot/stdout
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
+	for r in refs//foo/bar refs//foo//bar refs////////foo//bar; do
+		got ref -r $testroot/repo -l $r > $testroot/stdout
+
+		echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
+		cmp -s $testroot/stdout $testroot/stdout.expected
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			diff -u $testroot/stdout.expected $testroot/stdout
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done
+
+	# attempt to list non-existing references
+	for r in refs/fo bar baz moo riffs /refs/abc refs/foo/bar/baz/moo; do
+		got ref -r $testroot/repo -l $r > $testroot/stdout
+
+		echo -n > $testroot/stdout.expected
+		cmp -s $testroot/stdout $testroot/stdout.expected
+		ret="$?"
+		if [ "$ret" != "0" ]; then
+			diff -u $testroot/stdout.expected $testroot/stdout
+			test_done "$testroot" "$ret"
+			return 1
+		fi
+	done	
+
+	test_done "$testroot" "$ret"
+}
+
 run_test test_ref_create
 run_test test_ref_delete
+run_test test_ref_list