Commit 84de91061b4d6cf5649efd5a42c4c3b90550332c

Stefan Sperling 2020-12-26T20:48:43

Add a refs argument to got_repo_match_object_id(), replacing 'resolve_tags' Make use of this where possible to avoid re-reading references from disk. ok naddy

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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
diff --git a/got/got.c b/got/got.c
index 5f87f8a..5ae8df4 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2743,8 +2743,15 @@ cmd_checkout(int argc, char *argv[])
 
 	if (commit_id_str) {
 		struct got_object_id *commit_id;
+		struct got_reflist_head refs;
+		SIMPLEQ_INIT(&refs);
+		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
+		    NULL);
+		if (error)
+			goto done;
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
+		got_ref_list_free(&refs);
 		if (error)
 			goto done;
 		error = check_linear_ancestry(commit_id,
@@ -3050,8 +3057,15 @@ cmd_update(int argc, char *argv[])
 		if (error != NULL)
 			goto done;
 	} else {
+		struct got_reflist_head refs;
+		SIMPLEQ_INIT(&refs);
+		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
+		    NULL);
+		if (error)
+			goto done;
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
+		got_ref_list_free(&refs);
 		free(commit_id_str);
 		commit_id_str = NULL;
 		if (error)
@@ -3815,6 +3829,10 @@ cmd_log(int argc, char *argv[])
 	if (error)
 		goto done;
 
+	error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
+	if (error)
+		goto done;
+
 	if (start_commit == NULL) {
 		struct got_reference *head_ref;
 		struct got_commit_object *commit = NULL;
@@ -3834,13 +3852,13 @@ cmd_log(int argc, char *argv[])
 		got_object_commit_close(commit);
 	} else {
 		error = got_repo_match_object_id(&start_id, NULL,
-		    start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
 		if (error != NULL)
 			goto done;
 	}
 	if (end_commit != NULL) {
 		error = got_repo_match_object_id(&end_id, NULL,
-		    end_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
 		if (error != NULL)
 			goto done;
 	}
@@ -3870,10 +3888,6 @@ cmd_log(int argc, char *argv[])
 		path = in_repo_path;
 	}
 
-	error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
-	if (error)
-		goto done;
-
 	error = print_commits(start_id, end_id, repo, path ? path : "",
 	    show_changed_paths, show_patch, search_pattern, diff_context,
 	    limit, log_branches, reverse_display_order, &refs);
@@ -4114,6 +4128,9 @@ cmd_diff(int argc, char *argv[])
 	int force_text_diff = 0;
 	const char *errstr;
 	char *path = NULL;
+	struct got_reflist_head refs;
+
+	SIMPLEQ_INIT(&refs);
 
 #ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
@@ -4256,13 +4273,17 @@ cmd_diff(int argc, char *argv[])
 		goto done;
 	}
 
+	error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
+	if (error)
+		return error;
+
 	error = got_repo_match_object_id(&id1, &label1, id_str1,
-	    GOT_OBJ_TYPE_ANY, 1, repo);
+	    GOT_OBJ_TYPE_ANY, &refs, repo);
 	if (error)
 		goto done;
 
 	error = got_repo_match_object_id(&id2, &label2, id_str2,
-	    GOT_OBJ_TYPE_ANY, 1, repo);
+	    GOT_OBJ_TYPE_ANY, &refs, repo);
 	if (error)
 		goto done;
 
@@ -4313,6 +4334,7 @@ done:
 		if (error == NULL)
 			error = repo_error;
 	}
+	got_ref_list_free(&refs);
 	return error;
 }
 
@@ -4559,8 +4581,15 @@ cmd_blame(int argc, char *argv[])
 		if (error != NULL)
 			goto done;
 	} else {
+		struct got_reflist_head refs;
+		SIMPLEQ_INIT(&refs);
+		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
+		    NULL);
+		if (error)
+			goto done;
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
+		got_ref_list_free(&refs);
 		if (error)
 			goto done;
 	}
@@ -4895,8 +4924,15 @@ cmd_tree(int argc, char *argv[])
 		if (error != NULL)
 			goto done;
 	} else {
+		struct got_reflist_head refs;
+		SIMPLEQ_INIT(&refs);
+		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
+		    NULL);
+		if (error)
+			goto done;
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
+		got_ref_list_free(&refs);
 		if (error)
 			goto done;
 	}
@@ -5609,12 +5645,19 @@ cmd_branch(int argc, char *argv[])
 	else if (delref)
 		error = delete_branch(repo, worktree, delref);
 	else {
+		struct got_reflist_head refs;
+		SIMPLEQ_INIT(&refs);
+		error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
+		    NULL);
+		if (error)
+			goto done;
 		if (commit_id_arg == NULL)
 			commit_id_arg = worktree ?
 			    got_worktree_get_head_ref_name(worktree) :
 			    GOT_REF_HEAD;
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
+		got_ref_list_free(&refs);
 		if (error)
 			goto done;
 		error = add_branch(repo, argv[0], commit_id);
@@ -5931,6 +5974,9 @@ add_tag(struct got_repository *repo, struct got_worktree *worktree,
 	char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
 	char *tagmsg_path = NULL, *tag_id_str = NULL;
 	int preserve_tagmsg = 0;
+	struct got_reflist_head refs;
+
+	SIMPLEQ_INIT(&refs);
 
 	/*
 	 * Don't let the user create a tag name with a leading '-'.
@@ -5944,8 +5990,12 @@ add_tag(struct got_repository *repo, struct got_worktree *worktree,
 	if (err)
 		return err;
 
+	err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
+	if (err)
+		goto done;
+
 	err = got_repo_match_object_id(&commit_id, &label, commit_arg,
-	    GOT_OBJ_TYPE_COMMIT, 1, repo);
+	    GOT_OBJ_TYPE_COMMIT, &refs, repo);
 	if (err)
 		goto done;
 
@@ -6027,6 +6077,7 @@ done:
 	free(tagmsg);
 	free(tagmsg_path);
 	free(tagger);
+	got_ref_list_free(&refs);
 	return err;
 }
 
@@ -9571,6 +9622,9 @@ cmd_cat(int argc, char *argv[])
 	const char *commit_id_str = NULL;
 	struct got_object_id *id = NULL, *commit_id = NULL;
 	int ch, obj_type, i, force_path = 0;
+	struct got_reflist_head refs;
+
+	SIMPLEQ_INIT(&refs);
 
 #ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
@@ -9636,10 +9690,14 @@ cmd_cat(int argc, char *argv[])
 	if (error)
 		goto done;
 
+	error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
+	if (error)
+		goto done;
+
 	if (commit_id_str == NULL)
 		commit_id_str = GOT_REF_HEAD;
 	error = got_repo_match_object_id(&commit_id, NULL,
-	    commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
+	    commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
 	if (error)
 		goto done;
 
@@ -9651,7 +9709,8 @@ cmd_cat(int argc, char *argv[])
 				break;
 		} else {
 			error = got_repo_match_object_id(&id, &label, argv[i],
-			    GOT_OBJ_TYPE_ANY, 0, repo);
+			    GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
+			    repo);
 			if (error) {
 				if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
 				    error->code != GOT_ERR_NOT_REF)
@@ -9703,6 +9762,7 @@ done:
 		if (error == NULL)
 			error = repo_error;
 	}
+	got_ref_list_free(&refs);
 	return error;
 }
 
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index fe98a92..53ce046 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -175,9 +175,12 @@ static const struct got_error	*gw_get_repo_owner(char **, struct gw_trans *,
 static const struct got_error	*gw_get_time_str(char **, time_t, int);
 static const struct got_error	*gw_get_repo_age(char **, struct gw_trans *,
 				    char *, const char *, int);
-static const struct got_error	*gw_output_file_blame(struct gw_trans *);
-static const struct got_error	*gw_output_blob_buf(struct gw_trans *);
-static const struct got_error	*gw_output_repo_tree(struct gw_trans *);
+static const struct got_error	*gw_output_file_blame(struct gw_trans *,
+				    struct gw_header *);
+static const struct got_error	*gw_output_blob_buf(struct gw_trans *,
+				    struct gw_header *);
+static const struct got_error	*gw_output_repo_tree(struct gw_trans *,
+				    struct gw_header *);
 static const struct got_error	*gw_output_diff(struct gw_trans *,
 				    struct gw_header *);
 static const struct got_error	*gw_output_repo_tags(struct gw_trans *,
@@ -399,7 +402,7 @@ gw_blame(struct gw_trans *gw_trans)
 	    "blame", KATTR__MAX);
 	if (kerr != KCGI_OK)
 		goto done;
-	error = gw_output_file_blame(gw_trans);
+	error = gw_output_file_blame(gw_trans, header);
 	if (error)
 		goto done;
 	kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
@@ -443,7 +446,7 @@ gw_blob(struct gw_trans *gw_trans)
 	if (error)
 		goto done;
 
-	error = gw_output_blob_buf(gw_trans);
+	error = gw_output_blob_buf(gw_trans, header);
 done:
 
 	if (error) {
@@ -1653,7 +1656,7 @@ gw_tree(struct gw_trans *gw_trans)
 	    "tree", KATTR__MAX);
 	if (kerr != KCGI_OK)
 		goto done;
-	error = gw_output_repo_tree(gw_trans);
+	error = gw_output_repo_tree(gw_trans, header);
 	if (error)
 		goto done;
 
@@ -2854,13 +2857,15 @@ gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
 	if (header->parent_id != NULL &&
 	    strncmp(header->parent_id, "/dev/null", 9) != 0) {
 		error = got_repo_match_object_id(&id1, &label1,
-			header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
+			header->parent_id, GOT_OBJ_TYPE_ANY,
+			&header->refs, gw_trans->repo);
 		if (error)
 			goto done;
 	}
 
 	error = got_repo_match_object_id(&id2, &label2,
-	    header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
+	    header->commit_id, GOT_OBJ_TYPE_ANY, &header->refs,
+	    gw_trans->repo);
 	if (error)
 		goto done;
 
@@ -3951,7 +3956,7 @@ done:
 }
 
 static const struct got_error *
-gw_output_file_blame(struct gw_trans *gw_trans)
+gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
 {
 	const struct got_error *error = NULL;
 	struct got_object_id *obj_id = NULL;
@@ -3975,7 +3980,7 @@ gw_output_file_blame(struct gw_trans *gw_trans)
 		goto done;
 
 	error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
-	    GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
+	    GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
 	if (error)
 		goto done;
 
@@ -4056,7 +4061,7 @@ done:
 }
 
 static const struct got_error *
-gw_output_blob_buf(struct gw_trans *gw_trans)
+gw_output_blob_buf(struct gw_trans *gw_trans, struct gw_header *header)
 {
 	const struct got_error *error = NULL;
 	struct got_object_id *obj_id = NULL;
@@ -4081,7 +4086,7 @@ gw_output_blob_buf(struct gw_trans *gw_trans)
 		goto done;
 
 	error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
-	    GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
+	    GOT_OBJ_TYPE_COMMIT, &header->refs, gw_trans->repo);
 	if (error)
 		goto done;
 
@@ -4148,7 +4153,7 @@ done:
 }
 
 static const struct got_error *
-gw_output_repo_tree(struct gw_trans *gw_trans)
+gw_output_repo_tree(struct gw_trans *gw_trans, struct gw_header *header)
 {
 	const struct got_error *error = NULL;
 	struct got_object_id *tree_id = NULL, *commit_id = NULL;
@@ -4198,7 +4203,7 @@ gw_output_repo_tree(struct gw_trans *gw_trans)
 
 	} else {
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
+		    gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, &header->refs,
 		    gw_trans->repo);
 		if (error)
 			goto done;
diff --git a/include/got_repository.h b/include/got_repository.h
index 58d890d..1b274cc 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -94,6 +94,7 @@ char *got_repo_get_path_gitconfig(struct got_repository *);
 char *got_repo_get_path_gotconfig(struct got_repository *);
 
 struct got_reference;
+struct got_reflist_head;
 
 /*
  * Obtain a reference, by name, from a repository.
@@ -119,22 +120,25 @@ const struct got_error *got_repo_match_object_id_prefix(struct got_object_id **,
 
 /*
  * Given an object ID string or reference name, attempt to find a corresponding
- * commit object. Tags can optionally be ignored during matching.
+ * commit object.
  * The object type may be restricted to commit, tree, blob, or tag.
+ * Tags will only be matched if a list of references is provided.
  * GOT_OBJ_TYPE_ANY will match any type of object.
  * A human-readable label can optionally be returned, which the caller should
  * dispose of with free(3).
  * Return GOT_ERR_NO_OBJ if no matching commit can be found.
  */
 const struct got_error *got_repo_match_object_id(struct got_object_id **,
-    char **, const char *, int, int, struct got_repository *);
+    char **, const char *, int, struct got_reflist_head *,
+    struct got_repository *);
 
 /*
- * Attempt to find a tag object with a given name and target object type.
+ * Search the provided list of references for a tag with a given name
+ * and target object type.
  * Return GOT_ERR_NO_OBJ if no matching tag can be found.
  */
 const struct got_error *got_repo_object_match_tag(struct got_tag_object **,
-    const char *, int, struct got_repository *);
+    const char *, int, struct got_reflist_head *, struct got_repository *);
 
 /* A callback function which is invoked when a path is imported. */
 typedef const struct got_error *(*got_repo_import_cb)(void *, const char *);
diff --git a/lib/repository.c b/lib/repository.c
index 7a2d299..bfbd39a 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1467,7 +1467,7 @@ done:
 
 const struct got_error *
 got_repo_match_object_id(struct got_object_id **id, char **label,
-    const char *id_str, int obj_type, int resolve_tags,
+    const char *id_str, int obj_type, struct got_reflist_head *refs,
     struct got_repository *repo)
 {
 	const struct got_error *err;
@@ -1478,9 +1478,9 @@ got_repo_match_object_id(struct got_object_id **id, char **label,
 	if (label)
 		*label = NULL;
 
-	if (resolve_tags) {
+	if (refs) {
 		err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
-		    repo);
+		    refs, repo);
 		if (err == NULL) {
 			*id = got_object_id_dup(
 			    got_object_tag_get_object_id(tag));
@@ -1529,26 +1529,22 @@ done:
 
 const struct got_error *
 got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
-    int obj_type, struct got_repository *repo)
+    int obj_type, struct got_reflist_head *refs, struct got_repository *repo)
 {
-	const struct got_error *err;
-	struct got_reflist_head refs;
+	const struct got_error *err = NULL;
 	struct got_reflist_entry *re;
 	struct got_object_id *tag_id;
 	int name_is_absolute = (strncmp(name, "refs/", 5) == 0);
 
-	SIMPLEQ_INIT(&refs);
 	*tag = NULL;
 
-	err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
-	if (err)
-		return err;
-
-	SIMPLEQ_FOREACH(re, &refs, entry) {
+	SIMPLEQ_FOREACH(re, refs, entry) {
 		const char *refname;
 		refname = got_ref_get_name(re->ref);
 		if (got_ref_is_symbolic(re->ref))
 			continue;
+		if (strncmp(refname, "refs/tags/", 10) != 0)
+			continue;
 		if (!name_is_absolute)
 			refname += strlen("refs/tags/");
 		if (strcmp(refname, name) != 0)
@@ -1567,7 +1563,6 @@ got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
 		*tag = NULL;
 	}
 
-	got_ref_list_free(&refs);
 	if (err == NULL && *tag == NULL)
 		err = got_error_path(name, GOT_ERR_NO_OBJ);
 	return err;
diff --git a/tog/tog.c b/tog/tog.c
index ee2324c..c02189d 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2537,7 +2537,7 @@ input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
 			struct got_object_id *start_id;
 			err = got_repo_match_object_id(&start_id, NULL,
 			    s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
-			    GOT_OBJ_TYPE_COMMIT, 1, s->repo);
+			    GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
 			if (err)
 				return err;
 			free(s->start_id);
@@ -2763,7 +2763,7 @@ cmd_log(int argc, char *argv[])
 	if (start_commit == NULL) {
 		error = got_repo_match_object_id(&start_id, &label,
 		    worktree ? got_worktree_get_head_ref_name(worktree) :
-		    GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
 		if (error)
 			goto done;
 		head_ref_name = label;
@@ -2774,7 +2774,7 @@ cmd_log(int argc, char *argv[])
 		else if (error->code != GOT_ERR_NOT_REF)
 			goto done;
 		error = got_repo_match_object_id(&start_id, NULL,
-		    start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
 		if (error)
 			goto done;
 	}
@@ -3833,12 +3833,12 @@ cmd_diff(int argc, char *argv[])
 		goto done;
 
 	error = got_repo_match_object_id(&id1, &label1, id_str1,
-	    GOT_OBJ_TYPE_ANY, 1, repo);
+	    GOT_OBJ_TYPE_ANY, &tog_refs, repo);
 	if (error)
 		goto done;
 
 	error = got_repo_match_object_id(&id2, &label2, id_str2,
-	    GOT_OBJ_TYPE_ANY, 1, repo);
+	    GOT_OBJ_TYPE_ANY, &tog_refs, repo);
 	if (error)
 		goto done;
 
@@ -4740,7 +4740,7 @@ cmd_blame(int argc, char *argv[])
 		got_ref_close(head_ref);
 	} else {
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
 	}
 	if (error != NULL)
 		goto done;
@@ -5548,7 +5548,7 @@ cmd_tree(int argc, char *argv[])
 	if (commit_id_arg == NULL) {
 		error = got_repo_match_object_id(&commit_id, &label,
 		    worktree ? got_worktree_get_head_ref_name(worktree) :
-		    GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
 		if (error)
 			goto done;
 		head_ref_name = label;
@@ -5559,7 +5559,7 @@ cmd_tree(int argc, char *argv[])
 		else if (error->code != GOT_ERR_NOT_REF)
 			goto done;
 		error = got_repo_match_object_id(&commit_id, NULL,
-		    commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
+		    commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
 		if (error)
 			goto done;
 	}
@@ -6353,6 +6353,9 @@ tog_log_with_path(int argc, char *argv[])
 	struct got_object_id *commit_id = NULL, *id = NULL;
 	char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
 	char *commit_id_str = NULL, **cmd_argv = NULL;
+	struct got_reflist_head refs;
+
+	SIMPLEQ_INIT(&refs);
 
 	cwd = getcwd(NULL, 0);
 	if (cwd == NULL)
@@ -6380,9 +6383,12 @@ tog_log_with_path(int argc, char *argv[])
 	if (error)
 		goto done;
 
+	error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
+	if (error)
+		goto done;
 	error = got_repo_match_object_id(&commit_id, NULL, worktree ?
 	    got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
-	    GOT_OBJ_TYPE_COMMIT, 1, repo);
+	    GOT_OBJ_TYPE_COMMIT, &refs, repo);
 	if (error)
 		goto done;
 
@@ -6429,6 +6435,7 @@ done:
 			free(cmd_argv[i]);
 		free(cmd_argv);
 	}
+	got_ref_list_free(&refs);
 	return error;
 }