git-notifier 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/git-notifier +150 -0
- metadata +55 -0
data/bin/git-notifier
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# TODO
|
3
|
+
# 1) Error handling with notifications
|
4
|
+
# 4) check dependencies at startup
|
5
|
+
# 5) complete help() above
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'daemons'
|
9
|
+
require 'ruby-growl'
|
10
|
+
require 'digest/sha1'
|
11
|
+
|
12
|
+
def usage
|
13
|
+
usage = <<END
|
14
|
+
|
15
|
+
Usage:
|
16
|
+
------
|
17
|
+
|
18
|
+
git-notifier start|stop|add|clear|status
|
19
|
+
|
20
|
+
|
21
|
+
Examples:
|
22
|
+
---------
|
23
|
+
|
24
|
+
Start the notifier:
|
25
|
+
git-notifier start
|
26
|
+
|
27
|
+
Stop the notifier:
|
28
|
+
git-notifier stop
|
29
|
+
|
30
|
+
Add a repository to the watch list:
|
31
|
+
git-notifier add <repo_uri> <branch>
|
32
|
+
Example: git-notifier add git@github.com:marcocampana/git-notifier.git master
|
33
|
+
|
34
|
+
Remove all watched repositories from the watch list
|
35
|
+
git-notifier clear
|
36
|
+
|
37
|
+
Show all the watched repositories
|
38
|
+
git-notifier status
|
39
|
+
END
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_repo
|
43
|
+
(puts usage and return) if ARGV.size < 2
|
44
|
+
repo_uri = ARGV[1]
|
45
|
+
branch = ARGV[2] || 'master'
|
46
|
+
repo_name = repo_uri.split("/").last
|
47
|
+
repo_local_path = "/var/tmp/git-notifier_#{Digest::SHA1.hexdigest(repo_uri)}_#{repo_name}_#{branch}"
|
48
|
+
# TODO Truncate the SHA1 in the filename
|
49
|
+
|
50
|
+
if !File.exists?( repo_local_path )
|
51
|
+
puts "git-notifier: adding #{repo_uri} to watch list... (this might take a while)"
|
52
|
+
output = `mkdir #{repo_local_path}; cd #{repo_local_path}; git clone #{repo_uri} . 2>/dev/null`
|
53
|
+
# TODO Add error handling in case the repo does not exist
|
54
|
+
end
|
55
|
+
puts "git-notifier: repo '#{repo_uri}' added to watch list"
|
56
|
+
end
|
57
|
+
|
58
|
+
def start
|
59
|
+
demonize
|
60
|
+
end
|
61
|
+
|
62
|
+
def stop
|
63
|
+
demonize
|
64
|
+
puts "git-notifier is now stopped"
|
65
|
+
end
|
66
|
+
|
67
|
+
def status
|
68
|
+
watched_repos = []
|
69
|
+
available_repos.each do |repo_dirname|
|
70
|
+
prefix, repo_sha1, repo_name, repo_branch = repo_dirname.split('_')
|
71
|
+
watched_repos << [repo_name, repo_branch]
|
72
|
+
end
|
73
|
+
if watched_repos.any?
|
74
|
+
# TODO Show if the notifier is running or not
|
75
|
+
puts "git-notifier: the following repositories are in the watch list"
|
76
|
+
watched_repos.each do |repo|
|
77
|
+
puts " - #{repo[0]} (#{repo[1]})"
|
78
|
+
end
|
79
|
+
else
|
80
|
+
puts "git-notifier: no repositories are being watched at the moment\nUse: 'git-notifier add <repo_uri>' to add repos to watch"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def available_repos
|
85
|
+
repos = []
|
86
|
+
Dir.new('/var/tmp').each do |repo_dir|
|
87
|
+
repos << repo_dir if repo_dir =~ /^git-notifier_/
|
88
|
+
end
|
89
|
+
repos
|
90
|
+
end
|
91
|
+
|
92
|
+
def clear_all
|
93
|
+
repos = available_repos
|
94
|
+
repos.each do |repo|
|
95
|
+
puts "/var/tmp/#{repo} deleted"
|
96
|
+
`cd /var/tmp; rm -Rf #{repo}` if repo =~ /^git-notifier/
|
97
|
+
end
|
98
|
+
puts "git-notifier: no repositories are being watched at the moment\nUse: 'git-notifier add <repo_uri>' to add repos to watch" if repos.empty?
|
99
|
+
end
|
100
|
+
|
101
|
+
def demonize
|
102
|
+
Daemons.run_proc('git-notifier', :dir_mode => :normal, :dir => '/var/tmp/', :monitor => false) do
|
103
|
+
g = Growl.new "localhost", "ruby-growl", ["git-notifier"]
|
104
|
+
# g.notify "git-notifier", "GIT Notifier", "Start watching\nrepo: #{ARGV[0]}\nbranch: #{@@branch}"
|
105
|
+
g.notify "git-notifier", "GIT Notifier", "Start notifier"
|
106
|
+
|
107
|
+
loop do
|
108
|
+
# TODO store available_repoes in a variable if adding while running doesn't work
|
109
|
+
available_repos.each do |repo_dirname|
|
110
|
+
prefix, repo_sha1, repo_name, repo_branch = repo_dirname.split('_')
|
111
|
+
|
112
|
+
git_pull_output = `cd /var/tmp/#{repo_dirname}; git checkout -b #{repo_branch} >/dev/null 2>&1; git pull origin #{repo_branch} 2>/dev/null`
|
113
|
+
|
114
|
+
if git_pull_output =~ /Updating (.+)\.\.(.+)/
|
115
|
+
from_commit = $1
|
116
|
+
to_commit = $2
|
117
|
+
|
118
|
+
git_log_output = `cd /var/tmp/#{repo_dirname}; git log -1 $1`
|
119
|
+
|
120
|
+
notification_lines = git_log_output.split( "\n" )
|
121
|
+
sha1 = notification_lines[0]
|
122
|
+
author = notification_lines[1]
|
123
|
+
date = notification_lines[2]
|
124
|
+
body = notification_lines[3 .. notification_lines.size - 1].to_s.gsub( /\t/, '')
|
125
|
+
# TODO truncate commit sha1 and author name stripping off email
|
126
|
+
|
127
|
+
g.notify "git-notifier", sha1 + date, author + "\n" + body
|
128
|
+
end
|
129
|
+
# TODO increase sleep time
|
130
|
+
sleep( 60 )
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
case ARGV[0]
|
137
|
+
when 'start'
|
138
|
+
start
|
139
|
+
when 'add'
|
140
|
+
add_repo
|
141
|
+
when 'stop'
|
142
|
+
stop
|
143
|
+
when 'clear'
|
144
|
+
clear_all
|
145
|
+
when 'status'
|
146
|
+
status
|
147
|
+
else
|
148
|
+
puts usage
|
149
|
+
end
|
150
|
+
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: git-notifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marco Campana
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-13 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: " git-notifier is a gem that allows you to watch one or more git repositories and receive a growl notification when a change is committed"
|
17
|
+
email: m.campana@gmail.com
|
18
|
+
executables:
|
19
|
+
- git-notifier
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files: []
|
25
|
+
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://github.com/marcocampana/git-notifier
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project: git-notifier
|
50
|
+
rubygems_version: 1.3.3
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: git-notifier is a gem that allows you to watch one or more git repositories and receive a growl notification when a change is committed
|
54
|
+
test_files: []
|
55
|
+
|