Class: MGit::Repository
- Inherits:
-
Object
show all
- Includes:
- Output
- Defined in:
- lib/mgit/repository.rb
Constant Summary
collapse
- SIMPLE_FLAGS =
{ 'A' => :index, 'M' => :dirty, '??' => :untracked }
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Output
#perror, #pinfo, #psystem, #ptable, #pwarn
Constructor Details
#initialize(name, path) ⇒ Repository
Returns a new instance of Repository.
9
10
11
12
|
# File 'lib/mgit/repository.rb', line 9
def initialize(name, path)
@name = name
@path = path
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/mgit/repository.rb', line 7
def name
@name
end
|
#path ⇒ Object
Returns the value of attribute path.
7
8
9
|
# File 'lib/mgit/repository.rb', line 7
def path
@path
end
|
Instance Method Details
#available? ⇒ Boolean
14
15
16
17
18
19
20
21
|
# File 'lib/mgit/repository.rb', line 14
def available?
unless File.directory?(path)
pwarn "Repository #{name} is not available anymore. Failed to read #{path}."
return false
end
true
end
|
#branches ⇒ Object
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/mgit/repository.rb', line 38
def branches
in_repo do
System.git('branch --no-color')
.stdout
.strip
.split("\n")
.map { |b| b.split(' ') }
.map { |b| { name: b.last, current: (b.size == 2) } }
end
end
|
#current_branch ⇒ Object
23
24
25
26
27
28
|
# File 'lib/mgit/repository.rb', line 23
def current_branch
in_repo do
sc = System.git('rev-parse --abbrev-ref HEAD')
sc.success? ? sc.stdout.strip : 'HEAD'
end
end
|
#current_head ⇒ Object
30
31
32
|
# File 'lib/mgit/repository.rb', line 30
def current_head
in_repo { System.git('rev-parse --verify --short HEAD').stdout.strip }
end
|
#dirty? ⇒ Boolean
126
127
128
|
# File 'lib/mgit/repository.rb', line 126
def dirty?
[:index, :dirty].any? { |f| flags.include?(f) }
end
|
#divergence ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/mgit/repository.rb', line 108
def divergence
divergence = []
status_lines do |s|
if s.split[0] == '##'
if (m = %r{## ([\w,/]+)\.\.\.([\w,/]+) \[ahead (\d+), behind (\d+)\]}.match(s))
divergence << { ahead: { branch: m[2], by: m[3] }, behind: { branch: m[2], by: m[4] } }
elsif (m = %r{## ([\w,/]+)\.\.\.([\w,/]+) \[(\w+) (\d+)\]}.match(s))
if m[3] =~ /behind/
divergence << { behind: { branch: m[2], by: m[4] } }
else
divergence << { ahead: { branch: m[2], by: m[4] } }
end
end
end
end
divergence
end
|
#flags ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/mgit/repository.rb', line 88
def flags
flags = Set.new
status_lines do |s|
indicator = s.split[0]
case indicator
when *SIMPLE_FLAGS.keys
flags << SIMPLE_FLAGS[indicator]
when '##'
if %r{## [\w,/]+\.\.\.[\w,/]+ \[.+\]} =~ s
flags << :diverged
elsif /## HEAD \(no branch\)/ =~ s
flags << :detached
end
end
end
flags
end
|
#has_commit?(obj) ⇒ Boolean
34
35
36
|
# File 'lib/mgit/repository.rb', line 34
def has_commit?(obj)
in_repo { !(System.git("rev-parse --quiet --verify #{obj}").stdout.empty?) }
end
|
#in_repo ⇒ Object
Also known as:
chdir
130
131
132
|
# File 'lib/mgit/repository.rb', line 130
def in_repo
Dir.chdir(path) { yield }
end
|
#remote_branches ⇒ Object
64
65
66
67
68
|
# File 'lib/mgit/repository.rb', line 64
def remote_branches
in_repo do
System.git('branch -r').stdout.split("\n").map { |a| a.split(' ')[0] }
end
end
|
#remote_tracking_branches(upstream_exists_only = true) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/mgit/repository.rb', line 49
def remote_tracking_branches(upstream_exists_only = true)
rb = remote_branches
a = in_repo do
System.git("for-each-ref --format='%(refname:short) %(upstream:short)' refs/heads")
.stdout
.split("\n")
.map { |b| b.split(' ') }
.reject { |b| b.size != 2 }
.select { |b| !upstream_exists_only || rb.include?(b[1]) }
end
Hash[a]
end
|
#unmerged_commits(branch, upstream) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/mgit/repository.rb', line 70
def unmerged_commits(branch, upstream)
in_repo do
System.git("log --pretty=format:'%h#%an#%s' --reverse --relative-date #{branch}..#{upstream}")
.stdout
.split("\n")
.map { |line| line.split('#') }
.map do |words|
{
commit: words[0],
author: words[1],
subject: words[2..-1].join('#')
}
end
end
end
|