LoginSignup
0
0

More than 1 year has passed since last update.

Git Objects

Posted at
echo '123' | git hash-object -w --stdin
# 3670911e6f6449df03f225ee1efb5acb8bfd4bc7

find .git/objects -type f
# .git/objects/36/70911e6f6449df03f225ee1efb5acb8bfd4bc7

git cat-file -p 3670911e6f6449df03f225ee1efb5acb8bfd4bc7
# zqw

####################

echo 'version 1' > ver.txt

git hash-object -w ver.txt
# 83baae61804e65cc73a7201a7252750c76066a30

find .git/objects -type f
# .git/objects/83/baae61804e65cc73a7201a7252750c76066a30

git cat-file -p  83baae61804e65cc73a7201a7252750c76066a30
# version 1

####################

git cat-file -t 3670911e6f6449df03f225ee1efb5acb8bfd4bc7
# blob
git update-index --add new.txt

####################

git write-tree
4873235733fd7a44f2c3e73937dd09e064cbac53

####################

echo 'first commit' | git commit-tree 4873235
0b74f629a3b25a22821634bc34cdd3077256ce39

####################

git cat-file -p 0b74f629a3b25a22821634bc34cdd3077256ce39
  tree 4873235733fd7a44f2c3e73937dd09e064cbac53
  author shooter <shooter@bitcoin.org> 1520676818 +0800
  committer shooter <shooter@bitcoin.org> 1520676818 +0800

  first commit
git log --stat 0b74f6
commit 0b74f629a3b25a22821634bc34cdd3077256ce39
Author: shooter <shooter@bitcoin.org>
Date:   Sat Mar 05 08:13:38 2017 +0800

    first commit

 new.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

Git 如何存储对象

通过 Ruby 脚本语言存储一个 git blob 对象

require 'digest'
require 'zlib'

content = "what is up, doc?"

header = "blob #{content.length}\0"
# => "blob 16\u0000"

store = header + content
# => "blob 16\u0000what is up, doc?"

sha1 = Digest::SHA1.hexdigest(store)
# => "bd9dbf5aae1a3862dd1526723246b20206e5fc37"

sha1[0,2]
# => "bd"

sha1[2,38]
# => "9dbf5aae1a3862dd1526723246b20206e5fc37"

zlib_content = Zlib::Deflate.deflate(store)
# => "x\x9CK\xCA\xC9OR04c(\xCFH,Q\xC8,V(-\xD0QH\xC9O\xB6\a\x00_\x1C\a\x9D"

path = '.git/objects/' + sha1[0,2] + '/' + sha1[2,38]
# => ".git/objects/bd/9dbf5aae1a3862dd1526723246b20206e5fc37"

FileUtils.mkdir_p(File.dirname(path))
# => [".git/objects/bd"]

File.open(path, 'w') { |f| f.write zlib_content }
# => 32

参考:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0