Chris@441
|
1 # Redmine - project management software
|
Chris@441
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
|
Chris@441
|
4 #
|
Chris@0
|
5 # This program is free software; you can redistribute it and/or
|
Chris@0
|
6 # modify it under the terms of the GNU General Public License
|
Chris@0
|
7 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
8 # of the License, or (at your option) any later version.
|
Chris@441
|
9 #
|
Chris@0
|
10 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
13 # GNU General Public License for more details.
|
Chris@441
|
14 #
|
Chris@0
|
15 # You should have received a copy of the GNU General Public License
|
Chris@0
|
16 # along with this program; if not, write to the Free Software
|
Chris@0
|
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
18
|
Chris@0
|
19 require 'redmine/scm/adapters/git_adapter'
|
Chris@0
|
20
|
Chris@0
|
21 class Repository::Git < Repository
|
Chris@0
|
22 attr_protected :root_url
|
Chris@0
|
23 validates_presence_of :url
|
Chris@0
|
24
|
Chris@245
|
25 def self.human_attribute_name(attribute_key_name)
|
Chris@441
|
26 attr_name = attribute_key_name
|
Chris@441
|
27 if attr_name == "url"
|
Chris@441
|
28 attr_name = "path_to_repository"
|
Chris@441
|
29 end
|
Chris@441
|
30 super(attr_name)
|
Chris@245
|
31 end
|
Chris@245
|
32
|
Chris@245
|
33 def self.scm_adapter_class
|
Chris@0
|
34 Redmine::Scm::Adapters::GitAdapter
|
Chris@0
|
35 end
|
Chris@245
|
36
|
Chris@0
|
37 def self.scm_name
|
Chris@0
|
38 'Git'
|
Chris@0
|
39 end
|
Chris@0
|
40
|
Chris@441
|
41 def report_last_commit
|
Chris@441
|
42 extra_report_last_commit
|
Chris@441
|
43 end
|
Chris@441
|
44
|
Chris@441
|
45 def extra_report_last_commit
|
Chris@441
|
46 return false if extra_info.nil?
|
Chris@441
|
47 v = extra_info["extra_report_last_commit"]
|
Chris@441
|
48 return false if v.nil?
|
Chris@441
|
49 v.to_s != '0'
|
Chris@441
|
50 end
|
Chris@441
|
51
|
Chris@441
|
52 def supports_directory_revisions?
|
Chris@441
|
53 true
|
Chris@441
|
54 end
|
Chris@441
|
55
|
Chris@245
|
56 def repo_log_encoding
|
Chris@245
|
57 'UTF-8'
|
Chris@245
|
58 end
|
Chris@245
|
59
|
Chris@117
|
60 # Returns the identifier for the given git changeset
|
Chris@117
|
61 def self.changeset_identifier(changeset)
|
Chris@117
|
62 changeset.scmid
|
Chris@117
|
63 end
|
Chris@117
|
64
|
Chris@117
|
65 # Returns the readable identifier for the given git changeset
|
Chris@117
|
66 def self.format_changeset_identifier(changeset)
|
Chris@117
|
67 changeset.revision[0, 8]
|
Chris@117
|
68 end
|
Chris@117
|
69
|
Chris@0
|
70 def branches
|
Chris@0
|
71 scm.branches
|
Chris@0
|
72 end
|
Chris@0
|
73
|
Chris@0
|
74 def tags
|
Chris@0
|
75 scm.tags
|
Chris@0
|
76 end
|
Chris@0
|
77
|
Chris@245
|
78 def find_changeset_by_name(name)
|
Chris@245
|
79 return nil if name.nil? || name.empty?
|
Chris@245
|
80 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
|
Chris@245
|
81 return e if e
|
Chris@245
|
82 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
|
Chris@245
|
83 end
|
Chris@245
|
84
|
Chris@441
|
85 def entries(path=nil, identifier=nil)
|
Chris@441
|
86 scm.entries(path,
|
Chris@441
|
87 identifier,
|
Chris@441
|
88 options = {:report_last_commit => extra_report_last_commit})
|
Chris@441
|
89 end
|
Chris@441
|
90
|
Chris@441
|
91 # In Git and Mercurial, revisions are not in date order.
|
Chris@441
|
92 # Redmine Mercurial fixed issues.
|
Chris@441
|
93 # * Redmine Takes Too Long On Large Mercurial Repository
|
Chris@441
|
94 # http://www.redmine.org/issues/3449
|
Chris@441
|
95 # * Sorting for changesets might go wrong on Mercurial repos
|
Chris@441
|
96 # http://www.redmine.org/issues/3567
|
Chris@441
|
97 #
|
Chris@441
|
98 # Database revision column is text, so Redmine can not sort by revision.
|
Chris@441
|
99 # Mercurial has revision number, and revision number guarantees revision order.
|
Chris@441
|
100 # Redmine Mercurial model stored revisions ordered by database id to database.
|
Chris@441
|
101 # So, Redmine Mercurial model can use correct ordering revisions.
|
Chris@441
|
102 #
|
Chris@441
|
103 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
|
Chris@441
|
104 # to get limited revisions from old to new.
|
Chris@441
|
105 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
|
Chris@441
|
106 #
|
Chris@0
|
107 # The repository can still be fully reloaded by calling #clear_changesets
|
Chris@0
|
108 # before fetching changesets (eg. for offline resync)
|
Chris@0
|
109 def fetch_changesets
|
Chris@441
|
110 scm_brs = branches
|
Chris@441
|
111 return if scm_brs.nil? || scm_brs.empty?
|
Chris@441
|
112 h1 = extra_info || {}
|
Chris@441
|
113 h = h1.dup
|
Chris@441
|
114 h["branches"] ||= {}
|
Chris@441
|
115 h["db_consistent"] ||= {}
|
Chris@441
|
116 if changesets.count == 0
|
Chris@441
|
117 h["db_consistent"]["ordering"] = 1
|
Chris@441
|
118 merge_extra_info(h)
|
Chris@441
|
119 self.save
|
Chris@441
|
120 elsif ! h["db_consistent"].has_key?("ordering")
|
Chris@441
|
121 h["db_consistent"]["ordering"] = 0
|
Chris@441
|
122 merge_extra_info(h)
|
Chris@441
|
123 self.save
|
Chris@441
|
124 end
|
Chris@441
|
125 scm_brs.each do |br|
|
Chris@441
|
126 from_scmid = nil
|
Chris@441
|
127 from_scmid = h["branches"][br]["last_scmid"] if h["branches"][br]
|
Chris@441
|
128 h["branches"][br] ||= {}
|
Chris@441
|
129 scm.revisions('', from_scmid, br, {:reverse => true}) do |rev|
|
Chris@441
|
130 db_rev = find_changeset_by_name(rev.revision)
|
Chris@245
|
131 transaction do
|
Chris@441
|
132 if db_rev.nil?
|
Chris@441
|
133 save_revision(rev)
|
Chris@245
|
134 end
|
Chris@441
|
135 h["branches"][br]["last_scmid"] = rev.scmid
|
Chris@441
|
136 merge_extra_info(h)
|
Chris@441
|
137 self.save
|
Chris@245
|
138 end
|
Chris@245
|
139 end
|
Chris@245
|
140 end
|
Chris@0
|
141 end
|
Chris@0
|
142
|
Chris@441
|
143 def save_revision(rev)
|
Chris@441
|
144 changeset = Changeset.new(
|
Chris@441
|
145 :repository => self,
|
Chris@441
|
146 :revision => rev.identifier,
|
Chris@441
|
147 :scmid => rev.scmid,
|
Chris@441
|
148 :committer => rev.author,
|
Chris@441
|
149 :committed_on => rev.time,
|
Chris@441
|
150 :comments => rev.message
|
Chris@441
|
151 )
|
Chris@441
|
152 if changeset.save
|
Chris@441
|
153 rev.paths.each do |file|
|
Chris@441
|
154 Change.create(
|
Chris@441
|
155 :changeset => changeset,
|
Chris@441
|
156 :action => file[:action],
|
Chris@441
|
157 :path => file[:path])
|
Chris@441
|
158 end
|
Chris@441
|
159 end
|
Chris@441
|
160 end
|
Chris@441
|
161 private :save_revision
|
Chris@441
|
162
|
Chris@0
|
163 def latest_changesets(path,rev,limit=10)
|
Chris@0
|
164 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
|
Chris@0
|
165 return [] if revisions.nil? || revisions.empty?
|
Chris@0
|
166
|
Chris@0
|
167 changesets.find(
|
Chris@441
|
168 :all,
|
Chris@0
|
169 :conditions => [
|
Chris@441
|
170 "scmid IN (?)",
|
Chris@0
|
171 revisions.map!{|c| c.scmid}
|
Chris@0
|
172 ],
|
Chris@0
|
173 :order => 'committed_on DESC'
|
Chris@0
|
174 )
|
Chris@0
|
175 end
|
Chris@0
|
176 end
|