关于git:git-中的对象

4次阅读

共计 1383 个字符,预计需要花费 4 分钟才能阅读完成。

git 中的对象

  1. blob
  2. tree
  3. commit

git 中所有对象保留格局

  1. store = type + 空格 + #{content. 字节数}+”\0″+content
  2. hashValue = hash(store)
  3. hashValue[0,1] 文件夹,hashValue[2,-1] 作为文件名字 即 .git/objects/[头 2 个哈希字母]/ 残余的哈希值
  4. 该文件的内容 为 zib 压缩 store 的二进制文件

blob 为 文本文件

tree 示意的目录

commit:以后根目录加上 提交人,提交 msg 信息

git 中所有对象保留格局举例

以后根目录:1.txt src (目录 1.txt 外面的内容为 12)src/1.java ,src/2.java

blob 对象的 store = blob +"" +2 +"\0"+12

tree 对应的对象就是目录,即有 2 个目录,一个是根目录,一个是 src 目录

tree 的 content 格局比 blob 简单

即是 mode + hash + path mode 示意文件的类型,100644 一般文件,100755 可执行的文件

hash , 提到了过了,文件 就是下面的计算形式,目录 就是递归就子目录,子文件。

path : 相对路径。

如何计算 tree 对应的 hash .

获取以后所有文件的绝对于 .git 根目录的 相对路径。即是 1.txt ,src/1.java ,src/java

这三者都是 blob 对象。依照 blob 先存储 hash 目录 ==》文件内容

src/1.txt src/2.txt split “/” 写进 tree 的 content :

100644 +” ” +1.txt 方才的 hash 值 + 1.txt

100644 +” ” +2.txt 方才的 hash 值 + 2.txt

而后是

store = type + 空格 + #{content. 字节数}+”\0″+content

store = tree +""+" 假设下面 2 行字节 200"+"\0"+content

在 hash (store) 选前 2 个做目录 \ 前面所有的都是 hash 做文件名字 ==》zlib(store)

这个是 src 目录的 tree

还有一个 git 根目录 即是 1.txt 和 src . 计算形式一样,置底而上

commit 对应 hash

commit 对像内容为

tree 以后根门路的 hash +”\n”

+” 上个 commiter hash 值 ”

commiter name , commiter email +”\n”+timestamp

+ 空白行

+commit message

git add . 产生了什么

以后门路下所有子文件,(不是文件夹,除掉.git 目录和.gitignore 意外),对每个文件进行 hash , 要是不在 index, 生成 blob 类型文件,并记录在 index 文件中 (有着更新,其实更新就更新 文件门路和 hash 映射)

index 文件外面没有文件夹,都是目录。

git add . 当某个文件夹下 1.txt,没有文本内容,也没有 hash 值,记录分隔门路

这边文章里有解释 index 的组成部分

git commit 产生了什么

git commt 次要是生成 对应的 tree 对像,从以后的.git 根目录,每个目录对应一个 tree 对象(这里蕴含了 对象的长久化)

生成 commiter 对象(这里蕴含了 对象的长久化)。

参考的文章

git 官网文档 blob, tree commit 的形容 以及查看对象内容命令

python 写的 git

index 的组成部分

node.js 写的 git

实现的参考思路

git python 的其余实现

正文完
 0