阿里妹导读:经过不断地迭代,如今Git的功能越来越完善和强大。然而Git的第一个提交源码仅约1000行,当时的Git实现了哪些功能?本文将从源码开始,分析其核心思想,挖掘背后优秀的设计原理。
文末福利:《Python语言基础1:语法入门》技术公开课。
# 获取 git 源码
$ git clone https://github.com/git/git.git
# 查看第一个提交
$ git log --date-order --reverse
commit e83c5163316f89bfbde7d9ab23ca2e25604af290
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date: Thu Apr 7 15:13:13 2005 -0700
Initial revision of "git", the information manager from hell
# 变更为第一个提交,指定commit-id
$ git checkout e83c5163316f89bfbde7d9ab23ca2e25604af290
$ tree -h
.
├── [2.4K] cache.h
├── [ 503] cat-file.c # 查看objects文件
├── [4.0K] commit-tree.c # 提交tree
├── [1.2K] init-db.c # 初始化仓库
├── [ 970] Makefile
├── [5.5K] read-cache.c # 读取当前索引文件内容
├── [8.2K] README
├── [ 986] read-tree.c # 读取tree
├── [2.0K] show-diff.c # 查看diff内容
├── [5.3K] update-cache.c # 添加文件或目录
└── [1.4K] write-tree.c # 写入到tree
# 统计代码行数,总共1089行
$ find . "(" -name "*.c" -or -name "*.h" -or -name "Makefile" ")" -print | xargs wc -l
...
1089 total
$ git diff ./Makefile
...
-LIBS= -lssl
+LIBS= -lssl -lz -lcrypto
...
# 编译
$ make
Write programs that do one thing and do it well. ——Unix philosophy
init-db
# 运行init-db初始化仓库
$ init-db
defaulting to private storage area
# 查看初始化后的目录结构
$ tree . -a
.
└── .dircache # git工作目录
└── objects # objects文件
├── 00
├── 01
├── 02
├── ...... # 省略
├── fe
└── ff
258 directories, 0 files
$ update-cache <file> ...
# 新增README.md文件
$ echo "hello git" > README.md
# 提交
$ update-cache README.md
# 查看索引文件
$ hexdump -C .dircache/index
00000000 43 52 49 44 01 00 00 00 01 00 00 00 af a4 fc 8e |CRID............|
00000010 5e 34 9d dd 31 8b 4c 8e 15 ca 32 05 5a e9 a4 c8 |^4..1.L...2.Z...|
00000020 af bd 4c 5f bf fb 41 37 af bd 4c 5f bf fb 41 37 |..L_..A7..L_..A7|
00000030 00 03 01 00 91 16 d2 04 b4 81 00 00 ee 03 00 00 |................|
00000040 ee 03 00 00 0a 00 00 00 bb 12 25 52 ab 7b 40 20 |..........%R.{@ |
00000050 b5 f6 12 cc 3b bd d5 b4 3d 1f d3 a8 09 00 52 45 |....;...=.....RE|
00000060 41 44 4d 45 2e 6d 64 00 |ADME.md.|
00000068
# 查看objects内容,sha1值从索引文件中获取
$ cat-file bb122552ab7b4020b5f612cc3bbdd5b43d1fd3a8
temp_git_file_61uTTP: blob
$ cat ./temp_git_file_RwpU8b
hello git
cat-file <sha1>
cat-file 会把内容读取到temp_git_file_rLcGKX
cat-file 82f8604c3652fa5762899b5ff73eb37bef2da795
temp_git_file_tBTXFM: blob
# 查看 temp_git_file_tBTXFM 文件内容
cat ./temp_git_file_tBTXFM
hello git!
show-diff
创建文件并提交到暂存区
echo "hello git!" > README.md
update-cache README.md
# 当前无差异
show-diff
README.md: ok
# 更改README.md
echo "hello world!" > README.md
# 查看diff
show-diff
README.md: 82f8604c3652fa5762899b5ff73eb37bef2da795
--- - 2020-08-31 17:33:50.047881667 +0800
+++ README.md 2020-08-31 17:33:47.827740680 +0800
@@ -1 +1 @@
-hello git!
+hello world!
write-tree
提交
write-tree
c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
# 查看objects内容
cat-file c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
temp_git_file_r90ft5: tree
cat ./temp_git_file_r90ft5
100664 README.md��`L6R�Wb��_�>�{�-��
read-tree <sha1>
提交
write-tree
c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
# 读取tree对象
read-tree c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
100664 README.md (82f8604c3652fa5762899b5ff73eb37bef2da795)
$ commit-tree <sha1> [-p <sha1>]* < changelog
# 写入到tree
$ write-tree
c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
# 提交tree
$ echo "first commit" > changelog
$ commit-tree c771b3ab2fe3b7e43099290d3e99a3e8c414ec72 < changelog
Committing initial tree c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
7ea820bd363e24f5daa5de8028d77d88260503d9
# 查看commit对象内容
$ cat-file 7ea820bd363e24f5daa5de8028d77d88260503d9
temp_git_file_CIfJsg: commit
$ cat temp_git_file_CIfJsg
tree c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
author Xiaowen Xia <chenan.xxw@aos-hw09> Tue Sep 1 10:56:16 2020
committer Xiaowen Xia <chenan.xxw@aos-hw09> Tue Sep 1 10:56:16 2020
first commit
Write programs to work together.
——Unix philosophy
$ tree .git/objects
.git/objects
├── 02
│ └── 77ec89d7ba8c46a16d86f219b21cfe09a611e1
├── ...... # 省略
├── be
│ ├── adb5bac00c74c97da7f471905ab0da8b50229c
│ └── ee7b5e8ab6ae1c0c1f3cfa2c4643aacdb30b9b
├── ...... # 省略
├── c9
│ └── f6098f3ba06cf96e1248e9f39270883ba0e82e
├── ...... # 省略
├── cf
│ ├── 631abbf3c4cec0911cb60cc307f3dce4f7a000
│ └── 9e478ab3fc98680684cc7090e84644363a4054
├── ...... # 省略
└── ff
查看 blob 对象内容
cat-file 82f8604c3652fa5762899b5ff73eb37bef2da795temp_git_file_tBTXFM: blob
$ cat ./temp_git_file_tBTXFM
hello git!
查看 tree 对象内容
cat-file c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
temp_git_file_r90ft5: tree
$ cat ./temp_git_file_r90ft5
100664 README.md��`L6R�Wb��_�>�{�-��
# 查看 commit 对象内容
cat-file 7ea820bd363e24f5daa5de8028d77d88260503d9
temp_git_file_CIfJsg: commit
cat temp_git_file_CIfJsg
tree c771b3ab2fe3b7e43099290d3e99a3e8c414ec72
author Xiaowen Xia <chenan.xxw@aos-hw09> Tue Sep 1 10:56:16 2020
committer Xiaowen Xia <chenan.xxw@aos-hw09> Tue Sep 1 10:56:16 2020
first commit
$ hexdump -C .dircache/index
00000000 43 52 49 44 01 00 00 00 01 00 00 00 ae 73 c4 f2 |CRID.........s..|
00000010 ce 32 c9 6f 13 20 0d 56 9c e8 cf 0d d3 75 10 c8 |.2.o. .V.....u..|
00000020 94 ad 4c 5f f4 5c 42 06 94 ad 4c 5f f4 5c 42 06 |..L_.\B...L_.\B.|
00000030 00 03 01 00 91 16 d2 04 b4 81 00 00 ee 03 00 00 |................|
00000040 ee 03 00 00 0b 00 00 00 a3 f4 a0 66 c5 46 39 78 |...........f.F9x|
00000050 1e 30 19 a3 20 42 e3 82 84 ee 31 54 09 00 52 45 |.0.. B....1T..RE|
00000060 41 44 4d 45 2e 6d 64 00 |ADME.md.|
static int verify_hdr(struct cache_header *hdr, unsigned long size)
{
SHA_CTX c;
unsigned char sha1[20];
/* 省略 */
/* 计算索引文件头sha1值 */
SHA1_Init(&c);
SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
SHA1_Final(sha1, &c);
/* 省略 */
return 0;
}
Use software leverage to your advantage. ——Unix philosophy
参考资料
Git官方网站:https://git-scm.com Git官方文档中心:https://git-scm.com/doc Git官网的Git底层原理介绍:Git Internals - Git Objects zlib 官方网站:http://zlib.net 浅析Git存储—对象、打包文件及打包文件索引 (https://www.jianshu.com/p/923bf0485995) 深入理解Git - 一切皆commit (https://www.cnblogs.com/jasongrass/p/10582449.html) 深入理解Git - Git底层对象(https://www.cnblogs.com/jasongrass/p/10582465.html)
零基础学习
《Python语言基础1:语法入门》
本课程共58课时,从环境搭建开始,详细讲授Python的基本语法、数据类型、运算符、流程控制及简单的小游戏开发,带你零基础快速入门Python,掌握语言基础,感受Python的强大与便捷。
点击“阅读原文”,立即学习吧!