Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'redmine/scm/adapters/git_adapter'
|
Chris@0
|
19
|
Chris@0
|
20 class Repository::Git < Repository
|
Chris@0
|
21 attr_protected :root_url
|
Chris@0
|
22 validates_presence_of :url
|
Chris@0
|
23
|
Chris@245
|
24 ATTRIBUTE_KEY_NAMES = {
|
Chris@245
|
25 "url" => "Path to repository",
|
Chris@245
|
26 }
|
Chris@245
|
27 def self.human_attribute_name(attribute_key_name)
|
Chris@245
|
28 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
|
Chris@245
|
29 end
|
Chris@245
|
30
|
Chris@245
|
31 def self.scm_adapter_class
|
Chris@0
|
32 Redmine::Scm::Adapters::GitAdapter
|
Chris@0
|
33 end
|
Chris@245
|
34
|
Chris@0
|
35 def self.scm_name
|
Chris@0
|
36 'Git'
|
Chris@0
|
37 end
|
Chris@0
|
38
|
Chris@245
|
39 def repo_log_encoding
|
Chris@245
|
40 'UTF-8'
|
Chris@245
|
41 end
|
Chris@245
|
42
|
Chris@119
|
43 # Returns the identifier for the given git changeset
|
Chris@119
|
44 def self.changeset_identifier(changeset)
|
Chris@119
|
45 changeset.scmid
|
Chris@119
|
46 end
|
Chris@119
|
47
|
Chris@119
|
48 # Returns the readable identifier for the given git changeset
|
Chris@119
|
49 def self.format_changeset_identifier(changeset)
|
Chris@119
|
50 changeset.revision[0, 8]
|
Chris@119
|
51 end
|
Chris@119
|
52
|
Chris@0
|
53 def branches
|
Chris@0
|
54 scm.branches
|
Chris@0
|
55 end
|
Chris@0
|
56
|
Chris@0
|
57 def tags
|
Chris@0
|
58 scm.tags
|
Chris@0
|
59 end
|
Chris@0
|
60
|
Chris@245
|
61 def find_changeset_by_name(name)
|
Chris@245
|
62 return nil if name.nil? || name.empty?
|
Chris@245
|
63 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
|
Chris@245
|
64 return e if e
|
Chris@245
|
65 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
|
Chris@245
|
66 end
|
Chris@245
|
67
|
Chris@0
|
68 # With SCM's that have a sequential commit numbering, redmine is able to be
|
Chris@0
|
69 # clever and only fetch changesets going forward from the most recent one
|
Chris@0
|
70 # it knows about. However, with git, you never know if people have merged
|
Chris@0
|
71 # commits into the middle of the repository history, so we should parse
|
Chris@0
|
72 # the entire log. Since it's way too slow for large repositories, we only
|
Chris@0
|
73 # parse 1 week before the last known commit.
|
Chris@0
|
74 # The repository can still be fully reloaded by calling #clear_changesets
|
Chris@0
|
75 # before fetching changesets (eg. for offline resync)
|
Chris@0
|
76 def fetch_changesets
|
Chris@0
|
77 c = changesets.find(:first, :order => 'committed_on DESC')
|
Chris@0
|
78 since = (c ? c.committed_on - 7.days : nil)
|
Chris@0
|
79
|
Chris@0
|
80 revisions = scm.revisions('', nil, nil, :all => true, :since => since)
|
Chris@0
|
81 return if revisions.nil? || revisions.empty?
|
Chris@0
|
82
|
Chris@0
|
83 recent_changesets = changesets.find(:all, :conditions => ['committed_on >= ?', since])
|
Chris@0
|
84
|
Chris@0
|
85 # Clean out revisions that are no longer in git
|
Chris@0
|
86 recent_changesets.each {|c| c.destroy unless revisions.detect {|r| r.scmid.to_s == c.scmid.to_s }}
|
Chris@0
|
87
|
Chris@0
|
88 # Subtract revisions that redmine already knows about
|
Chris@0
|
89 recent_revisions = recent_changesets.map{|c| c.scmid}
|
Chris@0
|
90 revisions.reject!{|r| recent_revisions.include?(r.scmid)}
|
Chris@0
|
91
|
Chris@0
|
92 # Save the remaining ones to the database
|
Chris@245
|
93 unless revisions.nil?
|
Chris@245
|
94 revisions.each do |rev|
|
Chris@245
|
95 transaction do
|
Chris@245
|
96 changeset = Changeset.new(
|
Chris@245
|
97 :repository => self,
|
Chris@245
|
98 :revision => rev.identifier,
|
Chris@245
|
99 :scmid => rev.scmid,
|
Chris@245
|
100 :committer => rev.author,
|
Chris@245
|
101 :committed_on => rev.time,
|
Chris@245
|
102 :comments => rev.message)
|
Chris@245
|
103
|
Chris@245
|
104 if changeset.save
|
Chris@245
|
105 rev.paths.each do |file|
|
Chris@245
|
106 Change.create(
|
Chris@245
|
107 :changeset => changeset,
|
Chris@245
|
108 :action => file[:action],
|
Chris@245
|
109 :path => file[:path])
|
Chris@245
|
110 end
|
Chris@245
|
111 end
|
Chris@245
|
112 end
|
Chris@245
|
113 end
|
Chris@245
|
114 end
|
Chris@0
|
115 end
|
Chris@0
|
116
|
Chris@0
|
117 def latest_changesets(path,rev,limit=10)
|
Chris@0
|
118 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
|
Chris@0
|
119 return [] if revisions.nil? || revisions.empty?
|
Chris@0
|
120
|
Chris@0
|
121 changesets.find(
|
Chris@0
|
122 :all,
|
Chris@0
|
123 :conditions => [
|
Chris@0
|
124 "scmid IN (?)",
|
Chris@0
|
125 revisions.map!{|c| c.scmid}
|
Chris@0
|
126 ],
|
Chris@0
|
127 :order => 'committed_on DESC'
|
Chris@0
|
128 )
|
Chris@0
|
129 end
|
Chris@0
|
130 end
|