re-implementing git · re-implementing git (a small part at least) thibault allançon november 2018...
TRANSCRIPT
![Page 1: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/1.jpg)
Re-implementing git(a small part at least)
Thibault AllançonNovember 2018
1
![Page 2: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/2.jpg)
Motivation
• Learning git inner workings
• Learning a new programming language: Rust
« What I cannot create, I donot understand »
— Richard Feynman
2
![Page 3: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/3.jpg)
Motivation
• Learning git inner workings• Learning a new programming language: Rust
2
![Page 4: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/4.jpg)
Disclaimers
• Needs some basic git knowledge• Skips over some implementation details
• Hard to fit everything into one slide• Not necessary to understand the core mechanics
3
![Page 5: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/5.jpg)
Table of contents
1. Git internals
2. Basic commands
3. Branches
4
![Page 6: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/6.jpg)
Git internals
![Page 7: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/7.jpg)
Fundamentals
Snapshots, not differences
repo/.git/
HEADobjects/refs/
heads/remotes/
...
5
![Page 8: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/8.jpg)
Fundamentals
Snapshots, not differences
repo/.git/
HEADobjects/refs/
heads/remotes/
...
5
![Page 9: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/9.jpg)
Git internals
Git objects
![Page 10: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/10.jpg)
Objects types
git has 3 kinds∗ of objects:
• blob
: stores binary data
• tree
: list of blobs, or other trees
• commit
: snapshot’s metadatas
file1 file2 file3
dir2
dir1
commit
6
![Page 11: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/11.jpg)
Objects types
git has 3 kinds∗ of objects:
• blob: stores binary data• tree
: list of blobs, or other trees
• commit
: snapshot’s metadatas
file1 file2 file3
dir2
dir1
commit
file1:Hello World!
file2:This is a file.
file3:some file content
6
![Page 12: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/12.jpg)
Objects types
git has 3 kinds∗ of objects:
• blob: stores binary data• tree: list of blobs, or other trees• commit
: snapshot’s metadatas
file1 file2 file3
dir2
dir1
commit
dir1:blob file3tree dir2
dir2:blob file1blob file2
6
![Page 13: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/13.jpg)
Objects types
git has 3 kinds∗ of objects:
• blob: stores binary data• tree: list of blobs, or other trees• commit: snapshot’s metadatas
file1 file2 file3
dir2
dir1
commit
commit:tree dir1author John Doe <[email protected]> timecommitter John Doe <[email protected]> time
here is the commit message6
![Page 14: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/14.jpg)
Objects types
git has 3 kinds∗ of objects:
• blob: stores binary data• tree: list of blobs, or other trees• commit: snapshot’s metadatas
file1 file2 file3
dir2
dir1
commit
ImportantObjects are uniquely identified with a 40-hexdigit SHA-1 hash.
6
![Page 15: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/15.jpg)
Objects storage
Every object is stored following this format:
• header: "obj_type data_len"
• null byte• object data
The location of the object is defined as:.git/objects/hash[..2]/hash[2..]
NoteObjects are compressed when stored.
7
![Page 16: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/16.jpg)
Objects storage
Every object is stored following this format:
• header: "obj_type data_len"
• null byte• object data
The location of the object is defined as:.git/objects/hash[..2]/hash[2..]
NoteObjects are compressed when stored.
7
![Page 17: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/17.jpg)
Objects storage
Every object is stored following this format:
• header: "obj_type data_len"
• null byte• object data
The location of the object is defined as:.git/objects/hash[..2]/hash[2..]
NoteObjects are compressed when stored.
7
![Page 18: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/18.jpg)
git hash-object
Our first plumbing command!
hash-object: data, type, writeheader = (type, space byte, data.len())object = (header, null byte, data)hash = SHA-1(object)if write
path = hash[..2]/hash[2..]compress objectwrite object to .git/objects/path
return hash
8
![Page 19: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/19.jpg)
Git internals
The index
![Page 20: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/20.jpg)
Git workflow
working directoryrepo/
staging area.git/index
local repo.git/
git add
git commit
9
![Page 21: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/21.jpg)
Index storage
The index is a binary file (in .git/index) storing blobs list for nextcommit
• header: DIRC2 nb_entries
• entry: file metadata, file size, hash, flags, path
• index file checksum
NoteEntries are sorted by path.
Two new plumbing commands: read_index, write_index.
10
![Page 22: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/22.jpg)
Index storage
The index is a binary file (in .git/index) storing blobs list for nextcommit
• header: DIRC2 nb_entries
• entry: file metadata, file size, hash, flags, path
• index file checksum
NoteEntries are sorted by path.
Two new plumbing commands: read_index, write_index.
10
![Page 23: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/23.jpg)
Index storage
The index is a binary file (in .git/index) storing blobs list for nextcommit
• header: DIRC2 nb_entries
• entry: file metadata, file size, hash, flags, path
• index file checksum
NoteEntries are sorted by path.
Two new plumbing commands: read_index, write_index.
10
![Page 24: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/24.jpg)
Index storage
The index is a binary file (in .git/index) storing blobs list for nextcommit
• header: DIRC2 nb_entries
• entry: file metadata, file size, hash, flags, path
• index file checksum
NoteEntries are sorted by path.
Two new plumbing commands: read_index, write_index.
10
![Page 25: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/25.jpg)
Recap
• 3 kinds of objects: blob, tree, commit
• All objects are stored in the same way, and identified using aunique 40-hexdigit hash
• The index is a list of blobs which will be used for the nextcommit
Practice time!
11
![Page 26: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/26.jpg)
Recap
• 3 kinds of objects: blob, tree, commit• All objects are stored in the same way, and identified using a
unique 40-hexdigit hash
• The index is a list of blobs which will be used for the nextcommit
Practice time!
11
![Page 27: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/27.jpg)
Recap
• 3 kinds of objects: blob, tree, commit• All objects are stored in the same way, and identified using a
unique 40-hexdigit hash• The index is a list of blobs which will be used for the next
commit
Practice time!
11
![Page 28: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/28.jpg)
Recap
• 3 kinds of objects: blob, tree, commit• All objects are stored in the same way, and identified using a
unique 40-hexdigit hash• The index is a list of blobs which will be used for the next
commit
Practice time!
11
![Page 29: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/29.jpg)
Basic commands
![Page 30: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/30.jpg)
Basic commands
git init
![Page 31: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/31.jpg)
git init
$ mkdir repo$ cd repo$ git initInitialized empty Git repository
repo/.git/
HEADobjects/refs/
heads/remotes/
...
12
![Page 32: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/32.jpg)
git init
$ mkdir repo$ cd repo$ git initInitialized empty Git repository
repo/.git/
HEADobjects/refs/
heads/remotes/
...
12
![Page 33: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/33.jpg)
git init
$ mkdir repo$ cd repo$ git initInitialized empty Git repository
repo/.git/
HEADobjects/refs/
heads/remotes/
...
init creates the .git directory (duh.)
12
![Page 34: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/34.jpg)
Basic commands
git add
![Page 35: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/35.jpg)
git add
Documentationadd files to the index
working directoryrepo/
staging area.git/index
local repo.git/
git add
git commit
13
![Page 36: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/36.jpg)
git add
Documentationadd files to the index
add: filesentries = read_index()for each files
create new index entryadd it to entries list
sort entrieswrite_index(entries)
13
![Page 37: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/37.jpg)
Basic commands
git status
![Page 38: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/38.jpg)
git status
Documentationfiles with differences between the working dir and the index
status:index = read_index()files = work_dir_files()for each files
if file.path in indexhash = hash-object(file, "blob")if hash != entry.hash
"modified"else
"new"for each index entry
if entry.path != all files path"deleted"
14
![Page 39: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/39.jpg)
Basic commands
git diff
![Page 40: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/40.jpg)
git diff
Documentationchanges between the working dir and the index
Far from being a trivial problem!
15
![Page 41: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/41.jpg)
git diff
Documentationchanges between the working dir and the index
Far from being a trivial problem!
15
![Page 42: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/42.jpg)
Diff example
void func1() {x += 1
}
void func2() {x += 2
}
void func1() {x += 1
}
void functhreehalves() {x += 1.5
}
void func2() {x += 2
}
16
![Page 43: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/43.jpg)
git diff - bad
void func1() {x += 1
}
- void func2() {+ void functhreehalves() {- x += 2+ x += 1.5
}++ void func2() {+ x += 2+ }
17
![Page 44: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/44.jpg)
git diff - good
void func1() {x += 1
+ }++ void functhreehalves() {+ x += 1.5
}
void func2() {x += 2
}
18
![Page 45: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/45.jpg)
git diff - better
void func1() {x += 1
}
+ void functhreehalves() {+ x += 1.5+ }+
void func2() {x += 2
}
19
![Page 46: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/46.jpg)
git diff
git diff --diff-algorithm=
• myers (default): the basic greedy diff algorithm• minimal: get the smallest possible diff• patience: try to get more meaningful diff• histogram: mainly used for its speed
Most diff algorithms are LCS-based (longest common subsequence)
20
![Page 47: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/47.jpg)
git diff
diff: pathsindex = read_index()for each paths
stored_file = get_index_entry(path)stored_obj = get_object(stored_file.hash)current_data = read_file(path)
print LCS_diff(stored_obj.data, current_data)
21
![Page 48: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/48.jpg)
Basic commands
git commit
![Page 49: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/49.jpg)
git commit
Documentationstores the index content as a new commit object
working directoryrepo/
staging area.git/index
local repo.git/
git add
git commit
write-tree: create a tree object from the current index
22
![Page 50: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/50.jpg)
git commit
Documentationstores the index content as a new commit object
working directoryrepo/
staging area.git/index
local repo.git/
git add
git commit
write-tree: create a tree object from the current index
22
![Page 51: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/51.jpg)
git write-tree
write-tree:entries = []index = read_index()for each index entries
parse entry infoappend new tree entry to entries list
hash = hash-object(entries, "tree", write=True)return hash
23
![Page 52: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/52.jpg)
git commit
commit: messagetree_hash = write-tree()content =
"tree tree_hashauthor author_name timecommitter committer_name timemessage"
hash = hash-object(content, "commit", write=True)return hash
24
![Page 53: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/53.jpg)
git commit - history
Great, we have snapshots...
...but we need a stream of snapshots
commit 1 commit 2 commit 3 commit 4
25
![Page 54: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/54.jpg)
git commit - history
Great, we have snapshots...
...but we need a stream of snapshots
commit 1 commit 2 commit 3 commit 4
25
![Page 55: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/55.jpg)
git commit
commit: messagetree_hash = write-tree()
+ parent_hash = get current commit (HEAD)content =
"tree tree_hash+ parent parent_hash
author author_name timecommitter committer_name timemessage"
hash = hash-object(content, "commit", write=True)+ update HEAD
return hash
26
![Page 56: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/56.jpg)
Recap
• git add is merely about adding a new line to the index
• git status/diff compares working dir and the index• git commit creates two new objects: a tree (based on the
index), and a commit
Practice time!
27
![Page 57: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/57.jpg)
Recap
• git add is merely about adding a new line to the index• git status/diff compares working dir and the index
• git commit creates two new objects: a tree (based on theindex), and a commit
Practice time!
27
![Page 58: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/58.jpg)
Recap
• git add is merely about adding a new line to the index• git status/diff compares working dir and the index• git commit creates two new objects: a tree (based on the
index), and a commit
Practice time!
27
![Page 59: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/59.jpg)
Recap
• git add is merely about adding a new line to the index• git status/diff compares working dir and the index• git commit creates two new objects: a tree (based on the
index), and a commit
Practice time!
27
![Page 60: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/60.jpg)
Branches
![Page 61: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/61.jpg)
Fundamentals
A branch is simply a lightweight movable pointer to a commit.
Problem: remembering commit’s hash is hard.
Solution: use files with simple names, containing the hash, andrefer to those files intead.
These are called references and are stored under:.git/refs/heads/
28
![Page 62: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/62.jpg)
Fundamentals
A branch is simply a lightweight movable pointer to a commit.
Problem: remembering commit’s hash is hard.
Solution: use files with simple names, containing the hash, andrefer to those files intead.
These are called references and are stored under:.git/refs/heads/
28
![Page 63: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/63.jpg)
Fundamentals
A branch is simply a lightweight movable pointer to a commit.
Problem: remembering commit’s hash is hard.
Solution: use files with simple names, containing the hash, andrefer to those files intead.
These are called references and are stored under:.git/refs/heads/
28
![Page 64: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/64.jpg)
Fundamentals
A branch is simply a lightweight movable pointer to a commit.
Problem: remembering commit’s hash is hard.
Solution: use files with simple names, containing the hash, andrefer to those files intead.
These are called references and are stored under:.git/refs/heads/
28
![Page 65: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/65.jpg)
Branches
git branch
![Page 66: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/66.jpg)
git branch
Documentationcreate a new branch
branch: namecheck if repo has at least 1 commitget current commit hash (HEAD)write the hash to .git/refs/heads/name
29
![Page 67: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/67.jpg)
Branches
git checkout
![Page 68: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/68.jpg)
HEAD file
commit 1 commit 2 commit 3
master
HEAD
HEAD
master is checked out
30
![Page 69: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/69.jpg)
HEAD file
commit 1 commit 2 commit 3
master
HEAD
HEAD
master is checked out
$ cat .git/HEADref: refs/heads/master
If the HEAD is pointing to a branch,it will not contain the commit hash,but a symlink to the branch
30
![Page 70: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/70.jpg)
HEAD file
commit 1 commit 2 commit 3
master
HEAD
HEAD
HEAD is detached
30
![Page 71: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/71.jpg)
HEAD file
commit 1 commit 2 commit 3
master
HEAD
HEAD
HEAD is detached
$ cat .git/HEADb445e58e2ada96566ec4966bd202c59ef1c2bdb7
30
![Page 72: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/72.jpg)
git checkout
Documentationswitch to a branch
checkout: refcheck if ref is a commit objectcompare trees between ref commit and HEAD commitfor each diff
add/modify/delete the fileupdate indexupdate HEAD
31
![Page 73: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/73.jpg)
Branches
git merge
![Page 74: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/74.jpg)
Case 1: fast-forward
commit 1 commit 2 commit 3
master
32
![Page 75: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/75.jpg)
Case 1: fast-forward
$ git branch fix_issue$ git checkout fix_issueSwitched to branch 'fix_issue'
commit 1 commit 2 commit 3
master
fix_issue
32
![Page 76: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/76.jpg)
Case 1: fast-forward
...$ git commit -m "commit 4"[fix_issue 538cfab] commit 4
commit 1 commit 2 commit 3
master
commit 4
fix_issue
32
![Page 77: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/77.jpg)
Case 1: fast-forward
$ git checkout masterSwitched to branch 'master'$ git merge fix_issueFast-forward
commit 1 commit 2 commit 3 commit 4
fix_issue
master
32
![Page 78: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/78.jpg)
Case 2: non fast-forward
commit 1 commit 2 commit 3 commit 4
master
fix_issue
commit 5
commit 4
fix_issue
33
![Page 79: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/79.jpg)
Case 2: non fast-forward
$ git checkout master...$ git commit -m "commit 5"[master 6600e17] commit 5
commit 1 commit 2 commit 3
master
commit 5
commit 4
fix_issue
33
![Page 80: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/80.jpg)
Case 2: non fast-forward
$ git merge fix_issueMerge made by the 'recursive' strategy.
commit 1 commit 2 commit 3
commit 5
commit 4
fix_issue
merge
master
33
![Page 81: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/81.jpg)
git merge
merge: refcheck if ref is a commit object// Case 1: fast-forwardif HEAD is ancestor of ref
update working dir from refupdate HEAD
// Case 2: non fast-forwardelse
get diffs from common ancestorupdate working dirupdate HEADif conflicts
"Need to resolve conflicts"else
commit("Merge ... into ...")
34
![Page 82: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/82.jpg)
Recap
• branches are simple files containing a hash
• HEAD can point to a branch, or a specific commit• fast-forward merge (easy one)• non fast-forward merge: use common ancestors
Practice time!
35
![Page 83: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/83.jpg)
Recap
• branches are simple files containing a hash• HEAD can point to a branch, or a specific commit
• fast-forward merge (easy one)• non fast-forward merge: use common ancestors
Practice time!
35
![Page 84: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/84.jpg)
Recap
• branches are simple files containing a hash• HEAD can point to a branch, or a specific commit• fast-forward merge (easy one)
• non fast-forward merge: use common ancestors
Practice time!
35
![Page 85: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/85.jpg)
Recap
• branches are simple files containing a hash• HEAD can point to a branch, or a specific commit• fast-forward merge (easy one)• non fast-forward merge: use common ancestors
Practice time!
35
![Page 86: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/86.jpg)
Recap
• branches are simple files containing a hash• HEAD can point to a branch, or a specific commit• fast-forward merge (easy one)• non fast-forward merge: use common ancestors
Practice time!
35
![Page 87: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/87.jpg)
Conclusion
![Page 88: Re-implementing git · Re-implementing git (a small part at least) Thibault Allançon November 2018 1. Motivation • Learning git inner workings • Learning a new programming language:](https://reader036.vdocument.in/reader036/viewer/2022070815/5f0f31f77e708231d442f52f/html5/thumbnails/88.jpg)
Resources
• https://git-scm.com/book/en/v2
• https://git-scm.com/docs
• https://matthew-brett.github.io/curious-git/
https://github.com/haltode/gitrs: the full implementation
36