Volume XXII, number 280Wednesday, October 7, 2026Latest message 55 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patchcommit-graph: fix writing generations with dates exceeding 34 bits

8 messages between Mar 17, 2026 and Mar 26, 2026, from Patrick Steinhardt, Junio C Hamano, Derrick Stolee, Karthik Nayak.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

Patrick SteinhardtMar 17, 2026, 19:02 UTC on lore

The `timestamp_t` type is declared as `uintmax_t` and thus typically has 64 bits of precision. Usually, the full precision of such dates is not required: it would be comforting to know that Git is still around in millions of years, but all in all the chance is rather low.

We abuse this fact in the commit-graph: instead of storing the full 64
bits of precision, committer dates only store 34 bits. This is still
plenty of headroom, as it means that we can represent dates until year
2514. Commits which are dated beyond that year will simply get a date
whose remaining bits are masked.

The result of this is somewhat curious: the committer date will be different depending on whether a commit gets parsed via the commit-graph or via the object database. This isn't really too much of an issue in general though, as we don't typically use the date parsed from the commit-graph in user-facing output.

But with 024b4c9697 (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16) it started to become a problem when writing the commit-graph itself. This commit changed `repo_parse_commit_no_graph()` so that we re-parse the commit via the object database in case it was already parsed beforehand via the commit-graph.

The consequence is that we may now act with two different commit dates at different stages:

  - Initially, we use the 34-bit precision timestamp when writing the
    chunk generation data. We thus correctly compute the offsets
    relative to the on-disk timestamp here.
  - Later, when writing the overflow data, we may end up with the
    full-precision timestamp. When the date is larger than 34 bits the
    result of this is an underflow when computing the offset.

This causes a mismatch in the number of generation data overflow records we want to write, and that ultimately causes Git to die.

Introduce a new helper function that computes the generation offset for a commit while correctly masking the date to 34 bits. This makes the previously-implicit assumptions about the commit date precision explicit and thus hopefully less fragile going forward.

Adapt sites that compute the offset to use the function.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
Hi,

this fixes a regression recently introduced by myself in 024b4c9697 (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16). The regression was found by GitLab's tests suite, see [1].

Thanks!
Patrick
[1]: https://gitlab.com/gitlab-org/gitlab/-/jobs/13522328632
---
 commit-graph.c          | 31 ++++++++++++++++++++++++++++---
 t/t5318-commit-graph.sh | 20 ++++++++++++++++++++
 2 files changed, 48 insertions(+), 3 deletions(-)
Show changes to 2 files +48 −3

commit-graph.c, t/t5318-commit-graph.sh

diff --git a/commit-graph.c b/commit-graph.c
index f8e24145a5..7e293a1775 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1319,6 +1319,31 @@ static int write_graph_chunk_data(struct hashfile *f,
 	return 0;
 }
 
+/*
+ * Compute the generation offset between the commit date and its generation.
+ * This is what's ultimately stored as generation number in the commit graph.
+ *
+ * Note that the computation of the commit date is more involved than you might
+ * think. Instead of using the full commit date, we're in fact masking bits so
+ * that only the 34 lowest bits are considered. This results from the fact that
+ * commit graphs themselves only ever store 34 bits of the commit date
+ * themselves.
+ *
+ * This means that if we have a commit date that exceeds 34 bits we'll end up
+ * in situations where depending on whether the commit has been parsed from the
+ * object database or the commit graph we'll have different dates, where the
+ * ones parsed from the object database would have full 64 bit precision.
+ *
+ * But ultimately, we only ever want the offset to be relative to what we
+ * actually end up storing on disk, and hence we have to mask all the other
+ * bits.
+ */
+static timestamp_t compute_generation_offset(struct commit *c)
+{
+	timestamp_t masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
+	return commit_graph_data_at(c)->generation - masked_date;
+}
+
 static int write_graph_chunk_generation_data(struct hashfile *f,
 					     void *data)
 {
@@ -1329,7 +1354,7 @@ static int write_graph_chunk_generation_data(struct hashfile *f,
 		struct commit *c = ctx->commits.items[i];
 		timestamp_t offset;
 		repo_parse_commit(ctx->r, c);
-		offset = commit_graph_data_at(c)->generation - c->date;
+		offset = compute_generation_offset(c);
 		display_progress(ctx->progress, ++ctx->progress_cnt);
 
 		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
@@ -1350,7 +1375,7 @@ static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
 	int i;
 	for (i = 0; i < ctx->commits.nr; i++) {
 		struct commit *c = ctx->commits.items[i];
-		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
+		timestamp_t offset = compute_generation_offset(c);
 		display_progress(ctx->progress, ++ctx->progress_cnt);
 
 		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
@@ -1741,7 +1766,7 @@ static void compute_generation_numbers(struct write_commit_graph_context *ctx)
 
 	for (i = 0; i < ctx->commits.nr; i++) {
 		struct commit *c = ctx->commits.items[i];
-		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
+		timestamp_t offset = compute_generation_offset(c);
 		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
 			ctx->num_generation_data_overflows++;
 	}
diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
index 98c6910963..1c40f904f8 100755
--- a/t/t5318-commit-graph.sh
+++ b/t/t5318-commit-graph.sh
@@ -417,6 +417,26 @@ test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow ch
 	test_cmp full/.git/objects/info/commit-graph commit-graph-upgraded
 '
 
+test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'overflow chunk when replacing commit-graph' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		cat >commit <<-EOF &&
+		tree $(test_oid empty_tree)
+		author Example <committer@example.com> 9223372036854775 +0000
+		committer Example <committer@example.com> 9223372036854775 +0000
+
+		Weird commit date
+		EOF
+		commit_id=$(git hash-object -t commit -w commit) &&
+		git reset --hard "$commit_id" &&
+		git commit-graph write --reachable &&
+		git commit-graph write --reachable --split=replace &&
+		git log
+	)
+'
+
 # the verify tests below expect the commit-graph to contain
 # exactly the commits reachable from the commits/8 branch.
 # If the file changes the set of commits in the list, then the

---
base-commit: ca1db8a0f7dc0dbea892e99f5b37c5fe5861be71
change-id: 20260317-pks-commit-graph-overflow-09c3fb6e259a
Junio C HamanoMar 17, 2026, 19:39 UTC in reply to Patrick Steinhardt on lore

Re: [PATCH] commit-graph: fix writing generations with dates exceeding 34 bits

Patrick Steinhardt <ps@pks.im> writes:
> this fixes a regression recently introduced by myself in 024b4c9697
> (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16).
> The regression was found by GitLab's tests suite, see [1].

Curious. GitLab's test suite runs pretending that it is way past year 2600 or something?

Show 30 quoted lines
> [1]: https://gitlab.com/gitlab-org/gitlab/-/jobs/13522328632
> ---
>  commit-graph.c          | 31 ++++++++++++++++++++++++++++---
>  t/t5318-commit-graph.sh | 20 ++++++++++++++++++++
>  2 files changed, 48 insertions(+), 3 deletions(-)
>
> diff --git a/commit-graph.c b/commit-graph.c
> index f8e24145a5..7e293a1775 100644
> --- a/commit-graph.c
> +++ b/commit-graph.c
> @@ -1319,6 +1319,31 @@ static int write_graph_chunk_data(struct hashfile *f,
>  	return 0;
>  }
>  
> +/*
> + * Compute the generation offset between the commit date and its generation.
> + * This is what's ultimately stored as generation number in the commit graph.
> ...
> + * actually end up storing on disk, and hence we have to mask all the other
> + * bits.
> + */
> +static timestamp_t compute_generation_offset(struct commit *c)
> +{
> +	timestamp_t masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
> +	return commit_graph_data_at(c)->generation - masked_date;
> +}
> +
>  static int write_graph_chunk_generation_data(struct hashfile *f,
>  					     void *data)
>  {

The code and explanation are both in line with why we want to do this change in the proposed log message.

Show 9 quoted lines
> +test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'overflow chunk when replacing commit-graph' '
> +	test_when_finished "rm -rf repo" &&
> +	git init repo &&
> +	(
> +		cd repo &&
> +		cat >commit <<-EOF &&
> +		tree $(test_oid empty_tree)
> +		author Example <committer@example.com> 9223372036854775 +0000
> +		committer Example <committer@example.com> 9223372036854775 +0000
This timestamp is way longer than 34-bit for sure.
100000110001001001101110100101111000110101001111110111 (base 2)
Show 14 quoted lines
> +
> +		Weird commit date
> +		EOF
> +		commit_id=$(git hash-object -t commit -w commit) &&
> +		git reset --hard "$commit_id" &&
> +		git commit-graph write --reachable &&
> +		git commit-graph write --reachable --split=replace &&
> +		git log
> +	)
> +'
> +
>  # the verify tests below expect the commit-graph to contain
>  # exactly the commits reachable from the commits/8 branch.
>  # If the file changes the set of commits in the list, then the
Will queue.  Thanks.
Patrick SteinhardtMar 19, 2026, 06:56 UTC in reply to Junio C Hamano on lore

Re: [PATCH] commit-graph: fix writing generations with dates exceeding 34 bits

On Tue, Mar 17, 2026 at 12:39:39PM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > this fixes a regression recently introduced by myself in 024b4c9697
> > (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16).
> > The regression was found by GitLab's tests suite, see [1].
> 
> Curious.  GitLab's test suite runs pretending that it is way past
> year 2600 or something?  

No, but one of the test repositories that we're running tests with [1] contains such a commit.

Patrick
[1]: https://gitlab.com/gitlab-org/gitlab-test
Derrick StoleeMar 20, 2026, 17:21 UTC in reply to Patrick Steinhardt on lore

Re: [PATCH] commit-graph: fix writing generations with dates exceeding 34 bits

On 3/19/2026 2:56 AM, Patrick Steinhardt wrote:
Show 16 quoted lines
> On Tue, Mar 17, 2026 at 12:39:39PM -0700, Junio C Hamano wrote:
>> Patrick Steinhardt <ps@pks.im> writes:
>>
>>> this fixes a regression recently introduced by myself in 024b4c9697
>>> (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16).
>>> The regression was found by GitLab's tests suite, see [1].
>>
>> Curious.  GitLab's test suite runs pretending that it is way past
>> year 2600 or something?  
> 
> No, but one of the test repositories that we're running tests with [1]
> contains such a commit.
> 
> Patrick
> 
> [1]: https://gitlab.com/gitlab-org/gitlab-test
 
I'm glad you have this test! Thanks for bringing such a data shape
into the Git test suite with this change so we can correct problems
this way in our inner loop testing.
I agree with Junio that the code looks good.

Thanks, -Stolee

Patrick SteinhardtMar 24, 2026, 06:18 UTC in reply to Patrick Steinhardt on lore

[PATCH v2] commit-graph: fix writing generations with dates exceeding 34 bits

The `timestamp_t` type is declared as `uintmax_t` and thus typically has 64 bits of precision. Usually, the full precision of such dates is not required: it would be comforting to know that Git is still around in millions of years, but all in all the chance is rather low.

We abuse this fact in the commit-graph: instead of storing the full 64
bits of precision, committer dates only store 34 bits. This is still
plenty of headroom, as it means that we can represent dates until year
2514. Commits which are dated beyond that year will simply get a date
whose remaining bits are masked.

The result of this is somewhat curious: the committer date will be different depending on whether a commit gets parsed via the commit-graph or via the object database. This isn't really too much of an issue in general though, as we don't typically use the date parsed from the commit-graph in user-facing output.

But with 024b4c9697 (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16) it started to become a problem when writing the commit-graph itself. This commit changed `repo_parse_commit_no_graph()` so that we re-parse the commit via the object database in case it was already parsed beforehand via the commit-graph.

The consequence is that we may now act with two different commit dates at different stages:

  - Initially, we use the 34-bit precision timestamp when writing the
    chunk generation data. We thus correctly compute the offsets
    relative to the on-disk timestamp here.
  - Later, when writing the overflow data, we may end up with the
    full-precision timestamp. When the date is larger than 34 bits the
    result of this is an underflow when computing the offset.

This causes a mismatch in the number of generation data overflow records we want to write, and that ultimately causes Git to die.

Introduce a new helper function that computes the generation offset for a commit while correctly masking the date to 34 bits. This makes the previously-implicit assumptions about the commit date precision explicit and thus hopefully less fragile going forward.

Adapt sites that compute the offset to use the function.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
Hi,

this fixes a regression recently introduced by myself in 024b4c9697 (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16). The regression was found by GitLab's tests suite, see [1].

Changes in v2:
  - Account for platforms where `timestamp_t` has 32 bit precision. This
    matches logic in `write_graph_chunk_data()`, where we also depend on
    the size of the commit timestamps.
  - Link to v1: https://lore.kernel.org/r/20260317-pks-commit-graph-overflow-v1-1-e6bee22cd826@pks.im
Thanks!
Patrick
[1]: https://gitlab.com/gitlab-org/gitlab/-/jobs/13522328632
---
 commit-graph.c          | 37 ++++++++++++++++++++++++++++++++++---
 t/t5318-commit-graph.sh | 20 ++++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)
Show changes to 2 files +54 −3

commit-graph.c, t/t5318-commit-graph.sh

diff --git a/commit-graph.c b/commit-graph.c
index f8e24145a5..cb514bfb60 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1319,6 +1319,37 @@ static int write_graph_chunk_data(struct hashfile *f,
 	return 0;
 }
 
+/*
+ * Compute the generation offset between the commit date and its generation.
+ * This is what's ultimately stored as generation number in the commit graph.
+ *
+ * Note that the computation of the commit date is more involved than you might
+ * think. Instead of using the full commit date, we're in fact masking bits so
+ * that only the 34 lowest bits are considered. This results from the fact that
+ * commit graphs themselves only ever store 34 bits of the commit date
+ * themselves.
+ *
+ * This means that if we have a commit date that exceeds 34 bits we'll end up
+ * in situations where depending on whether the commit has been parsed from the
+ * object database or the commit graph we'll have different dates, where the
+ * ones parsed from the object database would have full 64 bit precision.
+ *
+ * But ultimately, we only ever want the offset to be relative to what we
+ * actually end up storing on disk, and hence we have to mask all the other
+ * bits.
+ */
+static timestamp_t compute_generation_offset(struct commit *c)
+{
+	timestamp_t masked_date;
+
+	if (sizeof(timestamp_t) > 4)
+		masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
+	else
+		masked_date = c->date;
+
+	return commit_graph_data_at(c)->generation - masked_date;
+}
+
 static int write_graph_chunk_generation_data(struct hashfile *f,
 					     void *data)
 {
@@ -1329,7 +1360,7 @@ static int write_graph_chunk_generation_data(struct hashfile *f,
 		struct commit *c = ctx->commits.items[i];
 		timestamp_t offset;
 		repo_parse_commit(ctx->r, c);
-		offset = commit_graph_data_at(c)->generation - c->date;
+		offset = compute_generation_offset(c);
 		display_progress(ctx->progress, ++ctx->progress_cnt);
 
 		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
@@ -1350,7 +1381,7 @@ static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
 	int i;
 	for (i = 0; i < ctx->commits.nr; i++) {
 		struct commit *c = ctx->commits.items[i];
-		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
+		timestamp_t offset = compute_generation_offset(c);
 		display_progress(ctx->progress, ++ctx->progress_cnt);
 
 		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
@@ -1741,7 +1772,7 @@ static void compute_generation_numbers(struct write_commit_graph_context *ctx)
 
 	for (i = 0; i < ctx->commits.nr; i++) {
 		struct commit *c = ctx->commits.items[i];
-		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
+		timestamp_t offset = compute_generation_offset(c);
 		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
 			ctx->num_generation_data_overflows++;
 	}
diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
index 98c6910963..1c40f904f8 100755
--- a/t/t5318-commit-graph.sh
+++ b/t/t5318-commit-graph.sh
@@ -417,6 +417,26 @@ test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow ch
 	test_cmp full/.git/objects/info/commit-graph commit-graph-upgraded
 '
 
+test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'overflow chunk when replacing commit-graph' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		cat >commit <<-EOF &&
+		tree $(test_oid empty_tree)
+		author Example <committer@example.com> 9223372036854775 +0000
+		committer Example <committer@example.com> 9223372036854775 +0000
+
+		Weird commit date
+		EOF
+		commit_id=$(git hash-object -t commit -w commit) &&
+		git reset --hard "$commit_id" &&
+		git commit-graph write --reachable &&
+		git commit-graph write --reachable --split=replace &&
+		git log
+	)
+'
+
 # the verify tests below expect the commit-graph to contain
 # exactly the commits reachable from the commits/8 branch.
 # If the file changes the set of commits in the list, then the

---
base-commit: ca1db8a0f7dc0dbea892e99f5b37c5fe5861be71
change-id: 20260317-pks-commit-graph-overflow-09c3fb6e259a
Junio C HamanoMar 24, 2026, 15:42 UTC in reply to Patrick Steinhardt on lore

Re: [PATCH v2] commit-graph: fix writing generations with dates exceeding 34 bits

Patrick Steinhardt <ps@pks.im> writes:
> Changes in v2:
>   - Account for platforms where `timestamp_t` has 32 bit precision. This
>     matches logic in `write_graph_chunk_data()`, where we also depend on
>     the size of the commit timestamps.
Show 8 quoted lines
> +static timestamp_t compute_generation_offset(struct commit *c)
> +{
> +	timestamp_t masked_date;
> +
> +	if (sizeof(timestamp_t) > 4)
> +		masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
> +	else
> +		masked_date = c->date;

It is a bit surprising that on a platform where timestamp_t is only 32-bit wide, a smart-enough compiler would not find (1<<34) as suspicious. IOW, I would have expected this to be done not with runtime switch but with conditional compilation.

Patrick SteinhardtMar 25, 2026, 05:36 UTC in reply to Junio C Hamano on lore

Re: [PATCH v2] commit-graph: fix writing generations with dates exceeding 34 bits

On Tue, Mar 24, 2026 at 08:42:31AM -0700, Junio C Hamano wrote:
Show 19 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > Changes in v2:
> >   - Account for platforms where `timestamp_t` has 32 bit precision. This
> >     matches logic in `write_graph_chunk_data()`, where we also depend on
> >     the size of the commit timestamps.
> 
> > +static timestamp_t compute_generation_offset(struct commit *c)
> > +{
> > +	timestamp_t masked_date;
> > +
> > +	if (sizeof(timestamp_t) > 4)
> > +		masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
> > +	else
> > +		masked_date = c->date;
> 
> It is a bit surprising that on a platform where timestamp_t is only
> 32-bit wide, a smart-enough compiler would not find (1<<34) as
> suspicious. 

It probably would, but we don't test on any system where that's the case to the best of my knowledge. `timestamp_t` is defined as `uintmax_t`, and that should be 64 bit on almost all platforms. There's going to be exceptions though, even though I'm not sure whether such platforms even matter to us. But I later realized that we had similar checks elsewhere, so I decided to adapt accordingly.

> IOW, I would have expected this to be done not with runtime switch but
> with conditional compilation.

Well, it's written as a runtime switch, but I would expect all compilers to essentially turn this into conditional compilation. They should note that the condition is always true (or false) on a given platform. And checking Godbolt my assumption seems to hold. On x86-64 using GCC [1]:

    compute_generation_offset(unsigned long, unsigned long):
            movabs  rax, 17179869183
            and     rdi, rax
            mov     rax, rsi
            sub     rax, rdi
            ret
Thanks!
Patrick
[1]: https://godbolt.org/z/oa4xPohGb
Karthik NayakMar 26, 2026, 10:02 UTC in reply to Patrick Steinhardt on lore

Re: [PATCH v2] commit-graph: fix writing generations with dates exceeding 34 bits

Patrick Steinhardt <ps@pks.im> writes:
Show 11 quoted lines
> The `timestamp_t` type is declared as `uintmax_t` and thus typically has
> 64 bits of precision. Usually, the full precision of such dates is not
> required: it would be comforting to know that Git is still around in
> millions of years, but all in all the chance is rather low.
>
> We abuse this fact in the commit-graph: instead of storing the full 64
> bits of precision, committer dates only store 34 bits. This is still
> plenty of headroom, as it means that we can represent dates until year
> 2514. Commits which are dated beyond that year will simply get a date
> whose remaining bits are masked.
>
Okay so the structure in the commit graph is:

base + 8 => generation number (30 bits) + date high (2 bits) base + 12 => date low (30 bits)

date = date_high << 32 | date_low;
This can be seen in `write_graph_chunk_data()`, so makes sense.
Show 33 quoted lines
> The result of this is somewhat curious: the committer date will be
> different depending on whether a commit gets parsed via the commit-graph
> or via the object database. This isn't really too much of an issue in
> general though, as we don't typically use the date parsed from the
> commit-graph in user-facing output.
>
> But with 024b4c9697 (commit: make `repo_parse_commit_no_graph()` more
> robust, 2026-02-16) it started to become a problem when writing the
> commit-graph itself. This commit changed `repo_parse_commit_no_graph()`
> so that we re-parse the commit via the object database in case it was
> already parsed beforehand via the commit-graph.
>
> The consequence is that we may now act with two different commit dates
> at different stages:
>
>   - Initially, we use the 34-bit precision timestamp when writing the
>     chunk generation data. We thus correctly compute the offsets
>     relative to the on-disk timestamp here.
>
>   - Later, when writing the overflow data, we may end up with the
>     full-precision timestamp. When the date is larger than 34 bits the
>     result of this is an underflow when computing the offset.
>
> This causes a mismatch in the number of generation data overflow records
> we want to write, and that ultimately causes Git to die.
>
> Introduce a new helper function that computes the generation offset for
> a commit while correctly masking the date to 34 bits. This makes the
> previously-implicit assumptions about the commit date precision explicit
> and thus hopefully less fragile going forward.
>
> Adapt sites that compute the offset to use the function.
>
Well explained.
Show 63 quoted lines
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
> Hi,
>
> this fixes a regression recently introduced by myself in 024b4c9697
> (commit: make `repo_parse_commit_no_graph()` more robust, 2026-02-16).
> The regression was found by GitLab's tests suite, see [1].
>
> Changes in v2:
>   - Account for platforms where `timestamp_t` has 32 bit precision. This
>     matches logic in `write_graph_chunk_data()`, where we also depend on
>     the size of the commit timestamps.
>   - Link to v1: https://lore.kernel.org/r/20260317-pks-commit-graph-overflow-v1-1-e6bee22cd826@pks.im
>
> Thanks!
>
> Patrick
>
> [1]: https://gitlab.com/gitlab-org/gitlab/-/jobs/13522328632
> ---
>  commit-graph.c          | 37 ++++++++++++++++++++++++++++++++++---
>  t/t5318-commit-graph.sh | 20 ++++++++++++++++++++
>  2 files changed, 54 insertions(+), 3 deletions(-)
>
> diff --git a/commit-graph.c b/commit-graph.c
> index f8e24145a5..cb514bfb60 100644
> --- a/commit-graph.c
> +++ b/commit-graph.c
> @@ -1319,6 +1319,37 @@ static int write_graph_chunk_data(struct hashfile *f,
>  	return 0;
>  }
>
> +/*
> + * Compute the generation offset between the commit date and its generation.
> + * This is what's ultimately stored as generation number in the commit graph.
> + *
> + * Note that the computation of the commit date is more involved than you might
> + * think. Instead of using the full commit date, we're in fact masking bits so
> + * that only the 34 lowest bits are considered. This results from the fact that
> + * commit graphs themselves only ever store 34 bits of the commit date
> + * themselves.
> + *
> + * This means that if we have a commit date that exceeds 34 bits we'll end up
> + * in situations where depending on whether the commit has been parsed from the
> + * object database or the commit graph we'll have different dates, where the
> + * ones parsed from the object database would have full 64 bit precision.
> + *
> + * But ultimately, we only ever want the offset to be relative to what we
> + * actually end up storing on disk, and hence we have to mask all the other
> + * bits.
> + */
> +static timestamp_t compute_generation_offset(struct commit *c)
> +{
> +	timestamp_t masked_date;
> +
> +	if (sizeof(timestamp_t) > 4)
> +		masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
> +	else
> +		masked_date = c->date;
> +
> +	return commit_graph_data_at(c)->generation - masked_date;
> +}
> +
Looks good.
Show 57 quoted lines
>  static int write_graph_chunk_generation_data(struct hashfile *f,
>  					     void *data)
>  {
> @@ -1329,7 +1360,7 @@ static int write_graph_chunk_generation_data(struct hashfile *f,
>  		struct commit *c = ctx->commits.items[i];
>  		timestamp_t offset;
>  		repo_parse_commit(ctx->r, c);
> -		offset = commit_graph_data_at(c)->generation - c->date;
> +		offset = compute_generation_offset(c);
>  		display_progress(ctx->progress, ++ctx->progress_cnt);
>
>  		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
> @@ -1350,7 +1381,7 @@ static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
>  	int i;
>  	for (i = 0; i < ctx->commits.nr; i++) {
>  		struct commit *c = ctx->commits.items[i];
> -		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
> +		timestamp_t offset = compute_generation_offset(c);
>  		display_progress(ctx->progress, ++ctx->progress_cnt);
>
>  		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
> @@ -1741,7 +1772,7 @@ static void compute_generation_numbers(struct write_commit_graph_context *ctx)
>
>  	for (i = 0; i < ctx->commits.nr; i++) {
>  		struct commit *c = ctx->commits.items[i];
> -		timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
> +		timestamp_t offset = compute_generation_offset(c);
>  		if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
>  			ctx->num_generation_data_overflows++;
>  	}
> diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
> index 98c6910963..1c40f904f8 100755
> --- a/t/t5318-commit-graph.sh
> +++ b/t/t5318-commit-graph.sh
> @@ -417,6 +417,26 @@ test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow ch
>  	test_cmp full/.git/objects/info/commit-graph commit-graph-upgraded
>  '
>
> +test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'overflow chunk when replacing commit-graph' '
> +	test_when_finished "rm -rf repo" &&
> +	git init repo &&
> +	(
> +		cd repo &&
> +		cat >commit <<-EOF &&
> +		tree $(test_oid empty_tree)
> +		author Example <committer@example.com> 9223372036854775 +0000
> +		committer Example <committer@example.com> 9223372036854775 +0000
> +
> +		Weird commit date
> +		EOF
> +		commit_id=$(git hash-object -t commit -w commit) &&
> +		git reset --hard "$commit_id" &&
> +		git commit-graph write --reachable &&
> +		git commit-graph write --reachable --split=replace &&
> +		git log
> +	)
> +'

So we first write the commit graph once and then rewrite it. The second write must recompute the overflow chunk. Then we run 'git log' to exercise the commit graph.

Looks good!
Show 8 quoted lines
> +
>  # the verify tests below expect the commit-graph to contain
>  # exactly the commits reachable from the commits/8 branch.
>  # If the file changes the set of commits in the list, then the
>
> ---
> base-commit: ca1db8a0f7dc0dbea892e99f5b37c5fe5861be71
> change-id: 20260317-pks-commit-graph-overflow-09c3fb6e259a

Back to recent threads