Class: DeltaTest::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/delta_test/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Git

Returns a new instance of Git.



9
10
11
# File 'lib/delta_test/git.rb', line 9

def initialize(dir)
  @dir = Pathname.new(dir)
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



7
8
9
# File 'lib/delta_test/git.rb', line 7

def dir
  @dir
end

Instance Method Details

#add(path) ⇒ Boolean

Add a path to the index

Returns:

  • (Boolean)


136
137
138
139
# File 'lib/delta_test/git.rb', line 136

def add(path)
  _, _, s = exec(%q{add %s}, path.to_s)
  s.success?
end

#changed_files(base = 'master', head = 'HEAD', path: '.') ⇒ Array<String>

Get list of modified files in diff

Returns:

  • (Array<String>)


75
76
77
78
# File 'lib/delta_test/git.rb', line 75

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) ⇒ Boolean

Create commit

Returns:

  • (Boolean)


146
147
148
149
# File 'lib/delta_test/git.rb', line 146

def commit(message)
  _, _, s = exec(%q{commit -m %s}, message.to_s)
  s.success?
end

#exec(subcommand, *args) ⇒ Object

Util for executing command



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/delta_test/git.rb', line 154

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?Boolean

Check if in git managed directory

Returns:

  • (Boolean)


18
19
20
21
# 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?String

Check if it has a remote origin

Returns:

  • (String)


107
108
109
# File 'lib/delta_test/git.rb', line 107

def has_remote?
  !!remote_url rescue false
end

#ls_files(path: '.') ⇒ Array<String>

Get file list from git index

Returns:

  • (Array<String>)


62
63
64
65
# File 'lib/delta_test/git.rb', line 62

def ls_files(path: '.')
  o, _, s = exec(%q{ls-files -z %s}, path)
  s.success? ? o.split("\x0") : []
end

#ls_hashes(n) ⇒ Array<String>

Get list of hashes for the last N commits

Returns:

  • (Array<String>)


87
88
89
90
# File 'lib/delta_test/git.rb', line 87

def ls_hashes(n)
  o, _, s = exec(%q{log -z -n %d --format='%%H'}, n.to_i)
  s.success? ? o.split("\x0") : []
end

#pullBoolean

Pull from the origin master

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/delta_test/git.rb', line 116

def pull
  _, _, s = exec(%q{pull origin master})
  s.success?
end

#pushBoolean

Push to the origin master

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/delta_test/git.rb', line 126

def push
  _, _, s = exec(%q{push origin master})
  s.success?
end

#remote_urlString

Get url for the remote origin

Returns:

  • (String)


97
98
99
100
# File 'lib/delta_test/git.rb', line 97

def remote_url
  o, _, s = exec(%q{config --get remote.origin.url})
  s.success? ? o.strip : nil
end

#rev_parse(rev) ⇒ String

Get commit id from rev name

Returns:

  • (String)


40
41
42
43
# 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_dirString

Get root directory of git

Returns:

  • (String)


28
29
30
31
# 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) ⇒ Boolean

Compare two rev names by their commit ids

Returns:

  • (Boolean)


53
54
55
# File 'lib/delta_test/git.rb', line 53

def same_commit?(r1, r2)
  rev_parse(r1) == rev_parse(r2)
end