##// END OF EJS Templates
set default category_id instead of the object (#11665)...
set default category_id instead of the object (#11665) Rails 2.3 still has issues with synchronizing the association_id and association attributes of an object. That means, if you set the association with an object first and then just set the id afterwards, the object wins and the setting of the id gets lost. This is not an issue in Rails >= 3.1 anymore. Contributed by Holger Just. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/1.4-stable@10226 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r9177:29782860b7c2
r10043:14dcefaa97f9
Show More
git.rb
258 lines | 8.1 KiB | text/x-ruby | RubyLexer
Toshi MARUYAMA
scm: git: code clean up model....
r5630 # Redmine - project management software
Toshi MARUYAMA
remove trailing white-space from app/models/repository/git.rb...
r9025 # Copyright (C) 2006-2012 Jean-Philippe Lang
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
Toshi MARUYAMA
scm: git: code clean up model....
r5630 #
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
Toshi MARUYAMA
scm: git: code clean up model....
r5630 #
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
Toshi MARUYAMA
scm: git: code clean up model....
r5630 #
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/git_adapter'
class Repository::Git < Repository
attr_protected :root_url
validates_presence_of :url
Jean-Philippe Lang
human_attribute_name accepts optional argument....
r8166 def self.human_attribute_name(attribute_key_name, *args)
Toshi MARUYAMA
Rails3: scm: git: use .to_s for overriding human_attribute_name parameter...
r8855 attr_name = attribute_key_name.to_s
Toshi MARUYAMA
scm: use i18n string at 'Path to repository' setting in Mercurial, Git, Bazaar and Darcs....
r5409 if attr_name == "url"
attr_name = "path_to_repository"
end
Jean-Philippe Lang
human_attribute_name accepts optional argument....
r8166 super(attr_name, *args)
Toshi MARUYAMA
scm: add scm specific human_attribute_name for input validation....
r4855 end
Toshi MARUYAMA
scm: add scm command and version methods at repository models (#4273)....
r4702 def self.scm_adapter_class
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 Redmine::Scm::Adapters::GitAdapter
end
Toshi MARUYAMA
scm: add scm command and version methods at repository models (#4273)....
r4702
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 def self.scm_name
'Git'
end
Toshi MARUYAMA
scm: git: show only filename and filesize if setting of reporting last commit is disable (#8365, #7047)....
r5655 def report_last_commit
extra_report_last_commit
end
Toshi MARUYAMA
scm: git: change model report_last_commit() name to extra_report_last_commit() (#7047)....
r5541 def extra_report_last_commit
Toshi MARUYAMA
scm: git: get "extra_report_last_commit" value from extra_info (#8365, #7047)....
r5648 return false if extra_info.nil?
v = extra_info["extra_report_last_commit"]
return false if v.nil?
v.to_s != '0'
Toshi MARUYAMA
scm: git: add the model method whether reporting last commit in repository tree (#7047)....
r5536 end
Toshi MARUYAMA
scm: set supporting directory revisions or not at scm level....
r5024 def supports_directory_revisions?
true
end
Toshi MARUYAMA
scm: model: git: set revision graph support true (#5501)...
r7597 def supports_revision_graph?
true
end
Toshi MARUYAMA
scm: git: use --encoding=UTF-8 in "git log" (#3396)....
r4844 def repo_log_encoding
'UTF-8'
end
Toshi MARUYAMA
Changing revision label and identifier at SCM adapter level (#3724, #6092)...
r4493 # Returns the identifier for the given git changeset
def self.changeset_identifier(changeset)
changeset.scmid
end
# Returns the readable identifier for the given git changeset
def self.format_changeset_identifier(changeset)
changeset.revision[0, 8]
end
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735 def branches
scm.branches
end
def tags
scm.tags
end
Toshi MARUYAMA
scm: return nil at model default_branch and override at git model (#8458, #6713)....
r6010 def default_branch
scm.default_branch
Toshi MARUYAMA
scm: git: catch exception in model default_branch() (#8458, #6713)....
r6100 rescue Exception => e
logger.error "git: error during get default branch: #{e.message}"
nil
Toshi MARUYAMA
scm: return nil at model default_branch and override at git model (#8458, #6713)....
r6010 end
Toshi MARUYAMA
scm: git: implement find_changeset_by_name (#7047)....
r4731 def find_changeset_by_name(name)
return nil if name.nil? || name.empty?
e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
return e if e
changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
end
Toshi MARUYAMA
scm: git: override entries() in model for browsing tree performance (#7047)....
r5535 def entries(path=nil, identifier=nil)
Toshi MARUYAMA
scm: git: use the model value of whether reporting last commit in repository tree (#7047)....
r5537 scm.entries(path,
identifier,
Toshi MARUYAMA
scm: git: change model report_last_commit() name to extra_report_last_commit() (#7047)....
r5541 options = {:report_last_commit => extra_report_last_commit})
Toshi MARUYAMA
scm: git: override entries() in model for browsing tree performance (#7047)....
r5535 end
Toshi MARUYAMA
scm: git: recovery and improve comments of fetching from 1.1 about harmful influence that git does not have the revision number (#9472)...
r7538 # With SCMs that have a sequential commit numbering,
# such as Subversion and Mercurial,
# Redmine is able to be clever and only fetch changesets
# going forward from the most recent one it knows about.
Toshi MARUYAMA
remove trailing white-space from app/models/repository/git.rb...
r9025 #
Toshi MARUYAMA
scm: git: recovery and improve comments of fetching from 1.1 about harmful influence that git does not have the revision number (#9472)...
r7538 # However, Git does not have a sequential commit numbering.
#
# In order to fetch only new adding revisions,
Toshi MARUYAMA
scm: git: process new git revisions all at once rather than per branch (#10470)...
r9149 # Redmine needs to save "heads".
Toshi MARUYAMA
scm: git: recovery and improve comments of fetching from 1.1 about harmful influence that git does not have the revision number (#9472)...
r7538 #
Toshi MARUYAMA
scm: git: add comments of revision order in fetch_changesets()....
r5524 # In Git and Mercurial, revisions are not in date order.
Toshi MARUYAMA
scm: git: brush up model fetch_changesets() comment....
r5738 # Redmine Mercurial fixed issues.
Toshi MARUYAMA
scm: git: add comments of revision order in fetch_changesets()....
r5524 # * Redmine Takes Too Long On Large Mercurial Repository
# http://www.redmine.org/issues/3449
Toshi MARUYAMA
scm: git: code clean up model....
r5630 # * Sorting for changesets might go wrong on Mercurial repos
Toshi MARUYAMA
scm: git: add comments of revision order in fetch_changesets()....
r5524 # http://www.redmine.org/issues/3567
Toshi MARUYAMA
scm: git: brush up model fetch_changesets() comment....
r5738 #
Toshi MARUYAMA
scm: git: add comments of revision order in fetch_changesets()....
r5524 # Database revision column is text, so Redmine can not sort by revision.
# Mercurial has revision number, and revision number guarantees revision order.
Toshi MARUYAMA
scm: git: brush up model fetch_changesets() comment....
r5738 # Redmine Mercurial model stored revisions ordered by database id to database.
# So, Redmine Mercurial model can use correct ordering revisions.
Toshi MARUYAMA
scm: git: add comments of revision order in fetch_changesets()....
r5524 #
Toshi MARUYAMA
scm: git: brush up model fetch_changesets() comment....
r5738 # Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
# to get limited revisions from old to new.
Toshi MARUYAMA
scm: git: add comments of revision order in fetch_changesets()....
r5524 # But, Git 1.7.3.4 does not support --reverse with -n or --skip.
#
Jean-Philippe Lang
Do not parse the entire git log to fetch new commits (takes several minutes for a few thousands commits), but only 1 week before the last known commit (#4547, #4716)....
r3280 # The repository can still be fully reloaded by calling #clear_changesets
# before fetching changesets (eg. for offline resync)
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 def fetch_changesets
Toshi MARUYAMA
scm: git: fix fetching 7 days problem (#7146, #6013)....
r5668 scm_brs = branches
return if scm_brs.nil? || scm_brs.empty?
Toshi MARUYAMA
scm: git: process new git revisions all at once rather than per branch (#10470)...
r9149
Toshi MARUYAMA
scm: git: use hash#dup for extra_info in model fetch_changesets()....
r5736 h1 = extra_info || {}
h = h1.dup
Toshi MARUYAMA
scm: git: process new git revisions all at once rather than per branch (#10470)...
r9149 repo_heads = scm_brs.map{ |br| br.scmid }
h["heads"] ||= []
prev_db_heads = h["heads"].dup
if prev_db_heads.empty?
prev_db_heads += heads_from_branches_hash
end
return if prev_db_heads.sort == repo_heads.sort
Toshi MARUYAMA
scm: git: set revisions ordering inconsistent in existing database (#5357)....
r5733 h["db_consistent"] ||= {}
if changesets.count == 0
h["db_consistent"]["ordering"] = 1
merge_extra_info(h)
self.save
elsif ! h["db_consistent"].has_key?("ordering")
h["db_consistent"]["ordering"] = 0
merge_extra_info(h)
self.save
end
Toshi MARUYAMA
scm: git: process new git revisions all at once rather than per branch (#10470)...
r9149 save_revisions(prev_db_heads, repo_heads)
Toshi MARUYAMA
scm: git: backout r8839 (#8857)...
r9022 end
Toshi MARUYAMA
scm: git: process new git revisions all at once rather than per branch (#10470)...
r9149 def save_revisions(prev_db_heads, repo_heads)
Toshi MARUYAMA
scm: git: fix model source indents (#10470)...
r9150 h = {}
opts = {}
opts[:reverse] = true
opts[:excludes] = prev_db_heads
opts[:includes] = repo_heads
revisions = scm.revisions('', nil, nil, opts)
return if revisions.blank?
# Make the search for existing revisions in the database in a more sufficient manner
Toshi MARUYAMA
scm: git: add the comment of the reason to scan database in fetching every time (#10470)...
r9153 #
# Git branch is the reference to the specific revision.
# Git can *delete* remote branch and *re-push* branch.
#
# $ git push remote :branch
# $ git push remote branch
#
# After deleting branch, revisions remain in repository until "git gc".
# On git 1.7.2.3, default pruning date is 2 weeks.
# So, "git log --not deleted_branch_head_revision" return code is 0.
#
# After re-pushing branch, "git log" returns revisions which are saved in database.
# So, Redmine needs to scan revisions and database every time.
#
Toshi MARUYAMA
scm: git: fix model source indents (#10470)...
r9150 # This is replacing the one-after-one queries.
# Find all revisions, that are in the database, and then remove them from the revision array.
# Then later we won't need any conditions for db existence.
# Query for several revisions at once, and remove them from the revisions array, if they are there.
# Do this in chunks, to avoid eventual memory problems (in case of tens of thousands of commits).
Toshi MARUYAMA
scm: git: fix typo in fetching revisions comment....
r9152 # If there are no revisions (because the original code's algorithm filtered them),
Toshi MARUYAMA
scm: git: fix model source indents (#10470)...
r9150 # then this part will be stepped over.
# We make queries, just if there is any revision.
limit = 100
offset = 0
revisions_copy = revisions.clone # revisions will change
while offset < revisions_copy.size
recent_changesets_slice = changesets.find(
Toshi MARUYAMA
scm: git: performance improvements in fetching revisions (#8857, #9472)...
r9024 :all,
:conditions => [
'scmid IN (?)',
revisions_copy.slice(offset, limit).map{|x| x.scmid}
]
)
Toshi MARUYAMA
scm: git: fix model source indents (#10470)...
r9150 # Subtract revisions that redmine already knows about
recent_revisions = recent_changesets_slice.map{|c| c.scmid}
revisions.reject!{|r| recent_revisions.include?(r.scmid)}
offset += limit
end
Toshi MARUYAMA
scm: git: performance improvements in fetching revisions (#8857, #9472)...
r9024
Toshi MARUYAMA
scm: git: fix model source indents (#10470)...
r9150 revisions.each do |rev|
transaction do
# There is no search in the db for this revision, because above we ensured,
# that it's not in the db.
Toshi MARUYAMA
scm: git: use Changeset#create and Changeset#create_change as same with Subversion and Mercurial...
r9156 save_revision(rev)
Toshi MARUYAMA
scm: git: performance improvements in fetching revisions (#8857, #9472)...
r9024 end
Toshi MARUYAMA
scm: git: fix model source indents (#10470)...
r9150 end
h["heads"] = repo_heads.dup
merge_extra_info(h)
self.save
Toshi MARUYAMA
scm: git: refactor saving a revision in model....
r5504 end
Toshi MARUYAMA
scm: git: split fetch_changesets method...
r8694 private :save_revisions
Toshi MARUYAMA
scm: git: refactor saving a revision in model....
r5504
def save_revision(rev)
Toshi MARUYAMA
scm: mercurial: git: save parents in creating changesets (#10470)...
r9177 parents = (rev.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
Toshi MARUYAMA
scm: git: use Changeset#create and Changeset#create_change as same with Subversion and Mercurial...
r9156 changeset = Changeset.create(
Toshi MARUYAMA
scm: git: refactor saving a revision in model....
r5504 :repository => self,
:revision => rev.identifier,
:scmid => rev.scmid,
Toshi MARUYAMA
scm: git: code clean up model....
r5630 :committer => rev.author,
Toshi MARUYAMA
scm: git: move saving changesets from adapter to model (#3396)....
r4839 :committed_on => rev.time,
Toshi MARUYAMA
scm: mercurial: git: save parents in creating changesets (#10470)...
r9177 :comments => rev.message,
:parents => parents
Toshi MARUYAMA
scm: git: refactor saving a revision in model....
r5504 )
Toshi MARUYAMA
scm: git: use Changeset#create and Changeset#create_change as same with Subversion and Mercurial...
r9156 unless changeset.new_record?
rev.paths.each { |change| changeset.create_change(change) }
Toshi MARUYAMA
scm: git: move saving changesets from adapter to model (#3396)....
r4839 end
Toshi MARUYAMA
scm: git: return saved revision at model "save_revision" (#5501)...
r7534 changeset
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735 end
Toshi MARUYAMA
scm: git: refactor saving a revision in model....
r5504 private :save_revision
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735
Toshi MARUYAMA
scm: git: add model method to get heads from extra_info branches hash (#8857)...
r8696 def heads_from_branches_hash
h1 = extra_info || {}
h = h1.dup
h["branches"] ||= {}
h['branches'].map{|br, hs| hs['last_scmid']}
end
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735 def latest_changesets(path,rev,limit=10)
revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
return [] if revisions.nil? || revisions.empty?
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735 changesets.find(
Toshi MARUYAMA
scm: git: code clean up model....
r5630 :all,
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735 :conditions => [
Toshi MARUYAMA
scm: git: code clean up model....
r5630 "scmid IN (?)",
Eric Davis
Added branch and tag support to the git repository viewer. (#1406)...
r2735 revisions.map!{|c| c.scmid}
],
:order => 'committed_on DESC'
)
Jean-Philippe Lang
Merged Git support branch (r1200 to r1226)....
r1222 end
end