2012-07-17 09:02:56 +01:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
module Modules
|
|
|
|
module Models
|
|
|
|
module Git
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
validates_attachment_size :srpm, :less_than => 500.megabytes
|
|
|
|
validates_attachment_content_type :srpm, :content_type => ['application/octet-stream', "application/x-rpm", "application/x-redhat-package-manager"], :message => I18n.t('layout.invalid_content_type')
|
|
|
|
|
|
|
|
has_attached_file :srpm
|
|
|
|
# attr_accessible :srpm
|
|
|
|
|
|
|
|
after_create :create_git_repo
|
|
|
|
after_commit(:on => :create) {|p| p.fork_git_repo unless p.is_root?} # later with resque
|
|
|
|
after_commit(:on => :create) {|p| p.import_attached_srpm if p.srpm?} # later with resque # should be after create_git_repo
|
|
|
|
after_destroy :destroy_git_repo
|
|
|
|
# after_rollback lambda { destroy_git_repo rescue true if new_record? }
|
|
|
|
|
|
|
|
later :import_attached_srpm, :queue => :fork_import
|
|
|
|
later :fork_git_repo, :queue => :fork_import
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo
|
|
|
|
@repo ||= Grit::Repo.new(path) rescue Grit::Repo.new(GAP_REPO_PATH)
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
2013-03-07 08:06:34 +00:00
|
|
|
build_path(name_with_owner)
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def versions
|
2013-01-21 17:08:19 +00:00
|
|
|
repo.tags.map(&:name) + repo.branches.map(&:name)
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_file(path, data, options = {})
|
|
|
|
head = options[:head].to_s || default_branch
|
|
|
|
actor = get_actor(options[:actor])
|
|
|
|
filename = File.split(path).last
|
|
|
|
message = options[:message]
|
|
|
|
message = "Updated file #{filename}" if message.nil? or message.empty?
|
|
|
|
|
|
|
|
# can not write to unexisted branch
|
|
|
|
return false if repo.branches.select{|b| b.name == head}.size != 1
|
|
|
|
|
|
|
|
parent = repo.commits(head).first
|
|
|
|
|
|
|
|
index = repo.index
|
|
|
|
index.read_tree(parent.tree.id)
|
|
|
|
|
|
|
|
# can not create new file
|
|
|
|
return false if (index.current_tree / path).nil?
|
|
|
|
|
2013-03-07 14:54:44 +00:00
|
|
|
system "sudo chown -R rosa:rosa #{repo.path}" #FIXME Permission denied - /mnt/gitstore/git_projects/...
|
2012-07-17 09:02:56 +01:00
|
|
|
index.add(path, data)
|
2013-02-25 18:55:45 +00:00
|
|
|
if sha1 = index.commit(message, :parents => [parent], :actor => actor, :last_tree => parent.tree.id, :head => head)
|
2013-03-07 08:06:34 +00:00
|
|
|
Resque.enqueue(GitHook, owner.uname, name, sha1, sha1, "refs/heads/#{head}", 'commit', "user-#{options[:actor].id}", message)
|
2013-02-25 18:55:45 +00:00
|
|
|
end
|
|
|
|
sha1
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def paginate_commits(treeish, options = {})
|
2012-07-26 22:15:57 +01:00
|
|
|
options[:page] = options[:page].try(:to_i) || 1
|
|
|
|
options[:per_page] = options[:per_page].try(:to_i) || 20
|
2012-07-17 09:02:56 +01:00
|
|
|
|
|
|
|
skip = options[:per_page] * (options[:page] - 1)
|
|
|
|
last_page = (skip + options[:per_page]) >= repo.commit_count(treeish)
|
|
|
|
|
|
|
|
[repo.commits(treeish, options[:per_page], skip), options[:page], last_page]
|
|
|
|
end
|
|
|
|
|
|
|
|
def tree_info(tree, treeish = nil, path = nil)
|
2013-04-10 12:40:11 +01:00
|
|
|
grouped = tree.contents.sort_by{|c| c.name.downcase}.group_by(&:class)
|
|
|
|
[
|
|
|
|
grouped[Grit::Tree],
|
|
|
|
grouped[Grit::Blob],
|
|
|
|
grouped[Grit::Submodule]
|
|
|
|
].compact.flatten.map do |node|
|
|
|
|
node_path = File.join([path.present? ? path : nil, node.name].compact)
|
|
|
|
[
|
|
|
|
node,
|
|
|
|
node_path,
|
|
|
|
repo.log(treeish, node_path, :max_count => 1).first
|
|
|
|
]
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_srpm(srpm_path = srpm.path, branch_name = 'import')
|
2012-11-09 15:07:01 +00:00
|
|
|
token = User.find_by_uname('rosa_system').authentication_token
|
2012-12-20 11:53:25 +00:00
|
|
|
opts = [srpm_path, path, branch_name, Rails.root.join('bin', 'file-store.rb'), token, APP_CONFIG['file_store_url']].join(' ')
|
|
|
|
system("#{Rails.root.join('bin', 'import_srpm.sh')} #{opts} >> /dev/null 2>&1")
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
|
2013-02-25 14:35:39 +00:00
|
|
|
def is_empty?
|
2013-02-22 07:22:34 +00:00
|
|
|
repo.branches.count == 0
|
|
|
|
end
|
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
def build_path(dir)
|
2012-11-08 09:13:48 +00:00
|
|
|
File.join(APP_CONFIG['git_path'], 'git_projects', "#{dir}.git")
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_attached_srpm
|
|
|
|
if srpm?
|
|
|
|
import_srpm # srpm.path
|
|
|
|
self.srpm = nil; save # clear srpm
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_git_repo
|
|
|
|
if is_root?
|
|
|
|
Grit::Repo.init_bare(path)
|
|
|
|
write_hook
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fork_git_repo
|
2012-10-18 15:34:32 +01:00
|
|
|
dummy = Grit::Repo.new(path) rescue parent.repo.fork_bare(path, :shared => false)
|
2012-07-17 09:02:56 +01:00
|
|
|
write_hook
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_git_repo
|
|
|
|
FileUtils.rm_rf path
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_hook
|
2013-03-07 08:06:34 +00:00
|
|
|
hook = "/home/#{APP_CONFIG['shell_user']}/gitlab-shell/hooks/post-receive"
|
2012-07-17 09:02:56 +01:00
|
|
|
hook_file = File.join(path, 'hooks', 'post-receive')
|
2013-03-07 08:06:34 +00:00
|
|
|
FileUtils.ln_sf hook, hook_file
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_actor(actor = nil)
|
|
|
|
@last_actor = case actor.class.to_s
|
|
|
|
when 'Grit::Actor' then options[:actor]
|
|
|
|
when 'Hash' then Grit::Actor.new(actor[:name], actor[:email])
|
|
|
|
when 'String' then Grit::Actor.from_stirng(actor)
|
|
|
|
else begin
|
|
|
|
if actor.respond_to?(:name) and actor.respond_to?(:email)
|
|
|
|
Grit::Actor.new(actor.name, actor.email)
|
|
|
|
else
|
|
|
|
config = Grit::Config.new(repo)
|
|
|
|
Grit::Actor.new(config['user.name'], config['user.email'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@last_actor
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2013-03-07 08:06:34 +00:00
|
|
|
def process_hook(owner_uname, repo, newrev, oldrev, ref, newrev_type, user = nil, message = nil)
|
|
|
|
rec = GitHook.new(owner_uname, repo, newrev, oldrev, ref, newrev_type, user, message)
|
2012-07-17 09:02:56 +01:00
|
|
|
ActivityFeedObserver.instance.after_create rec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|