class DeltaTest::Git
Attributes
Public Class Methods
# File lib/delta_test/git.rb, line 9 def initialize(dir) @dir = Pathname.new(dir) end
Public Instance Methods
Add a path to the index
@return {Boolean}
# File lib/delta_test/git.rb, line 146 def add(path) _, _, s = exec(%q{add %s}, path.to_s) s.success? end
Get list of modified files in diff
@params {String} base @params {String} head
@return {Array<String>}
# File lib/delta_test/git.rb, line 85 def changed_files(base = 'master', head = 'HEAD', path: '.') o, _, s = exec(%q{diff --name-only -z %s %s %s}, base, head, path) s.success? ? o.split("\x0") : [] end
Create commit
@return {Boolean}
# File lib/delta_test/git.rb, line 156 def commit(message) _, _, s = exec(%q{commit -m %s}, message.to_s) s.success? end
Get name of the current branch
@return {String}
# File lib/delta_test/git.rb, line 50 def current_branch o, _, s = exec(%q{symbolic-ref --short HEAD}) s.success? ? o.strip : nil end
Util for executing command
# File lib/delta_test/git.rb, line 164 def exec(subcommand, *args) command = [ 'git', '--git-dir=%s', '--no-pager', subcommand, ].join(' ') args.unshift(@dir.join('.git')) args.map! { |a| a.is_a?(String) ? Shellwords.escape(a) : a } Open3.capture3(command % args, chdir: @dir) end
Check if in git managed directory
@return {Boolean}
# File lib/delta_test/git.rb, line 18 def git_repo? _, _, s = exec(%q{rev-parse --is-inside-work-tree}) rescue [] !!s && s.success? end
Check if it has a remote origin
@return {String}
# File lib/delta_test/git.rb, line 117 def has_remote? !!remote_url rescue false end
Get file list from git index
@return {Array<String>}
# File lib/delta_test/git.rb, line 72 def ls_files(path: '.') o, _, s = exec(%q{ls-files -z %s}, path) s.success? ? o.split("\x0") : [] end
Get list of hashes for the last N commits
@params {Integer} n
@return {Array<String>}
# File lib/delta_test/git.rb, line 97 def ls_hashes(n) o, _, s = exec(%q{log -z -n %d --format='%%H'}, n.to_i) s.success? ? o.split("\x0") : [] end
Pull from the origin master
@return {Boolean}
# File lib/delta_test/git.rb, line 126 def pull _, _, s = exec(%q{pull origin master}) s.success? end
Push to the origin master
@return {Boolean}
# File lib/delta_test/git.rb, line 136 def push _, _, s = exec(%q{push origin master}) s.success? end
Get url for the remote origin
@return {String}
# File lib/delta_test/git.rb, line 107 def remote_url o, _, s = exec(%q{config --get remote.origin.url}) s.success? ? o.strip : nil end
Get commit id from rev name
@params {String} rev - e.g., branch name
@return {String}
# File lib/delta_test/git.rb, line 40 def rev_parse(rev) o, _, s = exec(%q{rev-parse %s}, rev) s.success? ? o.strip : nil end
Get root directory of git
@return {String}
# File lib/delta_test/git.rb, line 28 def root_dir o, _, s = exec(%q{rev-parse --show-toplevel}) s.success? ? o.strip : nil end
Compare two rev names by their commit ids
@params {String} r1 @params {String} r2
@return {Boolean}
# File lib/delta_test/git.rb, line 63 def same_commit?(r1, r2) rev_parse(r1) == rev_parse(r2) end