Commit dd88155e484f5bbae1af4e3c8d020cabc2e647ab

Stefan Sperling 2019-06-29T23:26:36

disambiguate abbreviated object IDs based on object type

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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
diff --git a/TODO b/TODO
index 033920c..7aaa8ea 100644
--- a/TODO
+++ b/TODO
@@ -9,7 +9,6 @@ lib:
   (maybe let got-read-pack cache ref/offset delta objects in object_cache.c?)
 - improve performance of usr.bin/diff and port these changes back to got
   (e.g. diffing between versions of sys/dev/pci/pcidevs is too slow)
-- disambiguate abbreviated object IDs based on object type
 
 tog:
 - implement horizonal scrolling in all views
diff --git a/got/got.c b/got/got.c
index 63ce30e..aa4785e 100644
--- a/got/got.c
+++ b/got/got.c
@@ -561,7 +561,7 @@ cmd_checkout(int argc, char *argv[])
 	if (commit_id_str) {
 		struct got_object_id *commit_id;
 		error = got_repo_match_object_id_prefix(&commit_id,
-		    commit_id_str, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
 		if (error != NULL)
 			goto done;
 		error = check_linear_ancestry(commit_id,
@@ -742,7 +742,7 @@ cmd_update(int argc, char *argv[])
 			goto done;
 	} else {
 		error = got_repo_match_object_id_prefix(&commit_id,
-		    commit_id_str, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -1191,7 +1191,7 @@ cmd_log(int argc, char *argv[])
 		}
 		if (commit == NULL) {
 			error = got_repo_match_object_id_prefix(&id,
-			    start_commit, repo);
+			    start_commit, GOT_OBJ_TYPE_COMMIT, repo);
 			if (error != NULL)
 				return error;
 		}
@@ -1429,7 +1429,8 @@ cmd_diff(int argc, char *argv[])
 		goto done;
 	}
 
-	error = got_repo_match_object_id_prefix(&id1, id_str1, repo);
+	error = got_repo_match_object_id_prefix(&id1, id_str1,
+	    GOT_OBJ_TYPE_ANY, repo);
 	if (error) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
@@ -1454,7 +1455,8 @@ cmd_diff(int argc, char *argv[])
 		}
 	}
 
-	error = got_repo_match_object_id_prefix(&id2, id_str2, repo);
+	error = got_repo_match_object_id_prefix(&id2, id_str2,
+	    GOT_OBJ_TYPE_ANY, repo);
 	if (error) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
@@ -1643,7 +1645,7 @@ cmd_blame(int argc, char *argv[])
 			goto done;
 	} else {
 		error = got_repo_match_object_id_prefix(&commit_id,
-		    commit_id_str, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -1875,7 +1877,7 @@ cmd_tree(int argc, char *argv[])
 			goto done;
 	} else {
 		error = got_repo_match_object_id_prefix(&commit_id,
-		    commit_id_str, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -2034,7 +2036,8 @@ add_ref(struct got_repository *repo, const char *refname, const char *target)
 	struct got_object_id *id;
 	struct got_reference *ref = NULL;
 
-	err = got_repo_match_object_id_prefix(&id, target, repo);
+	err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
+	    repo);
 	if (err) {
 		struct got_reference *target_ref;
 
@@ -2996,7 +2999,8 @@ cmd_cherrypick(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_repo_match_object_id_prefix(&commit_id, argv[0], repo);
+	error = got_repo_match_object_id_prefix(&commit_id, argv[0],
+	    GOT_OBJ_TYPE_COMMIT, repo);
 	if (error != NULL) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
@@ -3105,7 +3109,8 @@ cmd_backout(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_repo_match_object_id_prefix(&commit_id, argv[0], repo);
+	error = got_repo_match_object_id_prefix(&commit_id, argv[0],
+	    GOT_OBJ_TYPE_COMMIT, repo);
 	if (error != NULL) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
diff --git a/include/got_object.h b/include/got_object.h
index 4f5a750..5e46d49 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -45,8 +45,10 @@ SIMPLEQ_HEAD(got_object_id_queue, got_object_qid);
 const struct got_error *got_object_qid_alloc(struct got_object_qid **,
     struct got_object_id *);
 void got_object_qid_free(struct got_object_qid *);
+void got_object_id_queue_free(struct got_object_id_queue *);
 
 /* Object types. */
+#define GOT_OBJ_TYPE_ANY		0 /* wildcard value at run-time */
 #define GOT_OBJ_TYPE_COMMIT		1
 #define GOT_OBJ_TYPE_TREE		2
 #define GOT_OBJ_TYPE_BLOB		3
diff --git a/include/got_repository.h b/include/got_repository.h
index 69c2e09..d4ac915 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -60,4 +60,4 @@ const struct got_error *got_repo_init(const char *);
 
 /* Attempt to find a unique object ID for a given ID string prefix. */
 const struct got_error *got_repo_match_object_id_prefix(struct got_object_id **,
-    const char *, struct got_repository *);
+    const char *, int, struct got_repository *);
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index a5191eb..7e7c697 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -161,8 +161,8 @@ const struct got_error *got_packidx_open(struct got_packidx **,
     const char *, int);
 const struct got_error *got_packidx_close(struct got_packidx *);
 int got_packidx_get_object_idx(struct got_packidx *, struct got_object_id *);
-const struct got_error *got_packidx_match_id_str_prefix(struct got_object_id **,
-    struct got_packidx *, const char *);
+const struct got_error *got_packidx_match_id_str_prefix(
+    struct got_object_id_queue *, struct got_packidx *, const char *);
 
 const struct got_error *got_packfile_open_object(struct got_object **,
     struct got_pack *, struct got_packidx *, int, struct got_object_id *);
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 9559b34..d924810 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -128,6 +128,18 @@ got_object_qid_free(struct got_object_qid *qid)
 	free(qid);
 }
 
+void
+got_object_id_queue_free(struct got_object_id_queue *ids)
+{
+	struct got_object_qid *qid;
+
+	while (!SIMPLEQ_EMPTY(ids)) {
+		qid = SIMPLEQ_FIRST(ids);
+		SIMPLEQ_REMOVE_HEAD(ids, entry);
+		got_object_qid_free(qid);
+	}
+}
+
 const struct got_error *
 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
 {
@@ -343,20 +355,13 @@ parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
 void
 got_object_commit_close(struct got_commit_object *commit)
 {
-	struct got_object_qid *qid;
-
 	if (commit->refcnt > 0) {
 		commit->refcnt--;
 		if (commit->refcnt > 0)
 			return;
 	}
 
-	while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
-		qid = SIMPLEQ_FIRST(&commit->parent_ids);
-		SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
-		got_object_qid_free(qid);
-	}
-
+	got_object_id_queue_free(&commit->parent_ids);
 	free(commit->tree_id);
 	free(commit->author);
 	free(commit->committer);
diff --git a/lib/pack.c b/lib/pack.c
index b2200e9..f002e06 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -41,6 +41,7 @@
 #include "got_lib_delta.h"
 #include "got_lib_inflate.h"
 #include "got_lib_object.h"
+#include "got_lib_object_parse.h"
 #include "got_lib_privsep.h"
 #include "got_lib_pack.h"
 
@@ -444,9 +445,10 @@ got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id
 }
 
 const struct got_error *
-got_packidx_match_id_str_prefix(struct got_object_id **unique_id,
+got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
     struct got_packidx *packidx, const char *id_str_prefix)
 {
+	const struct got_error *err = NULL;
 	u_int8_t id0;
 	uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
 	char hex[3];
@@ -454,7 +456,7 @@ got_packidx_match_id_str_prefix(struct got_object_id **unique_id,
 	struct got_packidx_object_id *oid;
 	int i;
 
-	*unique_id = NULL;
+	SIMPLEQ_INIT(matched_ids);
 
 	if (prefix_len < 2)
 		return got_error(GOT_ERR_BAD_OBJ_ID_STR);
@@ -472,6 +474,7 @@ got_packidx_match_id_str_prefix(struct got_object_id **unique_id,
 	oid = &packidx->hdr.sorted_ids[i];
 	while (i < totobj && oid->sha1[0] == id0) {
 		char id_str[SHA1_DIGEST_STRING_LENGTH];
+		struct got_object_qid *qid;
 		int cmp;
 
 		if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
@@ -484,17 +487,24 @@ got_packidx_match_id_str_prefix(struct got_object_id **unique_id,
 		} else if (cmp > 0)
 			break;
 
-		if (*unique_id != NULL)
-			return got_error(GOT_ERR_AMBIGUOUS_ID);
-		*unique_id = malloc(sizeof(**unique_id));
-		if (*unique_id == NULL)
-			return got_error_from_errno("malloc");
-		memcpy((*unique_id)->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
+		err = got_object_qid_alloc_partial(&qid);
+		if (err)
+			break;
+		memcpy(qid->id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
+		SIMPLEQ_INSERT_TAIL(matched_ids, qid, entry);
 
 		oid = &packidx->hdr.sorted_ids[++i];
 	}
 
-	return NULL;
+	if (err) {
+		while (!SIMPLEQ_EMPTY(matched_ids)) {
+			struct got_object_qid *qid;
+			qid = SIMPLEQ_FIRST(matched_ids);
+			SIMPLEQ_REMOVE_HEAD(matched_ids, entry);
+			got_object_qid_free(qid);
+		}
+	}
+	return err;
 }
 
 const struct got_error *
diff --git a/lib/repository.c b/lib/repository.c
index 4bb4b6a..14de200 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -880,13 +880,16 @@ got_repo_init(const char *repo_path)
 
 static const struct got_error *
 match_packed_object(struct got_object_id **unique_id,
-    struct got_repository *repo, const char *id_str_prefix)
+    struct got_repository *repo, const char *id_str_prefix, int obj_type)
 {
 	const struct got_error *err = NULL;
 	char *path_packdir;
 	DIR *packdir;
 	struct dirent *dent;
 	char *path_packidx;
+	struct got_object_id_queue matched_ids;
+
+	SIMPLEQ_INIT(&matched_ids);
 
 	path_packdir = got_repo_get_path_objects_pack(repo);
 	if (path_packdir == NULL)
@@ -900,7 +903,8 @@ match_packed_object(struct got_object_id **unique_id,
 
 	while ((dent = readdir(packdir)) != NULL) {
 		struct got_packidx *packidx;
-		struct got_object_id *unique_id_in_pack;
+		struct got_object_qid *qid;
+
 
 		if (!is_packidx_filename(dent->d_name, dent->d_namlen))
 			continue;
@@ -916,29 +920,40 @@ match_packed_object(struct got_object_id **unique_id,
 		if (err)
 			break;
 
-		err = got_packidx_match_id_str_prefix(&unique_id_in_pack,
+		err = got_packidx_match_id_str_prefix(&matched_ids,
 		    packidx, id_str_prefix);
 		if (err) {
 			got_packidx_close(packidx);
 			break;
 		}
 		err = got_packidx_close(packidx);
-		if (err) {
-			free(unique_id_in_pack);
+		if (err)
 			break;
-		}
 
-		if (unique_id_in_pack) {
+		SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
+			if (obj_type != GOT_OBJ_TYPE_ANY) {
+				int matched_type;
+				err = got_object_get_type(&matched_type, repo,
+				    qid->id);
+				if (err)
+					goto done;
+				if (matched_type != obj_type)
+					continue;
+			}
 			if (*unique_id == NULL) {
-				*unique_id = unique_id_in_pack;
+				*unique_id = got_object_id_dup(qid->id);
+				if (*unique_id == NULL) {
+					err = got_error_from_errno("malloc");
+					goto done;
+				}
 			} else {
-				free(unique_id_in_pack);
 				err = got_error(GOT_ERR_AMBIGUOUS_ID);
 				break;
 			}
 		}
 	}
 done:
+	got_object_id_queue_free(&matched_ids);
 	free(path_packdir);
 	if (packdir && closedir(packdir) != 0 && err == NULL)
 		err = got_error_from_errno("closedir");
@@ -951,7 +966,7 @@ done:
 
 static const struct got_error *
 match_loose_object(struct got_object_id **unique_id, const char *path_objects,
-    const char *object_dir, const char *id_str_prefix,
+    const char *object_dir, const char *id_str_prefix, int obj_type,
     struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
@@ -1000,6 +1015,15 @@ match_loose_object(struct got_object_id **unique_id, const char *path_objects,
 		}
 
 		if (*unique_id == NULL) {
+			if (obj_type != GOT_OBJ_TYPE_ANY) {
+				int matched_type;
+				err = got_object_get_type(&matched_type, repo,
+				    &id);
+				if (err)
+					goto done;
+				if (matched_type != obj_type)
+					continue;
+			}
 			*unique_id = got_object_id_dup(&id);
 			if (*unique_id == NULL) {
 				err = got_error_from_errno("got_object_id_dup");
@@ -1025,7 +1049,7 @@ done:
 
 const struct got_error *
 got_repo_match_object_id_prefix(struct got_object_id **id,
-    const char *id_str_prefix, struct got_repository *repo)
+    const char *id_str_prefix, int obj_type, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *path_objects = got_repo_get_path_objects(repo);
@@ -1043,7 +1067,7 @@ got_repo_match_object_id_prefix(struct got_object_id **id,
 
 	len = strlen(id_str_prefix);
 	if (len >= 2) {
-		err = match_packed_object(id, repo, id_str_prefix);
+		err = match_packed_object(id, repo, id_str_prefix, obj_type);
 		if (err)
 			goto done;
 		object_dir = strndup(id_str_prefix, 2);
@@ -1052,7 +1076,7 @@ got_repo_match_object_id_prefix(struct got_object_id **id,
 			goto done;
 		}
 		err = match_loose_object(id, path_objects, object_dir,
-		    id_str_prefix, repo);
+		    id_str_prefix, obj_type, repo);
 	} else if (len == 1) {
 		int i;
 		for (i = 0; i < 0xf; i++) {
@@ -1061,11 +1085,12 @@ got_repo_match_object_id_prefix(struct got_object_id **id,
 				err = got_error_from_errno("asprintf");
 				goto done;
 			}
-			err = match_packed_object(id, repo, object_dir);
+			err = match_packed_object(id, repo, object_dir,
+			    obj_type);
 			if (err)
 				goto done;
 			err = match_loose_object(id, path_objects, object_dir,
-			    id_str_prefix, repo);
+			    id_str_prefix, obj_type, repo);
 			if (err)
 				goto done;
 		}
diff --git a/tog/tog.c b/tog/tog.c
index 79ae802..9093d14 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2222,7 +2222,7 @@ cmd_log(int argc, char *argv[])
 		    repo);
 	else
 		error = got_repo_match_object_id_prefix(&start_id,
-		    start_commit, repo);
+		    start_commit, GOT_OBJ_TYPE_COMMIT, repo);
 	if (error != NULL)
 		goto done;
 
@@ -2830,11 +2830,13 @@ cmd_diff(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_repo_match_object_id_prefix(&id1, id_str1, repo);
+	error = got_repo_match_object_id_prefix(&id1, id_str1,
+	    GOT_OBJ_TYPE_ANY, repo);
 	if (error)
 		goto done;
 
-	error = got_repo_match_object_id_prefix(&id2, id_str2, repo);
+	error = got_repo_match_object_id_prefix(&id2, id_str2,
+	    GOT_OBJ_TYPE_ANY, repo);
 	if (error)
 		goto done;
 
@@ -3685,7 +3687,7 @@ cmd_blame(int argc, char *argv[])
 		got_ref_close(head_ref);
 	} else {
 		error = got_repo_match_object_id_prefix(&commit_id,
-		    commit_id_str, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
 	}
 	if (error != NULL)
 		goto done;
@@ -4412,7 +4414,7 @@ cmd_tree(int argc, char *argv[])
 		error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
 	else
 		error = got_repo_match_object_id_prefix(&commit_id,
-		    commit_id_arg, repo);
+		    commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
 	if (error != NULL)
 		goto done;