News for hackers. Stuff that matters.
Re:Git’s database internals I: packed object store by srvmshr on Monday August 29, 2022 @10:58AM (#32639096)
In my mental model of git internals: I envision the initial commit stores a full copy in object store - and thereafter the states are captured as diffs. If a new file is added at a later commit stage, of course the "blob" will be stored, but subsequent commits will add the deltas to it - not a full copy.
Re:Git’s database internals I: packed object store by masklinn on Monday August 29, 2022 @11:12AM (#32639253)
That's not the case.

The actual model of git internals is that every commit always stores full copies (of modified entries, unmodified entries are dedup'd through the magic of content addressing).

Then as an optimisation of the packing process git will perform delta compression between objects based on a bunch of heuristics. Until you pack you get full copies, and depending how "hard" you pack you may or may not gets huge savings.

Furthermore, Git's pack files are ordered, so depending on the selection it makes it can diff later files from earlier ones (your model) or the reverse, or even both at the same time.

You can see some of the early packing heuristics at https://github.com/git/git/blob/master/Documentation/technic...

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @11:23AM (#32639377)
> That's not the case.

As far as the actual internals are concerned, yes. But as a model it's not wrong. It's like epicycles aren't wrong as a model either. GP's mental model of Git is mine as well, and it works very well for me.

Re:Git’s database internals I: packed object store by masklinn on Monday August 29, 2022 @11:26AM (#32639414)
> But as a model it's not wrong.

As a model it's wrong, and useless, and possibly harmful.

Because the "states are captured as diffs" model does not tell you anything true that's useful. And at worst it gives you incorrect ideas of the performance characteristics of the system e.g. that small changes are bad, because lots of changes means lots of diffs to apply which is slow.

By forgetting that model, you're strictly less wrong, and helped no less about reasoning about git.

> It's like epicycles aren't wrong as a model either.

But they were. "Adding epicycles" has literally become a saying about trying to pile on more crap to try and (fail to) make an incorrect model work.

And epicycles were at least trying to keep a simple model working, the "snapshot model" is more complicated than git's actual behaviour to start with.

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @01:16PM (#32640789)
> As a model it's wrong, and useless, and possibly harmful.

It's no more wrong than epicycles. It's not useless either: it works very well for me. For users (as opposed to Git developers), it's hard to see how it could be harmful.

Re:Git’s database internals I: packed object store by johannes1234321 on Monday August 29, 2022 @03:12PM (#32642248)
The harmful part comes from assumptions based on it.

Systems like CVS or Subversion indeed store diffs of file conceptually and storage wise. This has notable co sequences: when doing operations on a large span of revisions all takes long as a bunch of diffs have to be applied in sequence. This then leads to reluctance of small commits for the wrong reasons.

Wrong assumptions about the workings leading to wrong decisions is harmful.

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @03:32PM (#32642478)
> This has notable co sequences: when doing operations on a large span of revisions all takes long as a bunch of diffs have to be applied in sequence. This then leads to reluctance of small commits for the wrong reasons.

This happens in Git as well though. If you have to rebase your commits across a few thousand upstream commits, you'll be in for some pain. (For example, I've a commit to PostgreSQL to add ALWAYS DEFERRED constraints, and I've had to rebase it across thousands of upstream commits because I've lacked the time to see that contribution across the finish line.)

You seem to be indirectly arguing for merge-based workflows. (At least that's what I'm reading between the lines. Perhaps my reading is wrong, but I'll run with it until you tell me otherwise.) However, a) merging is not always accepted by upstream, and b) merge-based workflows suck.

I grant that merging is very simple with Git's internals: Git does the three-way merge thing, you manually merge conflicts, you create a commit for the conflict-resolved checked out state, recording the two parents in that commit, and you're done. This works because Git simply stores the objects as they are, internally using deltas -not quite diffs- to compress the object store. This is very simple, yes, but it yields a workflow that doesn't work for many projects. Rebasing is much better, and it always works, especially with [0].

So now you might wonder how to deal with the rebase-across-thousands-of-commits problem. Well, I've a script[0] (not originally mine, but I've hacked on it) that does something like bisection to find upstream commits causing conflicts so as to greatly speed up the process.

[0] https://gist.github.com/nicowilliams/ea2fa2b445c2db50d2ee650...

Re:Git’s database internals I: packed object store by johannes1234321 on Monday August 29, 2022 @05:32PM (#32643842)
If you got to rebase over many decisions you are likely in trouble anyways. There small commits can be helpful to ease dealing with merge conflicts.

However rebase is a single special operation. The diff-based storage affects all. From updating a local repository from upstream, diffing between some distance, ...

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @06:46PM (#32644523)
> The diff-based storage affects all.

No one is saying Git does or should do that.

> If you got to rebase over many decisions you are likely in trouble anyways.

That script I linked made it easy.

> There small commits can be helpful to ease dealing with merge conflicts.

They were indeed mostly small commits. Mine wasn't that small -- it's fairly invasive and not possible to split up into more than two commits.

Re:Git’s database internals I: packed object store by robertlagrant on Monday August 29, 2022 @01:15PM (#32640776)
Calling things harmful considered harmful.
Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @01:20PM (#32640842)
Especially without an explanation.

  them: harmful
  me: how?
  them: really harmful
  me: sure, but, can you explain it?
  them: like a bajillion harmful
Re:Git’s database internals I: packed object store by eminence32 on Monday August 29, 2022 @01:45PM (#32641178)
When you rename a file in git, you can get a glimps of its internal model -- renames are not explicitly tracked. Since git only stores full copies of each object, it has to employ heuristics to figure out if a (deleted file, created file) pair should be considered a rename. This is why you sometimes see a percentage next to a rename in a "git log" -- this is git's internal metric about how similar/dissimilar the files are.
Re:Git’s database internals I: packed object store by rossmohax on Monday August 29, 2022 @05:42PM (#32643931)
Is there any value in first class renames other than some performance boost ?
Re:Git’s database internals I: packed object store by dlkmp on Tuesday August 30, 2022 @08:16AM (#32649377)
I'd argue yes. I remember times where I was puzzled that some config file had been deleted (in a commit touching lots of files) only to find out after some analysis that is was simply moved/ renamed.
Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @11:20AM (#32639334)
This is a good mental model. The internals are the internals. But the model users see is that a repository is a bag of commits (deltas) that organize into a tree, with branches and tags being names for specific commits. With this model in mind anyone can become a Git power user.
Re:Git’s database internals I: packed object store by stu2b50 on Monday August 29, 2022 @11:35AM (#32639495)
From a user point of view, you should not see commits as diffs or deltas, but as a full snapshots of the contents of the repository.

Delta compression is just an implementation detail for storage optimization. Conceptually each commit is an immutable snapshot in its own right.

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @01:17PM (#32640803)
> From a user point of view, you should not see commits as diffs or deltas, but as a full snapshots of the contents of the repository.

That tends to lead to a merge-centric workflow.

The alternative makes it easy to understand rebase. Ergo it's good.

Re:Git’s database internals I: packed object store by stu2b50 on Monday August 29, 2022 @03:57PM (#32642780)
I don't see any particular reason that'd be the case. Even with rebase, the diff model causes some of git's behavior to no longer make sense. For instance, the fact that rebase causes an entirely new series of commits to be made - this is because rebase is just a layer over cherry-picking commits, and that cherry-picking commits calculates the diffs between two commits then applies the diff as its own, new commit.

If commits were diffs, then that process doesn't make any sense. It only makes sense when you think of commits as snapshots, and cherry-pick ing as taking a commit, calculating the diff with the current commit, and then applying that diff to make a brand new commit.

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @06:47PM (#32644534)
Yes, rebase is a series of cherry-picks. Each commit picked is treated as a set of diffs that are applied with conflict resolution, then committed, then off to the next. The mental model of commits as diffs works, obviously.

> If commits were diffs, then that process doesn't make any sense. It only makes sense when you think of commits as snapshots, and cherry-pick ing as taking a commit, calculating the diff with the current commit, and then applying that diff to make a brand new commit.

Sure it does. It works for me. You can use whatever mental model of Git that you works for you.

Re:Git’s database internals I: packed object store by juped on Monday August 29, 2022 @01:45PM (#32641177)
There are two ways commits can be treated in Git. Most of the time, a commit is a specific tree and some metadata. There are, however, some rebase-like operations that use the commit metadata to treat a commit as a diff against one or more parents which it points to. You're wrong in this discussion because it's about storage, but you do need to flip into this dual view of the commit graph to use rebase-like operations correctly.
Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @02:13PM (#32641502)
Exactly. Thinking of commits as diffs helps think about rebasing.
Re:Git’s database internals I: packed object store by josephg on Monday August 29, 2022 @11:36AM (#32639513)
> But the model users see is that a repository is a bag of commits (deltas)

Commits in your .git directory aren’t stored using deltas from a previous commit. Each different version of every file is stored in full.

Re:Git’s database internals I: packed object store by MaxBarraclough on Monday August 29, 2022 @11:55AM (#32639727)
From https://git-scm.com/book/en/v2/Git-Internals-Packfiles :

> You have two nearly identical 22K objects on your disk (each compressed to approximately 7K). Wouldn’t it be nice if Git could store one of them in full but then the second object only as the delta between it and the first?

> It turns out that it can. The initial format in which Git saves objects on disk is called a “loose” object format. However, occasionally Git packs up several of these objects into a single binary file called a “packfile” in order to save space and be more efficient.

...

> What is also interesting is that the second version of the file is the one that is stored intact, whereas the original version is stored as a delta — this is because you’re most likely to need faster access to the most recent version of the file.

Re:Git’s database internals I: packed object store by masklinn on Monday August 29, 2022 @01:03PM (#32640621)
That is what's covered by the article, but as various commenters have noted it's not part of git's model, it's an optimisation of git's storage.

So thinking in those terms for modelling / visualising git's behaviour is nonsensical.

Re:Git’s database internals I: packed object store by MaxBarraclough on Monday August 29, 2022 @01:23PM (#32640886)
It's not hard to make correct statements about the git user interface without being misleading about its internals.

josephg said:

> Each different version of every file is stored in full.

which is an untrue statement about how git internally manages its data. It would be fine had they instead said:

> From the user's point of view, it's as if each different version of every file is stored in full. Internally, git may perform compression, including differential compression.

This isn't the first time I've seen this confusion. This 2014 StackOverflow thread [0] does the same, as does an old HN thread. [1] The solution is clear communication.

[0] https://stackoverflow.com/q/8198105/

[1] https://news.ycombinator.com/item?id=25459757

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @01:22PM (#32640869)
Surely you must know what the word "model" means.
Re:Git’s database internals I: packed object store by juped on Monday August 29, 2022 @01:42PM (#32641125)
It doesn't mean "get out of jail free card for cryptonector being wrong", no.

The simplified model that leaves out tricky implementation details is just "every file that's different is a different blob object, stored in full on its own, under the hash of its contents". If you work with this model, you're basically correct in all your reasoning, except that in reality a bit less storage is used because some objects get packed. If you work with the wrong (wrong! not simplified, wrong!) model "every blob object is stored as base-and-deltas", you won't reason correctly about either how the object store presents itself in plumbing commands or how it's stored.

Re:Git’s database internals I: packed object store by cryptonector on Monday August 29, 2022 @02:16PM (#32641542)
> It doesn't mean "get out of jail free card for cryptonector being wrong", no.

I never said that Git commits are diffs. Keep it civil.

> The simplified model that leaves out tricky implementation details [...]

Models do that.

> If you work with this model, you're basically correct in all your reasoning, except that in reality a bit less storage is used because some objects get packed.

That's it? That's the one detail that should stop me thinking correctly based on this model?

Re:Git’s database internals I: packed object store by JoshTriplett on Monday August 29, 2022 @11:29AM (#32639439)
Git actually arranges to delta starting from the current version to older versions, so that it has less work to do to show you recent versions, and does more work as it works its way backwards in time.