Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git Internals for PHP Developers

Git Internals for PHP Developers

Presentation about Git Internals and the Gittern library given at the Symfony Sverige meet-up.

Magnus Nordlander

November 12, 2013
Tweet

More Decks by Magnus Nordlander

Other Decks in Programming

Transcript

  1. 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
  2. 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)
  3. 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
  4. 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”
  5. 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
  6. A git commit Tesla:Repo magnus$ git cat-file -p master tree

    2218eca56c1d48313b7ead4a51f8daddba4dfa41 parent 508216179125fa04fdfe4dc8135e5bef78986237 author Magnus Nordlander <[email protected]> 1383941897 +0100 committer Magnus Nordlander <[email protected]> 1383941897 +0100 ! Added file list ! Tesla:Repo magnus$
  7. 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$
  8. 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$
  9. 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$
  10. 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)
  11. 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
  12. 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
  13. What is Gittern? • Also has Gaufrette adapters for Git

    repos • Easily extendable beyond “ordinary” Git • Cache objects in Redis, Memcache • Multiple indexes
  14. 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.
  15. 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.
  16. 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?