Class: MGit::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/mgit/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path) ⇒ Repository

Returns a new instance of Repository.



7
8
9
10
# File 'lib/mgit/repository.rb', line 7

def initialize(name, path)
  @name = name
  @path = path
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/mgit/repository.rb', line 5

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/mgit/repository.rb', line 5

def path
  @path
end

Instance Method Details

#current_branchObject



12
13
14
15
16
17
# File 'lib/mgit/repository.rb', line 12

def current_branch
  in_repo { 
    b = `git rev-parse --abbrev-ref HEAD 2>&1`.strip 
    $?.exitstatus == 0 ? b : 'HEAD'
  }
end

#dirty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/mgit/repository.rb', line 76

def dirty?
  [:index, :dirty, :untracked].any? { |f| flags.include?(f) }
end

#divergenceObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mgit/repository.rb', line 60

def divergence
  divergence = []
  status_lines do |s|
    if s.split[0] == '##'
      if(m = /## ([\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

#flagsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mgit/repository.rb', line 39

def flags
  flags = Set.new
  status_lines do |s|
    case s.split[0]
    when 'A'
      flags << :index
    when 'M'
      flags << :dirty
    when '??'
      flags << :untracked
    when '##'
      if /## ([\w,\/]+)\.\.\.([\w,\/]+) \[(\w+) (\d+)\]/ =~ s
        flags << :diverged
      elsif /## HEAD \(no branch\)/ =~ s
        flags << :detached
      end
    end
  end
  flags
end

#remote_branchesObject



33
34
35
36
37
# File 'lib/mgit/repository.rb', line 33

def remote_branches
  in_repo do
    `git branch -r`.split("\n").map { |a| a.split(' ')[0] }
  end
end

#remote_tracking_branches(upstream_exists_only = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mgit/repository.rb', line 19

def remote_tracking_branches(upstream_exists_only = true)
  rb = remote_branches

  a = in_repo do
    `git for-each-ref --format='%(refname:short) %(upstream:short)' refs/heads`.
      split("\n").
      map { |b| b.split(' ') }.
      reject { |b| b.size != 2 }.
      select { |b| !upstream_exists_only || rb.include?(b[1]) }
  end
  
  Hash[a]
end