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).
307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/braid/operations.rb', line 307 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 # Yes, if path == '', "git read-tree --prefix=/" works. :/ invoke(:read_tree, "--prefix=#{path}/", update_worktree ? '-u' : '-i', item) end end |
#BlobWithMode ⇒ Object
Allow the class to be referenced as ‘git.BlobWithMode`.
278 279 280 |
# File 'lib/braid/operations.rb', line 278 def BlobWithMode Git::BlobWithMode end |
#branch ⇒ Object
394 395 396 397 |
# File 'lib/braid/operations.rb', line 394 def branch status, out, err = exec!("git branch | grep '*'") out[2..-1] end |
#checkout(treeish) ⇒ Object
208 209 210 211 |
# File 'lib/braid/operations.rb', line 208 def checkout(treeish) invoke(:checkout, treeish) true end |
#clone(*args) ⇒ Object
399 400 401 402 |
# File 'lib/braid/operations.rb', line 399 def clone(*args) # overrides builtin invoke(:clone, *args) end |
#commit(message, *args) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/braid/operations.rb', line 181 def commit(, *args) cmd = 'git commit --no-verify' if # allow nil = Tempfile.new('braid_commit') .print("Braid: #{}") .flush .close cmd << " -F #{.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
355 356 357 |
# File 'lib/braid/operations.rb', line 355 def config(args) invoke(:config, args) rescue nil end |
#diff_to_stdout(*args) ⇒ Object
375 376 377 378 379 |
# File 'lib/braid/operations.rb', line 375 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
386 387 388 |
# File 'lib/braid/operations.rb', line 386 def ensure_clean! status_clean? || raise(LocalChangesPresent) end |
#fetch(remote = nil, *args) ⇒ Object
203 204 205 206 |
# File 'lib/braid/operations.rb', line 203 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.)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/braid/operations.rb', line 285 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
390 391 392 |
# File 'lib/braid/operations.rb', line 390 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.
168 169 170 |
# File 'lib/braid/operations.rb', line 168 def is_inside_worktree invoke(:rev_parse, '--is-inside-work-tree') == 'true' end |
#make_tree_with_item(main_content, item_path, item) ⇒ Object
344 345 346 347 348 349 350 351 352 353 |
# File 'lib/braid/operations.rb', line 344 def make_tree_with_item(main_content, item_path, item) with_temporary_index do if main_content 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.
214 215 216 217 218 |
# File 'lib/braid/operations.rb', line 214 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.
258 259 260 261 262 263 264 |
# File 'lib/braid/operations.rb', line 258 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
266 267 268 |
# File 'lib/braid/operations.rb', line 266 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.
324 325 326 327 |
# File 'lib/braid/operations.rb', line 324 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.
177 178 179 |
# File 'lib/braid/operations.rb', line 177 def relative_working_dir invoke(:rev_parse, '--show-prefix') end |
#remote_add(remote, path) ⇒ Object
Implies tracking.
227 228 229 230 |
# File 'lib/braid/operations.rb', line 227 def remote_add(remote, path) invoke(:remote, 'add', remote, path) true end |
#remote_rm(remote) ⇒ Object
232 233 234 235 |
# File 'lib/braid/operations.rb', line 232 def remote_rm(remote) invoke(:remote, 'rm', remote) true end |
#remote_url(remote) ⇒ Object
Checks git remotes.
238 239 240 241 242 243 |
# File 'lib/braid/operations.rb', line 238 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.
154 155 156 157 158 159 160 161 |
# File 'lib/braid/operations.rb', line 154 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
245 246 247 248 |
# File 'lib/braid/operations.rb', line 245 def reset_hard(target) invoke(:reset, '--hard', target) true end |
#rev_parse(opt) ⇒ Object
220 221 222 223 224 |
# File 'lib/braid/operations.rb', line 220 def rev_parse(opt) invoke(:rev_parse, opt) rescue ShellExecutionError raise UnknownRevision, opt end |
#rm_r(path) ⇒ Object
359 360 361 362 |
# File 'lib/braid/operations.rb', line 359 def rm_r(path) invoke(:rm, '-r', path) true end |
#rm_r_cached(path) ⇒ Object
Remove from index only.
365 366 367 368 |
# File 'lib/braid/operations.rb', line 365 def rm_r_cached(path) invoke(:rm, '-r', '--cached', path) true end |
#status_clean? ⇒ Boolean
381 382 383 384 |
# File 'lib/braid/operations.rb', line 381 def status_clean? status, out, err = exec('git status') !out.split("\n").grep(/nothing to commit/).empty? end |
#tree_hash(path, treeish = 'HEAD') ⇒ Object
370 371 372 373 |
# File 'lib/braid/operations.rb', line 370 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.
335 336 337 338 339 340 341 342 |
# File 'lib/braid/operations.rb', line 335 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.
330 331 332 |
# File 'lib/braid/operations.rb', line 330 def write_tree invoke(:write_tree) end |