Top Banner
Re-implementing git (a small part at least) Thibault Allançon November 2018 1
89

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:

Jun 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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

Page 89: 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:

Questions?

Thanks for listening!

Thibault Allanç[email protected]

haltode @ irc.freenode.net

37