Skip to content

Commit 437cc16

Browse files
committed
commit-graph: implement "--additive" option
Teach git-commit-graph to add all commits from the existing commit-graph file to the file about to be written. This should be used when adding new commits without performing garbage collection. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 61c080c commit 437cc16

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

Documentation/git-commit-graph.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ With the `--stdin-commits` option, generate the new commit graph by
4343
walking commits starting at the commits specified in stdin as a list
4444
of OIDs in hex, one OID per line. (Cannot be combined with
4545
--stdin-packs.)
46+
+
47+
With the `--append` option, include all commits that are present in the
48+
existing commit-graph file.
4649

4750
'read'::
4851

@@ -72,6 +75,13 @@ $ echo <pack-index> | git commit-graph write --stdin-packs
7275
$ git show-ref -s | git commit-graph write --stdin-commits
7376
------------------------------------------------
7477

78+
* Write a graph file containing all commits in the current
79+
* commit-graph file along with those reachable from HEAD.
80+
+
81+
------------------------------------------------
82+
$ git rev-parse HEAD | git commit-graph write --stdin-commits --append
83+
------------------------------------------------
84+
7585
* Read basic information from the commit-graph file.
7686
+
7787
------------------------------------------------

builtin/commit-graph.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
static char const * const builtin_commit_graph_usage[] = {
99
N_("git commit-graph [--object-dir <objdir>]"),
1010
N_("git commit-graph read [--object-dir <objdir>]"),
11-
N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs|--stdin-commits]"),
11+
N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
1212
NULL
1313
};
1414

@@ -18,14 +18,15 @@ static const char * const builtin_commit_graph_read_usage[] = {
1818
};
1919

2020
static const char * const builtin_commit_graph_write_usage[] = {
21-
N_("git commit-graph write [--object-dir <objdir>] [--stdin-packs|--stdin-commits]"),
21+
N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
2222
NULL
2323
};
2424

2525
static struct opts_commit_graph {
2626
const char *obj_dir;
2727
int stdin_packs;
2828
int stdin_commits;
29+
int append;
2930
} opts;
3031

3132
static int graph_read(int argc, const char **argv)
@@ -94,6 +95,8 @@ static int graph_write(int argc, const char **argv)
9495
N_("scan pack-indexes listed by stdin for commits")),
9596
OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
9697
N_("start walk at commits listed by stdin")),
98+
OPT_BOOL(0, "append", &opts.append,
99+
N_("include all commits already in the commit-graph file")),
97100
OPT_END(),
98101
};
99102

@@ -131,7 +134,8 @@ static int graph_write(int argc, const char **argv)
131134
pack_indexes,
132135
packs_nr,
133136
commit_hex,
134-
commits_nr);
137+
commits_nr,
138+
opts.append);
135139

136140
return 0;
137141
}

commit-graph.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ void write_commit_graph(const char *obj_dir,
534534
const char **pack_indexes,
535535
int nr_packs,
536536
const char **commit_hex,
537-
int nr_commits)
537+
int nr_commits,
538+
int append)
538539
{
539540
struct packed_oid_list oids;
540541
struct packed_commit_list commits;
@@ -552,10 +553,24 @@ void write_commit_graph(const char *obj_dir,
552553
oids.nr = 0;
553554
oids.alloc = approximate_object_count() / 4;
554555

556+
if (append) {
557+
prepare_commit_graph_one(obj_dir);
558+
if (commit_graph)
559+
oids.alloc += commit_graph->num_commits;
560+
}
561+
555562
if (oids.alloc < 1024)
556563
oids.alloc = 1024;
557564
ALLOC_ARRAY(oids.list, oids.alloc);
558565

566+
if (append && commit_graph) {
567+
for (i = 0; i < commit_graph->num_commits; i++) {
568+
const unsigned char *hash = commit_graph->chunk_oid_lookup +
569+
commit_graph->hash_len * i;
570+
hashcpy(oids.list[oids.nr++].hash, hash);
571+
}
572+
}
573+
559574
if (pack_indexes) {
560575
struct strbuf packname = STRBUF_INIT;
561576
int dirlen;

commit-graph.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void write_commit_graph(const char *obj_dir,
4040
const char **pack_indexes,
4141
int nr_packs,
4242
const char **commit_hex,
43-
int nr_commits);
43+
int nr_commits,
44+
int append);
4445

4546
#endif
4647

t/t5318-commit-graph.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ test_expect_success 'build graph from commits with closure' '
190190
graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
191191
graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
192192

193+
test_expect_success 'build graph from commits appendly' '
194+
cd "$TRASH_DIRECTORY/full" &&
195+
git rev-parse merge/3 | git commit-graph write --stdin-commits --append &&
196+
test_path_is_file $objdir/info/commit-graph &&
197+
graph_read_expect "10" "large_edges"
198+
'
199+
200+
graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
201+
graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
202+
193203
test_expect_success 'setup bare repo' '
194204
cd "$TRASH_DIRECTORY" &&
195205
git clone --bare --no-local full bare &&

0 commit comments

Comments
 (0)