Class: Gitw::GitExe

Inherits:
Object
  • Object
show all
Defined in:
lib/gitw/git_exe.rb

Overview

git executable wrapper

Constant Summary collapse

DEFAULT_GIT_BIN =

git_bin

'git'

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: nil, git_bin: nil, env: ENV) ⇒ GitExe

Returns a new instance of GitExe.



14
15
16
17
18
# File 'lib/gitw/git_exe.rb', line 14

def initialize(options: nil, git_bin: nil, env: ENV)
  @options = options
  @git_bin = git_bin
  @env = env
end

Class Attribute Details

.git_binObject



279
280
281
# File 'lib/gitw/git_exe.rb', line 279

def git_bin
  @git_bin ||= DEFAULT_GIT_BIN
end

Instance Method Details

#absolute_git_dir(git_options: nil) ⇒ Object



124
125
126
127
# File 'lib/gitw/git_exe.rb', line 124

def absolute_git_dir(git_options: nil)
  dir = rev_parse(git_options: git_options, absolute_git_dir: nil)
  dir.strip
end

#add(*files, git_options: nil, **options) ⇒ Object



176
177
178
179
180
181
# File 'lib/gitw/git_exe.rb', line 176

def add(*files, git_options: nil, **options)
  add_git_options = git_opts(git_options)
  add_options = add_opts(options)

  exec(*add_git_options, 'add', *add_options, *files)
end

#add_opts(options = nil) ⇒ Object

add



170
171
172
173
174
# File 'lib/gitw/git_exe.rb', line 170

def add_opts(options = nil)
  GitOpts.new
         .from(@options)
         .from(options)
end

#clone(from, to, git_options: nil, **options) ⇒ Object



86
87
88
89
90
# File 'lib/gitw/git_exe.rb', line 86

def clone(from, to, git_options: nil, **options)
  clone_git_options = git_opts(git_options)
  clone_options = clone_opts(options)
  exec(*clone_git_options, 'clone', *clone_options, from, to)
end

#clone_opts(options = nil) ⇒ Object

clone



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gitw/git_exe.rb', line 74

def clone_opts(options = nil)
  GitOpts.new
         .allow(:verbose, short: '-v', long: '--verbose')
         .allow(:quiet, short: '-q', long: '--quiet')
         .allow(:no_checkout, long: '--no-checkout')
         .allow(:bare, long: '--bare')
         .allow(:mirror, long: '--mirror')
         .allow(:depth, long: '--depth', with_arg: true)
         .from(@options)
         .from(options)
end

#commit(*args, git_options: nil, **options) ⇒ Object



193
194
195
196
197
198
# File 'lib/gitw/git_exe.rb', line 193

def commit(*args, git_options: nil, **options)
  commit_git_options = git_opts(git_options)
  commit_options = commit_opts(options)

  exec(*commit_git_options, 'commit', *commit_options, *args)
end

#commit_opts(options = nil) ⇒ Object

commit



185
186
187
188
189
190
191
# File 'lib/gitw/git_exe.rb', line 185

def commit_opts(options = nil)
  GitOpts.new
         .allow(:all, short: '-a')
         .allow(:message, short: '-m', long: '--message', with_arg: true)
         .from(@options)
         .from(options)
end

#exec(*cmds, **opts) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/gitw/git_exe.rb', line 24

def exec(*cmds, **opts)
  result = Exec.new(git_bin, *cmds, **opts).exec
  result.raise_on_failure(Gitw::GitError)
  result.stdout
rescue StandardError => e
  raise Gitw::GitError, e.to_s
end

#fetch(*args, git_options: nil, **options) ⇒ Object



210
211
212
213
214
215
# File 'lib/gitw/git_exe.rb', line 210

def fetch(*args, git_options: nil, **options)
  fetch_git_options = git_opts(git_options)
  fetch_options = fetch_opts(options)

  exec(*fetch_git_options, 'fetch', *fetch_options, *args)
end

#fetch_opts(options = nil) ⇒ Object

fetch



202
203
204
205
206
207
208
# File 'lib/gitw/git_exe.rb', line 202

def fetch_opts(options = nil)
  GitOpts.new
         .allow(:all, long: '--all')
         .allow(:tags, short: '-t', long: '--tags')
         .from(@options)
         .from(options)
end

#git_binObject



20
21
22
# File 'lib/gitw/git_exe.rb', line 20

def git_bin
  @git_bin || @env['GIT_BIN'] || self.class.git_bin
end

#git_dir(git_options: nil) ⇒ Object



114
115
116
117
# File 'lib/gitw/git_exe.rb', line 114

def git_dir(git_options: nil)
  git_dir = rev_parse(git_options: git_options, git_dir: nil)
  git_dir.strip
end

#git_opts(options = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gitw/git_exe.rb', line 32

def git_opts(options = nil)
  GitOpts.new
         .allow(:c, short: '-c', with_arg: true, multiple: true)
         .allow(:git_dir, long: '--git-dir', with_arg: true)
         .allow(:work_tree, long: '--work-tree', with_arg: true)
         .allow(:dir, short: '-C', with_arg: true)
         .allow(:version, short: '-v', long: '--version')
         .add(:c, 'core.quotePath=true')
         .add(:c, 'color.ui=false')
         .from(@options)
         .from(options)
end

#init(*args, git_options: nil, **options) ⇒ Object



66
67
68
69
70
# File 'lib/gitw/git_exe.rb', line 66

def init(*args, git_options: nil, **options)
  init_git_options = git_opts(git_options)
  init_options = init_opts(options)
  exec(*init_git_options, 'init', *init_options, *args)
end

#init_opts(options = nil) ⇒ Object

init



57
58
59
60
61
62
63
64
# File 'lib/gitw/git_exe.rb', line 57

def init_opts(options = nil)
  GitOpts.new
         .allow(:quiet, short: '-q', long: '--quiet')
         .allow(:bare, long: '--bare')
         .allow(:init_branch, short: '-b', with_arg: true)
         .from(@options)
         .from(options)
end

#is_bare_repository(git_options: nil) ⇒ Object



139
140
141
142
# File 'lib/gitw/git_exe.rb', line 139

def is_bare_repository(git_options: nil)
  status = rev_parse(git_options: git_options, is_bare_repository: nil)
  status.strip.downcase == 'true'
end

#is_inside_git_dir(git_options: nil) ⇒ Object



129
130
131
132
# File 'lib/gitw/git_exe.rb', line 129

def is_inside_git_dir(git_options: nil)
  status = rev_parse(git_options: git_options, is_inside_git_dir: nil)
  status.strip.downcase == 'true'
end

#is_inside_work_tree(git_options: nil) ⇒ Object



134
135
136
137
# File 'lib/gitw/git_exe.rb', line 134

def is_inside_work_tree(git_options: nil)
  status = rev_parse(git_options: git_options, is_inside_work_tree: nil)
  status.strip.downcase == 'true'
end

#is_shallow_repository(git_options: nil) ⇒ Object



144
145
146
147
# File 'lib/gitw/git_exe.rb', line 144

def is_shallow_repository(git_options: nil)
  status = rev_parse(git_options: git_options, is_shallow_repository: nil)
  status.strip.downcase == 'true'
end

#pull(*args, git_options: nil, **options) ⇒ Object



227
228
229
230
231
232
# File 'lib/gitw/git_exe.rb', line 227

def pull(*args, git_options: nil, **options)
  pull_git_options = git_opts(git_options)
  pull_options = pull_opts(options)

  exec(*pull_git_options, 'pull', *pull_options, *args)
end

#pull_opts(options = nil) ⇒ Object

pull



219
220
221
222
223
224
225
# File 'lib/gitw/git_exe.rb', line 219

def pull_opts(options = nil)
  GitOpts.new
         .allow(:all, long: '--all')
         .allow(:tags, short: '-t', long: '--tags')
         .from(@options)
         .from(options)
end

#push(*args, git_options: nil, **options) ⇒ Object



245
246
247
248
249
250
# File 'lib/gitw/git_exe.rb', line 245

def push(*args, git_options: nil, **options)
  push_git_options = git_opts(git_options)
  push_options = push_opts(options)

  exec(*push_git_options, 'push', *push_options, *args)
end

#push_opts(options = nil) ⇒ Object

push



236
237
238
239
240
241
242
243
# File 'lib/gitw/git_exe.rb', line 236

def push_opts(options = nil)
  GitOpts.new
         .allow(:all, long: '--all')
         .allow(:mirror, long: '--mirror')
         .allow(:tags, long: '--tags')
         .from(@options)
         .from(options)
end

#remote(*args, git_options: nil, **options) ⇒ Object



261
262
263
264
265
266
# File 'lib/gitw/git_exe.rb', line 261

def remote(*args, git_options: nil, **options)
  remote_git_options = git_opts(git_options)
  remote_options = remote_opts(options)

  exec(*remote_git_options, 'remote', *remote_options, *args)
end

#remote_opts(options = nil) ⇒ Object

remote



254
255
256
257
258
259
# File 'lib/gitw/git_exe.rb', line 254

def remote_opts(options = nil)
  GitOpts.new
         .allow(:verbose, short: '-v')
         .from(@options)
         .from(options)
end

#remotes(git_options: nil) ⇒ Object



268
269
270
# File 'lib/gitw/git_exe.rb', line 268

def remotes(git_options: nil)
  Git::RemoteRefs.parse(remote(git_options: git_options, verbose: true))
end

#rev_parse(*args, git_options: nil, **options) ⇒ Object



108
109
110
111
112
# File 'lib/gitw/git_exe.rb', line 108

def rev_parse(*args, git_options: nil, **options)
  rev_parse_git_options = git_opts(git_options)
  rev_parse_options = rev_parse_opts(options)
  exec(*rev_parse_git_options, 'rev-parse', *rev_parse_options, *args)
end

#rev_parse_opts(options = nil) ⇒ Object

rev-parse



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gitw/git_exe.rb', line 94

def rev_parse_opts(options = nil)
  GitOpts.new
         .allow(:git_dir, long: '--git-dir')
         .allow(:git_common_dir, long: '--git-common-dir')
         .allow(:show_toplevel, long: '--show-toplevel')
         .allow(:absolute_git_dir, long: '--absolute-git-dir')
         .allow(:is_inside_git_dir, long: '--is-inside-git-dir')
         .allow(:is_inside_work_tree, long: '--is-inside-work-tree')
         .allow(:is_bare_repository, long: '--is-bare-repository')
         .allow(:is_shallow_repository, long: '--is-shallow-repository')
         .from(@options)
         .from(options)
end

#status(*args, git_options: nil, **options) ⇒ Object



158
159
160
161
162
# File 'lib/gitw/git_exe.rb', line 158

def status(*args, git_options: nil, **options)
  status_git_options = git_opts(git_options)
  status_options = status_opts(options)
  exec(*status_git_options, 'status', *status_options, *args)
end

#status_obj(*args, git_options: nil, **options) ⇒ Object



164
165
166
# File 'lib/gitw/git_exe.rb', line 164

def status_obj(*args, git_options: nil, **options)
  Git::Status.parse(status(*args, git_options: git_options, **options.merge(porcelain: true)))
end

#status_opts(options = nil) ⇒ Object

status



151
152
153
154
155
156
# File 'lib/gitw/git_exe.rb', line 151

def status_opts(options = nil)
  GitOpts.new
         .allow(:porcelain, long: '--porcelain')
         .from(@options)
         .from(options)
end

#toplevel(git_options: nil) ⇒ Object



119
120
121
122
# File 'lib/gitw/git_exe.rb', line 119

def toplevel(git_options: nil)
  toplevel_dir = rev_parse(git_options: git_options, show_toplevel: nil)
  toplevel_dir.strip
end

#versionObject

version



47
48
49
50
51
52
53
# File 'lib/gitw/git_exe.rb', line 47

def version
  version_git_opts = git_opts.add(:version)
  version = exec(*version_git_opts)
  version.strip
rescue StandardError
  nil
end