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).
293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/braid/operations.rb', line 293 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`.
264 265 266 |
# File 'lib/braid/operations.rb', line 264 def BlobWithMode Git::BlobWithMode end |
#branch ⇒ Object
383 384 385 386 |
# File 'lib/braid/operations.rb', line 383 def branch status, out, err = exec!("git branch | grep '*'") out[2..-1] end |
#checkout(treeish) ⇒ Object
194 195 196 197 |
# File 'lib/braid/operations.rb', line 194 def checkout(treeish) invoke(:checkout, treeish) true end |
#clone(*args) ⇒ Object
388 389 390 391 |
# File 'lib/braid/operations.rb', line 388 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 |
# File 'lib/braid/operations.rb', line 167 def commit(, *args) cmd = 'git commit --no-verify' 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
344 345 346 |
# File 'lib/braid/operations.rb', line 344 def config(args) invoke(:config, args) rescue nil end |
#diff_to_stdout(*args) ⇒ Object
364 365 366 367 368 |
# File 'lib/braid/operations.rb', line 364 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
375 376 377 |
# File 'lib/braid/operations.rb', line 375 def ensure_clean! status_clean? || raise(LocalChangesPresent) end |
#fetch(remote = nil, *args) ⇒ Object
189 190 191 192 |
# File 'lib/braid/operations.rb', line 189 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.)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/braid/operations.rb', line 271 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
379 380 381 |
# File 'lib/braid/operations.rb', line 379 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
330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/braid/operations.rb', line 330 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.
200 201 202 203 204 |
# File 'lib/braid/operations.rb', line 200 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.
244 245 246 247 248 249 250 |
# File 'lib/braid/operations.rb', line 244 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
252 253 254 |
# File 'lib/braid/operations.rb', line 252 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.
310 311 312 313 |
# File 'lib/braid/operations.rb', line 310 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.
213 214 215 216 |
# File 'lib/braid/operations.rb', line 213 def remote_add(remote, path) invoke(:remote, 'add', remote, path) true end |
#remote_rm(remote) ⇒ Object
218 219 220 221 |
# File 'lib/braid/operations.rb', line 218 def remote_rm(remote) invoke(:remote, 'rm', remote) true end |
#remote_url(remote) ⇒ Object
Checks git remotes.
224 225 226 227 228 229 |
# File 'lib/braid/operations.rb', line 224 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
231 232 233 234 |
# File 'lib/braid/operations.rb', line 231 def reset_hard(target) invoke(:reset, '--hard', target) true end |
#rev_parse(opt) ⇒ Object
206 207 208 209 210 |
# File 'lib/braid/operations.rb', line 206 def rev_parse(opt) invoke(:rev_parse, opt) rescue ShellExecutionError raise UnknownRevision, opt end |
#rm_r(path) ⇒ Object
348 349 350 351 |
# File 'lib/braid/operations.rb', line 348 def rm_r(path) invoke(:rm, '-r', path) true end |
#rm_r_cached(path) ⇒ Object
Remove from index only.
354 355 356 357 |
# File 'lib/braid/operations.rb', line 354 def rm_r_cached(path) invoke(:rm, '-r', '--cached', path) true end |
#status_clean? ⇒ Boolean
370 371 372 373 |
# File 'lib/braid/operations.rb', line 370 def status_clean? status, out, err = exec('git status') !out.split("\n").grep(/nothing to commit/).empty? end |
#tree_hash(path, treeish = 'HEAD') ⇒ Object
359 360 361 362 |
# File 'lib/braid/operations.rb', line 359 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.
321 322 323 324 325 326 327 328 |
# File 'lib/braid/operations.rb', line 321 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.
316 317 318 |
# File 'lib/braid/operations.rb', line 316 def write_tree invoke(:write_tree) end |