Skip to content

Commit 81662d4

Browse files
committed
Support checking for object existence without refresh
Looking up a non-existent object currently always invokes `git_odb_refresh`. If looking up a large batch of objects, many of which may legitimately not exist, this will repeatedly refresh the ODB to no avail. Add a `git_odb_exists_ext` that accepts flags controlling the ODB lookup, and add a flag to suppress the refresh. This allows the user to control if and when they refresh (for instance, refreshing once before starting the batch).
1 parent 3993e9a commit 81662d4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

include/git2/odb.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@
2222
*/
2323
GIT_BEGIN_DECL
2424

25+
/** Flags controlling the behavior of ODB lookup operations */
26+
typedef enum {
27+
/**
28+
* Don't call `git_odb_refresh` if the lookup fails. Useful when doing
29+
* a batch of lookup operations for objects that may legitimately not
30+
* exist. When using this flag, you may wish to manually call
31+
* `git_odb_refresh` before processing a batch of objects.
32+
*/
33+
GIT_ODB_LOOKUP_NO_REFRESH = (1 << 0),
34+
} git_odb_lookup_flags_t;
35+
2536
/**
2637
* Function type for callbacks from git_odb_foreach.
2738
*/
@@ -155,6 +166,17 @@ GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_object_t *type_out, git
155166
*/
156167
GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
157168

169+
/**
170+
* Determine if the given object can be found in the object database, with
171+
* extended options.
172+
*
173+
* @param db database to be searched for the given object.
174+
* @param id the object to search for.
175+
* @param flags flags affecting the lookup (see `git_odb_lookup_flags_t`)
176+
* @return 1 if the object was found, 0 otherwise
177+
*/
178+
GIT_EXTERN(int) git_odb_exists_ext(git_odb *db, const git_oid *id, unsigned int flags);
179+
158180
/**
159181
* Determine if an object can be found in the object database by an
160182
* abbreviated object ID.

src/odb.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,11 @@ int git_odb__freshen(git_odb *db, const git_oid *id)
882882
}
883883

884884
int git_odb_exists(git_odb *db, const git_oid *id)
885+
{
886+
return git_odb_exists_ext(db, id, 0);
887+
}
888+
889+
int git_odb_exists_ext(git_odb *db, const git_oid *id, unsigned int flags)
885890
{
886891
git_odb_object *object;
887892

@@ -899,7 +904,7 @@ int git_odb_exists(git_odb *db, const git_oid *id)
899904
if (odb_exists_1(db, id, false))
900905
return 1;
901906

902-
if (!git_odb_refresh(db))
907+
if (!(flags & GIT_ODB_LOOKUP_NO_REFRESH) && !git_odb_refresh(db))
903908
return odb_exists_1(db, id, true);
904909

905910
/* Failed to refresh, hence not found */

0 commit comments

Comments
 (0)