Coder Social home page Coder Social logo

git-hash's Introduction

git-hash

Understanding Git's hash calculations and implementing them in shell code

Sources

These provided all necessary information without resorting to reading Git's source code, but at least most of the shell implementations were not completely correct; their flaws often only start to show, when reading files with embedded newlines or other formatting characters or binary data / executables.

WWW search: https://www.startpage.com/do/settings?query=git%20hash%20calculation

Documentation for git hash-object, which is more comprehensible than its man-page:
https://git-scm.com/docs/git-hash-object

Git internal workings

Git's ongoing / stalled transition from SHA-1 to SHA-256

Hashing a file

A file corresponds to Git's type blob.

Using git hash-object

  • cat "$filepath" | git hash-object -t blob --no-filters --stdin --literally
  • git hash-object -t blob --no-filters --literally "$filepath"
  • git hash-object -t blob "$filepath"
  • git hash-object "$filepath"

Equivalent shell code

  • printf 'blob %s\0' "$(wc -c < "$filepath")" | cat - "$filepath" | sha1sum
  • printf 'blob %s\0' "$(find -L "${filepath%/*}" -maxdepth 1 -name "${filepath##*/}" -type f -printf %s)" | cat - "$filepath" | sha1sum
  • printf 'blob %s\0' "$(find -L "$(dirname "$filepath")" -maxdepth 1 -name "$(basename "$filepath")" -type f -printf %s)" | cat - "$filepath" | sha1sum
  • stat -L --printf='blob %s\0' "$filepath" | cat - "$filepath" | sha1sum
  • stat -L --printf='%F blob %s\0' "$filepath" | grep -z '^regular file blob ' | sed -z 's/^regular file \(blob .*\)/\1/' | cat - "$filepath" | sha1sum

Recreating an object entry

ToDo # find -L . -type d -path "*/.git" -prune -o -printf "1%#05m %f\t%p\t%h\n" # find -L . -type d -path "*/.git" -prune -o -perm /111 -printf "100775 %f\t%p\t%h\n" -o \! -perm /111 -printf "100664 %f\t%p\t%h\n" find -L . -type d -path "*/.git" -prune -o -readable -xtype l -type f -printf "120000 %f\t%p\t%h\n" -o -readable -executable -type f -printf "100775 %f\t%p\t%h\n" -o -readable \! -executable -type f -printf "100664 %f\t%p\t%h\n" find -L . -type d -path "*/.git" -prune -o -readable -type f \( -xtype l -printf "120000 %f\t%p\t%h\n" -o -executable -printf "100775 %f\t%p\t%h\n" -o \! -executable -printf "100664 %f\t%p\t%h\n" \) # Separate links and dirs according to https://git-scm.com/book/en/v2/Git-Internals-Git-Objects#_tree_objects

Recreating a tree object's hash

A tree object comprises one or multiple object entries.

ToDo

git-hash's People

Contributors

fvsamson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.