Skip to content

Commit cd46052

Browse files
tonydelanuezethomson
authored andcommitted
odb: Implement option for overriding of default odb backend priority
Introduce GIT_OPT_SET_ODB_LOOSE_PRIORITY and GIT_OPT_SET_ODB_PACKED_PRIORITY to allow overriding the default priority values for the default ODB backends. Libgit2 has historically assumed that most objects for long- running operations will be packed, therefore GIT_LOOSE_PRIORITY is set to 1 by default, and GIT_PACKED_PRIORITY to 2. When a client allows libgit2 to set the default backends, they can specify an override for the two priority values in order to change the order in which each ODB backend is accessed.
1 parent cc68c19 commit cd46052

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

include/git2/common.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ typedef enum {
207207
GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS,
208208
GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE,
209209
GIT_OPT_GET_MWINDOW_FILE_LIMIT,
210-
GIT_OPT_SET_MWINDOW_FILE_LIMIT
210+
GIT_OPT_SET_MWINDOW_FILE_LIMIT,
211+
GIT_OPT_SET_ODB_PACKED_PRIORITY,
212+
GIT_OPT_SET_ODB_LOOSE_PRIORITY
211213
} git_libgit2_opt_t;
212214

213215
/**
@@ -421,6 +423,14 @@ typedef enum {
421423
* > authentication, use expect/continue when POSTing data.
422424
* > This option is not available on Windows.
423425
*
426+
* opts(GIT_OPT_SET_ODB_PACKED_PRIORITY, int priority)
427+
* > Override the default priority of the packed ODB backend which
428+
* > is added when default backends are assigned to a repository
429+
*
430+
* opts(GIT_OPT_SET_ODB_LOOSE_PRIORITY, int priority)
431+
* > Override the default priority of the loose ODB backend which
432+
* > is added when default backends are assigned to a repository
433+
*
424434
* @param option Option key
425435
* @param ... value to set the option
426436
* @return 0 on success, <0 on failure

src/libgit2.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ extern size_t git_mwindow__mapped_limit;
5050
extern size_t git_mwindow__file_limit;
5151
extern size_t git_indexer__max_objects;
5252
extern bool git_disable_pack_keep_file_checks;
53+
extern int git_odb__packed_priority;
54+
extern int git_odb__loose_priority;
5355

5456
char *git__user_agent;
5557
char *git__ssl_ciphers;
@@ -368,6 +370,14 @@ int git_libgit2_opts(int key, ...)
368370
git_http__expect_continue = (va_arg(ap, int) != 0);
369371
break;
370372

373+
case GIT_OPT_SET_ODB_PACKED_PRIORITY:
374+
git_odb__packed_priority = va_arg(ap, int);
375+
break;
376+
377+
case GIT_OPT_SET_ODB_LOOSE_PRIORITY:
378+
git_odb__loose_priority = va_arg(ap, int);
379+
break;
380+
371381
default:
372382
git_error_set(GIT_ERROR_INVALID, "invalid option key");
373383
error = -1;

src/odb.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
#define GIT_ALTERNATES_FILE "info/alternates"
2525

26+
#define GIT_ALTERNATES_MAX_DEPTH 5
27+
2628
/*
2729
* We work under the assumption that most objects for long-running
2830
* operations will be packed
2931
*/
30-
#define GIT_LOOSE_PRIORITY 1
31-
#define GIT_PACKED_PRIORITY 2
32-
33-
#define GIT_ALTERNATES_MAX_DEPTH 5
32+
int git_odb__loose_priority = 1;
33+
int git_odb__packed_priority = 2;
3434

3535
bool git_odb__strict_hash_verification = true;
3636

@@ -613,12 +613,12 @@ int git_odb__add_default_backends(
613613

614614
/* add the loose object backend */
615615
if (git_odb_backend_loose(&loose, objects_dir, -1, db->do_fsync, 0, 0) < 0 ||
616-
add_backend_internal(db, loose, GIT_LOOSE_PRIORITY, as_alternates, inode) < 0)
616+
add_backend_internal(db, loose, git_odb__loose_priority, as_alternates, inode) < 0)
617617
return -1;
618618

619619
/* add the packed file backend */
620620
if (git_odb_backend_pack(&packed, objects_dir) < 0 ||
621-
add_backend_internal(db, packed, GIT_PACKED_PRIORITY, as_alternates, inode) < 0)
621+
add_backend_internal(db, packed, git_odb__packed_priority, as_alternates, inode) < 0)
622622
return -1;
623623

624624
if (git_mutex_lock(&db->lock) < 0) {

tests/odb/sorting.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,26 @@ void test_odb_sorting__alternate_backends_sorting(void)
6868

6969
check_backend_sorting(_odb);
7070
}
71+
72+
void test_odb_sorting__override_default_backend_priority(void)
73+
{
74+
git_odb *new_odb;
75+
git_odb_backend *loose, *packed, *backend;
76+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ODB_LOOSE_PRIORITY, 5));
77+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ODB_PACKED_PRIORITY, 3));
78+
git_odb_backend_pack(&packed, "./testrepo.git/objects");
79+
git_odb_backend_loose(&loose, "./testrepo.git/objects", -1, 0, 0, 0);
80+
81+
cl_git_pass(git_odb_open(&new_odb, cl_fixture("testrepo.git/objects")));
82+
cl_assert_equal_sz(2, git_odb_num_backends(new_odb));
83+
84+
cl_git_pass(git_odb_get_backend(&backend, new_odb, 0));
85+
cl_assert_equal_p(loose->read, backend->read);
86+
87+
cl_git_pass(git_odb_get_backend(&backend, new_odb, 1));
88+
cl_assert_equal_p(packed->read, backend->read);
89+
90+
git_odb_free(new_odb);
91+
loose->free(loose);
92+
packed->free(packed);
93+
}

0 commit comments

Comments
 (0)