Commit e09a504cc5b72ff4eca5d539b6fb50c8d01d0036

Stefan Sperling 2019-06-28T20:59:14

introduce support for abbreviated object IDs

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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
diff --git a/got/got.c b/got/got.c
index a4975d6..63ce30e 100644
--- a/got/got.c
+++ b/got/got.c
@@ -560,8 +560,8 @@ cmd_checkout(int argc, char *argv[])
 
 	if (commit_id_str) {
 		struct got_object_id *commit_id;
-		error = got_object_resolve_id_str(&commit_id, repo,
-		    commit_id_str);
+		error = got_repo_match_object_id_prefix(&commit_id,
+		    commit_id_str, repo);
 		if (error != NULL)
 			goto done;
 		error = check_linear_ancestry(commit_id,
@@ -741,8 +741,8 @@ cmd_update(int argc, char *argv[])
 		if (error != NULL)
 			goto done;
 	} else {
-		error = got_object_resolve_id_str(&commit_id, repo,
-		    commit_id_str);
+		error = got_repo_match_object_id_prefix(&commit_id,
+		    commit_id_str, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -1190,8 +1190,8 @@ cmd_log(int argc, char *argv[])
 				goto done;
 		}
 		if (commit == NULL) {
-			error = got_object_resolve_id_str(&id, repo,
-			    start_commit);
+			error = got_repo_match_object_id_prefix(&id,
+			    start_commit, repo);
 			if (error != NULL)
 				return error;
 		}
@@ -1429,7 +1429,7 @@ cmd_diff(int argc, char *argv[])
 		goto done;
 	}
 
-	error = got_object_resolve_id_str(&id1, repo, id_str1);
+	error = got_repo_match_object_id_prefix(&id1, id_str1, repo);
 	if (error) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
@@ -1454,7 +1454,7 @@ cmd_diff(int argc, char *argv[])
 		}
 	}
 
-	error = got_object_resolve_id_str(&id2, repo, id_str2);
+	error = got_repo_match_object_id_prefix(&id2, id_str2, repo);
 	if (error) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
@@ -1642,8 +1642,8 @@ cmd_blame(int argc, char *argv[])
 		if (error != NULL)
 			goto done;
 	} else {
-		error = got_object_resolve_id_str(&commit_id, repo,
-		    commit_id_str);
+		error = got_repo_match_object_id_prefix(&commit_id,
+		    commit_id_str, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -1874,8 +1874,8 @@ cmd_tree(int argc, char *argv[])
 		if (error != NULL)
 			goto done;
 	} else {
-		error = got_object_resolve_id_str(&commit_id, repo,
-		    commit_id_str);
+		error = got_repo_match_object_id_prefix(&commit_id,
+		    commit_id_str, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -2034,7 +2034,7 @@ add_ref(struct got_repository *repo, const char *refname, const char *target)
 	struct got_object_id *id;
 	struct got_reference *ref = NULL;
 
-	err = got_object_resolve_id_str(&id, repo, target);
+	err = got_repo_match_object_id_prefix(&id, target, repo);
 	if (err) {
 		struct got_reference *target_ref;
 
@@ -2996,7 +2996,7 @@ cmd_cherrypick(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
+	error = got_repo_match_object_id_prefix(&commit_id, argv[0], repo);
 	if (error != NULL) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
@@ -3105,7 +3105,7 @@ cmd_backout(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
+	error = got_repo_match_object_id_prefix(&commit_id, argv[0], repo);
 	if (error != NULL) {
 		struct got_reference *ref;
 		if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
diff --git a/include/got_error.h b/include/got_error.h
index c05ac92..83b7c99 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -28,7 +28,7 @@
 #define GOT_ERR_BAD_OBJ_HDR	10
 #define GOT_ERR_OBJ_TYPE	11
 #define GOT_ERR_BAD_OBJ_DATA	12
-/* 13 is currently free for re-use */
+#define GOT_ERR_AMBIGUOUS_ID	13
 #define GOT_ERR_BAD_PACKIDX	14
 #define GOT_ERR_PACKIDX_CSUM	15
 #define GOT_ERR_BAD_PACKFILE	16
@@ -115,7 +115,7 @@ static const struct got_error {
 	{ GOT_ERR_BAD_OBJ_HDR,	"bad object header" },
 	{ GOT_ERR_OBJ_TYPE,	"wrong type of object" },
 	{ GOT_ERR_BAD_OBJ_DATA,	"bad object data" },
-	{ 13,			"unused error code" },
+	{ GOT_ERR_AMBIGUOUS_ID, "ambiguous object ID" },
 	{ GOT_ERR_BAD_PACKIDX,	"bad pack index file" },
 	{ GOT_ERR_PACKIDX_CSUM, "pack index file checksum error" },
 	{ GOT_ERR_BAD_PACKFILE,	"bad pack file" },
diff --git a/include/got_repository.h b/include/got_repository.h
index 0b61b49..69c2e09 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -57,3 +57,7 @@ const struct got_error *got_repo_map_path(char **, struct got_repository *,
 
 /* Create a new repository in an empty directory at a specified path. */
 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 *);
diff --git a/lib/diff.c b/lib/diff.c
index 449f4a6..241a941 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -24,8 +24,8 @@
 #include <sha1.h>
 #include <zlib.h>
 
-#include "got_repository.h"
 #include "got_object.h"
+#include "got_repository.h"
 #include "got_error.h"
 #include "got_diff.h"
 #include "got_opentemp.h"
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index 34beeb2..7af741d 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -161,6 +161,10 @@ 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 *);
+typedef const struct got_error *(*got_packidx_for_each_id_cb)(void *,
+    struct got_object_id *);
+const struct got_error *got_packidx_for_each_id(struct got_packidx *,
+    got_packidx_for_each_id_cb cb, void *);
 
 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.c b/lib/object.c
index 691ab91..60a273e 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1596,4 +1596,3 @@ done:
 	}
 	return err;
 }
-
diff --git a/lib/pack.c b/lib/pack.c
index 9f38ab3..269e6fa 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -444,6 +444,27 @@ got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id
 }
 
 const struct got_error *
+got_packidx_for_each_id(struct got_packidx *packidx,
+    got_packidx_for_each_id_cb cb, void *cb_arg)
+{
+	const struct got_error *err = NULL;
+	uint32_t nobj = betoh32(packidx->hdr.fanout_table[0xff]);
+	struct got_packidx_object_id *oid;
+	struct got_object_id id;
+	int i;
+
+	for (i = 0; i < nobj; i++) {
+		oid = &packidx->hdr.sorted_ids[i];
+		memcpy(&id, oid->sha1, SHA1_DIGEST_LENGTH);
+		err = cb(cb_arg, &id);
+		if (err)
+			break;
+	}
+
+	return err;
+}
+
+const struct got_error *
 got_pack_stop_privsep_child(struct got_pack *pack)
 {
 	const struct got_error *err = NULL;
diff --git a/lib/repository.c b/lib/repository.c
index 34d0da4..f4cf798 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -21,6 +21,7 @@
 #include <sys/mman.h>
 #include <sys/syslimits.h>
 
+#include <ctype.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <dirent.h>
@@ -48,6 +49,7 @@
 #include "got_lib_pack.h"
 #include "got_lib_privsep.h"
 #include "got_lib_worktree.h"
+#include "got_lib_sha1.h"
 #include "got_lib_object_cache.h"
 #include "got_lib_repository.h"
 
@@ -875,3 +877,219 @@ got_repo_init(const char *repo_path)
 
 	return NULL;
 }
+
+struct match_packidx_id_args {
+	const char *id_str_prefix;
+	struct got_object_id *unique_id;
+};
+
+static const struct got_error *
+match_packidx_id(void *arg, struct got_object_id *id)
+{
+	const struct got_error *err;
+	struct match_packidx_id_args *a = arg;
+	char *id_str;
+
+	err = got_object_id_str(&id_str, id);
+	if (err)
+		return err;
+
+	if (strncmp(a->id_str_prefix, id_str, strlen(a->id_str_prefix)) == 0) {
+		if (a->unique_id == NULL) {
+			a->unique_id = got_object_id_dup(id);
+			if (a->unique_id == NULL)
+				err = got_error_from_errno("got_object_id_dup");
+		}
+	}
+
+	free(id_str);
+	return err;
+}
+
+static const struct got_error *
+match_packed_object_id_prefix(struct got_object_id **unique_id,
+    struct got_repository *repo, const char *id_str_prefix)
+{
+	const struct got_error *err = NULL;
+	struct match_packidx_id_args args;
+	char *path_packdir;
+	DIR *packdir;
+	struct dirent *dent;
+	char *path_packidx;
+
+	*unique_id = NULL;
+
+	path_packdir = got_repo_get_path_objects_pack(repo);
+	if (path_packdir == NULL)
+		return got_error_from_errno("got_repo_get_path_objects_pack");
+
+	packdir = opendir(path_packdir);
+	if (packdir == NULL) {
+		err = got_error_from_errno2("opendir", path_packdir);
+		goto done;
+	}
+
+	while ((dent = readdir(packdir)) != NULL) {
+		struct got_packidx *packidx;
+		const struct got_error *cb_err;
+
+		if (!is_packidx_filename(dent->d_name, dent->d_namlen))
+			continue;
+
+		if (asprintf(&path_packidx, "%s/%s", path_packdir,
+		    dent->d_name) == -1) {
+			err = got_error_from_errno("asprintf");
+			goto done;
+		}
+
+		err = got_packidx_open(&packidx, path_packidx, 0);
+		free(path_packidx);
+		if (err)
+			goto done;
+
+		args.id_str_prefix = id_str_prefix;
+		args.unique_id = NULL;
+		cb_err = got_packidx_for_each_id(packidx, match_packidx_id,
+		    &args);
+		err = got_packidx_close(packidx);
+		if (err)
+			break;
+		if (cb_err) {
+			err = cb_err;
+			break;
+		}
+
+		if (args.unique_id) {
+			if (*unique_id == NULL)
+				*unique_id = args.unique_id;
+			else {
+				err = got_error(GOT_ERR_AMBIGUOUS_ID);
+				break;
+			}
+		}
+	}
+done:
+	free(path_packdir);
+	if (packdir && closedir(packdir) != 0 && err == NULL)
+		err = got_error_from_errno("closedir");
+	if (err) {
+		free(*unique_id);
+		*unique_id = NULL;
+	}
+	return err;
+}
+
+static const struct got_error *
+match_object_id(struct got_object_id **unique_id, const char *path_objects,
+    const char *object_dir, const char *id_str_prefix,
+    struct got_repository *repo)
+{
+	const struct got_error *err = NULL;
+	char *path;
+	DIR *dir = NULL;
+	struct dirent *dent;
+	struct got_object_id id;
+	int i;
+
+	for (i = 0; i < strlen(id_str_prefix); i++) {
+		if (isxdigit((unsigned char)id_str_prefix[i]))
+			continue;
+		return got_error(GOT_ERR_BAD_OBJ_ID_STR);
+	}
+
+	/* Search pack index. */
+	err = match_packed_object_id_prefix(unique_id, repo, id_str_prefix);
+	if (err)
+		return err;
+
+	/* Search on-disk directories. */
+	if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
+		err = got_error_from_errno("asprintf");
+		goto done;
+	}
+
+	dir = opendir(path);
+	if (dir == NULL) {
+		err = got_error_from_errno2("opendir", path);
+		goto done;
+	}
+	while ((dent = readdir(dir)) != NULL) {
+		char *id_str;
+		if (strcmp(dent->d_name, ".") == 0 ||
+		    strcmp(dent->d_name, "..") == 0)
+			continue;
+
+		if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
+			err = got_error_from_errno("asprintf");
+			goto done;
+		}
+
+		if (!got_parse_sha1_digest(id.sha1, id_str))
+			continue;
+
+		if (strncmp(id_str_prefix, id_str,
+		    strlen(id_str_prefix)) != 0) {
+			free(id_str);
+			continue;
+		}
+
+		if (*unique_id == NULL) {
+			*unique_id = got_object_id_dup(&id);
+			if (*unique_id == NULL) {
+				err = got_error_from_errno("got_object_id_dup");
+				free(id_str);
+				goto done;
+			}
+		} else {
+			err = got_error(GOT_ERR_AMBIGUOUS_ID);
+			free(id_str);
+			goto done;
+		}
+	}
+
+	if (err == NULL && *unique_id == NULL)
+		err = got_error(GOT_ERR_NO_OBJ);
+done:
+	if (err) {
+		free(*unique_id);
+		*unique_id = NULL;
+	}
+	if (dir && closedir(dir) != 0 && err == NULL)
+		err = got_error_from_errno("closedir");
+	free(path);
+	return err;
+}
+
+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 struct got_error *err = NULL;
+	char *path_objects = got_repo_get_path_objects(repo);
+	char *object_dir = NULL;
+	size_t len;
+
+	len = strlen(id_str_prefix);
+	if (len >= 2) {
+		object_dir = strndup(id_str_prefix, 2);
+		if (object_dir == NULL)
+			return got_error_from_errno("strdup");
+		err = match_object_id(id, path_objects, object_dir,
+		    id_str_prefix, repo);
+	} else if (len == 1) {
+		int i;
+		for (i = 0; i < 0xf; i++) {
+			if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
+			    == -1)
+				return got_error_from_errno("asprintf");
+			err = match_object_id(id, path_objects, object_dir,
+			    id_str_prefix, repo);
+			if (err)
+				break;
+		}
+	} else
+		return got_error(GOT_ERR_BAD_OBJ_ID_STR);
+
+	free(object_dir);
+	return err;
+}
diff --git a/tog/tog.c b/tog/tog.c
index fc0bb75..79ae802 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2221,8 +2221,8 @@ cmd_log(int argc, char *argv[])
 		    got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
 		    repo);
 	else
-		error = got_object_resolve_id_str(&start_id, repo,
-		    start_commit);
+		error = got_repo_match_object_id_prefix(&start_id,
+		    start_commit, repo);
 	if (error != NULL)
 		goto done;
 
@@ -2830,11 +2830,11 @@ cmd_diff(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	error = got_object_resolve_id_str(&id1, repo, id_str1);
+	error = got_repo_match_object_id_prefix(&id1, id_str1, repo);
 	if (error)
 		goto done;
 
-	error = got_object_resolve_id_str(&id2, repo, id_str2);
+	error = got_repo_match_object_id_prefix(&id2, id_str2, repo);
 	if (error)
 		goto done;
 
@@ -3684,8 +3684,8 @@ cmd_blame(int argc, char *argv[])
 		error = got_ref_resolve(&commit_id, repo, head_ref);
 		got_ref_close(head_ref);
 	} else {
-		error = got_object_resolve_id_str(&commit_id, repo,
-		    commit_id_str);
+		error = got_repo_match_object_id_prefix(&commit_id,
+		    commit_id_str, repo);
 	}
 	if (error != NULL)
 		goto done;
@@ -4411,8 +4411,8 @@ cmd_tree(int argc, char *argv[])
 	if (commit_id_arg == NULL)
 		error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
 	else
-		error = got_object_resolve_id_str(&commit_id, repo,
-		    commit_id_arg);
+		error = got_repo_match_object_id_prefix(&commit_id,
+		    commit_id_arg, repo);
 	if (error != NULL)
 		goto done;