Class: Braid::Operations::Git
Defined Under Namespace
Classes: BlobWithMode
Instance Method Summary collapse
-
#add_item_to_index(item, path, update_worktree) ⇒ Object
Add the item (as returned by get_tree_item) to the index at the given path.
-
#BlobWithMode ⇒ Object
Allow the class to be referenced as
git.BlobWithMode. - #branch ⇒ Object
- #checkout(treeish) ⇒ Object
- #clone(*args) ⇒ Object
- #commit(message, *args) ⇒ Object
- #config(args) ⇒ Object
- #diff_to_stdout(*args) ⇒ Object
- #ensure_clean! ⇒ Object
- #fetch(remote = nil, *args) ⇒ Object
-
#get_tree_item(tree, path) ⇒ Object
Get the item at the given path in the given tree.
- #head ⇒ Object
-
#is_inside_worktree ⇒ Object
If the current directory is not inside a git repository at all, this command will fail with “fatal: Not a git repository” and that will be propagated as a ShellExecutionError.
- #make_tree_with_item(main_content, item_path, item) ⇒ Object
-
#merge_base(target, source) ⇒ Object
Returns the base commit or nil.
-
#merge_trees(base_treeish, local_treeish, remote_treeish) ⇒ Object
Merge three trees (local_treeish should match the current state of the index) and update the index and working tree.
- #read_ls_files(prefix) ⇒ Object
-
#read_tree_im(treeish) ⇒ Object
Read tree into the root of the index.
-
#relative_working_dir ⇒ Object
Get the prefix of the current directory relative to the worktree.
-
#remote_add(remote, path) ⇒ Object
Implies tracking.
- #remote_rm(remote) ⇒ Object
-
#remote_url(remote) ⇒ Object
Checks git remotes.
-
#repo_file_path(path) ⇒ Object
Get the physical path to a file in the git repository (e.g., ‘MERGE_MSG’), taking into account worktree configuration.
- #reset_hard(target) ⇒ Object
- #rev_parse(opt) ⇒ Object
- #rm_r(path) ⇒ Object
-
#rm_r_cached(path) ⇒ Object
Remove from index only.
- #status_clean? ⇒ Boolean
- #tree_hash(path, treeish = 'HEAD') ⇒ Object
-
#with_temporary_index ⇒ Object
Execute a block using a temporary git index file, initially empty.
-
#write_tree ⇒ Object
Write a tree object for the current index and return its ID.
Methods inherited from Proxy
command, #require_version, #require_version!, #version
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Braid::Operations::Proxy
Instance Method Details
#add_item_to_index(item, path, update_worktree) ⇒ Object
Add the item (as returned by get_tree_item) to the index at the given path. If update_worktree is true, then update the worktree, otherwise disregard the state of the worktree (most useful with a temporary index file).
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/braid/operations.rb', line 294 def add_item_to_index(item, path, update_worktree) if item.is_a?(BlobWithMode) # Our minimum git version is 1.6.0 and the new --cacheinfo syntax # wasn't added until 2.0.0. invoke(:update_index, '--add', '--cacheinfo', item.mode, item.hash, path) if update_worktree # XXX If this fails, we've already updated the index. invoke(:checkout_index, path) end else # According to # https://lore.kernel.org/git/e48a281a4d3db0a04c0609fcb8658e4fcc797210.1646166271.git.gitgitgadget@gmail.com/, # `--prefix=` is valid if the path is empty. invoke(:read_tree, "--prefix=#{path}", update_worktree ? '-u' : '-i', item) end end |
#BlobWithMode ⇒ Object
Allow the class to be referenced as git.BlobWithMode.
265 266 267 |
# File 'lib/braid/operations.rb', line 265 def BlobWithMode Git::BlobWithMode end |
#branch ⇒ Object
386 387 388 389 |
# File 'lib/braid/operations.rb', line 386 def branch _, out, _ = exec!("git branch | grep '*'") out[2..-1] end |
#checkout(treeish) ⇒ Object
195 196 197 198 |
# File 'lib/braid/operations.rb', line 195 def checkout(treeish) invoke(:checkout, treeish) true end |
#clone(*args) ⇒ Object
391 392 393 394 |
# File 'lib/braid/operations.rb', line 391 def clone(*args) # overrides builtin invoke(:clone, *args) end |
#commit(message, *args) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/braid/operations.rb', line 167 def commit(, *args) cmd = 'git commit --no-verify' = nil if # allow nil = Tempfile.new('braid_commit') .print("Braid: #{message}") .flush .close cmd << " -F #{message_file.path}" end cmd << " #{args.join(' ')}" unless args.empty? status, out, err = exec(cmd) .unlink if if status == 0 true elsif out.match(/nothing.* to commit/) false else raise ShellExecutionError, err end end |
#config(args) ⇒ Object
347 348 349 |
# File 'lib/braid/operations.rb', line 347 def config(args) invoke(:config, args) rescue nil end |
#diff_to_stdout(*args) ⇒ Object
367 368 369 370 371 |
# File 'lib/braid/operations.rb', line 367 def diff_to_stdout(*args) # For now, ignore the exit code. It can be 141 (SIGPIPE) if the user # quits the pager before reading all the output. system("git diff #{args.join(' ')}") end |
#ensure_clean! ⇒ Object
378 379 380 |
# File 'lib/braid/operations.rb', line 378 def ensure_clean! status_clean? || raise(LocalChangesPresent) end |
#fetch(remote = nil, *args) ⇒ Object
190 191 192 193 |
# File 'lib/braid/operations.rb', line 190 def fetch(remote = nil, *args) args.unshift "-n #{remote}" if remote exec!("git fetch #{args.join(' ')}") end |
#get_tree_item(tree, path) ⇒ Object
Get the item at the given path in the given tree. If it’s a tree, just return its hash; if it’s a blob, return a BlobWithMode object. (This is how we remember the mode for single-file mirrors.)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/braid/operations.rb', line 272 def get_tree_item(tree, path) if path.nil? || path == '' tree else m = /^([^ ]*) ([^ ]*) ([^\t]*)\t.*$/.match(invoke(:ls_tree, tree, path)) mode = m[1] type = m[2] hash = m[3] if type == 'tree' hash elsif type == 'blob' return BlobWithMode.new(hash, mode) else raise ShellExecutionError, 'Tree item is not a tree or a blob' end end end |
#head ⇒ Object
382 383 384 |
# File 'lib/braid/operations.rb', line 382 def head rev_parse('HEAD') end |
#is_inside_worktree ⇒ Object
If the current directory is not inside a git repository at all, this command will fail with “fatal: Not a git repository” and that will be propagated as a ShellExecutionError. is_inside_worktree can return false when inside a bare repository and in certain other rare cases such as when the GIT_WORK_TREE environment variable is set.
154 155 156 |
# File 'lib/braid/operations.rb', line 154 def is_inside_worktree invoke(:rev_parse, '--is-inside-work-tree') == 'true' end |
#make_tree_with_item(main_content, item_path, item) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/braid/operations.rb', line 333 def make_tree_with_item(main_content, item_path, item) with_temporary_index do # If item_path is '', then rm_r_cached will fail. But in that case, # we can skip loading the main content because it would be deleted # anyway. if main_content && item_path != '' read_tree_im(main_content) rm_r_cached(item_path) end add_item_to_index(item, item_path, false) write_tree end end |
#merge_base(target, source) ⇒ Object
Returns the base commit or nil.
201 202 203 204 205 |
# File 'lib/braid/operations.rb', line 201 def merge_base(target, source) invoke(:merge_base, target, source) rescue ShellExecutionError nil end |
#merge_trees(base_treeish, local_treeish, remote_treeish) ⇒ Object
Merge three trees (local_treeish should match the current state of the index) and update the index and working tree.
The usage of ‘git merge-recursive’ doesn’t seem to be officially documented, but it does accept trees. When a single base is passed, the ‘recursive’ part (i.e., merge of bases) does not come into play and only the trees matter. But for some reason, Git’s smartest tree merge algorithm is only available via the ‘recursive’ strategy.
245 246 247 248 249 250 251 |
# File 'lib/braid/operations.rb', line 245 def merge_trees(base_treeish, local_treeish, remote_treeish) invoke(:merge_recursive, base_treeish, "-- #{local_treeish} #{remote_treeish}") true rescue ShellExecutionError => error # 'CONFLICT' messages go to stdout. raise MergeError, error.out end |
#read_ls_files(prefix) ⇒ Object
253 254 255 |
# File 'lib/braid/operations.rb', line 253 def read_ls_files(prefix) invoke('ls-files', prefix) end |
#read_tree_im(treeish) ⇒ Object
Read tree into the root of the index. This may not be the preferred way to do it, but it seems to work.
313 314 315 316 |
# File 'lib/braid/operations.rb', line 313 def read_tree_im(treeish) invoke(:read_tree, '-im', treeish) true end |
#relative_working_dir ⇒ Object
Get the prefix of the current directory relative to the worktree. Empty string if it’s the root of the worktree, otherwise ends with a slash. In some cases in which the current directory is not inside a worktree at all, this will successfully return an empty string, so it may be desirable to check is_inside_worktree first.
163 164 165 |
# File 'lib/braid/operations.rb', line 163 def relative_working_dir invoke(:rev_parse, '--show-prefix') end |
#remote_add(remote, path) ⇒ Object
Implies tracking.
214 215 216 217 |
# File 'lib/braid/operations.rb', line 214 def remote_add(remote, path) invoke(:remote, 'add', remote, path) true end |
#remote_rm(remote) ⇒ Object
219 220 221 222 |
# File 'lib/braid/operations.rb', line 219 def remote_rm(remote) invoke(:remote, 'rm', remote) true end |
#remote_url(remote) ⇒ Object
Checks git remotes.
225 226 227 228 229 230 |
# File 'lib/braid/operations.rb', line 225 def remote_url(remote) key = "remote.#{remote}.url" invoke(:config, key) rescue ShellExecutionError nil end |
#repo_file_path(path) ⇒ Object
Get the physical path to a file in the git repository (e.g., ‘MERGE_MSG’), taking into account worktree configuration. The returned path may be absolute or relative to the current working directory.
140 141 142 143 144 145 146 147 |
# File 'lib/braid/operations.rb', line 140 def repo_file_path(path) if require_version('2.5') # support for --git-path invoke(:rev_parse, '--git-path', path) else # Git < 2.5 doesn't support linked worktrees anyway. File.join(invoke(:rev_parse, '--git-dir'), path) end end |
#reset_hard(target) ⇒ Object
232 233 234 235 |
# File 'lib/braid/operations.rb', line 232 def reset_hard(target) invoke(:reset, '--hard', target) true end |
#rev_parse(opt) ⇒ Object
207 208 209 210 211 |
# File 'lib/braid/operations.rb', line 207 def rev_parse(opt) invoke(:rev_parse, opt) rescue ShellExecutionError raise UnknownRevision, opt end |
#rm_r(path) ⇒ Object
351 352 353 354 |
# File 'lib/braid/operations.rb', line 351 def rm_r(path) invoke(:rm, '-r', path) true end |
#rm_r_cached(path) ⇒ Object
Remove from index only.
357 358 359 360 |
# File 'lib/braid/operations.rb', line 357 def rm_r_cached(path) invoke(:rm, '-r', '--cached', path) true end |
#status_clean? ⇒ Boolean
373 374 375 376 |
# File 'lib/braid/operations.rb', line 373 def status_clean? _, out, _ = exec('git status') !out.split("\n").grep(/nothing to commit/).empty? end |
#tree_hash(path, treeish = 'HEAD') ⇒ Object
362 363 364 365 |
# File 'lib/braid/operations.rb', line 362 def tree_hash(path, treeish = 'HEAD') out = invoke(:ls_tree, treeish, '-d', path) out.split[2] end |
#with_temporary_index ⇒ Object
Execute a block using a temporary git index file, initially empty.
324 325 326 327 328 329 330 331 |
# File 'lib/braid/operations.rb', line 324 def with_temporary_index Dir.mktmpdir('braid_index') do |dir| Operations::with_modified_environment( {'GIT_INDEX_FILE' => File.join(dir, 'index')}) do yield end end end |
#write_tree ⇒ Object
Write a tree object for the current index and return its ID.
319 320 321 |
# File 'lib/braid/operations.rb', line 319 def write_tree invoke(:write_tree) end |