Skip to content

bug/Stripping-sha256-Prefix #207

@matthewpeterkort

Description

@matthewpeterkort

Git LFS Fix: Stripping "sha256:" Prefix

This change is about stripping the "sha256:" label so the rest of the system can process the hash correctly. Here is the breakdown of why that + (adding the prefix) was causing your test to fail:

1. The Input format (LFS Pointer)

A Git LFS pointer file looks like this on disk:

version https://git-lfs.github.com/spec/v1
oid sha256:7ddbb7fe9de07dd288e8d57eec8833c1b176cdd54969954a23ddc82c795ad09f
size 35

Git LFS includes the sha256: prefix in the text file to tell you what kind of hash it is.

2. The Bug (Prefix Inflation)

The code I changed was reading that line and storing it in your local cache like this:

  • Old Code: oid = "sha256:7ddbb7fe..." (71 characters total)
  • New Code: oid = "7ddbb7fe..." (Exactly 64 characters)

3. The Failure Point (Validation)

Later, when you run git push, the pre-push hook reads that OID from the cache and calls the storage logic. That logic has a strict "Sanity Check" (in lfs/store.go):

func (s *ObjectStore) ObjectPath(oid string) (string, error) {
if len(oid) != 64 {
return "", fmt.Errorf("error: %s is not a valid sha256 hash", oid)
}
// ...
}

Because the old code included sha256:, the length was 71 characters instead of 64. This tripped the validation and gave you the exact error message in your log:
...error: sha256:7ddbb7fe... is not a valid sha256 hash

4. Path Corruption

Even if it passed validation, it would have tried to create a folder path using the literal characters s, h, a, 2, 5, 6, :, which would result in a broken directory structure like:
.git/drs/lfs/objects/sh/a2/sha256:7ddbb7...

By removing the prefix, we ensure the OID is a clean, 64-character hex string that matches what the rest of the Go program expects.


Note on your Test Change:
I noticed you just updated the test file (main_test.go) back to expect sha256:deadbeef. If you run that test now, it will fail because the run() code is now (correctly) producing just deadbeef. To make the tests pass and keep the fix, the expectations in the test should look for the raw hash: deadbeef.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions