Slide 1

Slide 1 text

動かして理解する Gitの内側 @natmark / Cookpad Inc. 2019.05.18 未来大×企業エンジニア 春のLT大会

Slide 2

Slide 2 text

始める前に 「動かして覚える」なので、手元にGitの実 行環境がある方はスライドを見ながら
 コマンドを叩くと面白いかも?
 (一緒に動かす場合は、gitとzlibを
 インストールしておいてください) 
 10分しかないので、LTは割と早口です

Slide 3

Slide 3 text

佐藤 敦也 さとう あつや クックパッド株式会社 買物事業部 エンジニア 未来大卒業生(2018年度) / 松原克弥研究室 !O@BUNBSL !OBUNBSL

Slide 4

Slide 4 text

Git使ったことありますか?"

Slide 5

Slide 5 text

Gitとは • 一言で言うと
 「分散型バージョン管理システム」 (使ったことない人が多かった時用の説明資料) 分散型の説明は省きます

Slide 6

Slide 6 text

バージョン管理とは (使ったことない人が多かった時用の説明資料)

Slide 7

Slide 7 text

バージョン管理とは (使ったことない人が多かった時用の説明資料) どれが最新なの…

Slide 8

Slide 8 text

Gitを使うと (使ったことない人が多かった時用の説明資料) フォルダには最新 のファイルだけ

Slide 9

Slide 9 text

Gitを使うと (使ったことない人が多かった時用の説明資料) 変更の履歴を 確認できる

Slide 10

Slide 10 text

Gitを使うと (使ったことない人が多かった時用の説明資料) 変更差分が見れる

Slide 11

Slide 11 text

Gitは差分情報を保存しておいて、 差分情報からバージョン管理が できる便利なやつ!!!

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

果たして、これは本当にそうなのだろうか?

Slide 14

Slide 14 text

動かして理解する Gitの内側 (ここから本番)

Slide 15

Slide 15 text

(ちなみに) .git の中を 覗いたことがある人"

Slide 16

Slide 16 text

Gitのリポジトリを作る $ mkdir git-inside-workshop $ cd git-inside-workshop $ git init Initialized empty Git repository in /Users/atsuya-sato/ Desktop/git-inside-workshop/.git/ $ ls -a . .. .git

Slide 17

Slide 17 text

.gitを覗いてみる $ cd .git $ ls -a . .. HEAD config description hooks info objects refs

Slide 18

Slide 18 text

.gitを覗いてみる $ cd .git $ ls -a . .. HEAD config description hooks info objects refs いろいろでてきた わからん!

Slide 19

Slide 19 text

.gitをGit管理する $ pwd /Users/atsuya-sato/Desktop/git-inside-workshop/.git $ git init Initialized empty Git repository in /Users/atsuya-sato/ Desktop/git-inside-workshop/.git/.git/

Slide 20

Slide 20 text

git initの生成物をCommit $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) HEAD config description hooks/ info/ nothing added to commit but untracked files present (use "git add" to track)

Slide 21

Slide 21 text

git initの生成物をCommit $ git add . $ git commit -m “Initial Commit” [master (root-commit) 7349f42] Initial Commit 15 files changed, 655 insertions(+) create mode 100644 HEAD create mode 100644 config create mode 100644 description create mode 100755 hooks/applypatch-msg.sample create mode 100755 hooks/commit-msg.sample create mode 100755 hooks/fsmonitor-watchman.sample create mode 100755 hooks/post-update.sample create mode 100755 hooks/pre-applypatch.sample create mode 100755 hooks/pre-commit.sample create mode 100755 hooks/pre-push.sample create mode 100755 hooks/pre-rebase.sample create mode 100755 hooks/pre-receive.sample create mode 100755 hooks/prepare-commit-msg.sample create mode 100755 hooks/update.sample create mode 100644 info/exclude

Slide 22

Slide 22 text

1. git addで 何が起きているのか

Slide 23

Slide 23 text

git addで何が起きているのか? $ echo “Hello World” > hello.txt On branch master Untracked files: (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track) $ git status $ pwd /Users/atsuya-sato/Desktop/git-inside-workshop/ $ cd ../

Slide 24

Slide 24 text

git addで何が起きているのか? $ git add hello.txt $ cd .git $ git status -uall On branch master Untracked files: (use "git add <file>..." to include in what will be committed) index objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 nothing added to commit but untracked files present (use "git add" to track)

Slide 25

Slide 25 text

git addで何が起きているのか? • hello.txtをgit addしたことによって ‣objects/ 55/7db03de997c86a4a028e1ebd3a1ceb22 5be238 ‣index • の2つのファイルが生成されたことが分かった

Slide 26

Slide 26 text

objects/ 55/7db03de997c86a4a028e1eb d3a1ceb225be238 とは一体なんなのだろうか

Slide 27

Slide 27 text

objects/の正体 • objects/ の正体はGitオブジェクトと呼ばれるもの • Gitオブジェクトのヘッダ部とデータ部に対してSHA-1の チェックサムを用いたファイル名で格納する(SHA-1ハッ シュの初めの2文字がサブディレクトリ・残りの38文字を ファイル名とする) ‣Gitオブジェクトのチェックサムが 557db03de997c86a4a028e1ebd3a1ceb225be238 
 の場合 ‣objects/55/7db03de997c86a4a028e1ebd3a1ceb
 225be238 
 に保存される

Slide 28

Slide 28 text

objects/の正体 • objects/ の正体はGitオブジェクトと呼ばれるもの • GitはGitオブジェクトとして以下のオブジェクト を生成する ‣blobオブジェクト ‣treeオブジェクト ‣commitオブジェクト(tagオブジェクト)

Slide 29

Slide 29 text

blobオブジェクト • ファイルのデータそのもの • ファイル名や属性は含まれない

Slide 30

Slide 30 text

treeオブジェクト • フォルダ構造を表す • treeオブジェクトは以下のようなものを持つ ‣ファイルの属性 ‣blobオブジェクト(サブフォルダが存在する場 合はtreeオブジェクト)への参照 ‣ファイル名

Slide 31

Slide 31 text

blobとtreeの関係 . ├── new.txt ├── test.txt └── bak └── test.txt

Slide 32

Slide 32 text

commitオブジェクト • git commitした時のCommit情報 • commitオブジェクトが持つものは ‣親commitへの参照 ‣トップレベルのtreeへの参照 ‣コミットしたユーザーの情報 ‣タイムスタンプ ‣コミットメッセージ

Slide 33

Slide 33 text

commitとblobとtreeの関係

Slide 34

Slide 34 text

objects/55/7db03de997c86a4a0
 28e1ebd3a1ceb225be238 を詳しく見てみる

Slide 35

Slide 35 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 を詳しく見てみる $ pwd /Users/atsuya-sato/Desktop/git-inside-workshop/.git $ cat objects/55/7db03de997c86a4a028e1ebd3
 a1ceb225be238 xKOR04bH/IAI%

Slide 36

Slide 36 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 を詳しく見てみる $ pwd /Users/atsuya-sato/Desktop/git-inside-workshop/.git $ cat objects/55/7db03de997c86a4a028e1ebd3
 a1ceb225be238 xKOR04bH/IAI% なんだこれは…

Slide 37

Slide 37 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 を詳しく見てみる $ cat objects/55/7db03de997c86a4a028e1ebd3a1
 ceb225be238 $ zlib -d < objects/55/7db03de997c86a4a028e1eb
 d3a1ceb225be238 xKOR04bH/IAI% blob 12Hello World

Slide 38

Slide 38 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 を詳しく見てみる $ cat objects/55/7db03de997c86a4a028e1ebd3a1
 ceb225be238 $ zlib -d < objects/55/7db03de997c86a4a028e1eb
 d3a1ceb225be238 xKOR04bH/IAI% blob 12Hello World 実はzlibで 圧縮されている 中身が出てきた

Slide 39

Slide 39 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 をさらに詳しく見てみる $ zlib -d < objects/55/7db03de997c86a4a028e1eb
 d3a1ceb225be238 | xxd 00000000: 626c 6f62 2031 3200 4865 6c6c 6f20 576f 00000010: 726c 640a blob 12.Hello Wo rld.

Slide 40

Slide 40 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 をさらに詳しく見てみる $ zlib -d < objects/55/7db03de997c86a4a028e1eb
 d3a1ceb225be238 | xxd 00000000: 626c 6f62 2031 3200 4865 6c6c 6f20 576f 00000010: 726c 640a blob 12.Hello Wo rld. 16進ダンプを 表示する

Slide 41

Slide 41 text

objects/ 55/7db03de997c86a4a028e1ebd3a1ceb225be238 をさらに詳しく見てみる 00000000: 62 6c 6f 62 20 31 32 00 48 65 6c 6c 6f 20 57 6f 00000010: 72 6c 64 0a b b l o (Space) 1 2 (NUL) H e l l o W o r l d (改行) (Space) • ASCIIコードと照らし合わせると… <オブジェクトタイプ> 0x20 <ファイルバイト数> 0x00 <データバイト> • オブジェクトのフォーマットは になっている!

Slide 42

Slide 42 text

ちなみに、Gitオブジェクトは git cat-file コマンドでもみることができる $ cd ../ /Users/atsuya-sato/Desktop/git-inside-workshop/.git $ git cat-file -t 557db03de997c86a4a028e1ebd3
 a1ceb225be238 $ pwd blob $ git cat-file -p 557d Hello World SHA-1ハッシュの 先頭4文字だけでも 名前解決できる $ git cat-file -s 557d 12

Slide 43

Slide 43 text

git addをするとblobオブジェクト が生成されることが分かった!

Slide 44

Slide 44 text

git addで何が起きているのか? • hello.txtをgit addしたことによって ‣objects/ 55/7db03de997c86a4a028e1ebd3a1ceb22 5be238 ‣index • の2つのファイルが生成されたことが分かった

Slide 45

Slide 45 text

indexとはなんなのか

Slide 46

Slide 46 text

indexとは何なのか • ステージング情報を保持してるバイナリファイ ル • コンフリクトした際の競合の情報などもindex が持っている • ステージング領域とは「次にコミットしたとき にコンテンツとして登録されるもの」

Slide 47

Slide 47 text

indexを見てみる…? $ cat .git/index | xxd /Users/atsuya-sato/Desktop/git-inside-workshop $ pwd 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 7990 bb91 d0e6 ff77 8de7 af5b 00000060: a6ea fb95 c5b9 643a DIRC........\.?. ._..\.?.._...... ................ ....U}.=...jJ... .:..”[.8..hello. txt......y.w...[ ......d:

Slide 48

Slide 48 text

indexを見てみる…? 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 7990 bb91 d0e6 ff77 8de7 af5b 00000060: a6ea fb95 c5b9 643a

Slide 49

Slide 49 text

indexを見てみる…? 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 7990 bb91 d0e6 ff77 8de7 af5b 00000060: a6ea fb95 c5b9 643a (時間があれば後で読み方を教えます)

Slide 50

Slide 50 text

indexを見てみる $ git ls-files -s 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 0 hello.txt • ls-files -sでステージング領域にキャッシュされているファイル の一覧を見ることができる • ステージフラグ: 普段は0、マージコンフリクトの解消(3-way マージ)の際にステージ1, 2, 3としてインデックスに保存 • indexファイルは他にもいろいろ情報を持っているが省略(後で 時間があれば紹介) FileMode (Permission) blobのSHA1ハッシュ ステージフラグ ファイルパス

Slide 51

Slide 51 text

2. git commitで 何が起きているのか

Slide 52

Slide 52 text

先にgit addで出来た.git/の
 生成物をコミットしておく $ cd .git On branch master Untracked files: (use "git add <file>..." to include in what will be committed) index objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 nothing added to commit but untracked files present (use "git add" to track) $ git status -uall $ git add . $ git commit -m “Exec git add hello.txt” /Users/atsuya-sato/Desktop/git-inside-workshop $ pwd

Slide 53

Slide 53 text

git commitで何が起きているのか $ cd ../ /Users/atsuya-sato/Desktop/git-inside-workshop/.git $ pwd $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: hello.txt $ git commit -m “Add hello.txt” [master (root-commit) 87a9891] Add hello.txt 1 file changed, 1 insertion(+) create mode 100644 hello.txt

Slide 54

Slide 54 text

git commitで何が起きているのか $ cd .git $ git status -uall On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: index Untracked files: (use "git add <file>..." to include in what will be committed) COMMIT_EDITMSG logs/HEAD logs/refs/heads/master objects/87/a989174c548a6a8b0e78a017d00db3a4e16d12 objects/97/b49d4c943e3715fe30f141cc6f27a8548cee0e refs/heads/master no changes added to commit (use "git add" and/or "git commit -a")

Slide 55

Slide 55 text

git commitで何が起きているのか $ cd .git $ git status -uall On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: index Untracked files: (use "git add <file>..." to include in what will be committed) COMMIT_EDITMSG logs/HEAD logs/refs/heads/master objects/87/a989174c548a6a8b0e78a017d00db3a4e16d12 objects/97/b49d4c943e3715fe30f141cc6f27a8548cee0e refs/heads/master no changes added to commit (use "git add" and/or "git commit -a") たくさんファイル が出てきた!!

Slide 56

Slide 56 text

git commitで 何が起きているのか? • hello.txtをgit addした後、git commitしたことによって ‣index • が更新された ‣COMMIT_EDITMSGS (説明省略) ‣logs/HEAD (説明省略) ‣logs/refs/heads/master (説明省略) ‣objects/87/a989174c548a6a8b0e78a017d00db3a4e16d12 ‣objects/97/b49d4c943e3715fe30f141cc6f27a8548cee0e ‣refs/heads/master • が生成された

Slide 57

Slide 57 text

git commitで生成された
 Gitオブジェクトを見てみる • git commitで追加された ‣objects/87/ a989174c548a6a8b0e78a017d00db 3a4e16d12 ‣objects/97/ b49d4c943e3715fe30f141cc6f27a8 548cee0e • を見てみる

Slide 58

Slide 58 text

git commitで生成された
 Gitオブジェクトを見てみる $ git cat-file -t 87a9 commit $ git cat-file -s 87a9 tree 97b49d4c943e3715fe30f141cc6f27a8548cee0e author atsuya-sato 1557809638 +0900 committer atsuya-sato 1557809638 +0900 Add hello.txt git commitで追加されたtreeオブジェクト(97b4)を ルートに持つcommitオブジェクトが生成されている $ cd ../ $ pwd /Users/atsuya-sato/Desktop/git-inside-workshop

Slide 59

Slide 59 text

git commitで生成された
 Gitオブジェクトを見てみる $ git cat-file -t 97b4 tree $ git cat-file -s 97b4 100644 blob 557db03de997c86a4a028e1ebd3a1ceb225be238 hello.txt git addで追加されたhello.txtのblobオブジェクト (557d)を持つtreeオブジェクト(97b4)が生成されている

Slide 60

Slide 60 text

git commitで 何が起きているのか? • hello.txtをgit addした後、git commitしたことによって ‣index • が更新された ‣COMMIT_EDITMSGS (説明省略) ‣logs/HEAD (説明省略) ‣logs/refs/heads/master (説明省略) ‣objects/87/a989174c548a6a8b0e78a017d00db3a4e16d12 ‣objects/97/b49d4c943e3715fe30f141cc6f27a8548cee0e ‣refs/heads/master • が生成された

Slide 61

Slide 61 text

refs/heads/masterをみる $ cat .git/refs/heads/master 87a989174c548a6a8b0e78a017d00db3a4e16d12 commitオブジェクトのSHA-1ハッシュを持ってい ることが分かる • refs/heads/masterはmasterブランチのHEAD コミットへの参照を持っている

Slide 62

Slide 62 text

git commitで 何が起きているのか? • hello.txtをgit addした後、git commitしたことによって ‣index • が更新された ‣COMMIT_EDITMSGS (説明省略) ‣logs/HEAD (説明省略) ‣logs/refs/heads/master (説明省略) ‣objects/87/a989174c548a6a8b0e78a017d00db3a4e16d12 ‣objects/97/b49d4c943e3715fe30f141cc6f27a8548cee0e ‣refs/heads/master • が生成された

Slide 63

Slide 63 text

indexを見てみる $ cat .git/index | xxd 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 5452 4545 0000 0019 0031 2030 00000060: 0a97 b49d 4c94 3e37 15fe 30f1 41cc 6f27 00000070: a854 8cee 0e94 aef3 b413 ed22 4737 8e4b 00000080: 95c5 ea68 cafa 4937 f4 DIRC........\.?. ._..\.?.._...... ................ ....U}.=...jJ... .:.."[.8..hello. txt.TREE.....1 0 ....L.>7..0.A.o' .T........."G7.K ...h..I7.

Slide 64

Slide 64 text

indexを見てみる 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 5452 4545 0000 0019 0031 2030 00000060: 0a97 b49d 4c94 3e37 15fe 30f1 41cc 6f27 00000070: a854 8cee 0e94 aef3 b413 ed22 4737 8e4b 00000080: 95c5 ea68 cafa 4937 f4 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 7990 bb91 d0e6 ff77 8de7 af5b 00000060: a6ea fb95 c5b9 643a • git add後 • git commit後 85バイト目からバイナリ が変化していることが分かる

Slide 65

Slide 65 text

indexをみる • git commitによってindexに追加されたのは、 Cached treeという、Treeの状態を表す情報 • Cached treeは、現在のGit Workspace内全て のtreeの名前・treeのSHA-1ハッシュ・ subtreeの数・tree内のblobの数を持つ

Slide 66

Slide 66 text

indexをみる • https://github.com/yoichi/git-cat-index • というツールを使うと、indexをいい感じ に見ることができる DIRC (dircache), 1 entries 557db03de997c86a4a028e1ebd3a1ceb225be238 (stage:0) 100644 hello.txt TREE 97b49d4c943e3715fe30f141cc6f27a8548cee0e (0/1) $ git_cat_index.py .git/index これがCached tree

Slide 67

Slide 67 text

3. gitは本当に差分を保存 しているのか?

Slide 68

Slide 68 text

先にgit commitで出来た.git/の
 生成物をコミットしておく $ cd .git $ git add . $ git commit -m “Exec git commit” /Users/atsuya-sato/Desktop/git-inside-workshop $ pwd

Slide 69

Slide 69 text

hello.txtを編集してadd & commitする $ cd ../ $ git add hello.txt $ echo “Hello FUN” > hello.txt $ cat hello.txt Hello FUN Hello Worldを Hello FUNに編集 $ git commit -m “Update hello.txt” [master 0741a4a] Update hello.txt 1 file changed, 1 insertion(+), 1 deletion(-) commitオブジェクト
 (0741)ができた

Slide 70

Slide 70 text

.gitの変更を見る $ cd .git $ git status -uall On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: COMMIT_EDITMSG modified: index modified: logs/HEAD modified: logs/refs/heads/master modified: refs/heads/master Untracked files: (use "git add <file>..." to include in what will be committed) objects/07/41a4a634a5c550e59ed013275ebea2b5bedef6 objects/2d/ccf803893c8e418bdaa03f0c4af005517f8e88 objects/70/2e500c6260d7caaf75f266ac27eb8215108f76 一番上がcommit オブジェクト

Slide 71

Slide 71 text

commitオブジェクト
 の中身を見る $ git cat-file -p 0741 tree 702e500c6260d7caaf75f266ac27eb8215108f76 parent 87a989174c548a6a8b0e78a017d00db3a4e16d12 author atsuya-sato 1557813718 +0900 committer atsuya-sato 1557813718 +0900 Update hello.txt $ cd ../ (702e)というtreeオブジェクトができているのが分かる

Slide 72

Slide 72 text

treeオブジェクト
 の中身を見る $ git cat-file -p 702e 100644 blob 2dccf803893c8e418bdaa03f0c4af005517f8e88 hello.txt 先ほどとblobオブジェクトのSHA-1 ハッシュが変わっているのが分かる

Slide 73

Slide 73 text

blobオブジェクト
 の中身を見る $ cd .git $ zlib -d < objects/55/7db03de997c86a4a028e1eb
 d3a1ceb225be238 blob 12Hello World $ zlib -d < objects/2d/ccf803893c8e418bdaa03f0 c4af005517f8e88 blob 10Hello FUN

Slide 74

Slide 74 text

blobオブジェクト
 の中身を見る • 実はGitが保存しているのは差分ではなく、「スナップ ショット」 • blobに関してはファイルデータそのものを保存する • オブジェクトファイルに対してSHA-1で生成した checksumをファイル名にしているので、ファイルに変 更がなければ、新たなblobオブジェクトは作成されない • treeが持つsubtreeやblobが更新されると、新たなtreeオ ブジェクトが作成される

Slide 75

Slide 75 text

Gitはスナップショットを保存しておいて、commitオブ ジェクトとcommitに紐づくtree/blobの生成によって
 バージョン管理をしている

Slide 76

Slide 76 text

おまけ1: Gitの作り方

Slide 77

Slide 77 text

Gitの作り方 • Gitの内側を学ぶ • .gitをgit管理して挙動を見る • Gitの内側(https://git-scm.com/ book/ja/v1/ Git%E3%81%AE%E5%86%85%E 5%81%B4)を見る

Slide 78

Slide 78 text

Gitの作り方 • Write yourself a Git
 (https://wyag.thb.lt/)をやる • Gitの作り方がPythonのソースコード付 きで説明されている(ただし英語) • 上の記事を見るとinit, hash-object, cat- file, log, ls-tree, show-ref, tag, rev- parseが作れる

Slide 79

Slide 79 text

自分もSwiftで作ってみた https://github.com/natmark/Gift Write yourself a Gitのコマンド + add, commit
 まで実装した

Slide 80

Slide 80 text

おまけ2: 紹介しなかった ディレクトリ

Slide 81

Slide 81 text

紹介しなかったファイル ・description gitwebがリポジトリーの情報を表示するのに使用 ・config git config —localするときに読み書きされてるのがここ ・info/ info/excludeというファイルがあり、コミット対象外にしたいファイルを記述するのに 使用(.gitignoreと異なり、他人と共有されない) ・hooks/ Gitの処理の各フェーズごとに自動的に実行されるスクリプトを設定するのに使用 (例: pre-push) ・refs/ branchやtagが指しているコミットへの参照を保存

Slide 82

Slide 82 text

おまけ3: indexの読み方

Slide 83

Slide 83 text

indexを読む 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 5452 4545 0000 0019 0031 2030 00000060: 0a97 b49d 4c94 3e37 15fe 30f1 41cc 6f27 00000070: a854 8cee 0e94 aef3 b413 ed22 4737 8e4b 00000080: 95c5 ea68 cafa 4937 f4 DIRC (dircache), 1 entries 557db03de997c86a4a028e1ebd3a1ceb225be238 (stage:0) 100644 hello.txt TREE 97b49d4c943e3715fe30f141cc6f27a8548cee0e (0/1)

Slide 84

Slide 84 text

indexを読む 00000000: 4449 5243 0000 0002 0000 0001 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 5452 4545 0000 0019 0031 2030 00000060: 0a97 b49d 4c94 3e37 15fe 30f1 41cc 6f27 00000070: a854 8cee 0e94 aef3 b413 ed22 4737 8e4b 00000080: 95c5 ea68 cafa 4937 f4

Slide 85

Slide 85 text

indexを読む • Header 00000000: 4449 5243 0000 0002 0000 0001 Signature Format Entries D I R C (Dircache) 2 Indexファイルの
 フォーマット 1 Stageされてる ファイルの数

Slide 86

Slide 86 text

• Index entry(1/2) indexを読む 00000000: 5cda 3fb1 00000010: 195f eaed 5cda 3fb1 195f eaed 0100 0004 00000020: 008c a4c2 0000 81a4 0000 01f6 0000 0014 00000030: 0000 000c created_at sec created_at msec updated_at msec updated_at sec deviceID inode mode uid gid file size 12 bytes 9217218 9217218 502 20 16777220 1557807025 425716461 1557807025 425716461

Slide 87

Slide 87 text

00000030: 557d b03d e997 c86a 4a02 8e1e 00000040: bd3a 1ceb 225b e238 0009 6865 6c6c 6f2e 00000050: 7478 7400 • Index entry(2/2) indexを読む SHA-1 Hash SHA-1 Hash 557db03de997c86a4a028e1e bd3a1ceb225be238 Flag File path File path zero padding hello. txt Flag: 0009!0000000000001001 assume valid flag(1bit) | extended flag(1bit) | stageFlag(2bit) | nameLength(12bit)

Slide 88

Slide 88 text

• Cached tree indexを読む 00000050: 5452 4545 0000 0019 0031 2030 00000060: 0a97 b49d 4c94 3e37 15fe 30f1 41cc 6f27 00000070: a854 8cee 0e SHA-1 Hash SHA-1 Hash (LF) (Space) (NUL) Signature T R E E size 25 bytes 1 0 index entries count subtrees 
 count 97b49d4c943e3715fe30f141cc6f27 root tree以外は(LF)の後に ディレクトリ名が来る a8548cee0e

Slide 89

Slide 89 text

00000070: 94 aef3 b413 ed22 4737 8e4b 00000080: 95c5 ea68 cafa 4937 f4 • Checksum indexを読む • 117バイト目までのバイナリデータの SHA-1ハッシュ SHA-1 Hash SHA-1 Hash 94aef3b413ed2247378e4b 95c5ea68cafa4937f4

Slide 90

Slide 90 text

indexの詳しい仕様 SHA-1 Hash https://github.com/git/git/blob/master/ Documentation/technical/index-format.txt