2011-05-12 11:06:57 +04:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2008-03-12 23:28:49 +03:00
|
|
|
# Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
|
2011-05-12 11:06:57 +04:00
|
|
|
#
|
2008-03-12 23:28:49 +03:00
|
|
|
# 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.
|
2011-05-12 11:06:57 +04:00
|
|
|
#
|
2008-03-12 23:28:49 +03:00
|
|
|
# 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.
|
2011-05-12 11:06:57 +04:00
|
|
|
#
|
2008-03-12 23:28:49 +03:00
|
|
|
# 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
|
|
|
|
|
2011-12-18 17:20:19 +04:00
|
|
|
def self.human_attribute_name(attribute_key_name, *args)
|
2012-02-24 18:59:31 +04:00
|
|
|
attr_name = attribute_key_name.to_s
|
2011-04-23 11:37:03 +04:00
|
|
|
if attr_name == "url"
|
|
|
|
attr_name = "path_to_repository"
|
|
|
|
end
|
2011-12-18 17:20:19 +04:00
|
|
|
super(attr_name, *args)
|
2011-03-01 08:14:03 +03:00
|
|
|
end
|
|
|
|
|
2011-02-14 11:45:34 +03:00
|
|
|
def self.scm_adapter_class
|
2008-03-12 23:28:49 +03:00
|
|
|
Redmine::Scm::Adapters::GitAdapter
|
|
|
|
end
|
2011-02-14 11:45:34 +03:00
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def self.scm_name
|
|
|
|
'Git'
|
|
|
|
end
|
|
|
|
|
2011-05-13 07:55:21 +04:00
|
|
|
def report_last_commit
|
|
|
|
extra_report_last_commit
|
|
|
|
end
|
|
|
|
|
2011-05-06 07:43:11 +04:00
|
|
|
def extra_report_last_commit
|
2011-05-13 07:51:14 +04:00
|
|
|
return false if extra_info.nil?
|
|
|
|
v = extra_info["extra_report_last_commit"]
|
|
|
|
return false if v.nil?
|
|
|
|
v.to_s != '0'
|
2011-05-05 15:43:09 +04:00
|
|
|
end
|
|
|
|
|
2011-03-16 03:50:11 +03:00
|
|
|
def supports_directory_revisions?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-11-03 12:12:24 +04:00
|
|
|
def supports_revision_graph?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-02-28 17:12:47 +03:00
|
|
|
def repo_log_encoding
|
|
|
|
'UTF-8'
|
|
|
|
end
|
|
|
|
|
2011-01-02 12:45:05 +03:00
|
|
|
# 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
|
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
def branches
|
|
|
|
scm.branches
|
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
|
|
|
scm.tags
|
|
|
|
end
|
|
|
|
|
2011-06-30 07:46:32 +04:00
|
|
|
def default_branch
|
|
|
|
scm.default_branch
|
2011-07-10 15:08:00 +04:00
|
|
|
rescue Exception => e
|
|
|
|
logger.error "git: error during get default branch: #{e.message}"
|
|
|
|
nil
|
2011-06-30 07:46:32 +04:00
|
|
|
end
|
|
|
|
|
2011-02-16 14:16:53 +03:00
|
|
|
def find_changeset_by_name(name)
|
2012-06-10 22:38:31 +04:00
|
|
|
if name.present?
|
|
|
|
changesets.where(:revision => name.to_s).first ||
|
|
|
|
changesets.where('scmid LIKE ?', "#{name}%").first
|
|
|
|
end
|
2011-02-16 14:16:53 +03:00
|
|
|
end
|
|
|
|
|
2011-05-05 15:42:46 +04:00
|
|
|
def entries(path=nil, identifier=nil)
|
2012-06-10 22:32:09 +04:00
|
|
|
entries = scm.entries(path, identifier, :report_last_commit => extra_report_last_commit)
|
|
|
|
load_entries_changesets(entries)
|
|
|
|
entries
|
2011-05-05 15:42:46 +04:00
|
|
|
end
|
|
|
|
|
2011-10-27 04:35:43 +04:00
|
|
|
# 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.
|
2012-03-07 11:56:30 +04:00
|
|
|
#
|
2011-10-27 04:35:43 +04:00
|
|
|
# However, Git does not have a sequential commit numbering.
|
|
|
|
#
|
|
|
|
# In order to fetch only new adding revisions,
|
2012-03-28 21:18:04 +04:00
|
|
|
# Redmine needs to save "heads".
|
2011-10-27 04:35:43 +04:00
|
|
|
#
|
2011-05-05 03:38:11 +04:00
|
|
|
# In Git and Mercurial, revisions are not in date order.
|
2011-05-20 02:58:38 +04:00
|
|
|
# Redmine Mercurial fixed issues.
|
2011-05-05 03:38:11 +04:00
|
|
|
# * Redmine Takes Too Long On Large Mercurial Repository
|
|
|
|
# http://www.redmine.org/issues/3449
|
2011-05-12 11:06:57 +04:00
|
|
|
# * Sorting for changesets might go wrong on Mercurial repos
|
2011-05-05 03:38:11 +04:00
|
|
|
# http://www.redmine.org/issues/3567
|
2011-05-20 02:58:38 +04:00
|
|
|
#
|
2011-05-05 03:38:11 +04:00
|
|
|
# Database revision column is text, so Redmine can not sort by revision.
|
|
|
|
# Mercurial has revision number, and revision number guarantees revision order.
|
2011-05-20 02:58:38 +04:00
|
|
|
# Redmine Mercurial model stored revisions ordered by database id to database.
|
|
|
|
# So, Redmine Mercurial model can use correct ordering revisions.
|
2011-05-05 03:38:11 +04:00
|
|
|
#
|
2011-05-20 02:58:38 +04:00
|
|
|
# Redmine Mercurial adapter uses "hg log -r 0:tip --limit 10"
|
|
|
|
# to get limited revisions from old to new.
|
2011-05-05 03:38:11 +04:00
|
|
|
# But, Git 1.7.3.4 does not support --reverse with -n or --skip.
|
|
|
|
#
|
2010-02-07 18:17:21 +03:00
|
|
|
# The repository can still be fully reloaded by calling #clear_changesets
|
|
|
|
# before fetching changesets (eg. for offline resync)
|
2008-03-12 23:28:49 +03:00
|
|
|
def fetch_changesets
|
2011-05-13 14:38:45 +04:00
|
|
|
scm_brs = branches
|
|
|
|
return if scm_brs.nil? || scm_brs.empty?
|
2012-03-28 21:18:04 +04:00
|
|
|
|
2011-05-19 06:38:14 +04:00
|
|
|
h1 = extra_info || {}
|
|
|
|
h = h1.dup
|
2012-03-28 21:18:04 +04:00
|
|
|
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
|
|
|
|
|
2011-05-19 06:16:27 +04:00
|
|
|
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
|
2012-03-28 21:18:04 +04:00
|
|
|
save_revisions(prev_db_heads, repo_heads)
|
2012-03-07 09:57:44 +04:00
|
|
|
end
|
|
|
|
|
2012-03-28 21:18:04 +04:00
|
|
|
def save_revisions(prev_db_heads, repo_heads)
|
2012-03-28 21:18:19 +04:00
|
|
|
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
|
2012-03-29 14:05:13 +04:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2012-03-28 21:18:19 +04:00
|
|
|
# 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).
|
2012-03-29 06:42:20 +04:00
|
|
|
# If there are no revisions (because the original code's algorithm filtered them),
|
2012-03-28 21:18:19 +04:00
|
|
|
# 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(
|
2012-03-07 11:56:15 +04:00
|
|
|
:all,
|
|
|
|
:conditions => [
|
|
|
|
'scmid IN (?)',
|
|
|
|
revisions_copy.slice(offset, limit).map{|x| x.scmid}
|
|
|
|
]
|
|
|
|
)
|
2012-03-28 21:18:19 +04:00
|
|
|
# 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
|
2012-03-07 11:56:15 +04:00
|
|
|
|
2012-03-28 21:18:19 +04:00
|
|
|
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.
|
2012-03-30 02:41:20 +04:00
|
|
|
save_revision(rev)
|
2012-03-07 11:56:15 +04:00
|
|
|
end
|
2012-03-28 21:18:19 +04:00
|
|
|
end
|
|
|
|
h["heads"] = repo_heads.dup
|
|
|
|
merge_extra_info(h)
|
|
|
|
self.save
|
2011-05-04 07:44:10 +04:00
|
|
|
end
|
2012-02-08 15:10:46 +04:00
|
|
|
private :save_revisions
|
2011-05-04 07:44:10 +04:00
|
|
|
|
|
|
|
def save_revision(rev)
|
2012-04-03 12:05:30 +04:00
|
|
|
parents = (rev.parents || []).collect{|rp| find_changeset_by_name(rp)}.compact
|
2012-03-30 02:41:20 +04:00
|
|
|
changeset = Changeset.create(
|
2011-05-04 07:44:10 +04:00
|
|
|
:repository => self,
|
|
|
|
:revision => rev.identifier,
|
|
|
|
:scmid => rev.scmid,
|
2011-05-12 11:06:57 +04:00
|
|
|
:committer => rev.author,
|
2011-02-28 07:46:42 +03:00
|
|
|
:committed_on => rev.time,
|
2012-04-03 12:05:30 +04:00
|
|
|
:comments => rev.message,
|
|
|
|
:parents => parents
|
2011-05-04 07:44:10 +04:00
|
|
|
)
|
2012-03-30 02:41:20 +04:00
|
|
|
unless changeset.new_record?
|
|
|
|
rev.paths.each { |change| changeset.create_change(change) }
|
2011-02-28 07:46:42 +03:00
|
|
|
end
|
2011-10-25 18:58:03 +04:00
|
|
|
changeset
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
2011-05-04 07:44:10 +04:00
|
|
|
private :save_revision
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2012-02-09 04:11:03 +04:00
|
|
|
def heads_from_branches_hash
|
|
|
|
h1 = extra_info || {}
|
|
|
|
h = h1.dup
|
|
|
|
h["branches"] ||= {}
|
|
|
|
h['branches'].map{|br, hs| hs['last_scmid']}
|
|
|
|
end
|
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
def latest_changesets(path,rev,limit=10)
|
|
|
|
revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
|
|
|
|
return [] if revisions.nil? || revisions.empty?
|
2008-03-12 23:28:49 +03:00
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
changesets.find(
|
2011-05-12 11:06:57 +04:00
|
|
|
:all,
|
2009-08-16 02:41:40 +04:00
|
|
|
:conditions => [
|
2011-05-12 11:06:57 +04:00
|
|
|
"scmid IN (?)",
|
2009-08-16 02:41:40 +04:00
|
|
|
revisions.map!{|c| c.scmid}
|
2013-01-03 15:40:39 +04:00
|
|
|
]
|
2009-08-16 02:41:40 +04:00
|
|
|
)
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2012-11-21 11:44:29 +04:00
|
|
|
|
|
|
|
def clear_extra_info_of_changesets
|
2012-11-21 12:17:56 +04:00
|
|
|
return if extra_info.nil?
|
2012-11-21 11:44:29 +04:00
|
|
|
v = extra_info["extra_report_last_commit"]
|
|
|
|
write_attribute(:extra_info, nil)
|
|
|
|
h = {}
|
|
|
|
h["extra_report_last_commit"] = v
|
|
|
|
merge_extra_info(h)
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
private :clear_extra_info_of_changesets
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|