class DeltaTest::Git

Attributes

dir[R]

Public Class Methods

new(dir) click to toggle source
# File lib/delta_test/git.rb, line 9
def initialize(dir)
  @dir = Pathname.new(dir)
end

Public Instance Methods

add(path) click to toggle source

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
changed_files(base = 'master', head = 'HEAD', path: '.') click to toggle source

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
commit(message) click to toggle source

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
current_branch() click to toggle source

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
exec(subcommand, *args) click to toggle source

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
git_repo?() click to toggle source

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
has_remote?() click to toggle source

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
ls_files(path: '.') click to toggle source

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
ls_hashes(n) click to toggle source

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() click to toggle source

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() click to toggle source

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
remote_url() click to toggle source

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
rev_parse(rev) click to toggle source

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
root_dir() click to toggle source

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
same_commit?(r1, r2) click to toggle source

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