Slide 1

Slide 1 text

MiniGit: Ruby API for the Git command Maciej Pasternacki WRUG, May 22, 2013 3ofcoins @mpasternacki

Slide 2

Slide 2 text

https://github.com/3ofcoins/minigit/ Ruby Git libraries – Ruby/Git – Grit – Rugged – EZGit

Slide 3

Slide 3 text

https://github.com/3ofcoins/minigit/ Ruby Git libraries – Ruby/Git – Grit – Rugged – EZGit }Repository abstractions

Slide 4

Slide 4 text

https://github.com/3ofcoins/minigit/ e Git API NAME git - the stupid content tracker SYNOPSIS git [--version] [--help] [-c =] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=] [--work-tree=] [--namespace=] []

Slide 5

Slide 5 text

https://github.com/3ofcoins/minigit/ e Git API $ git usage: git [--version] [--exec-path[=]] [--html-path] [--man-path] [--info- path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=] [--work-tree=] [--namespace=] [-c name=value] [--help] [] The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List, create, or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository diff Show changes between commits, commit and working tree, etc fetch Download objects and refs from another repository grep Print lines matching a pattern Create an empty git repository or reinitialize an existing one Show commit logs

Slide 6

Slide 6 text

https://github.com/3ofcoins/minigit/ 1.9.3(main) 1> require 'minigit' => true 1.9.3(main) 2> MiniGit => MiniGit

Slide 7

Slide 7 text

https://github.com/3ofcoins/minigit/ 1.9.3(main) 1> require 'minigit' => true 1.9.3(main) 2> MiniGit => MiniGit 1.9.3(main) 3> MiniGit.status # On branch master # Changes not staged for commit: # modified: Gemfile # modified: Rakefile # # Untracked files: # History.md no changes added to commit => true 1.9.3(main) 4> MiniGit::Capturing.status => "# On branch master\n# Changes not sta file\n#\tmodified: Rakefile\n#\n# Untra

Slide 8

Slide 8 text

https://github.com/3ofcoins/minigit/ => true 1.9.3(main) 2> MiniGit => MiniGit 1.9.3(main) 3> MiniGit.status # On branch master # Changes not staged for commit: # modified: Gemfile # modified: Rakefile # # Untracked files: # History.md no changes added to commit => true 1.9.3(main) 4> MiniGit::Capturing.status => "# On branch master\n# Changes not staged for commit:\n#\tmodified: Gem file\n#\tmodified: Rakefile\n#\n# Untracked files:\n#\tHistory.md\nno chan ges added to commit\n" 1.9.3(main) 5> MiniGit::Capturing.status.lines.map(&:strip) => ["# On branch master", "# Changes not staged for commit:", "#\tmodified: Gemfile", "#\tmodified: Rakefile", "#", "# Untracked files:", "#\tHistory.md", "no changes added to commit"] 1.9.3(main) 6>

Slide 9

Slide 9 text

https://github.com/3ofcoins/minigit/ # History.md no changes added to commit => true 1.9.3(main) 4> MiniGit::Capturing.status => "# On branch master\n# Changes not staged for commit:\n#\tmod file\n#\tmodified: Rakefile\n#\n# Untracked files:\n#\tHistory ges added to commit\n" 1.9.3(main) 5> MiniGit::Capturing.status.lines.map(&:strip) => ["# On branch master", "# Changes not staged for commit:", "#\tmodified: Gemfile", "#\tmodified: Rakefile", "#", "# Untracked files:", "#\tHistory.md", "no changes added to commit"] 1.9.3(main) 6>

Slide 10

Slide 10 text

https://github.com/3ofcoins/minigit/ MiniGit.status :s => true # git status -s MiniGit.status :short => true # git status --short MiniGit.log :n => 5, :grep => 'foo' # git log -n 5 --grep=foo MiniGit.log({:n => 5}, 'path/') # git log -n 5 path/

Slide 11

Slide 11 text

https://github.com/3ofcoins/minigit/ "#", "# Untracked files:", "#\tHistory.md", "no changes added to commit"] 1.9.3(main) 6> git = MiniGit.new => # 1.9.3(main) 7> git.branch develop * master => true 1.9.3(main) 8> git.capturing.branch => " develop\n* master\n" 1.9.3(main) 9> git = MiniGit::Capturing.n => # 1.9.3(main) 10> git.branch => " develop\n* master\n"

Slide 12

Slide 12 text

https://github.com/3ofcoins/minigit/ develop * master => true 1.9.3(main) 8> git.capturing.branch => " develop\n* master\n" 1.9.3(main) 9> git = MiniGit::Capturing.new => # 1.9.3(main) 10> git.branch => " develop\n* master\n" 1.9.3(main) 11> git.noncapturing.branch develop * master => true 1.9.3(main) 12>

Slide 13

Slide 13 text

https://github.com/3ofcoins/minigit/ from /Users/japhy/Projekty/minigit/lib/minigit.rb:75:in `block in 1.9.3(main) 21> 1.9.3(main) 21> git = MiniGit.new('../omnibus-ralph/Rakefile') => # 1.9.3(main) 22> git.log :n => 3, :oneline => true 9ba9a68 rake tidy 682671b Add some libraries, mostly PIL deps 3609724 Better isolation; dylib health check => true 1.9.3(main) 23>

Slide 14

Slide 14 text

https://github.com/3ofcoins/minigit/ But What For?

Slide 15

Slide 15 text

https://github.com/3ofcoins/minigit/ describe 'a rails app' do let(:git){MiniGit::Capturing.new(__FILE__)} it 'should not change db/schema.rb' do puts diff=git.diff('db/schema.rb') assert diff.empty?, 'db/schema.rb modified' end end

Slide 16

Slide 16 text

https://github.com/3ofcoins/minigit/ Keep local settings in .git/config, read them from Rake tasks

Slide 17

Slide 17 text

github.com/3ofcoins/vendorificator v-demo|master ⇒ git lg --color | cat * c633c6c (HEAD, master) Merge branch 'vendor/cookbooks/bluepill' |\ | * 2d7f57d (tag: vendor/cookbooks/bluepill/2.2.0, vendor/cookbooks/bluepill) Conjured cookbook bluepill version 2.2 .0 Origin: http://s3.amazonaws.com/ community-files.opscode.com/cookbook_versions/tarballs/2806/original/ bluepill.tgz ?1357674185 Checksum: d59ab8d1785264a06ebf89f08f377a25e98292ce10084d5c94cfcf5947b4cc61 * d24472c Merge branch 'vendor/cookbooks/runit' |\ | * 1c79974 (tag: vendor/cookbooks/runit/1.0.0, vendor/cookbooks/runit) Conjured cookbook runit version 1.0.0 Origin : http://s3.amazonaws.com/ community-files.opscode.com/cookbook_versions/tarballs/2959/original/ runit.tgz?1359413695 Checksum: 14f4f217fc7cd39cdcd83994437e199d65e5fe7401f9df5f1af2de825fa34bb3 * 50bfb63 Merge branch 'vendor/cookbooks/perl' |\ | * 37c0698 (tag: vendor/cookbooks/perl/1.1.0, vendor/cookbooks/perl) Conjured cookbook perl version 1.1.0 Origin: http://s3.amazonaws.com/community- files.opscode.com/cookbook_versions/tarballs/2652/original/perl.tgz?1355811654 Chec ksum: dedd691eed22df67d574b40595eb838ec8a67791399fe9cb7aa2fef9f3153dfd * 33ebf2d Merge branch 'vendor/cookbooks/xml' |\ | * aa2dac5 (tag: vendor/cookbooks/xml/1.1.2, vendor/cookbooks/xml) Conjured cookbook xml version 1.1.2 Origin: http ://s3.amazonaws.com/community- files.opscode.com/cookbook_versions/tarballs/2638/original/xml.tgz?1355781843 Checksum : 911cf6bce431288f0b58ff9be06662e588e634d29f0ee6f91e52ff384c07d420 …

Slide 18

Slide 18 text

https://github.com/3ofcoins/minigit/ # Public: Checks if the repository is clean. # # Returns boolean answer to the question. def clean? # copy code from http://stackoverflow.com/a/3879077/16390 git.update_index '-q', '--ignore-submodules', '--refresh' git.diff_files '--quiet', '--ignore-submodules', '--' git.diff_index '--cached', '--quiet', 'HEAD', '--ignore-submodules', '--' true rescue MiniGit::GitError false end

Slide 19

Slide 19 text

https://github.com/3ofcoins/minigit/ # Git helpers def remotes @remotes ||= git.capturing.remote.lines.map(&:strip) end def current_branch git.capturing.rev_parse({:abbrev_ref => true}, 'HEAD').strip end def fast_forwardable?(to, from) git.capturing.merge_base(to, from).strip == from end

Slide 20

Slide 20 text

https://github.com/3ofcoins/minigit/ tag = git.capturing.describe( { :exact_match => true, :match => _join(tag_name_base, '*') }, merged).strip tag unless tag.empty?

Slide 21

Slide 21 text

https://github.com/ 3ofcoins/minigit/