MCPcopy Index your code
hub / github.com/chrisdickinson/git-rs

github.com/chrisdickinson/git-rs @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
148 symbols 251 edges 29 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

git-rs

Implementing git in rust for fun and education!

If you're looking for a native Rust Git implementation ready for use in anger, you might consider looking at gitoxide instead!

This is actually my second stab at it, so big blocks will land in place from my first attempt. I'm trying again this year after reading more of "Programming Rust" (Blandy, Orendorff).

TODO

  • [x] Read objects from loose store
  • [x] Read objects from pack store
    • [x] Read packfile indexes
    • [x] Read delta'd objects
    • [x] Fix interface so we don't need to run open for each read()
    • [x] BUG: certain OFS deltas are misapplied.
      • [x] Isolate the error case
      • [x] Fix it
  • [x] Load refs off of disk
  • [x] Parse git signatures ("Identity"'s)
  • [x] Create iterator for walking commit graph
  • [x] Create iterator for walking trees
    • [ ] Materialize trees to disk (post gitindex?)
  • [x] Create index from packfile
    • [x] Rename Storage trait to Queryable
    • [x] Rework object loading API from <Type + Boxed reader> to "we take a writable object"
      • [x] Carry the rework out through StorageSet
    • [x] Create the index
    • [x] Wrap it in a nice API
  • [ ] refs v2
    • [ ] Load refs on demand
    • [ ] Load packed-refs
  • [ ] .git/index support
    • [ ] Read git index cache
    • [ ] Write git index cache
  • [ ] Create interface for writing new objects
  • [ ] Add benchmarks
  • [ ] Code coverage
  • [ ] Create packfile from list of objects (API TKTK)
  • [ ] Network protocol
    • [ ] receive-pack
    • [ ] send-pack
  • [ ] Try publishing to crates
    • [ ] Write documentation
    • [ ] Use crate in another project

PLAN

2022-04-30 Update

  • I did a bit of optimizing and my (completely unscientific) benchmarking has us pretty close to native git log!
  • I'm currently measuring the performance of git log --pretty=oneline >/dev/null vs. git_rs_log >/dev/null against a local checkout of nodejs/node.
    • This is on a M1 Pro Max Macbook Pro.
    • git_rs_log started out at ~500ms for a complete walk of the repo history. Vanilla git was seeing ~280-300ms.
  • I'm used to using DTrace + flamegraphs to profile, but to my dismay using DTrace requires booting into recovery mode on modern macOS & disabling system integrity protection.
    • at least, that was what my first investigation turned up. It looks like there may be other options I missed.
  • My coworker Eric suggested using macOS's Instruments.app instead via cargo-instruments which worked a treat.
  • Running cargo instruments necessitated exposing a way to set the current working directory for git_rs_log, so I added clap.
  • I'm pleased to report we were able to bring git_rs_log down to 300-320ms for a full walk of node's history. Here's what I did:
    • Switching deflate2 from it's miniz_oxide backend (the default, written in rust) to native zlib was the biggest boost
    • Switching a sort_by_key to sort_unstable_by_key in packfile index reads was a small (5-7ms) win. (Packfile indices have a fanout table, a list of ids in ascending order by id value, and a list of offsets-in-the-packfile whose order corresponds to the ids. In order to use a packfile index to read from a packfile, though, you need to be able to read the offset for your incoming id request and the next id in the packfile in offset order. Hence the sort_by_key call – once we've loaded the ids and offsets, we have to keep a mapping of position of id -> position in packfile.)
    • Tuning up the commit parser gave me another 10ms or so. Out of expediency, I had originally treated commits as HTTP transaction-like – newline-separated key/value headers followed by a double newline then the message. Now I actually store the well-known fields directly on the struct in parsed form. (There's still room for improvement here, too!)
      • This required adding an Id::from_ascii_bytes(&[u8]) for hexadecimal-encoded ids
        • Before this you'd have to bounce through std::str::from_utf8 which validates that the vector contains valid utf8 before we validate that it only contains hexadecimal chars; now we can do both in one step.
  • I'm pretty happy with that performance (for now), so I'm looking for something to pick up next. Options include:
    • Support for the worktree index file, .git/index. This is the start of the path for writing objects to the Git database.
    • Better support for refs.
    • Support for SHA256 object format. (Vanilla Git supports this now, so it'd be interesting to dig into how it works.)
    • Another Rust project, for a change of pace.
      • Postgres change data capture support, a la Debezium (but not tied to kafka.)
      • A return to WASM text parsing (I have a private project called "watto" for this.)

2022-04-27 Update

  • I'm back! I finally have some free time (and, maybe more importantly, available attention) so I'm revisiting this project after a few years.
  • Most recently, I fixed a bug with the "identity" parser. "Identities" are the bits of commit and tag metadata that look roughly like Humanname <email> 10100100100 +7300.
    • It turns out my parsing had a bug: it was dropping the last character of the email.
    • This was surfaced to me -- not by tests as it should have been, to my embarrassment -- but by trying to run git_rs_log against the Node repository. It turns out someone had committed without an email: Foobar <> 1010020203 -4000.
    • My off-by-one error turned this into a panic, with the program safely -- if unexpectedly -- crashing on that input.
    • I fixed the parser bug and golfed the parser itself down using match statements and constants.
  • While I was in that part of the code, I did a little editorializing: renaming identity to human_metadata.
  • I also took the oppportunity to lazily parse the human metadata. There's no need to walk that entire bytestring unless someone asks for it.
    • It turns out that we do ask for it during the course of git log: if we have multiple branches we need to load up the commit metadata to compare timestamps, as the output order depends on commit timestamp.
    • But for straightline chains of commits we don't need to load any of that up.
      • This saves ~10-20 milliseconds on git log in the node repository.
  • Burying the lede: git_rs_log is about 100-200ms slower than git log --pretty=oneline, run against the node repository.
    • Well, that certainly seems like a useful north star, does it not?
    • Where are we spending time that git isn't?
  • Buoyed by my recent deep dive into the LLVM ecosystem, I briefly explored profile-guided optimization.
    • I'm happy to report that I got a working setup and understood the results.
    • I'm less happy to report that, well, the results weren't stunning. This kind of checks out: if the performance gap is down to the number of I/O system calls we're making, assuming git makes fewer system calls that's where our perf gap will be.
    • So that's my current number one goal.
  • My number two goal is to modernize this repo and bring it up to the Rust standards that I picked up from $dayjob (and in particular, via @fishrock123.)
    • That means:
      • implementing more standard traits on types,
      • adding integration tests,
      • adding type docs,
      • and being a little bit more circumspect about what the crate exposes as a public API.

2019-02-08 Update

  • It's been a minute!
  • As you might have seen, figuring out packfile indexing has forced a lot of changes on the repo.
    • There's now a src/pack/read.rs file that holds generic read implementations for any BufRead + Seek.
    • The signature of the Storage trait changed -- instead of returning a boxed read object, it now accepts a Write destination.
    • Further, Storage is now Queryable (a better name!).
      • Because we moved from returning a Box to accepting generic Write, we could no longer box Queryables.
        • I didn't know this about Rust, so TIL!
      • StorageSet objects had to be rethought as a result -- they could no longer contain Box'd Storage objects.
        • Instead, we put the compiler to work -- because storage sets are known at compile time, I implemented Queryable for the unit type, (), two types (S, T), and arrays of single types Vec<T>.
        • This means that a StorageSet may hold a single, top-level Queryable, which might contain nested heterogenous Queryable definitions.
          • It gives me warm, fuzzy feelings :revolving_hearts:
  • You might also note that we're not actually done indexing packfiles. :scream:
    • Here's the sitch: in order to create a packfile index, you have to run a CRC32 over the compressed bytes in the packfile.
    • The ZlibDecoder will pull more bytes from the underlying stream than it needs, so you can't take the route of handing it a CrcReader and get good results.
    • It's got to be a multi-pass deal.
      • The current plan is: run one pass to get offsets, un-delta'd shas and types.
      • Run a second pass to resolve CRCs and decompress deltas. This can be done in parallel.

2019-01-23 Update

  • It's time to start indexing packfiles.
    • This'll let us start talking to external servers and cloning things!
  • However, it's kind of a pain.
    • Packfiles (viewed as a store) aren't hugely useful until you have an index, so I had designed them as an object that takes an optional index from outside.
      • My thinking was that if an index was not given, we would build one in-memory.
    • That just blew up in my face, a little bit. :boom:
    • In order to build an index from a packfile you have to iterate over all of the objects.
      • For each object, you want to record the offset and the SHA1 id of the object at that offset.
      • However, the object might be an offset or a reference delta.
        • That means that in order to index a packfile, you've got to be able to read delta'd objects at offsets within the packfile (implying you already have the Packfile instance created) and outside of the packfile ( implying you have a StorageSet.)
        • In other words: my assumptions about the program design are wrong.
    • So, in the next day or so I'll be reversing course.
      • It should be possible to produce a Packfile as a non-store object and iterate over it.
      • The "store" form of a packfile should be the combination of a Packfile and an Index (a PackfileStore.)
        • This means I'll be splitting the logic of src/stores/mmap_pack into "sequential packfile reads" and "random access packfile reads (with an index.)"
  • It's fun to be wrong :tada:

2019-01-21 Update

  • Well, that was a fun bug. Let's walk through it, shall we?
    • This occasionally showed up when a delta would decode another delta'd object.
      • I found a hash that would reliably fail to load.
      • We'd fail the read because the incoming base object would not match the 2nd delta's "base size". [Here][ref_10].
      • Removing the check to see if I got the deltas wrong would cause the thread to panic -- the delta's base size wasn't a lie.
    • First, I switched back to my old mmap-less packfile implementation, because I recently touched that code. "Revert the thing you touched last" is a winning strategy in these cases: doesn't cost expensive thinking, quickly puts bounds around the bug.
      • Alas, the old packfile implementation also had this bug. No dice.
    • I compared the output of this git implementation to my [JS implementation][ref_11].
      • I confirmed that the output of the JS implementation worked by comparing its output for the hash of concern to vanilla git.
      • After confirming that, I logged out the offsets being read from the file and the expected sizes. I compared this to similar debugging output I added to git-rs.
        • The offsets are the bound values sent into [the packfile][ref_12]. For the outermost read ("Give me the object represented by eff4c6de1bd15cb0d28581e4da17035dbf9e8596"), the offsets come from the packfile index.
        • For OFS_DELTA ("offset delta") types, the start offset is obtained by reading a [varint][ref_13] from the packfile and subtracting it from the current start offset.
      • The offsets and expected sizes matched!
        • This meant that:
          1. I was reading the correct data from the packfile
          2. I was reading varints correctly
          3. The bug must be in the application of the delta to a base object
      • From there I added logging above [these state transitions][ref_14], noting the particulars of the operation.
        • I added the same logging to the JS implementation, and found that (aside from totally bailing out ahead of the 2nd delta application) the commands were the same.
        • So it wasn't even that my delta code was wrong: it was my Read state machine.
    • At this point, I was like: "This is a read state machine bug. [I know this][ref_15]."
      • So, one of the things this state machine does is carefully bail out if it can't [write all of the bytes for a command][ref_16]. ("If there remains an

Extension points exported contracts — how you extend this code

Queryable (Interface)
(no doc) [6 implementers]
src/stores/mod.rs
Packfile (Interface)
(no doc) [2 implementers]
src/pack/mod.rs

Core symbols most depended-on inside this repo

from
called by 10
src/stores/fs.rs
as_ref
called by 8
src/objects/commit.rs
get
called by 6
src/stores/mod.rs
as_ref
called by 4
src/id.rs
get
called by 4
src/stores/loose.rs
get_and_load
called by 4
src/stores/mod.rs
packfile_read
called by 4
src/pack/read.rs
recurse_dir
called by 3
src/refs.rs

Shape

Method 78
Function 30
Class 29
Enum 9
Interface 2

Languages

Rust100%

Modules by API surface

src/id.rs16 symbols
src/human_metadata.rs15 symbols
src/delta.rs12 symbols
src/objects/commit.rs10 symbols
src/refs.rs9 symbols
src/objects/tree.rs9 symbols
src/walk/commits.rs7 symbols
src/stores/mod.rs7 symbols
src/stores/loose.rs7 symbols
src/pack/mod.rs7 symbols
src/objects/mod.rs6 symbols
src/crc.rs6 symbols

For agents

$ claude mcp add git-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact