Class: Redmine::Scm::Adapters::GitAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- Redmine::Scm::Adapters::GitAdapter
- Defined in:
- lib/redmine/scm/adapters/git_adapter.rb
Defined Under Namespace
Constant Summary collapse
- GIT_BIN =
Git executable name
Redmine::Configuration['scm_git_command'] || "git"
- GIT_DEFAULT_BRANCH_NAMES =
Repositories created after 2020 may have a default branch of “main” instead of “master”
%w[main master].freeze
Class Method Summary collapse
- .client_available ⇒ Object
- .client_command ⇒ Object
- .client_version ⇒ Object
- .scm_command_version ⇒ Object
- .scm_version_from_command_line ⇒ Object
- .sq_bin ⇒ Object
Instance Method Summary collapse
- #annotate(path, identifier = nil) ⇒ Object
- #branches ⇒ Object
- #cat(path, identifier = nil) ⇒ Object
- #default_branch ⇒ Object
- #diff(path, identifier_from, identifier_to = nil) ⇒ Object
- #entries(path = nil, identifier = nil, options = {}) ⇒ Object
- #entry(path = nil, identifier = nil) ⇒ Object
- #info ⇒ Object
- #lastrev(path, rev) ⇒ Object
- #path_encoding ⇒ Object
- #revisions(path, identifier_from, identifier_to, options = {}) ⇒ Object
- #tags ⇒ Object
- #valid_name?(name) ⇒ Boolean
Methods inherited from AbstractAdapter
#adapter_name, client_version_above?, client_version_string, #initialize, logger, #properties, #root_url, shell_quote, shell_quote_command, shellout, #supports_annotate?, #supports_cat?, #url, #with_leading_slash, #with_trailing_slash, #without_leading_slash, #without_trailing_slash
Methods included from Utils::Shell
shell_quote, shell_quote_command
Constructor Details
This class inherits a constructor from Redmine::Scm::Adapters::AbstractAdapter
Class Method Details
.client_available ⇒ Object
49 50 51 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 49 def client_available !client_version.empty? end |
.client_command ⇒ Object
37 38 39 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 37 def client_command @@bin ||= GIT_BIN end |
.client_version ⇒ Object
45 46 47 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 45 def client_version @@client_version ||= (scm_command_version || []) end |
.scm_command_version ⇒ Object
53 54 55 56 57 58 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 53 def scm_command_version scm_version = scm_version_from_command_line.b if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) m[2].scan(%r{\d+}).collect(&:to_i) end end |
.scm_version_from_command_line ⇒ Object
60 61 62 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 60 def scm_version_from_command_line shellout("#{sq_bin} --version") {|io| io.read}.to_s end |
.sq_bin ⇒ Object
41 42 43 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 41 def sq_bin @@sq_bin ||= shell_quote_command end |
Instance Method Details
#annotate(path, identifier = nil) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 365 def annotate(path, identifier=nil) identifier = 'HEAD' if identifier.blank? git_identifier = scm_iconv(@path_encoding, 'UTF-8', identifier) cmd_args = %w|blame --encoding=UTF-8| cmd_args << "-p" << git_identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path) blame = Annotate.new content = nil git_cmd(cmd_args) {|io| io.binmode; content = io.read} # git annotates binary files return nil if ScmData.binary?(content) identifier = '' # git shows commit author on the first occurrence only = {} prev_blames_by_commit = {} content.split("\n").each do |line| if line =~ /^([0-9a-f]{39,40})\s.*/ identifier = $1 elsif line =~ /^author (.+)/ [identifier] = $1.strip elsif line =~ /^previous (.+)/ prev_blames_by_commit[identifier] = $1.strip elsif line =~ /^\t(.*)/ blame.add_line( $1, Revision.new( :identifier => identifier, :revision => identifier, :scmid => identifier, :author => [identifier] ), prev_blames_by_commit[identifier] ) identifier = '' = '' end end blame rescue ScmCommandAborted nil end |
#branches ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 75 def branches return @branches if @branches @branches = [] cmd_args = %w|branch --no-color --verbose --no-abbrev| git_cmd(cmd_args) do |io| io.each_line do |line| branch_rev = line.match('\s*(\*?)\s*(.*?)\s*([0-9a-f]{40}).*$') next unless branch_rev bran = GitBranch.new(scm_iconv('UTF-8', @path_encoding, branch_rev[2])) bran.revision = branch_rev[3] bran.scmid = branch_rev[3] bran.is_default = (branch_rev[1] == '*') @branches << bran end end @branches.sort! rescue ScmCommandAborted nil end |
#cat(path, identifier = nil) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 407 def cat(path, identifier=nil) identifier = 'HEAD' if identifier.nil? git_identifier = scm_iconv(@path_encoding, 'UTF-8', identifier) cmd_args = %w|show --no-color| cmd_args << "#{git_identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}" cat = nil git_cmd(cmd_args) do |io| io.binmode cat = io.read end cat rescue ScmCommandAborted nil end |
#default_branch ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 110 def default_branch return if branches.blank? ( branches.detect(&:is_default) || branches.detect {|b| GIT_DEFAULT_BRANCH_NAMES.include?(b.to_s)} || branches.first ).to_s end |
#diff(path, identifier_from, identifier_to = nil) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 344 def diff(path, identifier_from, identifier_to=nil) path ||= '' cmd_args = [] if identifier_to cmd_args << "diff" << "--no-color" << identifier_to << identifier_from else cmd_args << "show" << "--no-color" << identifier_from end cmd_args << '--no-renames' if self.class.client_version_above?([2, 9]) cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty? diff = [] git_cmd(cmd_args) do |io| io.each_line do |line| diff << line end end diff rescue ScmCommandAborted nil end |
#entries(path = nil, identifier = nil, options = {}) ⇒ Object
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 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 135 def entries(path=nil, identifier=nil, ={}) path ||= '' p = scm_iconv(@path_encoding, 'UTF-8', path) entries = Entries.new cmd_args = %w|ls-tree -l| identifier = 'HEAD' if identifier.nil? git_identifier = scm_iconv(@path_encoding, 'UTF-8', identifier) cmd_args << "#{git_identifier}:#{p}" git_cmd(cmd_args) 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-]+)\t(.+)$/ type = $1 sha = $2 size = $3 name = $4.force_encoding(@path_encoding) full_path = p.empty? ? name : "#{p}/#{name}" n = scm_iconv('UTF-8', @path_encoding, name) full_p = scm_iconv('UTF-8', @path_encoding, full_path) unless entries.detect{|entry| entry.name == name} entries << Entry. new( { :name => n, :path => full_p, :kind => (type == "tree") ? 'dir' : 'file', :size => (type == "tree") ? nil : size, :lastrev => if [:report_last_commit] lastrev(full_path, identifier) else Revision.new end } ) end end end end entries.sort_by_name rescue ScmCommandAborted nil end |
#entry(path = nil, identifier = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 120 def entry(path=nil, identifier=nil) parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?} search_path = parts[0..-2].join('/') search_name = parts[-1] if search_path.blank? && search_name.blank? # Root entry Entry.new(:path => '', :kind => 'dir') else # Search for the entry in the parent directory es = entries(search_path, identifier, = {:report_last_commit => false}) es ? es.detect {|e| e.name == search_name} : nil end end |
#info ⇒ Object
69 70 71 72 73 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 69 def info Info.new(:root_url => url, :lastrev => lastrev('', nil)) rescue nil end |
#lastrev(path, rev) ⇒ Object
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 207 208 209 210 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 180 def lastrev(path, rev) return nil if path.nil? cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1| cmd_args << '--no-renames' if self.class.client_version_above?([2, 9]) cmd_args << rev if rev cmd_args << "--" << path unless path.empty? lines = [] git_cmd(cmd_args) {|io| lines = io.readlines} begin id = lines[0].split[1] = lines[1].match('Author:\s+(.*)$')[1] time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1]) Revision. new( { :identifier => id, :scmid => id, :author => , :time => time, :message => nil, :paths => nil } ) rescue NoMethodError => e logger.error("The revision '#{path}' has a wrong format") return nil end rescue ScmCommandAborted nil end |
#path_encoding ⇒ Object
65 66 67 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 65 def path_encoding @path_encoding end |
#revisions(path, identifier_from, identifier_to, options = {}) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 212 def revisions(path, identifier_from, identifier_to, ={}) revs = Revisions.new cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents --stdin| cmd_args << '--no-renames' if self.class.client_version_above?([2, 9]) cmd_args << "--reverse" if [:reverse] cmd_args << "-n" << [:limit].to_i.to_s if [:limit] cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty? revisions = [] if identifier_from || identifier_to revisions << +"" if identifier_from git_identifier_from = scm_iconv(@path_encoding, 'UTF-8', identifier_from) revisions[0] << "#{git_identifier_from}.." if identifier_from end if identifier_to git_identifier_to = scm_iconv(@path_encoding, 'UTF-8', identifier_to) revisions[0] << git_identifier_to.to_s if identifier_to end else unless [:includes].blank? revisions += [:includes] end unless [:excludes].blank? revisions += [:excludes].map{|r| "^#{r}"} end end git_cmd(cmd_args, {:write_stdin => true}) do |io| io.binmode io.puts(revisions.join("\n")) io.close_write files=[] changeset = {} # 0: not parsing desc or files, 1: parsing desc, 2: parsing files parsing_descr = 0 io.each_line do |line| if line =~ /^commit ([0-9a-f]{40})(( [0-9a-f]{40})*)$/ key = "commit" value = $1 parents_str = $2 if [1, 2].include?(parsing_descr) 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, :parents => changeset[:parents] } ) if block_given? yield revision else revs << revision end changeset = {} files = [] end changeset[:commit] = $1 unless parents_str.nil? or parents_str == "" changeset[:parents] = parents_str.strip.split(' ') end 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 [1, 2].include?(parsing_descr) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/ parsing_descr = 2 fileaction = $1 filepath = $2 p = scm_iconv('UTF-8', @path_encoding, filepath) files << {:action => fileaction, :path => p} elsif [1, 2].include?(parsing_descr) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/ parsing_descr = 2 fileaction = $1 filepath = $3 p = scm_iconv('UTF-8', @path_encoding, filepath) files << {:action => fileaction, :path => p} 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, :parents => changeset[:parents] } ) if block_given? yield revision else revs << revision end end end revs rescue ScmCommandAborted => e err_msg = "git log error: #{e.}" logger.error(err_msg) if block_given? raise CommandFailed, err_msg else revs end end |
#tags ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 97 def return @tags if @tags @tags = [] cmd_args = %w|tag| git_cmd(cmd_args) do |io| @tags = io.readlines.sort!.map{|t| scm_iconv('UTF-8', @path_encoding, t.strip)} end @tags rescue ScmCommandAborted nil end |
#valid_name?(name) ⇒ Boolean
422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/redmine/scm/adapters/git_adapter.rb', line 422 def valid_name?(name) return false unless name.is_a?(String) return false if name.start_with?('-', '/', 'refs/heads/', 'refs/remotes/') return false if name == 'HEAD' git_cmd ['show-ref', '--heads', '--tags', '--quiet', '--', name] true rescue ScmCommandAborted false end |