Class: Repository

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/repository.rb

Overview

redMine - project management software Copyright (C) 2006-2007 Jean-Philippe Lang

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.

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.

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.

Direct Known Subclasses

Bazaar, Cvs, Darcs, Filesystem, Git, Mercurial, Subversion

Defined Under Namespace

Classes: Bazaar, Cvs, Darcs, Filesystem, Git, Mercurial, Subversion

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_scmObject



155
156
157
# File 'app/models/repository.rb', line 155

def self.available_scm
  subclasses.collect {|klass| [klass.scm_name, klass.name]}
end

.factory(klass_name, *args) ⇒ Object



159
160
161
162
163
164
# File 'app/models/repository.rb', line 159

def self.factory(klass_name, *args)
  klass = "Repository::#{klass_name}".constantize
  klass.new(*args)
rescue
  nil
end

.fetch_changesetsObject

fetch new changesets for all repositories can be called periodically by an external script eg. ruby script/runner "Repository.fetch_changesets"



142
143
144
# File 'app/models/repository.rb', line 142

def self.fetch_changesets
  find(:all).each(&:fetch_changesets)
end

.scan_changesets_for_issue_idsObject

scan changeset comments to find related and fixed issues for all repositories



147
148
149
# File 'app/models/repository.rb', line 147

def self.scan_changesets_for_issue_ids
  find(:all).each(&:scan_changesets_for_issue_ids)
end

.scm_nameObject



151
152
153
# File 'app/models/repository.rb', line 151

def self.scm_name
  'Abstract'
end

Instance Method Details

#cat(path, identifier = nil) ⇒ Object



70
71
72
# File 'app/models/repository.rb', line 70

def cat(path, identifier=nil)
  scm.cat(path, identifier)
end

#changesets_for_path(path, options = {}) ⇒ Object

Default behaviour: we search in cached changesets



79
80
81
82
83
84
85
# File 'app/models/repository.rb', line 79

def changesets_for_path(path, options={})
  path = "/#{path}" unless path.starts_with?('/')
  Change.find(:all, :include => {:changeset => :user}, 
                    :conditions => ["repository_id = ? AND path = ?", id, path],
                    :order => "committed_on DESC, #{Changeset.table_name}.id DESC",
                    :limit => options[:limit]).collect(&:changeset)
end

#committer_ids=(h) ⇒ Object

Maps committers username to a user ids



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/repository.rb', line 106

def committer_ids=(h)
  if h.is_a?(Hash)
    committers.each do |committer, user_id|
      new_user_id = h[committer]
      if new_user_id && (new_user_id.to_i != user_id.to_i)
        new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
        Changeset.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer])
      end
    end
    @committers = nil
    true
  else
    false
  end
end

#committersObject

Returns an array of committers usernames and associated user_id



101
102
103
# File 'app/models/repository.rb', line 101

def committers
  @committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
end

#diff(path, rev, rev_to) ⇒ Object



74
75
76
# File 'app/models/repository.rb', line 74

def diff(path, rev, rev_to)
  scm.diff(path, rev, rev_to)
end

#entries(path = nil, identifier = nil) ⇒ Object



62
63
64
# File 'app/models/repository.rb', line 62

def entries(path=nil, identifier=nil)
  scm.entries(path, identifier)
end

#entry(path = nil, identifier = nil) ⇒ Object



58
59
60
# File 'app/models/repository.rb', line 58

def entry(path=nil, identifier=nil)
  scm.entry(path, identifier)
end

#find_committer_user(committer) ⇒ Object

Returns the Redmine User corresponding to the given committer It will return nil if the committer is not yet mapped and if no User with the same username or email was found



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/repository.rb', line 125

def find_committer_user(committer)
  if committer
    c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
    if c && c.user
      c.user
    elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
      username, email = $1.strip, $3
      u = User.(username)
      u ||= User.find_by_mail(email) unless email.blank?
      u
    end
  end
end

#latest_changesetObject



92
93
94
# File 'app/models/repository.rb', line 92

def latest_changeset
  @latest_changeset ||= changesets.find(:first)
end

#properties(path, identifier = nil) ⇒ Object



66
67
68
# File 'app/models/repository.rb', line 66

def properties(path, identifier=nil)
  scm.properties(path, identifier)
end

#relative_path(path) ⇒ Object

Returns a path relative to the url of the repository



88
89
90
# File 'app/models/repository.rb', line 88

def relative_path(path)
  path
end

#root_url=(arg) ⇒ Object

Removes leading and trailing whitespace



36
37
38
# File 'app/models/repository.rb', line 36

def root_url=(arg)
  write_attribute(:root_url, arg ? arg.to_s.strip : nil)
end

#scan_changesets_for_issue_idsObject



96
97
98
# File 'app/models/repository.rb', line 96

def scan_changesets_for_issue_ids
  self.changesets.each(&:scan_comment_for_issue_ids)
end

#scmObject



40
41
42
43
44
# File 'app/models/repository.rb', line 40

def scm
  @scm ||= self.scm_adapter.new url, root_url, , password
  update_attribute(:root_url, @scm.root_url) if root_url.blank?
  @scm
end

#scm_nameObject



46
47
48
# File 'app/models/repository.rb', line 46

def scm_name
  self.class.scm_name
end

#supports_annotate?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/repository.rb', line 54

def supports_annotate?
  scm.supports_annotate?
end

#supports_cat?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/repository.rb', line 50

def supports_cat?
  scm.supports_cat?
end

#url=(arg) ⇒ Object

Removes leading and trailing whitespace



31
32
33
# File 'app/models/repository.rb', line 31

def url=(arg)
  write_attribute(:url, arg ? arg.to_s.strip : nil)
end