Class: Squared::Workspace::Project::Git
- Extended by:
- Rake::DSL
- Includes:
- Format
- Defined in:
- lib/squared/workspace/project/git.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
#group, #name, #path, #project, #theme, #warning, #workspace
Class Method Summary collapse
Instance Method Summary collapse
- #checkout(flag, files = [], branch: nil, create: nil, commit: nil, detach: nil) ⇒ Object
- #commit(flag, files = [], message: nil, pass: false) ⇒ Object
- #diff(flag, files = [], branch: nil, index: 0) ⇒ Object
- #fetch(flag = nil, opts: []) ⇒ Object
- #files(flag, grep: nil) ⇒ Object
- #populate ⇒ Object
- #pull(flag = nil, sync: invoked_sync?('pull', flag), opts: []) ⇒ Object
- #rebase ⇒ Object
- #refs(flag, grep: nil) ⇒ Object
- #reset(flag, files = [], ref: nil) ⇒ Object
- #restore(flag, files) ⇒ Object
- #rev(flag, ref: nil, size: nil) ⇒ Object
- #stash(flag = :push, files = [], commit: nil) ⇒ Object
- #status ⇒ Object
Methods inherited from Base
as_path, #base_path, #build, #build?, #clean, #clean?, #copy, #copy?, #depend, #depend?, #dev?, #doc, #doc?, #enabled?, #has?, #initialize, #initialize_build, #initialize_logger, #initialize_script, #inspect, #log, ref, #refresh, #refresh?, #test, #test?, to_s, #to_s, #to_sym
Constructor Details
This class inherits a constructor from Squared::Workspace::Project::Base
Class Method Details
.is_a?(val) ⇒ Boolean
40 41 42 43 44 45 46 |
# File 'lib/squared/workspace/project/git.rb', line 40 def is_a?(val) if (val = as_path(val)) val.join('.git').directory? else super end end |
.populate(workspace, parallel: []) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/squared/workspace/project/git.rb', line 16 def populate(workspace, parallel: [], **) return if workspace.series.pull.empty? desc 'all[git?=rebase|stash]' task 'all', [:git] do |_, args| sync = ->(key) { parallel.include?(key) ? :"#{key}:sync" : key } pull = case args.git when 'rebase' sync.(:rebase) when 'stash' invoke sync.(:stash) sync.(:pull) else sync.(:pull) end Common::Task.invoke(pull, exception: workspace.exception) Common::Task.invoke(workspace.dev? ? :refresh : :build, exception: workspace.exception) end end |
.tasks ⇒ Object
36 37 38 |
# File 'lib/squared/workspace/project/git.rb', line 36 def tasks %i[pull rebase fetch stash status].freeze end |
Instance Method Details
#checkout(flag, files = [], branch: nil, create: nil, commit: nil, detach: nil) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/squared/workspace/project/git.rb', line 292 def checkout(flag, files = [], branch: nil, create: nil, commit: nil, detach: nil) cmd = git_session 'checkout' case flag when :branch cmd << '--detach' if detach == 'd' || option('detach') if create cmd << "-#{create}" << branch if (val = option('start-point')) cmd << val end else cmd << branch end cmd << commit when :detach cmd << "--#{flag}" << commit else cmd << "--#{flag}" append_ours append_head append_pathspec files end source end |
#commit(flag, files = [], message: nil, pass: false) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/squared/workspace/project/git.rb', line 378 def commit(flag, files = [], message: nil, pass: false) ||= option('message', 'm', zero: false) amend = flag.to_s.start_with?('amend') if ! && !amend return if pass raise_error('commit', 'GIT_MESSAGE="description"', hint: 'missing') end pathspec = if flag == :all || (amend && files.size == 1 && files.first == '*') '--all' else files = source_path(as_a(files)) raise_error('commit', 'pathspec', hint: 'missing') if files.empty? "-- #{files.join(' ')}" end if !push? source('git branch -vv --list', io: true, banner: false)[0].each do |val| origin = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val) next unless origin @origin = origin[1] @branch = origin[2] break end end raise_error('commit', 'work tree is not usable') unless push? cmd = git_session 'commit' cmd << '--dry-run' if option('dry-run') if amend cmd << '--amend' else cmd.delete('--amend') end if elsif flag == :'amend-orig' || option('no-edit') cmd << '--no-edit' end a = ['git add --verbose'] b = ['git push'] if dry_run? a << '--dry-run' b << '--dry-run' end a << pathspec b << '--force' if amend b << @origin << @branch puts if pass source a.join(' ') source source b.join(' ') end |
#diff(flag, files = [], branch: nil, index: 0) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/squared/workspace/project/git.rb', line 344 def diff(flag, files = [], branch: nil, index: 0) cmd = git_session 'diff' unless flag == :files if /^#[0-9a-f]{5,40}\#$/.match?(sha = files.first) sha = sha[1..-2] files.shift else sha = nil end end if (val = option('unified')).to_i > 0 cmd << "--unified=#{val}" end append_nocolor case flag when :cached cmd << '--cached' cmd << '--merge-base' if option('merge-base') when :branch cmd << branch when :files cmd << '--no-index' else if (val = option('index')) || index > 0 cmd << "HEAD~#{val || index}" elsif sha && option('merge-base') cmd << '--merge-base' end end cmd << sha append_pathspec(files, pass: flag == :files) source(exception: cmd.include?('--exit-code')) end |
#fetch(flag = nil, opts: []) ⇒ Object
221 222 223 224 225 226 |
# File 'lib/squared/workspace/project/git.rb', line 221 def fetch(flag = nil, opts: []) cmd = git_session 'fetch' cmd << '--all' if flag == :all || option('all') append_pull opts, OPT_FETCH, flag source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !workspace.series.multiple?) end |
#files(flag, grep: nil) ⇒ Object
336 337 338 339 340 341 342 |
# File 'lib/squared/workspace/project/git.rb', line 336 def files(flag, grep: nil) git_session 'ls-files', " --#{flag}" out, = source(io: true) print_item ret = write_lines(out, grep: grep) list_result(ret, 'files', grep: grep) end |
#populate ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/squared/workspace/project/git.rb', line 63 def populate(*) super return unless gitdir.exist? && !@exclude.include?(Git.ref) namespace name do @@tasks[Git.ref].each do |action, flags| namespace action do flags.each do |flag| case action when :pull desc format_desc(action, flag, OPT_PULL) task flag, [:opts] do |_, args| pull(flag, opts: args.to_a) end when :fetch desc format_desc(action, flag, OPT_FETCH) task flag, [:opts] do |_, args| fetch(flag, opts: args.to_a) end when :commit, :restore if flag == :all desc format_desc(action, flag, 'message?') task flag, [:message] do |_, args| commit(flag, message: args.fetch(:message, nil)) end else desc format_desc(action, flag, 'pathspec+') task flag, [:pathspec] do |_, args| files = args.to_a guard_params(action, flag, args: files) __send__(action, flag, files) end end when :stash if flag == :push desc format_desc(action, flag, 'pathspec*') task flag, [:pathspec] do |_, args| stash(flag, args.to_a) end else desc format_desc(action, flag, 'commit?') task flag, [:commit] do |_, args| stash(flag, commit: args.commit) end end when :rev desc format_desc(action, flag, "ref?=HEAD#{flag == :commit ? ',size?' : ''}") task flag, [:ref, :size] do |_, args| rev(flag, ref: args.ref, size: args.size) end when :refs, :files desc format_desc(action, flag, 'grep?=pattern') task flag, [:grep] do |_, args| __send__(action, flag, grep: args.fetch(:grep, nil)) end when :diff case flag when :head desc format_desc(action, flag, 'index?=0,pathspec*') task flag, [:pathspec] do |_, args| files = args.to_a index = /^\d+$/.match?(files.first) && !option('index') ? files.shift.to_i : 0 diff(flag, files, index: index) end when :cached desc format_desc(action, flag, 'pathspec*') task flag, [:pathspec] do |_, args| diff(flag, args.to_a) end when :branch desc format_desc(action, flag, 'name,pathspec*') task flag, [:name, :pathspec] do |_, args| guard_params(action, flag, args: args, key: :name) diff(flag, args.to_a[1..-1], branch: args.name) end when :files desc format_desc(action, flag, 'path1,path2') task flag, [:path1, :path2] do |_, args| guard_params(action, flag, args: args, key: :path1) guard_params(action, flag, args: args, key: :path2) diff(flag, [args.path1, args.path2]) end end when :checkout case flag when :branch desc format_desc(action, flag, 'name,create?=b|B,commit?,detach?=d') task flag, [:name, :create, :commit, :detach] do |_, args| guard_params(action, flag, args: args, key: :name) create = args.create if args.commit == 'd' detach = 'd' commit = nil elsif create == 'd' create = nil commit = nil detach = 'd' elsif create && create.size > 1 commit = create create = nil detach = args.commit else detach = args.detach commit = args.commit end guard_params(action, flag, args: { create: create }, key: :create, pat: /^b$/i) if create checkout(flag, branch: args.name, create: create, commit: commit, detach: detach) end when :detach desc format_desc(action, flag, 'branch/commit?') task flag, [:commit] do |_, args| checkout(flag, commit: args.commit) end else desc format_desc(action, flag, 'pathspec*') task flag, [:pathspec] do |_, args| checkout(flag, args.to_a) end end when :reset if flag == :head desc format_desc(action, flag, 'ref?=HEAD,pathspec+') task flag, [:ref, :pathspec] do |_, args| reset(flag, args.to_a[1..-1], ref: args.ref) end else desc format_desc(action, flag, 'ref?=HEAD') task flag, [:ref] do |_, args| reset(flag, ref: args.ref) end end end end end end end end |
#pull(flag = nil, sync: invoked_sync?('pull', flag), opts: []) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/squared/workspace/project/git.rb', line 201 def pull(flag = nil, sync: invoked_sync?('pull', flag), opts: []) cmd = git_session 'pull' if flag == :'no-rebase' cmd << '--no-rebase' elsif flag == :rebase || option('rebase') cmd << '--rebase' end if flag == :'no-commit' cmd << '--no-commit' elsif flag == :commit || option('commit') cmd << '--commit' end append_pull opts, OPT_PULL, flag source(sync: sync, stderr: true, exception: !workspace.series.multiple?) end |
#rebase ⇒ Object
217 218 219 |
# File 'lib/squared/workspace/project/git.rb', line 217 def rebase pull(:rebase, sync: invoked_sync?('rebase')) end |
#refs(flag, grep: nil) ⇒ Object
328 329 330 331 332 333 334 |
# File 'lib/squared/workspace/project/git.rb', line 328 def refs(flag, grep: nil) git_session 'ls-remote', " --#{flag}" out, = source(io: true) print_item ret = write_lines(out, grep: grep) list_result(ret, flag.to_s, grep: grep) end |
#reset(flag, files = [], ref: nil) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/squared/workspace/project/git.rb', line 271 def reset(flag, files = [], ref: nil) cmd = git_session 'reset' if flag == :head guard_params('reset', flag, args: files) append_commit ref append_pathspec files else case flag when :hard, :soft, :merge, :keep cmd << "--#{flag}" when :submodules append_submodules flag else cmd << '--mixed' cmd << '--no-refresh' if option('refresh', equals: '0') end append_commit ref end source end |
#restore(flag, files) ⇒ Object
432 433 434 435 436 437 438 439 440 441 |
# File 'lib/squared/workspace/project/git.rb', line 432 def restore(flag, files) cmd = git_session 'restore' case flag when :worktree, :staged, :overlay cmd << "--#{flag}" end append_ours append_pathspec(files, expect: true) source(stdout: true) end |
#rev(flag, ref: nil, size: nil) ⇒ Object
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/squared/workspace/project/git.rb', line 317 def rev(flag, ref: nil, size: nil) opt = if flag == :branch '--abbrev-ref' else (n = size.to_i) > 0 ? "--short=#{[n, 5].max}" : '--verify' end git_session 'rev-parse', opt append_commit ref source end |
#stash(flag = :push, files = [], commit: nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/squared/workspace/project/git.rb', line 228 def stash(flag = :push, files = [], commit: nil) cmd = git_session 'stash', flag.to_s case flag when :apply, :pop cmd << '--index' if option('index') cmd << commit else append_option %w[all staged include-untracked] option('message', 'm', zero: false) append_pathspec files end source(sync: invoked_sync?('stash', flag), exception: workspace.exception) end |
#status ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/squared/workspace/project/git.rb', line 242 def status cmd = git_session 'status', option('long') ? '--long' : '--short' if (val = option('ignore-submodules')) cmd << "--ignore-submodules=#{case val when '0', 'none' 'none' when '1', 'untracked' 'untracked' when '2', 'dirty' 'dirty' else 'all' end}" end append_pathspec out, = source(io: true) if && invoked_sync?('status') print_item = nil end ret = write_lines(out, banner: , sub: [ { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 }, { pat: /^([A-Z])(.+)$/, styles: :green }, { pat: /^(\?\?)(.+)$/, styles: :red }, { pat: /^(## )(.+)(\.{3})(.+)$/, styles: [nil, :green, nil, :red], index: -1 } ]) list_result(ret, 'files', action: 'modified') end |