public
Description: Gitorious aims to provide a great way of doing distributed opensource code collaboration.
Homepage: http://gitorious.org/projects/gitorious
Clone URL: git://github.com/dysinger/gitorious.git
js (author)
Mon Apr 28 12:11:49 -0700 2008
commit  35ad4575858b22e34b6a4426e6d933d7b0a0c9a9
tree    9114d29bfed6eaa65da43d7e46713cdce5cd52f0
parent  d3372aaf74ae26643e03e11f2cccdb9d65849457
gitorious / data / hooks / post-receive
100755 135 lines (109 sloc) 3.027 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env ruby
 
incpath = File.dirname(__FILE__)
$: << incpath
 
require 'rails_env'
require 'date'
 
gitdir = File.expand_path(File.join(incpath, ".."))
repository_name = File.basename(gitdir)
slug = ""
 
if repository_name =~ /\.git$/
  repository_name.sub!(/\.git$/, "")
  slug = File.basename(File.expand_path(File.join(gitdir, "..")))
end
 
git = Grit::Git.new(gitdir)
 
while line = gets
  #puts "@@> #{line.inspect}"
  # 3b480c5c30962c9f5b82a9c61a75992dc605de21 ee27ee3a022802c0569f9cdef6e9ad29aea096a8 refs/heads/master\n
  oldrev, newrev, revname = line.split(" ")
  current_rev = newrev
  newtype = oldtype = current_rev_type = "commit"
 
  action = :create
  if oldrev =~ /^0+$/
    action = :create
  else
    if newrev =~ /^0+$/
      action = :delete
    else
      action = :update
    end
  end
  
  if action != :delete
    newtype = git.cat_file({:t => true}, newrev)
  end
  
  if action == :update
    oldtype = git.cat_file({:t => true}, oldrev)
  end
  
  if action == :delete
      current_rev = oldrev
      current_rev_type = oldtype
  end
  
  # type => heads, tags, remotes
  # name => branch name
  path, type, name = revname.split("/")
  
  info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %cd%nmessage: %s", :s => true}, current_rev )
  
  hash = {}
  info.each { |line|
    if line =~ /(\w+):\s(.*)$/
      key = $1.to_sym
      value = $2
      
      value = Date.parse(value) if key == :date
      
      hash[key] = value
    end
  }
  
  # action => create, delete, update
  # rev_type => commit, tag
  
  user = User.find_by_email(hash[:email])
  if user.nil?
    # TODO: no user should be ok, no need to skip
    $stdout.puts "** The email '#{hash[:email]}' is not registered."
    next
  end
  
  project = Project.find_by_slug(slug)
  repository = nil
  
  project.repositories.each { |repo|
    if repo.name == repository_name
      repository = repo
      break
    end
  }
  
  action_id = nil
  ref = nil
  if current_rev_type == "commit"
    if type == "heads"
      case action
        when :create
          action_id = Action::CREATE_BRANCH
          ref = name
        when :update
          action_id = Action::COMMIT
          ref = current_rev
        when :delete
          action_id = Action::DELETE_BRANCH
          ref = name
      end
    elsif type == "tags"
      if action == :create
        action_id = Action::CREATE_TAG
        ref = name
      elsif action == :delete
        action_id = Action::DELETE_TAG
        ref = name
      end
    end
  elsif current_rev_type == "tag"
    if type == "tags"
      if action == :create
        action_id = Action::CREATE_TAG
        ref = name
      elsif action == :delete
        action_id = Action::DELETE_TAG
        ref = name
      end
    end
  end
  
  next unless action_id
  
  # puts "#{hash[:author]}: #{Action.name(action_id)} #{ref} on #{slug} [#{hash[:date]}]"
  # puts " #{hash[:message]}"
  
  project.create_event(action_id, repository, user, ref, hash[:message])
end
 
puts "[OK]"