Class: Gitw::GitExe
- Inherits:
-
Object
- Object
- Gitw::GitExe
- 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
- #absolute_git_dir(git_options: nil) ⇒ Object
- #add(*files, git_options: nil, **options) ⇒ Object
-
#add_opts(options = nil) ⇒ Object
add.
- #clone(from, to, git_options: nil, **options) ⇒ Object
-
#clone_opts(options = nil) ⇒ Object
clone.
- #commit(*args, git_options: nil, **options) ⇒ Object
-
#commit_opts(options = nil) ⇒ Object
commit.
- #exec(*cmds, **opts) ⇒ Object
- #fetch(*args, git_options: nil, **options) ⇒ Object
-
#fetch_opts(options = nil) ⇒ Object
fetch.
- #git_bin ⇒ Object
- #git_dir(git_options: nil) ⇒ Object
- #git_opts(options = nil) ⇒ Object
- #init(*args, git_options: nil, **options) ⇒ Object
-
#init_opts(options = nil) ⇒ Object
init.
-
#initialize(options: nil, git_bin: nil, env: ENV) ⇒ GitExe
constructor
A new instance of GitExe.
- #is_bare_repository(git_options: nil) ⇒ Object
- #is_inside_git_dir(git_options: nil) ⇒ Object
- #is_inside_work_tree(git_options: nil) ⇒ Object
- #is_shallow_repository(git_options: nil) ⇒ Object
- #pull(*args, git_options: nil, **options) ⇒ Object
-
#pull_opts(options = nil) ⇒ Object
pull.
- #push(*args, git_options: nil, **options) ⇒ Object
-
#push_opts(options = nil) ⇒ Object
push.
- #remote(*args, git_options: nil, **options) ⇒ Object
-
#remote_opts(options = nil) ⇒ Object
remote.
- #remotes(git_options: nil) ⇒ Object
- #rev_parse(*args, git_options: nil, **options) ⇒ Object
-
#rev_parse_opts(options = nil) ⇒ Object
rev-parse.
- #status(*args, git_options: nil, **options) ⇒ Object
- #status_obj(*args, git_options: nil, **options) ⇒ Object
-
#status_opts(options = nil) ⇒ Object
status.
- #toplevel(git_options: nil) ⇒ Object
-
#version ⇒ Object
version.
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 = @git_bin = git_bin @env = env end |
Class Attribute Details
.git_bin ⇒ Object
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: , 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, **) = git_opts() = add_opts() exec(*, 'add', *, *files) end |
#add_opts(options = nil) ⇒ Object
add
170 171 172 173 174 |
# File 'lib/gitw/git_exe.rb', line 170 def add_opts( = nil) GitOpts.new .from(@options) .from() 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, **) = git_opts() = clone_opts() exec(*, 'clone', *, 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( = 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() 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, **) = git_opts() = commit_opts() exec(*, 'commit', *, *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( = nil) GitOpts.new .allow(:all, short: '-a') .allow(:message, short: '-m', long: '--message', with_arg: true) .from(@options) .from() 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, **) = git_opts() = fetch_opts() exec(*, 'fetch', *, *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( = nil) GitOpts.new .allow(:all, long: '--all') .allow(:tags, short: '-t', long: '--tags') .from(@options) .from() end |
#git_bin ⇒ Object
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_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( = 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() 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, **) = git_opts() = init_opts() exec(*, 'init', *, *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( = nil) GitOpts.new .allow(:quiet, short: '-q', long: '--quiet') .allow(:bare, long: '--bare') .allow(:init_branch, short: '-b', with_arg: true) .from(@options) .from() end |
#is_bare_repository(git_options: nil) ⇒ Object
139 140 141 142 |
# File 'lib/gitw/git_exe.rb', line 139 def (git_options: nil) status = rev_parse(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: , 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: , 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: , 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, **) = git_opts() = pull_opts() exec(*, 'pull', *, *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( = nil) GitOpts.new .allow(:all, long: '--all') .allow(:tags, short: '-t', long: '--tags') .from(@options) .from() 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, **) = git_opts() = push_opts() exec(*, 'push', *, *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( = nil) GitOpts.new .allow(:all, long: '--all') .allow(:mirror, long: '--mirror') .allow(:tags, long: '--tags') .from(@options) .from() 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, **) = git_opts() = remote_opts() exec(*, 'remote', *, *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( = nil) GitOpts.new .allow(:verbose, short: '-v') .from(@options) .from() 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: , 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, **) = git_opts() = rev_parse_opts() exec(*, 'rev-parse', *, *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( = 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() 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, **) = git_opts() = status_opts() exec(*, 'status', *, *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, **) Git::Status.parse(status(*args, git_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( = nil) GitOpts.new .allow(:porcelain, long: '--porcelain') .from(@options) .from() 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: , show_toplevel: nil) toplevel_dir.strip end |
#version ⇒ Object
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 |