-
Notifications
You must be signed in to change notification settings - Fork 1
/
push-git-project
executable file
·85 lines (75 loc) · 3.21 KB
/
push-git-project
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
#!/usr/bin/env ruby
%w[rubygems optparse ostruct inifile].each {|l| require l }
REPO = {
:primary => { :host => "repoprimaryhost",
:gitosis => "repoprimarygitosis",
:gitosisgroup => "repoprimarygitosisgroup"},
:secondary => { :host => "reposecondaryhost",
:gitosis => "reposecondarygitosis",
:gitosisgroup => "reposecondarygitosisgroup"}
}
options = OpenStruct.new
def options.gitosis_conf=(file)
if File.file?(file)
super file
elsif File.file?(File.join(file, "gitosis.conf"))
super File.join(file, "gitosis.conf")
else
abort "I'm looking for a gitosis.conf in there (#{file})... and I can't find it."
end
end
# Defaults
options.hostname = REPO[:primary][:host]
options.gitosis_conf = REPO[:primary][:gitosis]
options.gitosis_group = REPO[:primary][:gitosisgroup]
# Parsing command line options
opts = OptionParser.new do |o|
o.on('-o', '--host HOSTNAME',
"non-default (#{REPO[:primary][:host]}) host to push project to") do |hostname|
options.hostname = hostname
end
o.on('-g', '--gitosis-conf CONF-FILE',
"non-default (#{REPO[:primary][:gitosis]})",
"gitosis project to push project to") do |conf_file|
options.gitosis_conf = conf_file
end
o.on('-p',
"projectfiles mode (project will be pushed to",
"~git/projectfiles/project instead of ~git/project)") do |p|
options.projectfiles = p ? "projectfiles/" : ""
end
o.on('-s', '--secondary',
"use secondary defaults",
"(-h #{REPO[:secondary][:host]} -g #{REPO[:secondary][:gitosis]})") do
options.hostname = REPO[:secondary][:host]
options.gitosis_conf = REPO[:secondary][:gitosis]
options.gitosis_group = REPO[:secondary][:gitosisgroup]
end
o.on('-h', "this help text") { puts o; exit }
o.parse!
end
if ARGV.size == 1
options.project = "#{options.projectfiles}#{ARGV.pop}"
elsif ARGV.empty?
options.project = "#{options.projectfiles}#{`basename \`pwd\``}".chomp
else
abort "Unexpected arguments.\n#{opts}"
end
# Pull gitosis project
`cd #{File.dirname(options.gitosis_conf)}; git pull origin master`
inifile = IniFile.new(options.gitosis_conf)
# Sanity checks
abort "I can't find section '#{options.gitosis_group}' and line 'writable = …' in #{options.gitosis_conf}" unless ( inifile[options.gitosis_group] && inifile[options.gitosis_group]['writable'] )
writable = inifile[options.gitosis_group]['writable'].dup.split(/ /)
abort "gitosis already knows #{options.project} is writable by #{options.gitosis_group}" if writable.include?(options.project)
abort "#{options.project} already has a remote 'origin'" if `git remote`.split.include?("origin")
# Processing
writable << options.project
gitosis_admin = writable.delete("gitosis-admin")
inifile[options.gitosis_group]['writable'] = "#{gitosis_admin ? "gitosis-admin " : ""}#{writable.sort.join(" ")}"
inifile.save
puts `cd #{File.dirname(options.gitosis_conf)}; git add gitosis.conf; git commit -m "add project #{options.project} as writable by #{options.gitosis_group}"; git push origin master`
puts `git remote add origin git@#{options.hostname}:#{options.project}; git push origin master`
puts
puts "Project pushed to:"
puts `git remote show origin`