Class: Gitrepo::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/gitrepo/status.rb,
lib/gitrepo/status/version.rb

Overview

Class Gitrepo::Status

Constant Summary collapse

VERSION =
'0.2.3'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ Status

Returns a new instance of Status.



9
10
11
12
13
# File 'lib/gitrepo/status.rb', line 9

def initialize(opt = {})
  @path = opt.fetch(:path)
  @report_branches = opt[:report_branches] || %w[master]
  repo
end

Instance Method Details

#ahead_behindObject



56
57
58
59
60
61
62
# File 'lib/gitrepo/status.rb', line 56

def ahead_behind
  branches = {}
  branchnames.each do |branch|
    branches[branch] = repo.ahead_behind(repo.head.target_id, "origin/#{branch}")
  end
  branches
end

#branchnamesObject



39
40
41
42
43
44
45
46
47
# File 'lib/gitrepo/status.rb', line 39

def branchnames
  branches_txt = ''
  Dir.chdir(@path) do
    branches_txt = `git branch --contains HEAD | grep -v 'detached at'`
  end
  branches = branches_txt.split("\n").map(&:strip)
  branches.insert(0, branches.delete('master')) if branches.include?('master')
  select_branches(branches)
end

#deep_diff(a, b) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gitrepo/status.rb', line 137

def deep_diff(a, b)
  (a.keys | b.keys).each_with_object({}) do |k, diff|
    if a[k] != b[k]
      diff[k] = if a[k].is_a?(Hash) && b[k].is_a?(Hash)
                  deep_diff(a[k], b[k])
                else
                  diff[k] = [a[k], b[k]]
                end
    end
    diff
  end
end

#fetch_allObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitrepo/status.rb', line 22

def fetch_all
  # libgit2 and https... a dificult story... :-(
  # remotes.each do |remote|
  #   repo.fetch remote.name
  # end
  cmd1 = 'git branch -r | grep -v "\->" | while read remote; do git branch --track "${remote#origin/}" "$remote" > /dev/null 2>&1; done'
  cmd2 = 'git fetch --all > /dev/null 2>&1'
  Dir.chdir(@path) do
    `#{cmd1}`
    `#{cmd2}`
  end
end

#puppet_module?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/gitrepo/status.rb', line 35

def puppet_module?
  !repo.index.get('metadata.json').nil?
end

#puppet_module_infoObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitrepo/status.rb', line 64

def puppet_module_info
  blob = repo.blob_at(repo.head.target_id, 'metadata.json')
   = JSON.parse(blob.content)
  diff = {}
  branchnames.each do |branch|
    blob = repo.blob_at(repo.branches[branch].target_id, 'metadata.json')
     = JSON.parse(blob.content)
    branch_diff = deep_diff(, )
    diff[branch] = branch_diff unless branch_diff.empty?
  end
  diff
end

#repoObject



15
16
17
18
19
20
# File 'lib/gitrepo/status.rb', line 15

def repo
  return @repo if @repo
  @repo = Rugged::Repository.discover(@path)
  @basepath = repo.path.chomp('/.git/')
  @repo
end

#select_branches(branches) ⇒ Object



49
50
51
52
53
54
# File 'lib/gitrepo/status.rb', line 49

def select_branches(branches)
  return branches if @report_branches.empty?
  b = branches.select { |e| @report_branches.include?(e) }
  return b unless b.empty?
  branches
end

#testObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gitrepo/status.rb', line 77

def test
  require 'awesome_print'
  # puts '--- repo.methods ---'
  # ap repo.methods
  puts '--- repo.path ---'
  ap repo.path

  ap repo.index.get('metadata.json')

  puts '--- repo.last_commit ---'
  ap repo.last_commit.tree_id

  puts "Is Puppet Module? (has '/metadata.json'?) : #{puppet_module?}"
  head = repo.head
  puts '--- repo.head ---'
  ap head

  # sha1 hash of the newest commit
  head_sha = head.target_id
  puts '--- repo.head.target_id ---'
  ap head_sha

  ap repo.ahead_behind(head_sha, 'origin/master')

  puts '--- repo remotes ---'
  repo.remotes.each do |remote|
    # ap remote.methods
    ap remote.url
  end

  puts '--- repo branch ---'
  repo.branches.each do |branch|
    # ap branch.methods
    ap branch.name
    ap branch.head?
    ap branch.upstream
  end

  # # the commit message associated the newest commit
  # commit = repo.lookup(head_sha)
  # puts '--- repo.lookup(repo.head.target_id) ---'
  # pp commit
  #
  # puts '--- repo object methods ---'
  # ap repo.methods
  #
  puts '--- repo remotes ---'
  repo.remotes.each do |remote|
    # ap remote.methods
    ap remote.name
    ap remote.url
  end
  #
  # puts '--- repo branch ---'
  # repo.branches.each do |branch|
  #   ap branch.methods
  #   ap branch.name
  # end
end