Class: Squared::Workspace::Project::Git
- Extended by:
- Rake::DSL
- Defined in:
- lib/squared/workspace/project/git.rb
Constant Summary
Constants included from Common
Instance Attribute Summary
Attributes inherited from Base
#dependfile, #exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #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
-
#initialize ⇒ Git
constructor
A new instance of Git.
- #populate ⇒ Object
- #pull(flag = nil, sync: invoked_sync?('pull', flag), opts: []) ⇒ Object
- #rebase ⇒ Object
- #ref ⇒ Object
- #refs(flag, grep: nil) ⇒ Object
- #reset(flag, files = [], ref: nil) ⇒ Object
- #restore(flag, files) ⇒ Object
- #rev(flag, ref: nil, size: nil) ⇒ Object
- #show(objs, pretty: nil, format: nil, abbrev: nil) ⇒ Object
- #stash(flag = nil, files = [], commit: nil) ⇒ Object
- #status ⇒ Object
- #tag(flag, refs, message: nil, commit: nil) ⇒ Object
Methods inherited from Base
#add, aliasargs, #allref, as_path, bannerargs, #basepath, #build, #build?, #clean, #clean?, #copy, #copy?, #depend, #depend?, #dependtype, #dev?, #doc, #doc?, #enabled?, #graph, #graph?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, #prod?, ref, #ref?, #script?, #test, #test?, to_s, #to_s, #to_sym, #variable_set, #version, #with
Methods included from Common::Format
Constructor Details
Class Method Details
.batchargs ⇒ Object
43 44 45 |
# File 'lib/squared/workspace/project/git.rb', line 43 def batchargs [ref, { 'pull-s': %i[stash pull], 'rebase-s': %i[stash rebase] }] end |
.config?(val) ⇒ Boolean
47 48 49 50 51 |
# File 'lib/squared/workspace/project/git.rb', line 47 def config?(val) return false unless (val = as_path(val)) val.join('.git').directory? end |
.populate(ws) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/squared/workspace/project/git.rb', line 14 def populate(ws, **) return if ws.series[:pull].empty? namespace(name = ws.task_name('git')) do all = ws.task_join(name, 'all') desc Common::Format.(*"#{all}[git?=rebase|stash,depend?]".split(':')) task 'all', [:git, :depend] do |_, args| cmd = case args.git when 'rebase' [ws.task_sync('rebase')] when 'stash' [ws.task_sync('stash'), ws.task_sync('pull')] else [ws.task_sync('pull')] end cmd << ws.task_sync('depend') if args.depend && !ws.series[:depend].empty? cmd << ws.task_sync('build') Common::Utils.task_invoke(*cmd, **ws.invokeargs) end ws.series.sync << all ws.series.multiple << all end end |
.tasks ⇒ Object
39 40 41 |
# File 'lib/squared/workspace/project/git.rb', line 39 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
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/squared/workspace/project/git.rb', line 349 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_first %w[ours theirs] append_head append_pathspec files end source end |
#commit(flag, files = [], message: nil, pass: false) ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/squared/workspace/project/git.rb', line 455 def commit(flag, files = [], message: nil, pass: false) ||= option('message', 'm', prefix: 'git', ignore: 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 raise_error('commit', 'pathspec', hint: 'missing') if (files = projectmap(files)).empty? "-- #{files.join(' ')}" end format = '%(if)%(HEAD)%(then)%(refname:short)...%(upstream:short)...%(upstream:track)%(end)' branch = nil origin = nil source('git fetch --no-tags --quiet', io: true, banner: false, stdout: true) out = source("git for-each-ref --format=\"#{format}\" refs/heads", io: true, banner: false, stdout: workspace.windows?).first (workspace.windows? ? out.lines : out).each do |line| next if (line = line.chomp).empty? branch, origin, hint = line.split('...') if hint && !hint.match?(/^\[(\D+0,\D+0)\]$/) raise_error('work tree is not usable', hint: hint[1..-2]) elsif origin.empty? return nil if pass raise_error('no remote upstream', hint: branch) end break end i = origin.index('/') branch = "#{branch}:#{origin[(i + 1)..-1]}" unless origin.end_with?("/#{branch}") origin = origin[0..(i - 1)] cmd = git_session 'commit' cmd << '--dry-run' if option('dry-run') cmd << '--amend' if amend if elsif flag == :'amend-orig' || option('no-edit') cmd << '--no-edit' end a = ['git add --verbose'] b = ['git push'] b << '--dry-run' if dry_run? a << pathspec b << '--force-with-lease' if amend b << origin << branch puts if pass source a.join(' ') source cmd source b.join(' ') end |
#diff(flag, files = [], branch: nil, index: 0) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/squared/workspace/project/git.rb', line 421 def diff(flag, files = [], branch: nil, index: 0) cmd = git_session 'diff' unless flag == :files if /\A#\h{5,40}\#\z/.match?(sha = files.first) sha = sha[1..-2] files.shift else sha = nil end end if (val = option('unified')).to_i > 0 cmd << shell_option('unified', val, escape: false) 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, parent: flag == :files) source(exception: cmd.include?('--exit-code')) end |
#fetch(flag = nil, opts: []) ⇒ Object
275 276 277 278 279 280 |
# File 'lib/squared/workspace/project/git.rb', line 275 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), **threadargs) end |
#files(flag, grep: nil) ⇒ Object
413 414 415 416 417 418 419 |
# File 'lib/squared/workspace/project/git.rb', line 413 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
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/squared/workspace/project/git.rb', line 79 def populate(*) super return unless ref?(Git.ref) namespace name do @@tasks[Git.ref].each do |action, flags| namespace action do flags.each do |flag| case action when :pull, :fetch desc format_desc(action, flag, action == :pull ? OPT_PULL : OPT_FETCH) task flag, [:opts] do |_, args| __send__(action, 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 = guard_params(action, flag, args: args.to_a) __send__(action, flag, files) end end when :tag case flag when :list desc format_desc(action, flag, 'pattern*') task flag, [:pattern] do |_, args| tag(flag, args.to_a) end when :merged desc format_desc(action, flag, 'commit,pattern*') task flag, [:commit] do |_, args| commit = guard_params(action, flag, args: args, key: :commit) tag(flag, args.extras, commit: commit) end when :delete desc format_desc(action, flag, 'name+') task flag, [:name] do |_, args| name = guard_params(action, flag, args: args.to_a) tag(flag, name) end else desc format_desc(action, flag, 'name,message?,commit?') task flag, [:name, :message, :commit] do |_, args| name = guard_params(action, flag, args: args, key: :name) tag(flag, [name], message: args., commit: args.commit) 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 diff(flag, files, index: /\A\d+\z/.match?(files[0]) && !option('index') ? files.shift.to_i : 0) 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] do |_, args| branch = guard_params(action, flag, args: args, key: :name) diff(flag, args.extras, branch: branch) end when :files desc format_desc(action, flag, 'path1,path2') task flag, [:path1, :path2] do |_, args| path1 = guard_params(action, flag, args: args, key: :path1) path2 = guard_params(action, flag, args: args, key: :path2) diff(flag, [path1, 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| branch = 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: /\Ab\z/i) if create checkout(flag, branch: branch, 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,pathspec+') task flag, [:ref] do |_, args| files = guard_params(action, flag, args: args.extras) reset(flag, files, ref: args.ref) end else desc format_desc(action, flag, 'ref?=HEAD') task flag, [:ref] do |_, args| reset(flag, ref: args.ref) end end when :show if flag == :oneline desc format_desc(action, flag, 'object*') task flag, [:object] do |_, args| show(args.to_a, pretty: 'oneline', abbrev: true) end else desc format_desc(action, flag, 'format,object*') task flag, [:format] do |_, args| show(args.extras, "#{flag}": args.format) end end end end end end end end |
#pull(flag = nil, sync: invoked_sync?('pull', flag), opts: []) ⇒ Object
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 249 def pull(flag = nil, sync: invoked_sync?('pull', flag), opts: []) cmd = git_session 'pull' if flag == :'no-rebase' || option('rebase', equals: '0') cmd << '--no-rebase' elsif flag == :rebase || option('rebase') cmd << '--rebase' end if flag == :'no-commit' || option('commit', equals: '0') cmd << '--no-commit' elsif flag == :commit || option('commit') cmd << '--commit' end append_pull opts, OPT_PULL, flag sub = if verbose [ { pat: /^(.+)(\|\s+\d+\s+)([^-]*)(-+)(.*)$/, styles: :red, index: 4 }, { pat: /^(.+)(\|\s+\d+\s+)(\++)(.*)$/, styles: :green, index: 3 } ] end source(sync: sync, sub: sub, **threadargs) end |
#rebase ⇒ Object
271 272 273 |
# File 'lib/squared/workspace/project/git.rb', line 271 def rebase pull(:rebase, sync: invoked_sync?('rebase')) end |
#ref ⇒ Object
75 76 77 |
# File 'lib/squared/workspace/project/git.rb', line 75 def ref Git.ref end |
#refs(flag, grep: nil) ⇒ Object
405 406 407 408 409 410 411 |
# File 'lib/squared/workspace/project/git.rb', line 405 def refs(flag, grep: nil) git_session 'ls-remote', "--#{flag}", '--refs' 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
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/squared/workspace/project/git.rb', line 329 def reset(flag, files = [], ref: nil) cmd = git_session 'reset' if flag == :head 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
511 512 513 514 515 516 |
# File 'lib/squared/workspace/project/git.rb', line 511 def restore(flag, files) git_session 'restore', "--#{flag}" append_first %w[ours theirs] append_pathspec(files, expect: true) source(stdout: true) end |
#rev(flag, ref: nil, size: nil) ⇒ Object
395 396 397 398 399 400 401 402 403 |
# File 'lib/squared/workspace/project/git.rb', line 395 def rev(flag, ref: nil, size: nil) git_session 'rev-parse', if flag == :branch '--abbrev-ref' else (n = size.to_i) > 0 ? shell_option('short', [n, 5].max, escape: false) : '--verify' end append_commit ref source end |
#show(objs, pretty: nil, format: nil, abbrev: nil) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/squared/workspace/project/git.rb', line 518 def show(objs, pretty: nil, format: nil, abbrev: nil) cmd = git_session 'show' if (flag = pretty || format) cmd << case (val = flag.downcase) when 'oneline', 'short', 'medium', 'full', 'fuller', 'reference', 'email', 'raw' shell_option('format', val, escape: false) else shell_option('pretty', flag, escape: false, quote: true) end end if (abbrev ||= option('abbrev')) == true cmd << '--abbrev-commit' elsif abbrev.to_i > 0 cmd << shell_option('abbrev', abbrev, escape: false) end append_value objs source(exception: false) end |
#stash(flag = nil, files = [], commit: nil) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/squared/workspace/project/git.rb', line 282 def stash(flag = nil, files = [], commit: nil) cmd = git_session 'stash', flag || 'push' case flag when :apply, :pop cmd << '--index' if option('index') cmd << commit else append_option %w[all staged include-untracked].freeze option('message', 'm', ignore: false) append_pathspec files end source(sync: invoked_sync?('stash', flag), **threadargs) end |
#status ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/squared/workspace/project/git.rb', line 296 def status cmd = git_session 'status', option('long') ? '--long' : '--short' cmd << '--branch' if option('branch') if (val = option('ignore-submodules', ignore: false)) cmd << shell_option('ignore-submodules', case val when '0', 'none' 'none' when '1', 'untracked' 'untracked' when '2', 'dirty' 'dirty' else 'all' end, escape: false) end append_pathspec out, = source(io: true) if invoked_sync?('status') print_item = nil end ret = write_lines(out, banner: , sub: if verbose [ { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 }, { pat: /^([A-Z])(.+)$/, styles: :green }, { pat: /^(\?\?)(.+)$/, styles: :red }, { pat: /^(## )(.+)(\.{3})(.+)$/, styles: [nil, :green, nil, :red], index: -1 } ] end) list_result(ret, 'files', action: 'modified') end |
#tag(flag, refs, message: nil, commit: nil) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/squared/workspace/project/git.rb', line 374 def tag(flag, refs, message: nil, commit: nil) cmd = git_session 'tag' case flag when :add if option('sign') cmd << '--sign' else out = cmd.to_s cmd << '--annotate' if %w[-s --sign -u --local-user].none? { |val| out.include?(" #{val}") } end append_value(refs, escape: false) append_head commit when :delete, :list, :merged cmd << shell_option(flag, commit) append_option(%w[contains sort] + (flag == :list ? ['merged'] : []), equals: true) unless flag == :delete append_value(refs, escape: false) end source end |