class GitHelpers::GitDir

these raw helpers are not called since we usually use higher level commands that provide all infos at once

Attributes

dir[R]
infos[W]
reldir[R]

Public Class Methods

new(dir=".") click to toggle source
# File lib/git_helpers/git_dir.rb, line 17
def initialize(dir=".")
        self.dir=dir
end

Public Instance Methods

all_files() click to toggle source
# File lib/git_helpers/git_dir.rb, line 106
def all_files
        run_simple("git ls-files -z").split("\0")
end
bare?() click to toggle source

are we in a bare repo?

# File lib/git_helpers/git_dir.rb, line 86
def bare?
        infos[:is_bare]
end
branch(branch="HEAD") click to toggle source
# File lib/git_helpers/git_dir.rb, line 172
def branch(branch="HEAD")
        GitBranch.new(branch, dir: self)
end
current_branch(always: true) click to toggle source

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
dir=(dir) click to toggle source
# 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
get_config(*args) click to toggle source
# File lib/git_helpers/git_dir.rb, line 127
def get_config(*args)
        run_simple("git config #{args.shelljoin}", chomp: true)
end
get_topic_branches(*branches, complete: :local) click to toggle source
# 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
git?() click to toggle source

are we a git repo?

# File lib/git_helpers/git_dir.rb, line 74
def git?
        infos[:git]
end
gitdir() click to toggle source

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
gitdir?() click to toggle source

are we in .git/?

# File lib/git_helpers/git_dir.rb, line 78
def gitdir?
        infos[:in_gitdir]
end
head() click to toggle source
# File lib/git_helpers/git_dir.rb, line 139
def head
        @head || @head=branch('HEAD')
end
infos(*args) click to toggle source
# File lib/git_helpers/git_dir.rb, line 40
def infos(*args)
        return @infos if @infos
        @infos=infos!(*args)
end
infos!(quiet: true) click to toggle source

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
prefix() click to toggle source

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
push_default() click to toggle source

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
raw_bare?() click to toggle source

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
raw_git?(quiet: false) click to toggle source

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
raw_gitdir() click to toggle source

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
raw_gitdir?() click to toggle source

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
raw_prefix() click to toggle source

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
raw_relative_toplevel() click to toggle source

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
raw_toplevel() click to toggle source

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
raw_worktree?() click to toggle source

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
relative_toplevel() click to toggle source

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!() click to toggle source

reset all caches

# File lib/git_helpers/git_dir.rb, line 35
def reset!
        @infos=nil
        @head=nil
end
run(*args, run_command: :run, **opts, &b) click to toggle source
# 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
run_simple(*args,**opts, &b) click to toggle source
# File lib/git_helpers/git_dir.rb, line 51
def run_simple(*args,**opts, &b)
        run(*args, run_command: :run_simple,**opts, &b)
end
run_success(*args,**opts, &b) click to toggle source
# File lib/git_helpers/git_dir.rb, line 54
def run_success(*args,**opts, &b)
        run(*args, run_command: :run_success, **opts, &b)
end
submodules() click to toggle source

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
to_s() click to toggle source
# File lib/git_helpers/git_dir.rb, line 26
def to_s
        @dir.to_s
end
toplevel() click to toggle source

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
with_dir() { |self| ... } click to toggle source

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
with_toplevel(&b) click to toggle source
# 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
worktree?() click to toggle source

are we in the worktree?

# File lib/git_helpers/git_dir.rb, line 82
def worktree?
        infos[:in_worktree]
end