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