gitflash 0.6.0 → 0.7.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +36 -0
  3. data/README.md +26 -2
  4. data/command_descriptions.yml +80 -0
  5. data/lib/gitflash/branch.rb +7 -2
  6. data/lib/gitflash/cherry_pick/log_parser.rb +63 -0
  7. data/lib/gitflash/cherry_pick.rb +120 -0
  8. data/lib/gitflash/cleanup.rb +70 -0
  9. data/lib/gitflash/cli.rb +38 -1
  10. data/lib/gitflash/commands/base.rb +12 -3
  11. data/lib/gitflash/commands/branches.rb +1 -0
  12. data/lib/gitflash/commands/clean.rb +51 -0
  13. data/lib/gitflash/commands/clean_criteria.rb +29 -0
  14. data/lib/gitflash/commands/delete.rb +17 -5
  15. data/lib/gitflash/commands/hook_install.rb +31 -10
  16. data/lib/gitflash/commands/hook_status.rb +24 -6
  17. data/lib/gitflash/commands/mark.rb +85 -0
  18. data/lib/gitflash/commands/pick.rb +143 -0
  19. data/lib/gitflash/commands/pick_control.rb +67 -0
  20. data/lib/gitflash/commands/pick_outcome.rb +52 -0
  21. data/lib/gitflash/commands/removal_report.rb +70 -0
  22. data/lib/gitflash/commands/undo.rb +41 -6
  23. data/lib/gitflash/commands/worktree_add.rb +100 -0
  24. data/lib/gitflash/commands/worktree_clean.rb +49 -0
  25. data/lib/gitflash/commands/worktree_command.rb +28 -0
  26. data/lib/gitflash/commands/worktree_list.rb +48 -0
  27. data/lib/gitflash/commands/worktree_lock.rb +58 -0
  28. data/lib/gitflash/commands/worktree_move.rb +52 -0
  29. data/lib/gitflash/commands/worktree_prune.rb +41 -0
  30. data/lib/gitflash/commands/worktree_remove.rb +126 -0
  31. data/lib/gitflash/commands/worktree_unlock.rb +12 -0
  32. data/lib/gitflash/config.rb +98 -0
  33. data/lib/gitflash/config_keys.rb +31 -0
  34. data/lib/gitflash/git/bash_command.rb +5 -2
  35. data/lib/gitflash/git/in_directory.rb +29 -0
  36. data/lib/gitflash/hook/branch_marker.rb +155 -0
  37. data/lib/gitflash/hook/claude.rb +54 -18
  38. data/lib/gitflash/hook/command_parser.rb +50 -12
  39. data/lib/gitflash/hook/creations.rb +85 -0
  40. data/lib/gitflash/hook/messages.rb +38 -0
  41. data/lib/gitflash/hook/rules.rb +6 -4
  42. data/lib/gitflash/hook/settings.rb +18 -5
  43. data/lib/gitflash/hook_cli.rb +20 -5
  44. data/lib/gitflash/ownership.rb +59 -0
  45. data/lib/gitflash/protection.rb +19 -0
  46. data/lib/gitflash/repo.rb +5 -5
  47. data/lib/gitflash/restore.rb +8 -7
  48. data/lib/gitflash/state_capture.rb +10 -5
  49. data/lib/gitflash/terminal_text.rb +9 -0
  50. data/lib/gitflash/ui.rb +1 -1
  51. data/lib/gitflash/version.rb +1 -1
  52. data/lib/gitflash/worktree.rb +32 -0
  53. data/lib/gitflash/worktree_cli.rb +115 -0
  54. data/lib/gitflash/worktree_revival.rb +55 -0
  55. data/lib/gitflash/worktrees.rb +147 -0
  56. data/schema/v1.json +1050 -2
  57. metadata +32 -1
@@ -6,8 +6,6 @@ module Gitflash
6
6
  # Uses `git branch -d` (merged branches only) unless --force is given.
7
7
  # The result lists each deleted branch with its last commit, so it can be recreated.
8
8
  class Delete < Base
9
- PROTECTED_NAMES = %w[main master].freeze
10
-
11
9
  def call(*names)
12
10
  branches = repo.branches
13
11
  candidates = branches.reject { |branch| protected?(branch) }.map(&:name)
@@ -34,8 +32,16 @@ module Gitflash
34
32
  options[:force] ? true : false
35
33
  end
36
34
 
35
+ def protection
36
+ @protection ||= Protection.new(config: config, checked_out: checked_out)
37
+ end
38
+
39
+ def checked_out
40
+ Worktrees.new(repo: repo).list.filter_map(&:branch)
41
+ end
42
+
37
43
  def protected?(branch)
38
- branch.current? || branch.default? || PROTECTED_NAMES.include?(branch.name)
44
+ protection.protected?(branch)
39
45
  end
40
46
 
41
47
  def validate!(names, branches)
@@ -48,11 +54,12 @@ module Gitflash
48
54
 
49
55
  usage_error!('protected_branch',
50
56
  "Refusing to delete protected branches: #{refused.join(', ')} " \
51
- '(current, default, main and master are protected)')
57
+ '(current, default, main, master, `protected` in .gitflash.yml, ' \
58
+ 'checked out in a worktree)')
52
59
  end
53
60
 
54
61
  def delete(names, shas)
55
- plan = { branches: names, force: force? }
62
+ plan = build_plan(names)
56
63
  return planned(plan, "Would delete:\n#{bullets(names)}") if ui.dry_run?
57
64
  return cancelled(plan) unless ui.confirm?(summary(names), plan: plan)
58
65
 
@@ -62,6 +69,11 @@ module Gitflash
62
69
  report_results(plan, outcomes, shas, snapshot)
63
70
  end
64
71
 
72
+ # The plan reported and returned in JSON; `clean` adds why each branch was chosen
73
+ def build_plan(names)
74
+ { branches: names, force: force? }
75
+ end
76
+
65
77
  def summary(names)
66
78
  mode = force? ? 'even if they have unmerged changes' : 'if they are fully merged'
67
79
  "You are about to delete the following branches #{mode}:\n\n#{bullets(names)}"
@@ -4,35 +4,56 @@ require 'fileutils'
4
4
 
5
5
  module Gitflash
6
6
  module Commands
7
- # Registers `gitflash hook claude` as a Claude Code PreToolUse hook for Bash commands.
8
- # Merges into the settings file and keeps everything else in it.
7
+ # Registers `gitflash hook claude` as a Claude Code hook for Bash commands: PreToolUse
8
+ # (snapshots before destructive git commands) and PostToolUse (marks branches an agent
9
+ # created). Merges into the settings file and keeps everything else in it.
9
10
  class HookInstall < Base
10
11
  SCOPES = Hook::Settings::SCOPES
12
+ MARKING_COMMAND = 'gitflash hook claude'
11
13
 
12
14
  def call
13
15
  plan = build_plan
14
16
  data = settings.read(plan[:file])
15
17
  entry = settings.entry(data)
16
- return already_installed(plan) if entry && entry['command'] == plan[:command]
18
+ marking = settings.entry(data, Hook::Claude::POST_EVENT)
19
+ return already_installed(plan) if up_to_date?(entry, marking, plan)
17
20
  return planned(plan, "Would add the gitflash hook to #{plan[:file]}") if ui.dry_run?
18
21
 
19
- install(data, entry, plan)
22
+ install(data, entry, marking, plan)
20
23
  end
21
24
 
22
25
  private
23
26
 
27
+ def up_to_date?(entry, marking, plan)
28
+ entry && entry['command'] == plan[:command] && marking
29
+ end
30
+
24
31
  def build_plan
25
32
  scope = choice(:scope, SCOPES, 'local')
26
- { scope: scope, file: settings.path(scope), command: hook_command,
27
- applies_to: settings.applies_to(scope) }
33
+ file = settings.path(scope)
34
+ refuse_symlink!(file) unless scope == 'user'
35
+ { scope: scope, file: file, command: hook_command,
36
+ marking_command: MARKING_COMMAND, applies_to: settings.applies_to(scope) }
37
+ end
38
+
39
+ # The repository decides what .claude holds, and a link there could point at the user's own
40
+ # settings; writing through it would change a scope other than the one asked for. The
41
+ # user scope may be a link (dotfiles) because the user owns it.
42
+ def refuse_symlink!(file)
43
+ linked = [file, File.dirname(file)].find { |path| File.symlink?(path) }
44
+ return unless linked
45
+
46
+ raise Error.new("#{linked} is a symbolic link; gitflash will not write settings through it",
47
+ code: 'invalid_settings')
28
48
  end
29
49
 
30
50
  def settings
31
51
  @settings ||= Hook::Settings.new(options[:scope] == 'user' ? nil : repo)
32
52
  end
33
53
 
34
- def install(data, entry, plan)
35
- entry ? entry['command'] = plan[:command] : add_entry(data, plan[:command])
54
+ def install(data, entry, marking, plan)
55
+ entry ? entry['command'] = plan[:command] : add_entry(data, 'PreToolUse', plan[:command])
56
+ add_entry(data, Hook::Claude::POST_EVENT, MARKING_COMMAND) unless marking
36
57
  FileUtils.mkdir_p(File.dirname(plan[:file]))
37
58
  File.write(plan[:file], "#{JSON.pretty_generate(data)}\n")
38
59
  ui.report(status: 'done', plan: plan, result: plan,
@@ -57,9 +78,9 @@ module Gitflash
57
78
  text: "Already installed in #{plan[:file]}.\nIt applies to #{plan[:applies_to]}.")
58
79
  end
59
80
 
60
- def add_entry(data, command)
81
+ def add_entry(data, event, command)
61
82
  hooks = (data['hooks'] ||= {})
62
- (hooks['PreToolUse'] ||= []) << {
83
+ (hooks[event] ||= []) << {
63
84
  'matcher' => 'Bash',
64
85
  'hooks' => [{ 'type' => 'command', 'command' => command, 'timeout' => 30 }]
65
86
  }
@@ -9,9 +9,7 @@ module Gitflash
9
9
  def call
10
10
  settings = Hook::Settings.new(inside_repo? ? repo : nil)
11
11
  files = settings.scopes.map { |scope| file_status(settings, scope) }
12
- executable = find_executable
13
- active = executable && files.any? { |file| file[:installed] }
14
- result = { active: active ? true : false, executable: executable, files: files }
12
+ result = summary(files, find_executable)
15
13
  ui.report(status: 'done', result: result, text: text(result))
16
14
  end
17
15
 
@@ -24,10 +22,19 @@ module Gitflash
24
22
  false
25
23
  end
26
24
 
25
+ def summary(files, executable)
26
+ installed = files.select { |file| file[:installed] }
27
+ active = executable && installed.any?
28
+ { active: active ? true : false, marking: active && installed.any? { |f| f[:marking] },
29
+ executable: executable, files: files }
30
+ end
31
+
27
32
  def file_status(settings, scope)
28
33
  path = settings.path(scope)
29
- entry = settings.entry(settings.read(path))
34
+ data = settings.read(path)
35
+ entry = settings.entry(data)
30
36
  { scope: scope, file: path, installed: !entry.nil?, command: entry&.fetch('command', nil),
37
+ marking: !settings.entry(data, Hook::Claude::POST_EVENT).nil?,
31
38
  applies_to: settings.applies_to(scope) }
32
39
  end
33
40
 
@@ -39,7 +46,7 @@ module Gitflash
39
46
  def text(result)
40
47
  lines = [headline(result)]
41
48
  result[:files].each do |file|
42
- state = file[:installed] ? "installed (#{file[:command]})" : 'not installed'
49
+ state = file[:installed] ? installed_text(file) : 'not installed'
43
50
  lines << " #{file[:scope].ljust(8)} #{file[:file]}: #{state}"
44
51
  lines << " applies to #{file[:applies_to]}" if file[:installed]
45
52
  end
@@ -47,8 +54,13 @@ module Gitflash
47
54
  lines.join("\n")
48
55
  end
49
56
 
57
+ def installed_text(file)
58
+ note = file[:marking] ? ', marks agent branches' : ', branch marking off'
59
+ "installed (#{file[:command]})#{note}"
60
+ end
61
+
50
62
  def headline(result)
51
- return 'gitflash hook: active' if result[:active]
63
+ return active_headline(result) if result[:active]
52
64
 
53
65
  reason = if result[:executable]
54
66
  'run `gitflash hook install`'
@@ -57,6 +69,12 @@ module Gitflash
57
69
  end
58
70
  "gitflash hook: not active (#{reason})"
59
71
  end
72
+
73
+ def active_headline(result)
74
+ return 'gitflash hook: active, marks agent branches' if result[:marking]
75
+
76
+ 'gitflash hook: active (branch marking off: run `gitflash hook install` again)'
77
+ end
60
78
  end
61
79
  end
62
80
  end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitflash
4
+ module Commands
5
+ # Marks branches as agent or human work (`--owner`, default agent) or removes the mark
6
+ # (`--clear`), so `gitflash clean --agent` knows which branches an agent created.
7
+ # Without branch names the current branch is marked. Needs no confirmation: only a git
8
+ # config key changes, and `--clear` or another `mark` reverses it.
9
+ class Mark < Base
10
+ def call(*names)
11
+ names = names.uniq
12
+ names = [current_branch!] if names.empty?
13
+ check_branches!(names)
14
+
15
+ previous = ownership.all
16
+ plan = { branches: names, owner: owner }
17
+ return planned(plan, "Would #{verb} #{names.join(', ')}") if ui.dry_run?
18
+
19
+ apply(plan, previous)
20
+ end
21
+
22
+ private
23
+
24
+ def ownership
25
+ @ownership ||= Ownership.new
26
+ end
27
+
28
+ # The owner to set, nil for --clear
29
+ def owner
30
+ given = options[:owner]
31
+ if options[:clear]
32
+ usage_error!('invalid_options', 'Use either --owner or --clear, not both') if given
33
+ return nil
34
+ end
35
+ return 'agent' if given.nil?
36
+ return given if Ownership::OWNERS.include?(given)
37
+
38
+ usage_error!('invalid_options', "Unknown owner '#{given}' (use agent or human)")
39
+ end
40
+
41
+ def verb
42
+ options[:clear] ? 'clear the mark of' : "mark as #{owner}"
43
+ end
44
+
45
+ def current_branch!
46
+ repo.current_branch || usage_error!('invalid_usage',
47
+ 'HEAD is detached: pass the branch to mark')
48
+ end
49
+
50
+ def check_branches!(names)
51
+ known = repo.branches.map(&:name)
52
+ unknown = names.reject { |name| known.include?(name) }
53
+ usage_error!('unknown_branch', "Unknown branch: #{unknown.join(', ')}") if unknown.any?
54
+ end
55
+
56
+ def apply(plan, previous)
57
+ plan[:branches].each { |name| write(name, plan[:owner]) }
58
+ rows = plan[:branches].map { |name| row(name, plan[:owner], previous[name]) }
59
+ status = rows.all? { |entry| entry[:owner] == entry[:previous] } ? 'noop' : 'done'
60
+ ui.report(status: status, plan: plan, result: { branches: rows }, text: text(rows))
61
+ end
62
+
63
+ def write(name, owner)
64
+ owner ? ownership.mark(name, owner) : ownership.clear(name)
65
+ end
66
+
67
+ def row(name, owner, previous)
68
+ { branch: name, owner: owner, previous: previous }
69
+ end
70
+
71
+ def text(rows)
72
+ rows.map { |row| line(row) }.join("\n")
73
+ end
74
+
75
+ def line(row)
76
+ if row[:owner] == row[:previous]
77
+ return "#{row[:branch]} is already #{row[:owner] || 'unmarked'}"
78
+ end
79
+ return "Cleared the mark of #{row[:branch]} (was #{row[:previous]})" unless row[:owner]
80
+
81
+ "Marked #{row[:branch]} as #{row[:owner]}"
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitflash
4
+ module Commands
5
+ # Cherry-picks commits from another branch. `--list` shows the commits of SOURCE whose change
6
+ # is not on the current branch yet (commits with an equivalent patch already here are marked
7
+ # `applied` and never picked). With SHAs, or chosen from a menu in a terminal, it applies them
8
+ # oldest first with `git cherry-pick -x` after saving a snapshot; `gitflash undo` returns to
9
+ # the starting point. On a conflict it stops and lists the files: see PickControl.
10
+ class Pick < Base
11
+ include PickOutcome
12
+
13
+ NOTHING = 'Nothing to pick: the changes are already on the current branch'
14
+
15
+ # --continue, --skip and --abort are handled by PickControl, everything else by Pick
16
+ def self.command_for(options)
17
+ PickControl::ACTIONS.any? { |action| options[action.to_sym] } ? PickControl : self
18
+ end
19
+
20
+ def call(source = nil, *shas)
21
+ check_idle!
22
+ source ||= pick_source
23
+ return 0 if source.nil?
24
+
25
+ candidates = cherry_pick.candidates(source_ref(source))
26
+ return list(source, candidates) if options[:list]
27
+
28
+ wanted = shas.empty? ? pick_from_menu(candidates) : shas
29
+ return 0 if wanted.nil?
30
+
31
+ pick(source, select_candidates(source, candidates, wanted))
32
+ end
33
+
34
+ private
35
+
36
+ # The branch picked from a menu of the other local branches (as `checkout` does), nil after
37
+ # telling the user there is no other branch
38
+ def pick_source
39
+ require_interactive!('Pass the source branch: gitflash pick SOURCE')
40
+ branches = repo.branches
41
+ return nothing_chosen('You only have one branch!') if branches.size < 2
42
+
43
+ ui.select('Select the branch to pick commits from', branches.reject(&:current?).map(&:name))
44
+ end
45
+
46
+ def check_idle!
47
+ return unless cherry_pick.in_progress?
48
+
49
+ usage_error!('invalid_usage', 'A cherry-pick is already in progress: use gitflash pick ' \
50
+ '--continue, --skip or --abort')
51
+ end
52
+
53
+ def source_ref(source)
54
+ ref = cherry_pick.source_ref(source)
55
+ ref || usage_error!('unknown_branch', "Unknown branch '#{source}'")
56
+ end
57
+
58
+ def list(source, candidates)
59
+ ui.report(status: 'done', result: { source: source, commits: candidates.map(&:to_h) },
60
+ text: table(candidates))
61
+ end
62
+
63
+ def table(candidates)
64
+ return 'No commits to pick.' if candidates.empty?
65
+
66
+ candidates.map { |candidate| row(candidate) }.join("\n")
67
+ end
68
+
69
+ def row(candidate)
70
+ stat = "+#{candidate.insertions} -#{candidate.deletions}"
71
+ "#{candidate.applied ? '=' : ' '} #{candidate.short_sha} " \
72
+ "#{TerminalText.safe(candidate.subject)} (#{TerminalText.safe(candidate.author)}, " \
73
+ "#{candidate.date[0, 10]}, #{candidate.files} files #{stat})"
74
+ end
75
+
76
+ # SHAs picked from a menu, nil after telling the user there is nothing to pick
77
+ def pick_from_menu(candidates)
78
+ require_interactive!('Pass the commits to pick: gitflash pick SOURCE SHA...')
79
+ fresh = candidates.reject(&:applied)
80
+ return nothing_chosen('No commits to pick') if fresh.empty?
81
+
82
+ chosen = ui.multi_select('Select commits to pick (oldest first)',
83
+ fresh.to_h { |candidate| [row(candidate), candidate.sha] })
84
+ chosen.empty? ? nothing_chosen('No commits selected') : chosen
85
+ end
86
+
87
+ def nothing_chosen(text)
88
+ report_nothing(text)
89
+ nil
90
+ end
91
+
92
+ # The asked-for commits in source order (oldest first), whatever order they were given in
93
+ def select_candidates(source, candidates, wanted)
94
+ found = wanted.map { |arg| find(arg, candidates, source) }
95
+ candidates.select { |candidate| found.include?(candidate) }
96
+ end
97
+
98
+ def find(arg, candidates, source)
99
+ matches = candidates.select { |candidate| candidate.sha.start_with?(arg.to_s) }
100
+ return matches.first if arg.to_s.length >= 4 && matches.size == 1
101
+
102
+ usage_error!('unknown_commit', "'#{arg}' is not a commit of #{source} that is missing " \
103
+ 'from the current branch (or it is ambiguous)')
104
+ end
105
+
106
+ def pick(source, chosen)
107
+ fresh, applied = chosen.partition { |candidate| !candidate.applied }
108
+ return report_nothing(NOTHING) if fresh.empty?
109
+
110
+ plan = build_plan(source, fresh, applied)
111
+ return planned(plan, "Would pick:\n#{bullets(plan)}") if ui.dry_run?
112
+ return cancelled(plan) unless confirm?(plan)
113
+
114
+ run(plan)
115
+ end
116
+
117
+ def build_plan(source, fresh, applied)
118
+ skipped = applied.map { |c| { sha: c.sha, reason: 'already on the current branch' } }
119
+ { source: source, commits: fresh.map(&:sha), skipped: skipped,
120
+ commit: options[:commit] != false }
121
+ end
122
+
123
+ def confirm?(plan)
124
+ ui.confirm?("You are about to pick:\n\n#{bullets(plan)}", plan: plan)
125
+ end
126
+
127
+ def run(plan)
128
+ snapshot = take_snapshot("gitflash pick #{plan[:source]}",
129
+ scope: %w[branches head worktree], branches: current_branch_names)
130
+ result = cherry_pick.apply(plan[:commits], commit: plan[:commit])
131
+ outcome(plan, result, plan[:commits], snapshot)
132
+ end
133
+
134
+ def bullets(plan)
135
+ plan[:commits].map { |sha| "* #{sha[0, 7]}" }.join("\n")
136
+ end
137
+
138
+ def report_nothing(text)
139
+ ui.report(status: 'noop', result: { applied: [] }, text: text)
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitflash
4
+ module Commands
5
+ # `gitflash pick --continue`, `--skip` or `--abort`: wraps git's own cherry-pick controls for
6
+ # a pick that stopped on conflicts. --skip and --abort discard work in progress, so they ask
7
+ # for confirmation and save a snapshot first; --continue adds to the work and does not.
8
+ class PickControl < Base
9
+ include PickOutcome
10
+
11
+ ACTIONS = %w[continue skip abort].freeze
12
+
13
+ def call(*)
14
+ action = chosen_action
15
+ usage_error!('invalid_usage', 'No cherry-pick in progress') unless cherry_pick.in_progress?
16
+
17
+ plan = build_plan(action)
18
+ return planned(plan, "Would #{action} the cherry-pick in progress") if ui.dry_run?
19
+ return perform(action, plan, nil) if action == 'continue'
20
+ return cancelled(plan) unless ui.confirm?(summary(action), plan: plan)
21
+
22
+ perform(action, plan, take_snapshot("gitflash pick --#{action}", scope: %w[head worktree]))
23
+ end
24
+
25
+ private
26
+
27
+ def chosen_action
28
+ given = ACTIONS.select { |action| options[action.to_sym] }
29
+ return given.first if given.one?
30
+
31
+ usage_error!('invalid_options', 'Use exactly one of --continue, --skip or --abort')
32
+ end
33
+
34
+ def build_plan(action)
35
+ progress = cherry_pick.progress
36
+ { action: action, current: progress.current, conflicted: progress.conflicted,
37
+ remaining: progress.remaining }
38
+ end
39
+
40
+ def summary(action)
41
+ "You are about to #{action} the cherry-pick in progress; " \
42
+ 'the conflict resolution made so far is discarded.'
43
+ end
44
+
45
+ def perform(action, plan, snapshot)
46
+ result = cherry_pick.public_send(action == 'continue' ? :continue : action.to_sym)
47
+ return outcome_after(action, plan, result, snapshot) if action != 'abort'
48
+
49
+ git_error!('cherry-pick --abort', result) unless result.success?
50
+ finish(action, plan, snapshot)
51
+ end
52
+
53
+ def outcome_after(action, plan, result, snapshot)
54
+ return conflict(plan, result, [], snapshot) if cherry_pick.in_progress?
55
+
56
+ git_error!("cherry-pick --#{action}", result) unless result.success?
57
+ finish(action, plan, snapshot)
58
+ end
59
+
60
+ def finish(action, plan, snapshot)
61
+ head = repo.resolve_commit('HEAD')
62
+ ui.report(status: 'done', plan: plan, result: { action: action, head: head },
63
+ undo: snapshot, text: "Cherry-pick #{action} done, now at #{head[0, 7]}")
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitflash
4
+ module Commands
5
+ # How `pick` and its --continue / --skip / --abort report a git cherry-pick run: done, or
6
+ # stopped on conflicts (status `failed`, error code `conflict`) with what is left to do.
7
+ module PickOutcome
8
+ private
9
+
10
+ def cherry_pick
11
+ @cherry_pick ||= CherryPick.new
12
+ end
13
+
14
+ # `requested` are the commits the user asked for, in order; `snapshot` reverts the start
15
+ def outcome(plan, result, requested, snapshot)
16
+ return finished(plan, requested, snapshot) if result.success? && !cherry_pick.in_progress?
17
+ return conflict(plan, result, requested, snapshot) if cherry_pick.in_progress?
18
+
19
+ git_error!('cherry-pick', result)
20
+ end
21
+
22
+ def finished(plan, requested, snapshot)
23
+ head = repo.resolve_commit('HEAD')
24
+ text = "Applied #{requested.size} commit(s), now at #{head[0, 7]}"
25
+ ui.report(status: 'done', plan: plan, result: { applied: requested, head: head },
26
+ undo: snapshot, text: text)
27
+ end
28
+
29
+ def conflict(plan, result, requested, snapshot)
30
+ progress = cherry_pick.progress
31
+ text = conflict_text(progress, result)
32
+ details = { applied: requested - progress.remaining - [progress.current],
33
+ current: progress.current, conflicted: progress.conflicted,
34
+ remaining: progress.remaining }
35
+ ui.report(status: 'failed', plan: plan, result: details, undo: snapshot, text: text,
36
+ error: { code: 'conflict', exit_code: 1, message: text })
37
+ end
38
+
39
+ def conflict_text(progress, result)
40
+ files = progress.conflicted.map { |file| "* #{file}" }.join("\n")
41
+ "Stopped on conflicts in:\n#{files}\n#{result.output}\n" \
42
+ 'Resolve them and stage the files, then run `gitflash pick --continue`; ' \
43
+ 'or `gitflash pick --skip` / `gitflash pick --abort`.'
44
+ end
45
+
46
+ def current_branch_names
47
+ branch = repo.current_branch
48
+ branch ? [branch] : []
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitflash
4
+ module Commands
5
+ # Reports the outcome of removing worktrees: one row per worktree with its own snapshot and
6
+ # undo command, failures with their reason. Used by `wt remove` and `wt clean`.
7
+ module RemovalReport
8
+ # What happened to one worktree; `snapshot` is nil when it was not removed
9
+ Removal = Data.define(:worktree, :result, :snapshot)
10
+
11
+ private
12
+
13
+ # Ignored files are never saved by snapshots, so `undo` cannot bring them back
14
+ def ignored_warning(plan)
15
+ rows = plan[:worktrees].select { |row| row[:ignored_count].positive? }
16
+ return '' if rows.empty?
17
+
18
+ lines = rows.map { |row| ignored_line(row) }
19
+ "\n\nIgnored files are not saved by snapshots and will be lost for good:\n" \
20
+ "#{lines.join("\n")}"
21
+ end
22
+
23
+ def ignored_line(row)
24
+ "* #{TerminalText.line(row[:path])}: #{row[:ignored_count]} " \
25
+ "(#{TerminalText.line(row[:ignored].join(', '))})"
26
+ end
27
+
28
+ def report_results(plan, outcomes)
29
+ removed, failed = outcomes.partition { |outcome| outcome.result.success? }
30
+ result = { removed: removed.map { |outcome| removed_row(outcome) },
31
+ failed: failed.map { |outcome| failed_row(outcome) } }
32
+ ui.report(status: failed.empty? ? 'done' : 'failed', plan: plan, result: result,
33
+ error: failure(failed, outcomes), text: results_text(result),
34
+ undo: single_undo(removed))
35
+ end
36
+
37
+ # `undo` in the envelope only when one worktree was removed; each row has its own
38
+ def single_undo(removed)
39
+ removed.first.snapshot if removed.size == 1
40
+ end
41
+
42
+ def failed_row(outcome)
43
+ { path: outcome.worktree.path, error: outcome.result.output }
44
+ end
45
+
46
+ def removed_row(outcome)
47
+ worktree = outcome.worktree
48
+ { path: worktree.path, branch: worktree.branch, head: worktree.head,
49
+ snapshot: outcome.snapshot.id, undo: "gitflash undo #{outcome.snapshot.id}" }
50
+ end
51
+
52
+ def failure(failed, outcomes)
53
+ return nil if failed.empty?
54
+
55
+ { code: 'git_failed', exit_code: 1,
56
+ message: "#{failed.size} of #{outcomes.size} worktrees were not removed" }
57
+ end
58
+
59
+ def results_text(result)
60
+ lines = result[:removed].map do |row|
61
+ "Removed #{TerminalText.line(row[:path])} (undo: #{row[:undo]})"
62
+ end
63
+ lines += result[:failed].map do |row|
64
+ "Not removed #{TerminalText.line(row[:path])}: #{row[:error]}"
65
+ end
66
+ lines.join("\n")
67
+ end
68
+ end
69
+ end
70
+ end