Class: DeltaTest::Git
- Inherits:
-
Object
- Object
- DeltaTest::Git
- Defined in:
- lib/delta_test/git.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
Instance Method Summary collapse
-
#add(path) ⇒ Boolean
Add a path to the index.
-
#changed_files(base = 'master', head = 'HEAD', path: '.') ⇒ Array<String>
Get list of modified files in diff.
-
#commit(message) ⇒ Boolean
Create commit.
-
#exec(subcommand, *args) ⇒ Object
Util for executing command.
-
#git_repo? ⇒ Boolean
Check if in git managed directory.
-
#has_remote? ⇒ String
Check if it has a remote origin.
-
#initialize(dir) ⇒ Git
constructor
A new instance of Git.
-
#ls_files(path: '.') ⇒ Array<String>
Get file list from git index.
-
#ls_hashes(n) ⇒ Array<String>
Get list of hashes for the last N commits.
-
#pull ⇒ Boolean
Pull from the origin master.
-
#push ⇒ Boolean
Push to the origin master.
-
#remote_url ⇒ String
Get url for the remote origin.
-
#rev_parse(rev) ⇒ String
Get commit id from rev name.
-
#root_dir ⇒ String
Get root directory of git.
-
#same_commit?(r1, r2) ⇒ Boolean
Compare two rev names by their commit ids.
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
#dir ⇒ Object (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
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
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
146 147 148 149 |
# File 'lib/delta_test/git.rb', line 146 def commit() _, _, s = exec(%q{commit -m %s}, .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
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
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
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
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 |
#pull ⇒ Boolean
Pull from the origin master
116 117 118 119 |
# File 'lib/delta_test/git.rb', line 116 def pull _, _, s = exec(%q{pull origin master}) s.success? end |
#push ⇒ Boolean
Push to the origin master
126 127 128 129 |
# File 'lib/delta_test/git.rb', line 126 def push _, _, s = exec(%q{push origin master}) s.success? end |
#remote_url ⇒ String
Get url for the remote origin
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
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_dir ⇒ String
Get root directory of git
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
53 54 55 |
# File 'lib/delta_test/git.rb', line 53 def same_commit?(r1, r2) rev_parse(r1) == rev_parse(r2) end |