Class: GitHelpers::GitDir

Inherits:
Object
  • Object
show all
Includes:
GitBranchInfos, GitExtraInfos, GitStats, GitStatus, GitSubmodules
Defined in:
lib/git_helpers/raw_helpers.rb,
lib/git_helpers/git_dir.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GitSubmodules

#foreach

Methods included from GitStatus

#format_status, #sequencer, #stash, #status

Methods included from GitBranchInfos

#ahead_behind, #branch_infos, #format_branch_infos, #name, #name_branch, #recursive_upstream

Methods included from GitExtraInfos

#aliases, #commit_children, #commits_by_files, #log_commits_by_files, #neck, #output_all_trails, #output_commit_children, #output_commits_by_files, #output_log_commits_by_files, #output_removed_files, #output_trails, #removed_files, #trails

Methods included from GitStats

#output_stats_authors, #output_stats_diff, #output_stats_lines, #stats_authors, #stats_diff, #stats_lines, #stats_lines_all, #summary

Constructor Details

#initialize(dir = ".") ⇒ GitDir

Returns a new instance of GitDir.



17
18
19
# File 'lib/git_helpers/git_dir.rb', line 17

def initialize(dir=".")
	self.dir=dir
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



15
16
17
# File 'lib/git_helpers/git_dir.rb', line 15

def dir
  @dir
end

#infos(*args) ⇒ Object



40
41
42
43
# File 'lib/git_helpers/git_dir.rb', line 40

def infos(*args)
	return @infos if @infos
	@infos=infos!(*args)
end

#reldirObject (readonly)

Returns the value of attribute reldir.



15
16
17
# File 'lib/git_helpers/git_dir.rb', line 15

def reldir
  @reldir
end

Instance Method Details

#all_filesObject



106
107
108
# File 'lib/git_helpers/git_dir.rb', line 106

def all_files
	run_simple("git ls-files -z").split("\0")
end

#bare?Boolean

are we in a bare repo?

Returns:

  • (Boolean)


86
87
88
# File 'lib/git_helpers/git_dir.rb', line 86

def bare?
	infos[:is_bare]
end

#branch(branch = "HEAD") ⇒ Object



172
173
174
# File 'lib/git_helpers/git_dir.rb', line 172

def branch(branch="HEAD")
	GitBranch.new(branch, dir: self)
end

#current_branch(always: true) ⇒ Object

deprecated run head.name instead



133
134
135
136
137
# 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

#get_config(*args) ⇒ Object



127
128
129
# 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) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# 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?Boolean

are we a git repo?

Returns:

  • (Boolean)


74
75
76
# File 'lib/git_helpers/git_dir.rb', line 74

def git?
	infos[:git]
end

#gitdirObject

get path to .git directory (can be relative or absolute)



102
103
104
# File 'lib/git_helpers/git_dir.rb', line 102

def gitdir
	d=infos[:gitdir] and ShellHelpers::Pathname.new(d)
end

#gitdir?Boolean

are we in .git/?

Returns:

  • (Boolean)


78
79
80
# File 'lib/git_helpers/git_dir.rb', line 78

def gitdir?
	infos[:in_gitdir]
end

#headObject



139
140
141
# File 'lib/git_helpers/git_dir.rb', line 139

def head
	@head || @head=branch('HEAD')
end

#infos!(quiet: true) ⇒ Object

infos without cache



59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

#prefixObject

relative path from toplevel to @dir



90
91
92
# File 'lib/git_helpers/git_dir.rb', line 90

def prefix
	d=infos[:prefix] and ShellHelpers::Pathname.new(d)
end

#push_defaultObject

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



150
151
152
# 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?Boolean

are we in a bare repo?

Returns:

  • (Boolean)


29
30
31
32
33
# 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) ⇒ Boolean

are we in a git folder?

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
# 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_gitdirObject

get path to .git directory (can be relative or absolute)



54
55
56
57
58
# 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?Boolean

are we in .git/?

Returns:

  • (Boolean)


17
18
19
20
21
# 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_prefixObject

relative path from toplevel to @dir



42
43
44
45
46
# 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_toplevelObject

return the relative path from @dir to the toplevel



48
49
50
51
52
# 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_toplevelObject

return the absolute path of the toplevel



36
37
38
39
40
# 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?Boolean

are we in the worktree?

Returns:

  • (Boolean)


23
24
25
26
27
# 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_toplevelObject

return the relative path from @dir to the toplevel



98
99
100
# File 'lib/git_helpers/git_dir.rb', line 98

def relative_toplevel
	d=infos[:cdup] and ShellHelpers::Pathname.new(d)
end

#reset!Object

reset all caches



35
36
37
38
# File 'lib/git_helpers/git_dir.rb', line 35

def reset!
	@infos=nil
	@head=nil
end

#run(*args, run_command: :run, **opts, &b) ⇒ Object



45
46
47
48
49
50
# 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) ⇒ Object



51
52
53
# 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) ⇒ Object



54
55
56
# File 'lib/git_helpers/git_dir.rb', line 54

def run_success(*args,**opts, &b)
	run(*args, run_command: :run_success, **opts, &b)
end

#submodulesObject

return a list of submodules



123
124
125
# 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_sObject



26
27
28
# File 'lib/git_helpers/git_dir.rb', line 26

def to_s
	@dir.to_s
end

#toplevelObject

return the absolute path of the toplevel



94
95
96
# File 'lib/git_helpers/git_dir.rb', line 94

def toplevel
	d=infos[:toplevel] and ShellHelpers::Pathname.new(d)
end

#with_dirObject

we could also use 'git -C #@dir' for each git invocation



30
31
32
# File 'lib/git_helpers/git_dir.rb', line 30

def with_dir
	Dir.chdir(@dir) { yield self }
end

#with_toplevel(&b) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# 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?Boolean

are we in the worktree?

Returns:

  • (Boolean)


82
83
84
# File 'lib/git_helpers/git_dir.rb', line 82

def worktree?
	infos[:in_worktree]
end