Commit dffa923615698bd6c802d12ece469d510f2b227b

Merge commit 'krawek/keep-all-commits'

* commit 'krawek/keep-all-commits':
Only save commit events for repository committers
Use only the master"s commits to create events
use the commit date
keep all commit events on git-push

Commit diff

app/models/project.rb

 
134134 super({:procs => [info]}.merge(opts))
135135 end
136136
137 def create_event(action_id, target, user, data = nil, body = nil)
138 events.create(:action => action_id, :target => target, :user => user, :body => body, :data => data)
137 def create_event(action_id, target, user, data = nil, body = nil, date = Time.now.utc)
138 events.create(:action => action_id, :target => target, :user => user,
139 :body => body, :data => data, :created_at => date)
139140 end
140141
141142 protected
toggle raw diff

data/hooks/post-receive

 
77require 'date'
88
99gitdir = File.expand_path(File.join(incpath, ".."))
10repository_name = File.basename(gitdir)
11slug = ""
10repository = Repository.find_by_path(gitdir)
1211
13if repository_name =~ /\.git$/
14 repository_name.sub!(/\.git$/, "")
15 slug = File.basename(File.expand_path(File.join(gitdir, "..")))
12if repository.nil?
13 $stderr.puts "Unknown repository"
14 exit 0
1615end
1716
17project = repository.project
1818git = Grit::Git.new(gitdir)
1919
2020while line = gets
21 #puts "@@> #{line.inspect}"
21# puts "@@> #{line.inspect}"
2222 # 3b480c5c30962c9f5b82a9c61a75992dc605de21 ee27ee3a022802c0569f9cdef6e9ad29aea096a8 refs/heads/master\n
2323 oldrev, newrev, revname = line.split(" ")
2424 current_rev = newrev
5151 # type => heads, tags, remotes
5252 # name => branch name
5353 path, type, name = revname.split("/")
54 revs = [ current_rev ]
55 emails = [git.show({:pretty => "format:%ce", :s => true}, current_rev)]
5456
55 info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %cd%nmessage: %s", :s => true}, current_rev )
56
57 hash = {}
58 info.each { |line|
59 if line =~ /(\w+):\s(.*)$/
60 key = $1.to_sym
61 value = $2
62
63 value = Date.parse(value) if key == :date
64
65 hash[key] = value
57 if type == "heads"
58 if action == :update
59 revs = git.rev_list({}, "#{oldrev}..#{newrev}").split("\n")
60 emails = git.log({:pretty => "format:%ce"}, "#{oldrev}..#{newrev}").split("\n")
61 elsif action == :create && name == "master"
62 revs = git.rev_list({}, current_rev).split("\n")
63 emails = git.log({:pretty => "format:%ce"}, current_rev).split("\n")
6664 end
67 }
68
69 # action => create, delete, update
70 # rev_type => commit, tag
71
72 user = User.find_by_email(hash[:email])
73 if user.nil?
74 # TODO: no user should be ok, no need to skip
75 # $stdout.puts "** The email '#{hash[:email]}' is not registered."
76 next
7765 end
7866
79 project = Project.find_by_slug(slug)
80 repository = nil
67 users = repository.committers.find(:all, :conditions => ["email in (?)", emails])
68 user_map = users.inject({}) { |hash, user| hash[user.email] = user; hash }
8169
82 project.repositories.each { |repo|
83 if repo.name == repository_name
84 repository = repo
85 break
70 revs.each do |sha1|
71 info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %ct%nmessage: %s", :s => true}, sha1)
72
73 hash = {}
74 info.each { |line|
75 if line =~ /(\w+):\s(.*)$/
76 key = $1.to_sym
77 value = $2
78
79 value = Time.at(value.to_i).utc if key == :date
80
81 hash[key] = value
82 end
83 }
84
85 # action => create, delete, update
86 # rev_type => commit, tag
87
88 user = user_map[hash[:email]]
89 if user.nil?
90 # TODO: no user should be ok, no need to skip
91 # $stdout.puts "** The email '#{hash[:email]}' is not registered."
92 next
8693 end
87 }
88
89 action_id = nil
90 ref = nil
91 if current_rev_type == "commit"
92 if type == "heads"
93 case action
94 when :create
95 action_id = Action::CREATE_BRANCH
94
95 action_id = nil
96 ref = nil
97 if current_rev_type == "commit"
98 if type == "heads"
99 case action
100 when :create
101 action_id = Action::CREATE_BRANCH
102 ref = name
103 when :update
104 action_id = Action::COMMIT
105 ref = sha1
106 when :delete
107 action_id = Action::DELETE_BRANCH
108 ref = name
109 end
110 elsif type == "tags"
111 if action == :create
112 action_id = Action::CREATE_TAG
96113 ref = name
97 when :update
98 action_id = Action::COMMIT
99 ref = current_rev
100 when :delete
101 action_id = Action::DELETE_BRANCH
114 elsif action == :delete
115 action_id = Action::DELETE_TAG
102116 ref = name
117 end
103118 end
104 elsif type == "tags"
105 if action == :create
106 action_id = Action::CREATE_TAG
107 ref = name
108 elsif action == :delete
109 action_id = Action::DELETE_TAG
110 ref = name
111 end
112 end
113 elsif current_rev_type == "tag"
114 if type == "tags"
115 if action == :create
116 action_id = Action::CREATE_TAG
117 ref = name
118 elsif action == :delete
119 action_id = Action::DELETE_TAG
120 ref = name
119 elsif current_rev_type == "tag"
120 if type == "tags"
121 if action == :create
122 action_id = Action::CREATE_TAG
123 ref = name
124 elsif action == :delete
125 action_id = Action::DELETE_TAG
126 ref = name
127 end
121128 end
122129 end
130
131 next unless action_id
132
133 project.create_event(action_id, repository, user, ref, hash[:message], hash[:date])
134 action = :update
123135 end
124
125 next unless action_id
126
127 # puts "#{hash[:author]}: #{Action.name(action_id)} #{ref} on #{slug} [#{hash[:date]}]"
128 # puts " #{hash[:message]}"
129
130 project.create_event(action_id, repository, user, ref, hash[:message])
131136end
132137
133138puts "[OK]"
toggle raw diff