Class: Ace::Git::Worktree::Atoms::PathExpander
- Inherits:
-
Object
- Object
- Ace::Git::Worktree::Atoms::PathExpander
- Defined in:
- lib/ace/git/worktree/atoms/path_expander.rb
Overview
Path expansion and validation for worktree operations
Simplified implementation focused on worktree-specific needs without over-engineered security patterns.
Class Method Summary collapse
-
.expand(path, base = nil) ⇒ String
Expand a path using standard Ruby path expansion.
-
.relative_to_git_root(path, git_root) ⇒ String
Get a relative path from git root.
-
.resolve(path, base = Dir.pwd) ⇒ String
Resolve a path relative to a base directory.
-
.safe_path?(path) ⇒ Boolean
Simple path safety validation Only checks for obviously dangerous patterns.
-
.validate_for_worktree(path, git_root = nil) ⇒ Hash
Validate a path for worktree creation.
-
.writable?(path, create_if_missing: false) ⇒ Boolean
Check if a path is writable.
Class Method Details
.expand(path, base = nil) ⇒ String
Expand a path using standard Ruby path expansion
26 27 28 29 |
# File 'lib/ace/git/worktree/atoms/path_expander.rb', line 26 def (path, base = nil) return "" if path.nil? || path.empty? base ? File.(path, base) : File.(path) end |
.relative_to_git_root(path, git_root) ⇒ String
Get a relative path from git root
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ace/git/worktree/atoms/path_expander.rb', line 148 def relative_to_git_root(path, git_root) = (path) = (git_root) # Use File.expand_path for consistent comparison normalized_path = File.() normalized_root = File.() if normalized_path.start_with?(normalized_root + "/") || normalized_path == normalized_root if normalized_path == normalized_root "." else relative_path = normalized_path[normalized_root.length..-1] relative_path.start_with?("/") ? relative_path[1..-1] : relative_path end else end end |
.resolve(path, base = Dir.pwd) ⇒ String
Resolve a path relative to a base directory
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ace/git/worktree/atoms/path_expander.rb', line 36 def resolve(path, base = Dir.pwd) = (path) = (base) if File.absolute_path?() else File.(path, ) end end |
.safe_path?(path) ⇒ Boolean
Simple path safety validation Only checks for obviously dangerous patterns
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/ace/git/worktree/atoms/path_expander.rb', line 173 def safe_path?(path) return false if path.nil? || path.empty? path_str = path.to_s # Check for null bytes and obviously dangerous patterns return false if path_str.include?("\x00") return false if path_str.include?("../../../") true end |
.validate_for_worktree(path, git_root = nil) ⇒ Hash
Validate a path for worktree creation
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 |
# File 'lib/ace/git/worktree/atoms/path_expander.rb', line 82 def validate_for_worktree(path, git_root = nil) = (path) # Check if parent directory exists and is writable parent_dir = File.dirname() unless File.exist?(parent_dir) return { valid: false, error: "Parent directory does not exist: #{parent_dir}", expanded_path: } end unless writable?(parent_dir) return { valid: false, error: "Parent directory is not writable: #{parent_dir}", expanded_path: } end # Check if worktree is being created directly in git root (not allowed) # Allow worktrees in subdirectories or outside git root entirely if git_root = File.(git_root) = File.() # Only prevent creation directly at git root, not in subdirectories if == return { valid: false, error: "Worktree cannot be created at git repository root. Use a subdirectory or different location.", expanded_path: } end end # Check if path already exists if File.exist?() if File.directory?() && !Dir.empty?() return { valid: false, error: "Directory already exists and is not empty: #{}", expanded_path: } elsif File.file?() return { valid: false, error: "Path exists but is a file: #{}", expanded_path: } end end { valid: true, error: nil, expanded_path: } end |
.writable?(path, create_if_missing: false) ⇒ Boolean
Check if a path is writable
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ace/git/worktree/atoms/path_expander.rb', line 52 def writable?(path, create_if_missing: false) = (path) begin # Check if path exists unless File.exist?() if create_if_missing FileUtils.mkdir_p() else return false end end # Test writability by trying to create a temp file temp_file = File.join(, ".ace_git_worktree_test_#{Time.now.to_i}_#{$$}") File.write(temp_file, "test") File.delete(temp_file) true rescue Errno::EACCES, Errno::EPERM false rescue false end end |