Class: Grit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/gx/enhance.rb

Defined Under Namespace

Classes: UnmergedFile

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current(goto = true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gx/enhance.rb', line 7

def self.current(goto=true)
  # cd to the top of this git tree. If run via git's alias
  # infrastructure, this is done for us. We do it again, just
  # to be sure.

  top = `git rev-parse --show-cdup 2>&1`.strip
  if goto
    Dir.chdir top unless top.empty?
    return Grit::Repo.new(".")
  else
    return Grit::Repo.new(top)
  end
end

Instance Method Details

#am_infoObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gx/enhance.rb', line 84

def am_info
  info = {}
  File.open("#{Grit.rebase_dir}/info") do |f|
    f.readlines.each do |line|
      line.strip!
      break if line.empty?
      key, val = line.split(": ")
      info[key.downcase.to_sym] = val
    end
  end

  if subject = info[:subject]
    subject.gsub!(/^\[PATCH\] /,"")
  end

  return info
end

#find_ancestor(left, right = nil) ⇒ Object

Given left and right, detect and return their closest common ancestor. Used to find the point to perform merges from.

right defaults to the current HEAD.



36
37
38
39
40
41
# File 'lib/gx/enhance.rb', line 36

def find_ancestor(left, right=nil)
  right ||= resolve_rev "HEAD"
  hash = @git.merge_base({}, left, right)
  return nil if $?.exitstatus != 0
  return hash.strip
end

#merge_info(branch) ⇒ Object



110
111
112
113
114
# File 'lib/gx/enhance.rb', line 110

def merge_info(branch)
  repo = @git.config({}, "branch.#{branch}.remote").strip
  ref =  @git.config({}, "branch.#{branch}.merge").strip
  return [repo, ref]
end

#merge_ref(branch) ⇒ Object



116
117
118
119
120
121
# File 'lib/gx/enhance.rb', line 116

def merge_ref(branch)
  repo, ref = merge_info(branch)
  return nil if repo.empty?
  path = "#{repo}/#{path2ref(ref)}"
  return path
end

#merge_url(branch) ⇒ Object



123
124
125
126
127
128
# File 'lib/gx/enhance.rb', line 123

def merge_url(branch)
  repo = @git.config({}, "branch.#{branch}.remote").strip
  return "local" if repo == "."
  
  @git.config({}, "remote.#{repo}.url").strip
end

#path2ref(name) ⇒ Object



106
107
108
# File 'lib/gx/enhance.rb', line 106

def path2ref(name)
  name.gsub %r!^refs/heads/!, ""
end

#remote_info(who, which = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gx/enhance.rb', line 130

def remote_info(who, which=nil)
  if which
    hash, name = @git.ls_remote({:timeout => false}, who, which).split(/\s+/, 2)
    return hash
  else
    ret = {}
    @git.ls_remote({:timeout => false}, who).split("\n").each do |line|
      hash, name = line.split(/\s+/, 2)
      ret[name] = hash
    end
    return ret
  end
end

#resolve_rev(hashish) ⇒ Object

Given hashish, parse it and return the hash it refers to.



24
25
26
27
28
# File 'lib/gx/enhance.rb', line 24

def resolve_rev(hashish)
  hash = @git.rev_parse({:verify => true}, hashish)
  return nil if $?.exitstatus != 0
  return hash.strip
end

#revs_between(left, right) ⇒ Object



43
44
45
# File 'lib/gx/enhance.rb', line 43

def revs_between(left, right)
  @git.rev_list({}, "#{left}..#{right}").split("\n")
end

#to_be_committedObject



102
103
104
# File 'lib/gx/enhance.rb', line 102

def to_be_committed
  @git.diff_index({:cached => true, :name_only => true}, "HEAD").split("\n")
end

#unmerged_filesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gx/enhance.rb', line 55

def unmerged_files
  files = Hash.new { |h,k| h[k] = UnmergedFile.new(k) }
  @git.ls_files({:u => true}).split("\n").each do |line|
    mode, hash, stage, name = line.split(/\s+/, 4)
    case stage
    when "1"
      files[name].original = hash
    when "2"
      files[name].yours = hash
    when "3"
      files[name].mine = hash
    end
  end

  return files
end