Skip to content

Commit 16110c9

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: normalize commit-graph filenames
When writing commit-graph files, we append path data to an object directory, which may be specified by the user via the '--object-dir' option. If the user supplies a trailing slash, or some other alternative path format, the resulting path may be usable for writing to the correct location. However, when expiring graph files from the <obj-dir>/info/commit-graphs directory during a write, we need to compare paths with exact string matches. Normalize the commit-graph filenames to avoid ambiguity. This creates extra allocations, but this is a constant multiple of the number of commit-graph files, which should be a number in the single digits. Further normalize the object directory in the context. Due to a comparison between g->obj_dir and ctx->obj_dir in split_graph_merge_strategy(), a trailing slash would prevent any merging of layers within the same object directory. The check is there to ensure we do not merge across alternates. Update the tests to include a case with this trailing slash problem. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a09c130 commit 16110c9

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

commit-graph.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@
4343

4444
char *get_commit_graph_filename(const char *obj_dir)
4545
{
46-
return xstrfmt("%s/info/commit-graph", obj_dir);
46+
char *filename = xstrfmt("%s/info/commit-graph", obj_dir);
47+
char *normalized = xmalloc(strlen(filename) + 1);
48+
normalize_path_copy(normalized, filename);
49+
free(filename);
50+
return normalized;
4751
}
4852

4953
static char *get_split_graph_filename(const char *obj_dir,
5054
const char *oid_hex)
5155
{
52-
return xstrfmt("%s/info/commit-graphs/graph-%s.graph",
53-
obj_dir,
54-
oid_hex);
56+
char *filename = xstrfmt("%s/info/commit-graphs/graph-%s.graph",
57+
obj_dir,
58+
oid_hex);
59+
char *normalized = xmalloc(strlen(filename) + 1);
60+
normalize_path_copy(normalized, filename);
61+
free(filename);
62+
return normalized;
5563
}
5664

5765
static char *get_chain_filename(const char *obj_dir)
@@ -746,7 +754,7 @@ struct packed_oid_list {
746754

747755
struct write_commit_graph_context {
748756
struct repository *r;
749-
const char *obj_dir;
757+
char *obj_dir;
750758
char *graph_name;
751759
struct packed_oid_list oids;
752760
struct packed_commit_list commits;
@@ -1729,7 +1737,6 @@ static void expire_commit_graphs(struct write_commit_graph_context *ctx)
17291737

17301738
if (!found)
17311739
unlink(path.buf);
1732-
17331740
}
17341741
}
17351742

@@ -1741,14 +1748,22 @@ int write_commit_graph(const char *obj_dir,
17411748
{
17421749
struct write_commit_graph_context *ctx;
17431750
uint32_t i, count_distinct = 0;
1751+
size_t len;
17441752
int res = 0;
17451753

17461754
if (!commit_graph_compatible(the_repository))
17471755
return 0;
17481756

17491757
ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
17501758
ctx->r = the_repository;
1751-
ctx->obj_dir = obj_dir;
1759+
1760+
/* normalize object dir with no trailing slash */
1761+
ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
1762+
normalize_path_copy(ctx->obj_dir, obj_dir);
1763+
len = strlen(ctx->obj_dir);
1764+
if (len && ctx->obj_dir[len - 1] == '/')
1765+
ctx->obj_dir[len - 1] = 0;
1766+
17521767
ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
17531768
ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
17541769
ctx->split = flags & COMMIT_GRAPH_SPLIT ? 1 : 0;
@@ -1856,6 +1871,7 @@ int write_commit_graph(const char *obj_dir,
18561871
free(ctx->graph_name);
18571872
free(ctx->commits.list);
18581873
free(ctx->oids.list);
1874+
free(ctx->obj_dir);
18591875

18601876
if (ctx->commit_graph_filenames_after) {
18611877
for (i = 0; i < ctx->num_commit_graphs_after; i++) {

t/t5324-split-commit-graph.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ test_expect_success 'create fork and chain across alternate' '
163163
test_line_count = 1 graph-files &&
164164
git -c core.commitGraph=true rev-list HEAD >expect &&
165165
git -c core.commitGraph=false rev-list HEAD >actual &&
166-
test_cmp expect actual
166+
test_cmp expect actual &&
167+
test_commit 14 &&
168+
git commit-graph write --reachable --split --object-dir=.git/objects/ &&
169+
test_line_count = 3 $graphdir/commit-graph-chain &&
170+
ls $graphdir/graph-*.graph >graph-files &&
171+
test_line_count = 1 graph-files
167172
)
168173
'
169174

0 commit comments

Comments
 (0)