diff --git a/bin/git-revhash b/bin/git-revhash new file mode 100755 index 0000000..1c02c06 --- /dev/null +++ b/bin/git-revhash @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# Use +# git-revhash 'tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\nparent 22c0fa5195a53f2e733ec75a9b6e9d1624a8b771\nauthor seanius 1138341044 +0000\ncommitter seanius 1138341044 +0000\n\nmaking dir structure...' # noqa +# output: 17a631d474f49bbebfdf3d885dcde470d7faafd7 + +echo -e $* | git hash-object --stdin -t commit diff --git a/bin/swh-revhash b/bin/swh-revhash new file mode 100755 index 0000000..795acd3 --- /dev/null +++ b/bin/swh-revhash @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# Use: +# swh-revhash 'tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\nparent 22c0fa5195a53f2e733ec75a9b6e9d1624a8b771\nauthor seanius 1138341044 +0000\ncommitter seanius 1138341044 +0000\n\nmaking dir structure...\n' # noqa +# output: 17a631d474f49bbebfdf3d885dcde470d7faafd7 + +# To compare with git: +# echo -e 'tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\nparent 22c0fa5195a53f2e733ec75a9b6e9d1624a8b771\nauthor seanius 1138341044 +0000\ncommitter seanius 1138341044 +0000\n\nmaking dir structure...' | git hash-object -t commit --stdin # noqa +# output: 17a631d474f49bbebfdf3d885dcde470d7faafd7 + + +import sys + +from swh.model import identifiers, hashutil + + +def revhash(revision_raw): + """Compute the revision hash. + + """ + if b'\\n' in revision_raw: # HACK: string have somehow their \n + # expanded to \\n + revision_raw = revision_raw.replace(b'\\n', b'\n') + + h = hashutil.hash_git_data(revision_raw, 'commit') + return identifiers.identifier_to_str(h) + + +if __name__ == '__main__': + revision_raw = sys.argv[1].encode('utf-8') + print(revhash(revision_raw))