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