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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -0
- data/README.md +26 -2
- data/command_descriptions.yml +80 -0
- data/lib/gitflash/branch.rb +7 -2
- data/lib/gitflash/cherry_pick/log_parser.rb +63 -0
- data/lib/gitflash/cherry_pick.rb +120 -0
- data/lib/gitflash/cleanup.rb +70 -0
- data/lib/gitflash/cli.rb +38 -1
- data/lib/gitflash/commands/base.rb +12 -3
- data/lib/gitflash/commands/branches.rb +1 -0
- data/lib/gitflash/commands/clean.rb +51 -0
- data/lib/gitflash/commands/clean_criteria.rb +29 -0
- data/lib/gitflash/commands/delete.rb +17 -5
- data/lib/gitflash/commands/hook_install.rb +31 -10
- data/lib/gitflash/commands/hook_status.rb +24 -6
- data/lib/gitflash/commands/mark.rb +85 -0
- data/lib/gitflash/commands/pick.rb +143 -0
- data/lib/gitflash/commands/pick_control.rb +67 -0
- data/lib/gitflash/commands/pick_outcome.rb +52 -0
- data/lib/gitflash/commands/removal_report.rb +70 -0
- data/lib/gitflash/commands/undo.rb +41 -6
- data/lib/gitflash/commands/worktree_add.rb +100 -0
- data/lib/gitflash/commands/worktree_clean.rb +49 -0
- data/lib/gitflash/commands/worktree_command.rb +28 -0
- data/lib/gitflash/commands/worktree_list.rb +48 -0
- data/lib/gitflash/commands/worktree_lock.rb +58 -0
- data/lib/gitflash/commands/worktree_move.rb +52 -0
- data/lib/gitflash/commands/worktree_prune.rb +41 -0
- data/lib/gitflash/commands/worktree_remove.rb +126 -0
- data/lib/gitflash/commands/worktree_unlock.rb +12 -0
- data/lib/gitflash/config.rb +98 -0
- data/lib/gitflash/config_keys.rb +31 -0
- data/lib/gitflash/git/bash_command.rb +5 -2
- data/lib/gitflash/git/in_directory.rb +29 -0
- data/lib/gitflash/hook/branch_marker.rb +155 -0
- data/lib/gitflash/hook/claude.rb +54 -18
- data/lib/gitflash/hook/command_parser.rb +50 -12
- data/lib/gitflash/hook/creations.rb +85 -0
- data/lib/gitflash/hook/messages.rb +38 -0
- data/lib/gitflash/hook/rules.rb +6 -4
- data/lib/gitflash/hook/settings.rb +18 -5
- data/lib/gitflash/hook_cli.rb +20 -5
- data/lib/gitflash/ownership.rb +59 -0
- data/lib/gitflash/protection.rb +19 -0
- data/lib/gitflash/repo.rb +5 -5
- data/lib/gitflash/restore.rb +8 -7
- data/lib/gitflash/state_capture.rb +10 -5
- data/lib/gitflash/terminal_text.rb +9 -0
- data/lib/gitflash/ui.rb +1 -1
- data/lib/gitflash/version.rb +1 -1
- data/lib/gitflash/worktree.rb +32 -0
- data/lib/gitflash/worktree_cli.rb +115 -0
- data/lib/gitflash/worktree_revival.rb +55 -0
- data/lib/gitflash/worktrees.rb +147 -0
- data/schema/v1.json +1050 -2
- metadata +32 -1
data/lib/gitflash/hook/rules.rb
CHANGED
|
@@ -41,9 +41,9 @@ module Gitflash
|
|
|
41
41
|
def clean(args, _dir)
|
|
42
42
|
flags = args.select { |arg| arg.start_with?('-') }
|
|
43
43
|
short = short_flags(args)
|
|
44
|
-
|
|
45
|
-
dry_run =
|
|
46
|
-
|
|
44
|
+
removes = any?(flags, %w[--force --interactive]) || short.match?(/[fi]/)
|
|
45
|
+
dry_run = flags.include?('--dry-run') || short.include?('n')
|
|
46
|
+
removes && !dry_run ? { scope: %w[worktree] } : nil
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def switch(args, _dir)
|
|
@@ -59,7 +59,9 @@ module Gitflash
|
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
def branch(args, _dir)
|
|
62
|
-
|
|
62
|
+
changes = any?(args, %w[--delete --force --move --copy]) ||
|
|
63
|
+
short_flags(args).match?(/[dDfmMcC]/)
|
|
64
|
+
return nil unless changes
|
|
63
65
|
|
|
64
66
|
{ scope: %w[branches], branches: args.reject { |arg| arg.start_with?('-') } }
|
|
65
67
|
end
|
|
@@ -49,11 +49,24 @@ module Gitflash
|
|
|
49
49
|
raise Error.new("#{path} is not valid JSON: #{e.message}", code: 'invalid_settings')
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
-
# The gitflash hook entry in parsed settings, or nil
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
# The gitflash hook entry for an event (PreToolUse by default) in parsed settings, or nil.
|
|
53
|
+
# Only groups whose matcher covers the Bash tool count: a gitflash command under another
|
|
54
|
+
# matcher (Edit, say) never runs for Bash commands.
|
|
55
|
+
def entry(settings, event = 'PreToolUse')
|
|
56
|
+
groups = Array(settings.dig('hooks', event))
|
|
57
|
+
bash = groups.select { |group| bash_matcher?(group['matcher']) }
|
|
58
|
+
bash.flat_map { |group| Array(group['hooks']) }
|
|
59
|
+
.find { |hook| hook['command'].to_s.match?(PATTERN) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Claude Code matches `matcher` against the tool name: empty or `*` is every tool, anything
|
|
63
|
+
# else is a pattern such as `Bash` or `Bash|Edit`
|
|
64
|
+
def bash_matcher?(matcher)
|
|
65
|
+
return true if matcher.to_s.empty? || matcher == '*'
|
|
66
|
+
|
|
67
|
+
'Bash'.match?(/\A(?:#{matcher})\z/)
|
|
68
|
+
rescue RegexpError
|
|
69
|
+
false
|
|
57
70
|
end
|
|
58
71
|
end
|
|
59
72
|
end
|
data/lib/gitflash/hook_cli.rb
CHANGED
|
@@ -14,7 +14,12 @@ module Gitflash
|
|
|
14
14
|
true
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
TERMINAL_NOTE = 'gitflash hook claude is run by Claude Code, not by hand. Use ' \
|
|
18
|
+
'`gitflash hook install` to set it up and `gitflash hook status` to check it.'
|
|
19
|
+
|
|
20
|
+
desc 'claude',
|
|
21
|
+
'Run as a Claude Code hook (PreToolUse and PostToolUse; reads the input on stdin)',
|
|
22
|
+
hide: true
|
|
18
23
|
long_desc <<~TEXT
|
|
19
24
|
Reads the Claude Code hook input on stdin. When the Bash command can discard work git
|
|
20
25
|
cannot restore (for example reset --hard, clean -f, checkout -- ., branch -D, stash drop),
|
|
@@ -24,10 +29,17 @@ module Gitflash
|
|
|
24
29
|
--mode ask save a snapshot and ask the user to approve the command
|
|
25
30
|
--mode deny block the command and point to the gitflash equivalent
|
|
26
31
|
|
|
27
|
-
|
|
32
|
+
As a PostToolUse hook it marks the branches the command created (git checkout -b, switch -c,
|
|
33
|
+
branch NAME, worktree add) as agent work, so `gitflash clean --agent` can find them. A branch
|
|
34
|
+
is only marked when it is new (its reflog says it was created a moment ago) and unmarked.
|
|
35
|
+
|
|
36
|
+
Not meant to be run by hand: in a terminal it only prints a note. Errors never block the
|
|
37
|
+
command: they are printed to stderr and the hook exits with 0.
|
|
28
38
|
TEXT
|
|
29
39
|
option :mode, type: :string, default: 'snapshot', enum: Hook::Claude::MODES
|
|
30
40
|
def claude
|
|
41
|
+
return puts(TERMINAL_NOTE) if $stdin.tty?
|
|
42
|
+
|
|
31
43
|
output = Hook::Claude.new(mode: options[:mode]).call($stdin.read)
|
|
32
44
|
puts JSON.generate(output) if output
|
|
33
45
|
rescue StandardError => e
|
|
@@ -36,8 +48,10 @@ module Gitflash
|
|
|
36
48
|
|
|
37
49
|
desc 'install', 'Register the gitflash hook in Claude Code settings'
|
|
38
50
|
long_desc <<~TEXT
|
|
39
|
-
Adds `gitflash hook claude` as a PreToolUse hook
|
|
40
|
-
|
|
51
|
+
Adds `gitflash hook claude` as a PreToolUse hook (snapshots before destructive commands) and
|
|
52
|
+
as a PostToolUse hook (marks branches an agent creates) for Bash commands. Keeps the rest of
|
|
53
|
+
the settings file unchanged and does nothing when both are already installed. Run it again
|
|
54
|
+
to add the PostToolUse hook to an older install.
|
|
41
55
|
|
|
42
56
|
--scope local .claude/settings.local.json at the repository root (default, not
|
|
43
57
|
committed); applies to sessions anywhere in the repository and its worktrees
|
|
@@ -59,7 +73,8 @@ module Gitflash
|
|
|
59
73
|
desc 'status', 'Show whether the gitflash hook protects Claude Code sessions here'
|
|
60
74
|
long_desc <<~TEXT
|
|
61
75
|
Lists the Claude Code settings files that apply to the current directory, whether each
|
|
62
|
-
holds the gitflash
|
|
76
|
+
holds the gitflash hooks (and whether branch marking is on) and which sessions it applies to,
|
|
77
|
+
and whether the gitflash
|
|
63
78
|
executable the hook runs is on PATH. Works outside a git repository (user scope only).
|
|
64
79
|
TEXT
|
|
65
80
|
def status
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Who created a branch: `agent` or `human`. Stored in git config as
|
|
5
|
+
# `branch.<name>.gitflash-owner`, which git removes together with the branch.
|
|
6
|
+
# A branch without a mark counts as human work and is never selected for automatic cleanup.
|
|
7
|
+
class Ownership
|
|
8
|
+
OWNERS = %w[agent human].freeze
|
|
9
|
+
SUFFIX = '.gitflash-owner'
|
|
10
|
+
PREFIX = 'branch.'
|
|
11
|
+
|
|
12
|
+
def initialize(bash: Git::BashCommand)
|
|
13
|
+
@bash = bash
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# { branch name => owner } for every marked branch
|
|
17
|
+
def all
|
|
18
|
+
stdout, _stderr, success = @bash.capture('git', 'config', '--local', '-z', '--get-regexp',
|
|
19
|
+
'^branch\..+\.gitflash-owner$')
|
|
20
|
+
return {} unless success
|
|
21
|
+
|
|
22
|
+
stdout.split("\0").filter_map { |entry| parse(entry) }.to_h
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The branches with their `owner` set from the marks
|
|
26
|
+
def annotate(branches)
|
|
27
|
+
owners = all
|
|
28
|
+
branches.map { |branch| branch.with(owner: owners[branch.name]) }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def mark(branch, owner)
|
|
32
|
+
unless OWNERS.include?(owner)
|
|
33
|
+
raise UsageError.new("Unknown owner '#{owner}' (use #{OWNERS.join(' or ')})",
|
|
34
|
+
code: 'invalid_usage')
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
@bash.exec('git', 'config', '--local', key(branch), owner)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Removes the mark; a branch without a mark is already unmarked
|
|
41
|
+
def clear(branch)
|
|
42
|
+
@bash.capture('git', 'config', '--local', '--unset', key(branch))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def key(branch)
|
|
48
|
+
"#{PREFIX}#{branch}#{SUFFIX}"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Entry from `-z`: "branch.<name>.gitflash-owner\nvalue". Branch names may contain dots.
|
|
52
|
+
def parse(entry)
|
|
53
|
+
full_key, value = entry.split("\n", 2)
|
|
54
|
+
return nil unless full_key.start_with?(PREFIX) && full_key.end_with?(SUFFIX)
|
|
55
|
+
|
|
56
|
+
[full_key.delete_prefix(PREFIX).delete_suffix(SUFFIX), value.to_s.strip]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Branches gitflash never deletes: the current and default branch, `main`, `master`, names
|
|
5
|
+
# matching `protected` in the settings, and any branch checked out in a worktree.
|
|
6
|
+
class Protection
|
|
7
|
+
NAMES = %w[main master].freeze
|
|
8
|
+
|
|
9
|
+
def initialize(config:, checked_out: [])
|
|
10
|
+
@config = config
|
|
11
|
+
@checked_out = checked_out
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def protected?(branch)
|
|
15
|
+
branch.current? || branch.default? || NAMES.include?(branch.name) ||
|
|
16
|
+
@config.protected?(branch.name) || @checked_out.include?(branch.name)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/gitflash/repo.rb
CHANGED
|
@@ -30,9 +30,8 @@ module Gitflash
|
|
|
30
30
|
# merged into the default branch (one extra git call).
|
|
31
31
|
def branches(merged_status: false)
|
|
32
32
|
merged_names = merged_status ? merged_branch_names : nil
|
|
33
|
-
branch_lines.map
|
|
34
|
-
|
|
35
|
-
end
|
|
33
|
+
list = branch_lines.map { |line| Branch.parse(line, default_branch:, merged_names:) }
|
|
34
|
+
Ownership.new(bash: @bash).annotate(list)
|
|
36
35
|
end
|
|
37
36
|
|
|
38
37
|
def current_branch
|
|
@@ -40,12 +39,13 @@ module Gitflash
|
|
|
40
39
|
end
|
|
41
40
|
|
|
42
41
|
def toplevel
|
|
43
|
-
@bash.exec('git', 'rev-parse', '--show-toplevel').
|
|
42
|
+
@bash.exec('git', 'rev-parse', '--show-toplevel').delete_suffix("\n")
|
|
44
43
|
end
|
|
45
44
|
|
|
46
45
|
# Root of the main checkout; differs from toplevel inside a linked worktree
|
|
47
46
|
def main_root
|
|
48
|
-
common = @bash.exec('git', 'rev-parse', '--path-format=absolute',
|
|
47
|
+
common = @bash.exec('git', 'rev-parse', '--path-format=absolute',
|
|
48
|
+
'--git-common-dir').delete_suffix("\n")
|
|
49
49
|
File.basename(common) == '.git' ? File.dirname(common) : toplevel
|
|
50
50
|
end
|
|
51
51
|
|
data/lib/gitflash/restore.rb
CHANGED
|
@@ -20,17 +20,18 @@ module Gitflash
|
|
|
20
20
|
}
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
# Parts of the state this restore changes; the state before an undo is saved with the same
|
|
24
|
+
def scope = @snapshot.scope
|
|
25
|
+
|
|
23
26
|
def empty?
|
|
24
27
|
plan[:branches].empty? && plan[:head].nil? && !plan[:worktree] && plan[:stashes].empty?
|
|
25
28
|
end
|
|
26
29
|
|
|
27
|
-
# Applies the plan; returns nil on success or the
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
end
|
|
33
|
-
nil
|
|
30
|
+
# Applies the plan (`only: :branches` just the branch tips); returns nil on success or the
|
|
31
|
+
# failed step's Result
|
|
32
|
+
def apply(only: nil)
|
|
33
|
+
(only == :branches ? branch_steps : steps).lazy.map { |args| run(*args) }
|
|
34
|
+
.find { |result| !result.success? }
|
|
34
35
|
end
|
|
35
36
|
|
|
36
37
|
private
|
|
@@ -48,13 +48,15 @@ module Gitflash
|
|
|
48
48
|
private
|
|
49
49
|
|
|
50
50
|
def git(*, env: {})
|
|
51
|
-
|
|
51
|
+
# Only git's final newline goes: a path may start or end with whitespace, even a carriage
|
|
52
|
+
# return, which `chomp` would take together with the newline
|
|
53
|
+
@bash.exec('git', *, env: env).delete_suffix("\n")
|
|
52
54
|
end
|
|
53
55
|
|
|
54
56
|
# Stripped stdout, or nil when the command fails
|
|
55
57
|
def try(*)
|
|
56
58
|
stdout, _stderr, success = @bash.capture('git', *)
|
|
57
|
-
success ? stdout.
|
|
59
|
+
success ? stdout.delete_suffix("\n") : nil
|
|
58
60
|
end
|
|
59
61
|
|
|
60
62
|
def rev(ref)
|
|
@@ -93,9 +95,9 @@ module Gitflash
|
|
|
93
95
|
# Tree of the working tree, built in a temporary copy of the index so the real one is untouched
|
|
94
96
|
def worktree_tree
|
|
95
97
|
top = git('rev-parse', '--show-toplevel')
|
|
96
|
-
Tempfile.create('gitflash-index',
|
|
98
|
+
Tempfile.create('gitflash-index', git('rev-parse', '--absolute-git-dir')) do |file|
|
|
97
99
|
file.close
|
|
98
|
-
real_index =
|
|
100
|
+
real_index = git('rev-parse', '--path-format=absolute', '--git-path', 'index')
|
|
99
101
|
FileUtils.cp(real_index, file.path) if File.exist?(real_index)
|
|
100
102
|
env = { 'GIT_INDEX_FILE' => file.path }
|
|
101
103
|
|
|
@@ -122,7 +124,10 @@ module Gitflash
|
|
|
122
124
|
sizes = file_sizes(top, paths)
|
|
123
125
|
small, large = sizes.partition { |_path, size| size <= MAX_UNTRACKED_FILE_BYTES }
|
|
124
126
|
.map { |group| group.map(&:first) }
|
|
125
|
-
|
|
127
|
+
# File names are literal paths, never pathspec magic such as `:(exclude)*`
|
|
128
|
+
small.each_slice(ADD_BATCH_SIZE) do |batch|
|
|
129
|
+
git('--literal-pathspecs', '-C', top, 'add', '--', *batch, env: env)
|
|
130
|
+
end
|
|
126
131
|
large
|
|
127
132
|
end
|
|
128
133
|
end
|
|
@@ -15,5 +15,14 @@ module Gitflash
|
|
|
15
15
|
format(char.ord > 0xFF ? '\\u%04X' : '\\x%02X', char.ord)
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
|
+
|
|
19
|
+
# Text that must stay on one line, such as a path inside a bullet list: as `safe`, and a
|
|
20
|
+
# newline or tab in it is shown as an escape too, so it cannot start a fake row. A literal
|
|
21
|
+
# backslash is doubled first, so two different texts never look the same (a real newline
|
|
22
|
+
# shows as `\n`, the two characters backslash and n as `\\n`); that keeps menu labels unique.
|
|
23
|
+
def line(text)
|
|
24
|
+
doubled = text.to_s.scrub.gsub('\\') { '\\\\' }
|
|
25
|
+
safe(doubled).gsub("\n") { '\\n' }.gsub("\t") { '\\t' }
|
|
26
|
+
end
|
|
18
27
|
end
|
|
19
28
|
end
|
data/lib/gitflash/ui.rb
CHANGED
|
@@ -69,7 +69,7 @@ module Gitflash
|
|
|
69
69
|
message = "#{summary}\nRe-run with --yes to confirm."
|
|
70
70
|
raise ConfirmationRequired.new(message, plan: plan) unless interactive?
|
|
71
71
|
|
|
72
|
-
prompt.proceed_with_warning(summary) { true } == true
|
|
72
|
+
prompt.proceed_with_warning(TerminalText.safe(summary)) { true } == true
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
private
|
data/lib/gitflash/version.rb
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# A checkout of the repository as reported by `git worktree list --porcelain`.
|
|
5
|
+
# `branch` is nil for a detached HEAD. `missing` means the directory is gone (git calls it
|
|
6
|
+
# prunable). `dirty` is nil when it cannot be known (missing directory or bare repository).
|
|
7
|
+
# ahead, behind, merged and owner are those of the branch, nil without a branch.
|
|
8
|
+
Worktree = Data.define(
|
|
9
|
+
:path, :head, :branch, :main, :bare, :locked, :lock_reason, :missing,
|
|
10
|
+
:dirty, :ahead, :behind, :merged, :owner
|
|
11
|
+
) do
|
|
12
|
+
alias_method :main?, :main
|
|
13
|
+
alias_method :locked?, :locked
|
|
14
|
+
alias_method :missing?, :missing
|
|
15
|
+
alias_method :dirty?, :dirty
|
|
16
|
+
|
|
17
|
+
# Why this worktree must not be removed, nil when it can go. `current` is true for the
|
|
18
|
+
# worktree gitflash runs in. The main checkout and the current one are never removable.
|
|
19
|
+
def removal_blocker(force:, current:)
|
|
20
|
+
return "#{TerminalText.line(path)} is the main checkout" if main?
|
|
21
|
+
return "#{TerminalText.line(path)} is the current worktree" if current
|
|
22
|
+
|
|
23
|
+
in_use_blocker unless force || (missing? && !locked?)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def in_use_blocker
|
|
27
|
+
return "#{TerminalText.line(path)} is locked (use --force)" if locked?
|
|
28
|
+
|
|
29
|
+
"#{TerminalText.line(path)} has uncommitted changes (use --force)" if dirty? != false
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'thor'
|
|
4
|
+
|
|
5
|
+
module Gitflash
|
|
6
|
+
# `gitflash worktree ...` (alias `wt`): worktrees for parallel agent work
|
|
7
|
+
class WorktreeCli < Thor
|
|
8
|
+
extend Configuration::Descriptions
|
|
9
|
+
include CommandRunner
|
|
10
|
+
|
|
11
|
+
namespace 'worktree'
|
|
12
|
+
|
|
13
|
+
def self.exit_on_failure?
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
desc 'list', 'List worktrees with branch, dirty and merge state'
|
|
18
|
+
long_desc <<~TEXT
|
|
19
|
+
Lists every worktree of the repository: absolute path, branch, HEAD, whether it has
|
|
20
|
+
uncommitted changes, ahead/behind counts, whether its branch is merged into the default
|
|
21
|
+
branch, lock state and whether its directory is missing.
|
|
22
|
+
|
|
23
|
+
Agents use the absolute paths from `--json`; no directory switching is needed.
|
|
24
|
+
|
|
25
|
+
Accepts the global --json option after the subcommand: gitflash wt list --json
|
|
26
|
+
TEXT
|
|
27
|
+
def list
|
|
28
|
+
run_command('worktree list', Commands::WorktreeList)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
desc 'add BRANCH', 'Add a worktree for a branch (existing, remote or new)'
|
|
32
|
+
long_desc <<~TEXT
|
|
33
|
+
Adds a worktree for BRANCH. An existing local branch is checked out; a branch that exists
|
|
34
|
+
only on origin is created to track it; any other name creates a new branch from HEAD
|
|
35
|
+
(or from --from). New branches are marked as agent work so `gitflash clean` can find them.
|
|
36
|
+
|
|
37
|
+
The directory defaults to ../<repo>.worktrees/<branch> (setting `worktree_dir` in
|
|
38
|
+
.gitflash.yml changes it). The result lists the absolute path: use it, no directory
|
|
39
|
+
switching is needed.
|
|
40
|
+
|
|
41
|
+
Needs no confirmation. Accepts the global --json and --dry-run options after the
|
|
42
|
+
subcommand: gitflash wt add feature --json
|
|
43
|
+
TEXT
|
|
44
|
+
option :path, type: :string, banner: 'DIR', desc: 'Directory for the worktree'
|
|
45
|
+
option :from, type: :string, banner: 'REF', desc: 'Start a new branch from REF (default HEAD)'
|
|
46
|
+
option :owner, type: :string, enum: Ownership::OWNERS,
|
|
47
|
+
desc: 'Mark a new branch as agent (default) or human work'
|
|
48
|
+
def add(branch = nil)
|
|
49
|
+
run_command('worktree add', Commands::WorktreeAdd, *[branch].compact)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
desc 'lock WORKTREE', 'Lock a worktree so prune, remove and clean leave it alone'
|
|
53
|
+
long_desc 'Locks the worktree given as a path or branch. Add --reason to say why.'
|
|
54
|
+
option :reason, type: :string, desc: 'Why the worktree is locked'
|
|
55
|
+
def lock(worktree = nil)
|
|
56
|
+
run_command('worktree lock', Commands::WorktreeLock, *[worktree].compact)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
desc 'unlock WORKTREE', 'Unlock a worktree'
|
|
60
|
+
def unlock(worktree = nil)
|
|
61
|
+
run_command('worktree unlock', Commands::WorktreeUnlock, *[worktree].compact)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
desc 'move WORKTREE PATH', 'Move a worktree to another directory'
|
|
65
|
+
long_desc <<~TEXT
|
|
66
|
+
Moves the worktree (a path or branch) to PATH. The main checkout and the current worktree
|
|
67
|
+
cannot be moved; a locked worktree needs --force.
|
|
68
|
+
TEXT
|
|
69
|
+
option :force, type: :boolean, default: false, desc: 'Also move a locked worktree'
|
|
70
|
+
def move(worktree = nil, destination = nil)
|
|
71
|
+
run_command('worktree move', Commands::WorktreeMove, *[worktree, destination].compact)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
desc 'prune', 'Forget worktrees whose directory is gone'
|
|
75
|
+
long_desc <<~TEXT
|
|
76
|
+
Removes git's record of worktrees whose directory no longer exists. Locked worktrees stay.
|
|
77
|
+
Branches are not touched. Asks for confirmation; pass --yes to skip it, or --dry-run.
|
|
78
|
+
TEXT
|
|
79
|
+
def prune
|
|
80
|
+
run_command('worktree prune', Commands::WorktreePrune)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
desc 'clean', 'Remove worktrees whose branch is merged, gone, stale or agent-created'
|
|
84
|
+
long_desc <<~TEXT
|
|
85
|
+
Removes worktrees whose branch matches the criteria of `gitflash clean`: merged into the
|
|
86
|
+
default branch or upstream gone by default, plus --stale [DAYS] and --agent. The main
|
|
87
|
+
checkout, the current worktree and worktrees on protected branches are never removed;
|
|
88
|
+
locked worktrees and worktrees with uncommitted changes are skipped (and listed with the
|
|
89
|
+
reason) unless --force is given.
|
|
90
|
+
|
|
91
|
+
Each removal saves a snapshot first, as `wt remove` does. Branches are kept: run
|
|
92
|
+
`gitflash clean` afterwards to delete them. Missing directories are handled by `wt prune`.
|
|
93
|
+
|
|
94
|
+
Asks for confirmation; pass --yes to skip it, or --dry-run.
|
|
95
|
+
TEXT
|
|
96
|
+
option :merged, type: :boolean, desc: 'Branches merged into the default branch'
|
|
97
|
+
option :gone, type: :boolean, desc: 'Branches whose upstream branch was deleted'
|
|
98
|
+
option :stale, type: :string, banner: 'DAYS', lazy_default: '',
|
|
99
|
+
desc: 'Branches without commits for DAYS days (default: stale_days setting)'
|
|
100
|
+
option :agent, type: :boolean, desc: 'Branches created by an agent'
|
|
101
|
+
option :force, type: :boolean, default: false,
|
|
102
|
+
desc: 'Also remove locked worktrees and worktrees with uncommitted changes'
|
|
103
|
+
def clean
|
|
104
|
+
run_command('worktree clean', Commands::WorktreeClean)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
desc 'remove [WORKTREE...]', 'Remove worktrees (paths or branch names), saving a snapshot first'
|
|
108
|
+
long_desc descriptions.worktree_remove.long
|
|
109
|
+
option :force, type: :boolean, default: false,
|
|
110
|
+
desc: 'Remove locked worktrees and worktrees with uncommitted changes'
|
|
111
|
+
def remove(*worktrees)
|
|
112
|
+
run_command('worktree remove', Commands::WorktreeRemove, *worktrees)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitflash
|
|
4
|
+
# Brings back a worktree that was removed after a snapshot of it: adds the worktree again on
|
|
5
|
+
# its branch (or detached HEAD) and restores its files. Branch tips are restored before the
|
|
6
|
+
# worktree is added, so a branch deleted or moved since is checked out at its saved tip and its
|
|
7
|
+
# files always match it.
|
|
8
|
+
class WorktreeRevival
|
|
9
|
+
def initialize(snapshot, repo:)
|
|
10
|
+
@snapshot = snapshot
|
|
11
|
+
@repo = repo
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Same shape as an undo plan, plus `recreate_worktree`
|
|
15
|
+
def plan
|
|
16
|
+
head = @snapshot.head
|
|
17
|
+
{ branches: Restore.new(@snapshot).plan[:branches],
|
|
18
|
+
head: { from: nil, to: head[:branch] || head[:sha] },
|
|
19
|
+
worktree: !@snapshot.worktree.nil?, stashes: [], snapshot: @snapshot.id,
|
|
20
|
+
recreate_worktree: @snapshot.worktree_path }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def scope
|
|
24
|
+
%w[branches]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def empty?
|
|
28
|
+
false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Returns nil on success or the failed step's Result
|
|
32
|
+
def apply
|
|
33
|
+
failure = Restore.new(@snapshot).apply(only: :branches)
|
|
34
|
+
return failure if failure
|
|
35
|
+
|
|
36
|
+
result = attach
|
|
37
|
+
return result unless result.success?
|
|
38
|
+
|
|
39
|
+
restore = Restore.new(@snapshot, bash: Git::InDirectory.new(@snapshot.worktree_path))
|
|
40
|
+
restore.apply
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def attach
|
|
46
|
+
worktrees = Worktrees.new(repo: @repo)
|
|
47
|
+
path = @snapshot.worktree_path
|
|
48
|
+
branch = @snapshot.head[:branch]
|
|
49
|
+
sha = @snapshot.head[:sha]
|
|
50
|
+
return worktrees.attach(path, detach_at: sha) unless branch
|
|
51
|
+
|
|
52
|
+
worktrees.attach(path, branch: branch, create_at: sha)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|