Skip to content

Commit 467185f

Browse files
authored
Merge pull request libgit2#4150 from libgit2/ethomson/freshen_trees
git_commit_create: freshen tree objects in commit
2 parents 3348570 + 52d03f3 commit 467185f

File tree

4 files changed

+90
-19
lines changed

4 files changed

+90
-19
lines changed

src/commit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ static int git_commit__create_internal(
159159
if (git_repository_odb__weakptr(&odb, repo) < 0)
160160
goto cleanup;
161161

162+
if (git_odb__freshen(odb, tree) < 0)
163+
goto cleanup;
164+
162165
if (git_odb_write(id, odb, buf.ptr, buf.size, GIT_OBJ_COMMIT) < 0)
163166
goto cleanup;
164167

src/odb.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ static int odb_freshen_1(
695695
return (int)found;
696696
}
697697

698-
static int odb_freshen(git_odb *db, const git_oid *id)
698+
int git_odb__freshen(git_odb *db, const git_oid *id)
699699
{
700700
assert(db && id);
701701

@@ -1167,7 +1167,7 @@ int git_odb_write(
11671167
assert(oid && db);
11681168

11691169
git_odb_hash(oid, data, len, type);
1170-
if (odb_freshen(db, oid))
1170+
if (git_odb__freshen(db, oid))
11711171
return 0;
11721172

11731173
for (i = 0; i < db->backends.length && error < 0; ++i) {
@@ -1293,7 +1293,7 @@ int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream)
12931293

12941294
git_hash_final(out, stream->hash_ctx);
12951295

1296-
if (odb_freshen(stream->backend->odb, out))
1296+
if (git_odb__freshen(stream->backend->odb, out))
12971297
return 0;
12981298

12991299
return stream->finalize_write(stream, out);

src/odb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ int git_odb__read_header_or_object(
9898
git_odb_object **out, size_t *len_p, git_otype *type_p,
9999
git_odb *db, const git_oid *id);
100100

101+
/* freshen an entry in the object database */
102+
int git_odb__freshen(git_odb *db, const git_oid *id);
103+
101104
/* fully free the object; internal method, DO NOT EXPORT */
102105
void git_odb_object__free(void *object);
103106

tests/odb/freshen.c

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,99 @@ void test_odb_freshen__cleanup(void)
1717
cl_git_sandbox_cleanup();
1818
}
1919

20-
#define LOOSE_STR "hey\n"
21-
#define LOOSE_ID "1385f264afb75a56a5bec74243be9b367ba4ca08"
22-
#define LOOSE_FN "13/85f264afb75a56a5bec74243be9b367ba4ca08"
20+
static void set_time_wayback(struct stat *out, const char *fn)
21+
{
22+
git_buf fullpath = GIT_BUF_INIT;
23+
struct p_timeval old[2];
24+
25+
old[0].tv_sec = 1234567890;
26+
old[0].tv_usec = 0;
27+
old[1].tv_sec = 1234567890;
28+
old[1].tv_usec = 0;
29+
30+
git_buf_joinpath(&fullpath, "testrepo.git/objects", fn);
31+
32+
cl_must_pass(p_utimes(git_buf_cstr(&fullpath), old));
33+
cl_must_pass(p_lstat(git_buf_cstr(&fullpath), out));
34+
git_buf_free(&fullpath);
35+
}
2336

24-
void test_odb_freshen__loose_object(void)
37+
#define LOOSE_STR "my new file\n"
38+
#define LOOSE_BLOB_ID "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"
39+
#define LOOSE_BLOB_FN "a7/1586c1dfe8a71c6cbf6c129f404c5642ff31bd"
40+
41+
void test_odb_freshen__loose_blob(void)
2542
{
2643
git_oid expected_id, id;
2744
struct stat before, after;
28-
struct p_timeval old_times[2];
2945

30-
cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_ID));
46+
cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_BLOB_ID));
47+
set_time_wayback(&before, LOOSE_BLOB_FN);
3148

32-
old_times[0].tv_sec = 1234567890;
33-
old_times[0].tv_usec = 0;
34-
old_times[1].tv_sec = 1234567890;
35-
old_times[1].tv_usec = 0;
49+
/* make sure we freshen a blob */
50+
cl_git_pass(git_blob_create_frombuffer(&id, repo, LOOSE_STR, CONST_STRLEN(LOOSE_STR)));
51+
cl_assert_equal_oid(&expected_id, &id);
52+
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_BLOB_FN, &after));
3653

37-
/* set time to way back */
38-
cl_must_pass(p_utimes("testrepo.git/objects/" LOOSE_FN, old_times));
39-
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &before));
54+
cl_assert(before.st_atime < after.st_atime);
55+
cl_assert(before.st_mtime < after.st_mtime);
56+
}
57+
58+
#define LOOSE_TREE_ID "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
59+
#define LOOSE_TREE_FN "94/4c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
60+
61+
void test_odb_freshen__loose_tree(void)
62+
{
63+
git_oid expected_id, id;
64+
git_tree *tree;
65+
struct stat before, after;
66+
67+
cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_TREE_ID));
68+
set_time_wayback(&before, LOOSE_TREE_FN);
69+
70+
cl_git_pass(git_tree_lookup(&tree, repo, &expected_id));
71+
cl_git_pass(git_tree_create_updated(&id, repo, tree, 0, NULL));
4072

41-
cl_git_pass(git_odb_write(&id, odb, LOOSE_STR, CONST_STRLEN(LOOSE_STR),
42-
GIT_OBJ_BLOB));
73+
/* make sure we freshen a tree */
4374
cl_assert_equal_oid(&expected_id, &id);
44-
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &after));
75+
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
76+
77+
cl_assert(before.st_atime < after.st_atime);
78+
cl_assert(before.st_mtime < after.st_mtime);
79+
80+
git_tree_free(tree);
81+
}
82+
83+
void test_odb_freshen__tree_during_commit(void)
84+
{
85+
git_oid tree_id, parent_id, commit_id;
86+
git_tree *tree;
87+
git_commit *parent;
88+
git_signature *signature;
89+
struct stat before, after;
90+
91+
cl_git_pass(git_oid_fromstr(&tree_id, LOOSE_TREE_ID));
92+
cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
93+
set_time_wayback(&before, LOOSE_TREE_FN);
4594

95+
cl_git_pass(git_oid_fromstr(&parent_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
96+
cl_git_pass(git_commit_lookup(&parent, repo, &parent_id));
97+
98+
cl_git_pass(git_signature_new(&signature,
99+
"Refresher", "refresher@example.com", 1488547083, 0));
100+
101+
cl_git_pass(git_commit_create(&commit_id, repo, NULL,
102+
signature, signature, NULL, "New commit pointing to old tree",
103+
tree, 1, (const git_commit **)&parent));
104+
105+
/* make sure we freshen the tree the commit points to */
106+
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
46107
cl_assert(before.st_atime < after.st_atime);
47108
cl_assert(before.st_mtime < after.st_mtime);
109+
110+
git_signature_free(signature);
111+
git_commit_free(parent);
112+
git_tree_free(tree);
48113
}
49114

50115
#define PACKED_STR "Testing a readme.txt\n"

0 commit comments

Comments
 (0)