tsantos-gitadm 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +7 -0
  2. data/gitadm +204 -0
  3. metadata +55 -0
data/README ADDED
@@ -0,0 +1,7 @@
1
+ gitadm is a script for assisting in the management of git repositories. It lets you add public keys to git accounts and it makes creating new repositories easier.
2
+
3
+ Here's an example of how to create a new repo on the server and populate it with the contents of the current directory:
4
+
5
+ ./gitadm create-repo -s gitserver -r myrepos/stuff.git --use-dir
6
+
7
+ This also automatically sets up tracking of the master branch for you so that you can use "git pull" and "git push" without arguments.
data/gitadm ADDED
@@ -0,0 +1,204 @@
1
+ #!/usr/bin/env ruby
2
+ # Author: Tom Santos
3
+ # http://github.com/tsantos/gitadm/tree/master
4
+
5
+ require 'optparse'
6
+ require 'ostruct'
7
+ require 'tempfile'
8
+
9
+ if ARGV.length == 0
10
+ s = <<EOF
11
+
12
+ gitadm is a tool for managing a git repositories. It does nothing for access control except to help
13
+ with adding ssh keys.
14
+
15
+ Usage:
16
+ add-key Adds a public key to the account on the specified server
17
+ checkout Checks out a branch with tracking
18
+ create-remote-branch Creates a new remote branch
19
+ create-repo Creates a new git repository
20
+
21
+ EOF
22
+ puts s
23
+ exit(1)
24
+ end
25
+
26
+ command = ARGV[0].gsub('-', '_').to_sym
27
+ ARGS = ARGV[1..-1] if ARGV.length > 1
28
+ options = OpenStruct.new
29
+ options.user = 'git'
30
+ options.server = 'git'
31
+
32
+ def user_opt opts, options
33
+ opts.on('-u', '--user [USER]', "The git user on the machine. Defaults to 'git'.") do |user|
34
+ options.user = user
35
+ end
36
+ end
37
+
38
+ def server_opt opts, options
39
+ opts.on('-s', '--server SERVER', "The host of the git repository. Defaults to 'git'.") do |server|
40
+ options.server = server
41
+ end
42
+ end
43
+
44
+ def help_opt opts
45
+ opts.on("-h", "--help", "Show this message") do
46
+ puts opts
47
+ exit
48
+ end
49
+ end
50
+
51
+ def create_branch branch_name, checkout = false
52
+ `git push origin master:refs/heads/#{branch_name}`
53
+ if checkout
54
+ `git checkout --track -b #{branch_name} origin/#{branch_name}`
55
+ end
56
+ end
57
+
58
+ =begin
59
+ add-key
60
+ We add a key to the authorized_keys file for the git account on the server. This will only
61
+ work if you already have your key pushed to the server. It's intended as a way to add new keys
62
+ for existing users.
63
+ =end
64
+ if :add_key == command
65
+ opts = OptionParser.new do |opts|
66
+ opts.banner = "Usage:"
67
+ user_opt(opts, options)
68
+ server_opt(opts, options)
69
+ opts.on('-f', '--pub-key-file [FILE]', 'The file containing the public key(s)') do |path|
70
+ options.pub_key_path = path
71
+ end
72
+ opts.on('-i', '--stdin', 'get the key(s) from stdin') do
73
+ options.stdin = true
74
+ end
75
+ help_opt(opts)
76
+ end
77
+ if !defined? ARGS then puts opts; exit(1); end
78
+ opts.parse! ARGS
79
+
80
+ # Setup the key file to send
81
+ file = nil
82
+
83
+ if options.stdin
84
+ file = Tempfile.new("gitadm")
85
+ File.open(file.path, 'w') do |f|
86
+ f.puts($stdin.readline)
87
+ end
88
+ elsif options.pub_key_path
89
+ file = File.new(options.pub_key_path)
90
+ end
91
+
92
+ ssh = "ssh #{options.user}@#{options.server}"
93
+
94
+ # Copy the key over as a temp file
95
+ file_name = File.basename(file.path)
96
+ `scp #{file.path} #{options.user}@#{options.server}:/tmp/#{file_name}`
97
+
98
+ # Figure out which authorized_keys file they're using
99
+ auth_file = ".ssh/authorized_keys"
100
+ auth_file = `#{ssh} '[ -f #{auth_file} ] && echo "#{auth_file}" || echo "#{auth_file}2"'`.strip
101
+
102
+ # Get the key into the auth file and cleanup temp
103
+ `#{ssh} 'cat /tmp/#{file_name} >> #{auth_file}'`
104
+ `#{ssh} 'rm /tmp/#{file_name}'`
105
+
106
+ =begin
107
+ checkout
108
+ Checks out a branch with tracking enabled. Basically just shorthand
109
+ =end
110
+ elsif :checkout == command
111
+ opts = OptionParser.new do |opts|
112
+ opts.banner = "Usage:"
113
+ opts.on('-b', '--branch-name NAME', 'The branch name to check out') do |name|
114
+ options.branch_name = name
115
+ end
116
+ help_opt(opts)
117
+ end
118
+ if !defined? ARGS then puts opts; exit(1); end
119
+ opts.parse! ARGS
120
+
121
+ `git checkout --track -b #{options.branch_name} origin/#{options.branch_name}`
122
+
123
+ =begin
124
+ create-remote-branch
125
+ Creates a remote branch off the master and optionally lets you check it out with
126
+ tracking.
127
+ =end
128
+ elsif :create_remote_branch == command
129
+ opts = OptionParser.new do |opts|
130
+ opts.banner = "Usage:"
131
+ opts.on('-b', '--branch-name NAME', 'The branch name to create') do |name|
132
+ options.branch_name = name
133
+ end
134
+ opts.on('-c', '--checkout', 'Also checkout the branch and track it') do
135
+ options.checkout = true
136
+ end
137
+ help_opt(opts)
138
+ end
139
+ if !defined? ARGS then puts opts; exit(1); end
140
+ opts.parse! ARGS
141
+
142
+ create_branch(options.branch_name, options.checkout)
143
+
144
+ =begin
145
+ create-repo
146
+ Creates a new bare git repository at the given path. It also allows you to send the current
147
+ directory to the new repo and associates the repository and the local dir. The --no-remote-create
148
+ option is for sending the contents of a dir to to a place where the git repository has already been
149
+ initialized like github.com.
150
+ =end
151
+ elsif :create_repo == command
152
+ options.checkout = false
153
+
154
+ opts = OptionParser.new do |opts|
155
+ opts.banner = "Usage:"
156
+ user_opt(opts, options)
157
+ server_opt(opts, options)
158
+ opts.on('-r', '--path-to-repo REPO', 'The path to the repository on the server') do |path|
159
+ options.repo_path = path
160
+ end
161
+ opts.on('-d', '--use-dir', "Associates the current dir with the new repo and sends the files") do
162
+ options.use_dir = true
163
+ end
164
+ opts.on('-i', '--init', "Instead of --use-dir. Init initializes the current dir but doesn't add all. You need to do git adds and a git push origin master before things will work.") do
165
+ options.init = true
166
+ end
167
+ opts.on( "--no-remote-create", "Don't create the dirs on the remote machine" ) do
168
+ options.no_remote_create = true
169
+ end
170
+ opts.on('-b', '--create-branches a,b,c', Array, "A comma separated list of branches") do |branches|
171
+ options.branches = branches
172
+ end
173
+ opts.on('-c', '--checkout-branches', "Also does a checkout of the branches if using --create-branches") do
174
+ options.checkout = true
175
+ end
176
+ help_opt(opts)
177
+ end
178
+ if !defined? ARGS then puts opts; exit(1); end
179
+ opts.parse! ARGS
180
+
181
+ # Make the new dir and init it
182
+ unless options.no_remote_create
183
+ ssh = "ssh #{options.user}@#{options.server}"
184
+ `#{ssh} 'mkdir -p #{options.repo_path}'`
185
+ `#{ssh} 'cd #{options.repo_path} ; git --bare init'`
186
+ end
187
+
188
+ # Upload dir contents and wire-up git pull and push
189
+ if options.use_dir || options.init
190
+ `git init`
191
+ `git remote add origin #{options.user}@#{options.server}:#{options.repo_path}`
192
+ if options.use_dir
193
+ `git add * ; git commit -m "Initial commit"`
194
+ `git push origin master`
195
+ end
196
+ `git config branch.master.remote origin ; git config branch.master.merge refs/heads/master`
197
+ `git config push.default matching` # stop the new git from complaining
198
+ end
199
+
200
+ if options.branches
201
+ options.branches.each { |branch| create_branch(branch, options.checkout) }
202
+ `git checkout master` if options.checkout
203
+ end
204
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsantos-gitadm
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tom Santos
8
+ autorequire:
9
+ bindir: .
10
+ cert_chain: []
11
+
12
+ date: 2009-03-31 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: gitadm helps with adding ssh keys to the git user and creating repositories
17
+ email: santos.tom@gmail.com
18
+ executables:
19
+ - gitadm
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - gitadm
27
+ has_rdoc: false
28
+ homepage: http://github.com/tsantos/gitadm
29
+ licenses:
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:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: gitadm helps with creating repos, checking out branches, creating remote branches, etc.
54
+ test_files: []
55
+