How to assign a Git SHA1's to a file without G

2019-01-03 20:46发布

As I understand it when Git assigns a SHA1 hash to a file this SHA1 is unique to the file based on its contents.

As a result if a file moves from one repository to another the SHA1 for the file remains the same as its contents have not changed.

How does Git calculate the SHA1 digest? Does it do it on the full uncompressed file contents?

I would like to emulate assigning SHA1's outside of Git.

标签: git sha1
12条回答
地球回转人心会变
2楼-- · 2019-01-03 20:51

A little goodie: in shell

echo -en "blob ${#CONTENTS}\0$CONTENTS" | sha1sum
查看更多
做个烂人
3楼-- · 2019-01-03 20:51
/// Calculates the SHA1 for a given string
let calcSHA1 (text:string) =
    text 
      |> System.Text.Encoding.ASCII.GetBytes
      |> (new System.Security.Cryptography.SHA1CryptoServiceProvider()).ComputeHash
      |> Array.fold (fun acc e -> 
           let t = System.Convert.ToString(e, 16)
           if t.Length = 1 then acc + "0" + t else acc + t) 
           ""
/// Calculates the SHA1 like git
let calcGitSHA1 (text:string) =
    let s = text.Replace("\r\n","\n")
    sprintf "blob %d%c%s" (s.Length) (char 0) s
      |> calcSHA1

This is a solution in F#.

查看更多
你好瞎i
4楼-- · 2019-01-03 20:52

A little Bash script that should produce identical output to git hash-object:

#!/bin/sh
( 
    echo -en 'blob '"$(stat -c%s "$1")"'\0';
    cat "$1" 
) | sha1sum | cut -d\  -f 1
查看更多
Animai°情兽
5楼-- · 2019-01-03 20:57

And in Perl (see also Git::PurePerl at http://search.cpan.org/dist/Git-PurePerl/ )

use strict;
use warnings;
use Digest::SHA1;

my @input = <>;

my $content = join("", @input);

my $git_blob = 'blob' . ' ' . length($content) . "\0" . $content;

my $sha1 = Digest::SHA1->new();

$sha1->add($git_blob);

print $sha1->hexdigest();
查看更多
▲ chillily
6楼-- · 2019-01-03 21:01

It is interesting to note that obviously Git adds a newline character to the end of the data before it will be hashed. A file containing nothing than "Hello World!" gets a blob hash of 980a0d5..., which the same as this one:

$ php -r 'echo sha1("blob 13" . chr(0) . "Hello World!\n") , PHP_EOL;'
查看更多
beautiful°
7楼-- · 2019-01-03 21:03

Full Python3 implementation:

import os
from hashlib import sha1

def hashfile(filepath):
    filesize_bytes = os.path.getsize(filepath)

    s = sha1()
    s.update(("blob %u\0" % filesize_bytes).encode('utf-8'))

    with open(filepath, 'rb') as f:
        s.update(f.read())

    return s.hexdigest() 
查看更多
登录 后发表回答