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

MiniGit: Ruby API for the Git command

MiniGit: Ruby API for the Git command

A short presentation on MiniGit, a Ruby library to programmatically run Git commands (https://github.com/3ofcoins/minigit/)

Maciej Pasternacki

May 22, 2013
Tweet

More Decks by Maciej Pasternacki

Other Decks in Programming

Transcript

  1. https://github.com/3ofcoins/minigit/ e Git API NAME git - the stupid content

    tracker SYNOPSIS git [--version] [--help] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] <command> [<args>]
  2. https://github.com/3ofcoins/minigit/ e Git API $ git usage: git [--version] [--exec-path[=<path>]]

    [--html-path] [--man-path] [--info- path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] [-c name=value] [--help] <command> [<args>] 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
  3. 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
  4. 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>
  5. 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>
  6. 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/
  7. https://github.com/3ofcoins/minigit/ "#", "# Untracked files:", "#\tHistory.md", "no changes added to

    commit"] 1.9.3(main) 6> git = MiniGit.new => #<MiniGit:0x007f81d2e6e190> 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 => #<MiniGit::Capturing:0x007f81d2eb7138> 1.9.3(main) 10> git.branch => " develop\n* master\n"
  8. 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 => #<MiniGit::Capturing:0x007f81d2eb7138> 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>
  9. 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') => #<MiniGit:0x007f81d2e55758 @git_dir="/Users/japhy/Projekty/omnibus-ralph/.git", @git_work_tree="/Users/japhy/Projekty/omnibus-ralph"> 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>
  10. 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
  11. 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 …
  12. 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
  13. 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
  14. https://github.com/3ofcoins/minigit/ tag = git.capturing.describe( { :exact_match => true, :match =>

    _join(tag_name_base, '*') }, merged).strip tag unless tag.empty?