Slide 1

Slide 1 text

Git Internals for PHP developers

Slide 2

Slide 2 text

Magnus Nordlander • Symfony Consultant at Fervo • 10 years PHP experience, 6 years with Symfony • SensioLabs Certified Symfony Developer (expert level) • Main author of the Gittern library

Slide 3

Slide 3 text

The .git directory

Slide 4

Slide 4 text

The .git directory

Slide 5

Slide 5 text

The .git directory

Slide 6

Slide 6 text

The .git directory

Slide 7

Slide 7 text

The Git Object Model Id (sha) Author Commiter Author time Commit time Message Parents Tree Commit Id (sha) Nodes (with name and mode) Tree Id (sha) Contents Blob Git Objects (simplified)

Slide 8

Slide 8 text

Git Object Store • Stores git objects • Loose or packed • Loose objects are stored as separate compressed files, in dirs • Packed objects are stored in packfiles, huge binary files containing lots of objects

Slide 9

Slide 9 text

The .git directory

Slide 10

Slide 10 text

Git Index • Not stored in Object Store • Everything needed to create a tree • And then some! • The place Git stages your files when you do “git add”

Slide 11

Slide 11 text

Cool fact about Git • Git tries really hard to never lose track of your data. • Once your changes have been staged, they're safe • Even if you rebase, delete branches, or even rewrite history, everything is still there • At least until next garbage collection, that is

Slide 12

Slide 12 text

A git commit Tesla:Repo magnus$ git cat-file -p master tree 2218eca56c1d48313b7ead4a51f8daddba4dfa41 parent 508216179125fa04fdfe4dc8135e5bef78986237 author Magnus Nordlander 1383941897 +0100 committer Magnus Nordlander 1383941897 +0100 ! Added file list ! Tesla:Repo magnus$

Slide 13

Slide 13 text

A git tree Tesla:Repo magnus$ git cat-file -p 2218eca56c1d48313b7ead4a51f8daddba4dfa41 040000 tree 40f6a21f79b2bd607934dd587d76720b84715a97 Mac 100644 blob 4965253d28bad16c822eabc7cc424fa9cad571d1 filelist.txt 040000 tree 8a151447c6982522075993514e5fe9302e0d99d1 iOS Tesla:Repo magnus$

Slide 14

Slide 14 text

The reflog Tesla:Repo magnus$ git reflog show master 9136858 master@{0}: commit: Added file list 5082161 master@{1}: commit: Added iOS devices 71f6de4 master@{2}: commit: Added more Macs 30d35c6 master@{3}: commit: Added Macbook Pro Retina Tesla:Repo magnus$

Slide 15

Slide 15 text

The reflog Tesla:Repo magnus$ git reset --hard HEAD^ HEAD is now at 5082161 Added iOS devices Tesla:Repo magnus$ git reflog show master 5082161 master@{0}: reset: moving to HEAD^ 9136858 master@{1}: commit: Added file list 5082161 master@{2}: commit: Added iOS devices 71f6de4 master@{3}: commit: Added more Macs 30d35c6 master@{4}: commit: Added Macbook Pro Retina Tesla:Repo magnus$

Slide 16

Slide 16 text

When would I want this?

Slide 17

Slide 17 text

When do I want Git? • Unstructured, versionable data • E-butik.se uses it for storing customer-editable templates • Whenever you want to push things to a server, because it’s cool • (But this requires the git binary)

Slide 18

Slide 18 text

Gittern handles that* • Somewhat inspired (interaction-wise) by Doctrine 2 • Create Git objects like any other PHP object, persist them to the repo, flush your changes • Fetch objects, read them, get proxies when they’re not directly loaded * Or at least a lot of it

Slide 19

Slide 19 text

What is Gittern? • Git Library written in pure PHP, no binary required! • Almost complete abstraction of the Git object model • Created at e-butik.se, for storing templates

Slide 20

Slide 20 text

What is Gittern? • Also has Gaufrette adapters for Git repos • Easily extendable beyond “ordinary” Git • Cache objects in Redis, Memcache • Multiple indexes

Slide 21

Slide 21 text

Working with Gittern • Reading is easy • Objects are immutable, once they have a SHA • Clone them to get mutable copies (without a SHA) • But not using the clone keyword… • Do your changes, then persist into the repo and flush.

Slide 22

Slide 22 text

Better Yet, Use Gaufrette! • Two adapters available • GitternCommitishReadOnlyAdapter • GitternIndexAdapter • Abstracts object manipulations so Git almost seems like any other filesystem • Leaky abstraction, either read-only or manual commits.

Slide 23

Slide 23 text

Some code

Slide 24

Slide 24 text

Some code

Slide 25

Slide 25 text

What’s next? • More docs • More tests (We do have ~90% line coverage though) • Full object model coverage (annotated tags, symlinks and submodules) • Reflog • Full commitish parsing • Configuration • Remote?

Slide 26

Slide 26 text

Resources • https://github.com/e-butik/Gittern • Visit fervo.se • E-mail me: [email protected] • Twitter: @drrotmos