Class: Redmine::Scm::Adapters::GitAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/redmine/scm/adapters/git_adapter.rb

Constant Summary

GIT_BIN =

Git executable name

"git"

Instance Method Summary

Methods inherited from AbstractAdapter

#adapter_name, client_version, client_version_above?, client_version_string, #entry, #initialize, #properties, #root_url, #shell_quote, #supports_annotate?, #supports_cat?, #url, #with_leading_slash, #with_trailling_slash, #without_leading_slash, #without_trailling_slash

Constructor Details

This class inherits a constructor from Redmine::Scm::Adapters::AbstractAdapter

Instance Method Details

- (Object) annotate(path, identifier = nil)



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 228

def annotate(path, identifier=nil)
  identifier = 'HEAD' if identifier.blank?
  cmd = "#{GIT_BIN} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}"
  blame = Annotate.new
  content = nil
  shellout(cmd) { |io| io.binmode; content = io.read }
  return nil if $? && $?.exitstatus != 0
  # git annotates binary files
  return nil if content.is_binary_data?
  identifier = ''
  # git shows commit author on the first occurrence only
  authors_by_commit = {}
  content.split("\n").each do |line|
    if line =~ /^([0-9a-f]{39,40})\s.*/
      identifier = $1
    elsif line =~ /^author (.+)/
      authors_by_commit[identifier] = $1.strip
    elsif line =~ /^\t(.*)/
      blame.add_line($1, Revision.new(:identifier => identifier, :author => authors_by_commit[identifier]))
      identifier = ''
      author = ''
    end
  end
  blame
end

- (Object) branches



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 35

def branches
  return @branches if @branches
  @branches = []
  cmd = "#{GIT_BIN} --git-dir #{target('')} branch"
  shellout(cmd) do |io|
    io.each_line do |line|
      @branches << line.match('\s*\*?\s*(.*)$')[1]
    end
  end
  @branches.sort!
end

- (Object) cat(path, identifier = nil)



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 254

def cat(path, identifier=nil)
  if identifier.nil?
    identifier = 'HEAD'
  end
  cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
  cat = nil
  shellout(cmd) do |io|
    io.binmode
    cat = io.read
  end
  return nil if $? && $?.exitstatus != 0
  cat
end

- (Object) default_branch



55
56
57
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 55

def default_branch
  branches.include?('master') ? 'master' : branches.first 
end

- (Object) diff(path, identifier_from, identifier_to = nil)



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 208

def diff(path, identifier_from, identifier_to=nil)
  path ||= ''

  if identifier_to
    cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" 
  else
    cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}"
  end

  cmd << " -- #{shell_quote path}" unless path.empty?
  diff = []
  shellout(cmd) do |io|
    io.each_line do |line|
      diff << line
    end
  end
  return nil if $? && $?.exitstatus != 0
  diff
end

- (Object) entries(path = nil, identifier = nil)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 59

def entries(path=nil, identifier=nil)
  path ||= ''
  entries = Entries.new
  cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l "
  cmd << shell_quote("HEAD:" + path) if identifier.nil?
  cmd << shell_quote(identifier + ":" + path) if identifier
  shellout(cmd)  do |io|
    io.each_line do |line|
      e = line.chomp.to_s
      if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/
        type = $1
        sha = $2
        size = $3
        name = $4
        full_path = path.empty? ? name : "#{path}/#{name}"
        entries << Entry.new({:name => name,
         :path => full_path,
         :kind => (type == "tree") ? 'dir' : 'file',
         :size => (type == "tree") ? nil : size,
         :lastrev => lastrev(full_path,identifier)
        }) unless entries.detect{|entry| entry.name == name}
      end
    end
  end
  return nil if $? && $?.exitstatus != 0
  entries.sort_by_name
end

- (Object) info



27
28
29
30
31
32
33
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 27

def info
  begin
    Info.new(:root_url => url, :lastrev => lastrev('',nil))
  rescue
    nil
  end
end

- (Object) lastrev(path, rev)



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
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 87

def lastrev(path,rev)
  return nil if path.nil?
  cmd = "#{GIT_BIN} --git-dir #{target('')} log --pretty=fuller --no-merges -n 1 "
  cmd << " #{shell_quote rev} " if rev 
  cmd <<  "-- #{path} " unless path.empty?
  shellout(cmd) do |io|
    begin
      id = io.gets.split[1]
      author = io.gets.match('Author:\s+(.*)$')[1]
      2.times { io.gets }
      time = io.gets.match('CommitDate:\s+(.*)$')[1]

      Revision.new({
        :identifier => id,
        :scmid => id,
        :author => author, 
        :time => time,
        :message => nil, 
        :paths => nil 
      })
    rescue NoMethodError => e
      logger.error("The revision '#{path}' has a wrong format")
      return nil
    end
  end
end

- (Object) revisions(path, identifier_from, identifier_to, options = {})



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 114

def revisions(path, identifier_from, identifier_to, options={})
  revisions = Revisions.new

  cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw --date=iso --pretty=fuller"
  cmd << " --reverse" if options[:reverse]
  cmd << " --all" if options[:all]
  cmd << " -n #{options[:limit]} " if options[:limit]
  cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from
  cmd << " #{shell_quote identifier_to} " if identifier_to
  cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since]
  cmd << " -- #{path}" if path && !path.empty?

  shellout(cmd) do |io|
    files=[]
    changeset = {}
    parsing_descr = 0  #0: not parsing desc or files, 1: parsing desc, 2: parsing files
    revno = 1

    io.each_line do |line|
      if line =~ /^commit ([0-9a-f]{40})$/
        key = "commit"
        value = $1
        if (parsing_descr == 1 || parsing_descr == 2)
          parsing_descr = 0
          revision = Revision.new({
            :identifier => changeset[:commit],
            :scmid => changeset[:commit],
            :author => changeset[:author],
            :time => Time.parse(changeset[:date]),
            :message => changeset[:description],
            :paths => files
          })
          if block_given?
            yield revision
          else
            revisions << revision
          end
          changeset = {}
          files = []
          revno = revno + 1
        end
        changeset[:commit] = $1
      elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
        key = $1
        value = $2
        if key == "Author"
          changeset[:author] = value
        elsif key == "CommitDate"
          changeset[:date] = value
        end
      elsif (parsing_descr == 0) && line.chomp.to_s == ""
        parsing_descr = 1
        changeset[:description] = ""
      elsif (parsing_descr == 1 || parsing_descr == 2) \
      && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
        parsing_descr = 2
        fileaction = $1
        filepath = $2
        files << {:action => fileaction, :path => filepath}
      elsif (parsing_descr == 1 || parsing_descr == 2) \
      && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\s+(.+)$/
        parsing_descr = 2
        fileaction = $1
        filepath = $3
        files << {:action => fileaction, :path => filepath}
      elsif (parsing_descr == 1) && line.chomp.to_s == ""
        parsing_descr = 2
      elsif (parsing_descr == 1)
        changeset[:description] << line[4..-1]
      end
    end 

    if changeset[:commit]
      revision = Revision.new({
        :identifier => changeset[:commit],
        :scmid => changeset[:commit],
        :author => changeset[:author],
        :time => Time.parse(changeset[:date]),
        :message => changeset[:description],
        :paths => files
      })

      if block_given?
        yield revision
      else
        revisions << revision
      end
    end
  end

  return nil if $? && $?.exitstatus != 0
  revisions
end

- (Object) tags



47
48
49
50
51
52
53
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 47

def tags
  return @tags if @tags
  cmd = "#{GIT_BIN} --git-dir #{target('')} tag"
  shellout(cmd) do |io|
    @tags = io.readlines.sort!.map{|t| t.strip}
  end
end