class GitHelpers::GitDir
these raw helpers are not called since we usually use higher level commands that provide all infos at once
Attributes
Public Class Methods
# File lib/git_helpers/git_dir.rb, line 17 def initialize(dir=".") self.dir=dir end
Public Instance Methods
# File lib/git_helpers/git_dir.rb, line 106 def all_files run_simple("git ls-files -z").split("\0") end
are we in a bare repo?
# File lib/git_helpers/git_dir.rb, line 86 def bare? infos[:is_bare] end
# File lib/git_helpers/git_dir.rb, line 172 def branch(branch="HEAD") GitBranch.new(branch, dir: self) end
deprecated run head.name instead
# File lib/git_helpers/git_dir.rb, line 133 def current_branch(always: true) branchname= run_simple("git symbolic-ref -q --short HEAD", chomp: true) branchname= run_simple("git rev-parse --short --verify HEAD", chomp: true) if always and branchname.empty? return branch(branchname) end
# File lib/git_helpers/git_dir.rb, line 21 def dir=(dir) @reldir=Pathname.new(dir.to_s) @dir=begin @reldir.realpath rescue @reldir end end def to_s @dir.to_s end #we could also use 'git -C #{@dir}' for each git invocation def with_dir Dir.chdir(@dir) { yield self } end #reset all caches def reset! @infos=nil @head=nil end def infos(*args) return @infos if @infos @infos=infos!(*args) end def run(*args, run_command: :run, **opts, &b) SH.logger.debug("run #{args} (#{opts})") with_dir do return SH.public_send(run_command, *args, **opts, &b) end end def run_simple(*args,**opts, &b) run(*args, run_command: :run_simple,**opts, &b) end def run_success(*args,**opts, &b) run(*args, run_command: :run_success, **opts, &b) end # infos without cache def infos!(quiet: true) infos={} status, out, _err=run("git rev-parse --is-inside-git-dir --is-inside-work-tree --is-bare-repository --show-prefix --show-toplevel --show-cdup --git-dir", chomp: :lines, quiet: quiet) infos[:git]=status.success? infos[:in_gitdir]=DR::Bool.to_bool out[0] infos[:in_worktree]=DR::Bool.to_bool out[1] infos[:is_bare]=DR::Bool.to_bool out[2] infos[:prefix]=out[3] infos[:toplevel]=out[4] infos[:cdup]=out[5] infos[:gitdir]=out[6] infos end #are we a git repo? def git? infos[:git] end #are we in .git/? def gitdir? infos[:in_gitdir] end #are we in the worktree? def worktree? infos[:in_worktree] end #are we in a bare repo? def bare? infos[:is_bare] end #relative path from toplevel to @dir def prefix d=infos[:prefix] and ShellHelpers::Pathname.new(d) end #return the absolute path of the toplevel def toplevel d=infos[:toplevel] and ShellHelpers::Pathname.new(d) end #return the relative path from @dir to the toplevel def relative_toplevel d=infos[:cdup] and ShellHelpers::Pathname.new(d) end #get path to .git directory (can be relative or absolute) def gitdir d=infos[:gitdir] and ShellHelpers::Pathname.new(d) end def all_files run_simple("git ls-files -z").split("\0") end def with_toplevel(&b) with_dir do dir=relative_toplevel if !dir.to_s.empty? Dir.chdir(dir,&b) else warn "No toplevel found, executing inside dir #{@dir}" with_dir(&b) end end end #return a list of submodules def submodules run_simple("git submodule status").each_line.map { |l| l.split[1] } end def get_config(*args) run_simple("git config #{args.shelljoin}", chomp: true) end # deprecated # run head.name instead def current_branch(always: true) branchname= run_simple("git symbolic-ref -q --short HEAD", chomp: true) branchname= run_simple("git rev-parse --short --verify HEAD", chomp: true) if always and branchname.empty? return branch(branchname) end def head @head || @head=branch('HEAD') end ## #return all branches that have an upstream ## #if branches=:all look through all branches ## def all_upstream_branches(branches) ## #TODO (or use branch_infos) ## upstreams=%x!git for-each-ref --format='%(upstream:short)' refs/heads/branch/! ## end def push_default run_simple("git config --get remote.pushDefault", chomp: true) || "origin" end def get_topic_branches(*branches, complete: :local) if branches.length >= 2 return branch(branches[0]), branch(branches[1]) elsif branches.length == 1 b=branch(branches[0]) if complete == :local return current_branch, b elsif complete == :remote return b, b.upstream else fail "complete keyword should be :local or :remote" end else c=current_branch return c, c.upstream end end def branch(branch="HEAD") GitBranch.new(branch, dir: self) end end
# File lib/git_helpers/git_dir.rb, line 127 def get_config(*args) run_simple("git config #{args.shelljoin}", chomp: true) end
# File lib/git_helpers/git_dir.rb, line 154 def get_topic_branches(*branches, complete: :local) if branches.length >= 2 return branch(branches[0]), branch(branches[1]) elsif branches.length == 1 b=branch(branches[0]) if complete == :local return current_branch, b elsif complete == :remote return b, b.upstream else fail "complete keyword should be :local or :remote" end else c=current_branch return c, c.upstream end end
are we a git repo?
# File lib/git_helpers/git_dir.rb, line 74 def git? infos[:git] end
get path to .git directory (can be relative or absolute)
# File lib/git_helpers/git_dir.rb, line 102 def gitdir d=infos[:gitdir] and ShellHelpers::Pathname.new(d) end
are we in .git/?
# File lib/git_helpers/git_dir.rb, line 78 def gitdir? infos[:in_gitdir] end
# File lib/git_helpers/git_dir.rb, line 139 def head @head || @head=branch('HEAD') end
# File lib/git_helpers/git_dir.rb, line 40 def infos(*args) return @infos if @infos @infos=infos!(*args) end
infos without cache
# File lib/git_helpers/git_dir.rb, line 59 def infos!(quiet: true) infos={} status, out, _err=run("git rev-parse --is-inside-git-dir --is-inside-work-tree --is-bare-repository --show-prefix --show-toplevel --show-cdup --git-dir", chomp: :lines, quiet: quiet) infos[:git]=status.success? infos[:in_gitdir]=DR::Bool.to_bool out[0] infos[:in_worktree]=DR::Bool.to_bool out[1] infos[:is_bare]=DR::Bool.to_bool out[2] infos[:prefix]=out[3] infos[:toplevel]=out[4] infos[:cdup]=out[5] infos[:gitdir]=out[6] infos end
relative path from toplevel to @dir
# File lib/git_helpers/git_dir.rb, line 90 def prefix d=infos[:prefix] and ShellHelpers::Pathname.new(d) end
return all branches that have an upstream if branches=:all look through all branches def all_upstream_branches(branches)
#TODO (or use branch_infos) upstreams=%x!git for-each-ref --format='%(upstream:short)' refs/heads/branch/!
end
# File lib/git_helpers/git_dir.rb, line 150 def push_default run_simple("git config --get remote.pushDefault", chomp: true) || "origin" end
are we in a bare repo?
# File lib/git_helpers/raw_helpers.rb, line 29 def raw_bare? with_dir do return DR::Bool.to_bool(%x/git rev-parse --is-bare-repository/) end end
are we in a git folder?
# File lib/git_helpers/raw_helpers.rb, line 7 def raw_git?(quiet: false) launch="git rev-parse" launch=launch + " 2>/dev/null" if quiet with_dir do system launch return DR::Bool.to_bool($?) end end
get path to .git directory (can be relative or absolute)
# File lib/git_helpers/raw_helpers.rb, line 54 def raw_gitdir with_dir do return Pathname.new(%x/git rev-parse --git-dir/.chomp) end end
are we in .git/?
# File lib/git_helpers/raw_helpers.rb, line 17 def raw_gitdir? with_dir do return DR::Bool.to_bool(%x/git rev-parse --is-inside-git-dir/) end end
relative path from toplevel to @dir
# File lib/git_helpers/raw_helpers.rb, line 42 def raw_prefix with_dir do return Pathname.new(%x/git rev-parse --show-prefix/.chomp) end end
return the relative path from @dir to the toplevel
# File lib/git_helpers/raw_helpers.rb, line 48 def raw_relative_toplevel with_dir do return Pathname.new(%x/git rev-parse --show-cdup/.chomp) end end
return the absolute path of the toplevel
# File lib/git_helpers/raw_helpers.rb, line 36 def raw_toplevel with_dir do return Pathname.new(%x/git rev-parse --show-toplevel/.chomp) end end
are we in the worktree?
# File lib/git_helpers/raw_helpers.rb, line 23 def raw_worktree? with_dir do return DR::Bool.to_bool(%x/git rev-parse --is-inside-work-tree/) end end
return the relative path from @dir to the toplevel
# File lib/git_helpers/git_dir.rb, line 98 def relative_toplevel d=infos[:cdup] and ShellHelpers::Pathname.new(d) end
reset all caches
# File lib/git_helpers/git_dir.rb, line 35 def reset! @infos=nil @head=nil end
# File lib/git_helpers/git_dir.rb, line 45 def run(*args, run_command: :run, **opts, &b) SH.logger.debug("run #{args} (#{opts})") with_dir do return SH.public_send(run_command, *args, **opts, &b) end end
# File lib/git_helpers/git_dir.rb, line 51 def run_simple(*args,**opts, &b) run(*args, run_command: :run_simple,**opts, &b) end
# File lib/git_helpers/git_dir.rb, line 54 def run_success(*args,**opts, &b) run(*args, run_command: :run_success, **opts, &b) end
return a list of submodules
# File lib/git_helpers/git_dir.rb, line 123 def submodules run_simple("git submodule status").each_line.map { |l| l.split[1] } end
# File lib/git_helpers/git_dir.rb, line 26 def to_s @dir.to_s end
return the absolute path of the toplevel
# File lib/git_helpers/git_dir.rb, line 94 def toplevel d=infos[:toplevel] and ShellHelpers::Pathname.new(d) end
we could also use 'git -C #{@dir}' for each git invocation
# File lib/git_helpers/git_dir.rb, line 30 def with_dir Dir.chdir(@dir) { yield self } end
# File lib/git_helpers/git_dir.rb, line 110 def with_toplevel(&b) with_dir do dir=relative_toplevel if !dir.to_s.empty? Dir.chdir(dir,&b) else warn "No toplevel found, executing inside dir #{@dir}" with_dir(&b) end end end
are we in the worktree?
# File lib/git_helpers/git_dir.rb, line 82 def worktree? infos[:in_worktree] end