Class: TBGit

Inherits:
Object
  • Object
show all
Defined in:
lib/tbgit.rb

Instance Method Summary collapse

Instance Method Details

#add_remotesObject

add each student repository as a remote



111
112
113
114
115
116
117
118
119
120
# File 'lib/tbgit.rb', line 111

def add_remotes
students = IO.readlines(@students_file)
students.each do |username|
	username.delete!("\n")
	puts "Adding Remote: " + username 
	remote_command = "git remote add " + username + " ssh://[email protected]/"+@organization+"/" + username + "-"+@reponame+".git" 
	puts remote_command
	system 	remote_command
end
end

#add_webhooks(args) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/tbgit.rb', line 331

def add_webhooks(args)

	options = {}
	opt_parser = OptionParser.new do |opts|
	  	opts.banner = "Usage: tbgit add-webhooks [options]"

	  opts.on("-y", "--yes","Proceed without asking for confirmation") do |y|
	    options[:yes] = y
	  end

	  opts.on("-o", "--organization NAME","Specify the name of the GitHub organization.") do |o|
	    options[:organization] = o
	  end

	  opts.on("-r", "--repo NAME","Specify the name of the student repositories.") do |r|
	    options[:reponame] = r
	  end

	  opts.on("-u", "--user USERNAME", "Your GitHub username.") do |u|
	  	options[:username] = u
	  end

	  opts.on("-p", "--pass PASSWORD", "Your GitHub password.") do |p|
	  	options[:password] = p
	  end

	  opts.on("-l", "--url URL", "Webhook URL") do |l|
	  	options[:url] = l
	  end

	  opts.on_tail("-h", "--help", "Show this message") do
	    puts opts
	    exit
	  end
	end
	opt_parser.parse!(args)

	if options[:organization] == nil
		puts "Organization Name:"
		organization = $stdin.gets.chomp
	else
		organization = options[:organization]
	end

	if options[:reponame] == nil
		puts "Student Repository Name:"
		reponame = $stdin.gets.chomp
	else
		reponame = options[:reponame]
	end

	if options[:username] == nil
		print "Username:"
		user = $stdin.gets.chomp
	else 
		user = options[:username]
	end

	if options[:password] == nil
		pass = ask("Password: ") { |q| q.echo = false }
	else 
		pass = options[:password]
	end

	if options[:url] == nil
		puts "Webhook url:"
		url = $stdin.gets.chomp
	else
		url = options[:url]
	end

	confirm(options[:yes],"Webhooks will be added to each student's GitHub repository. Continue?")

	on_each_ruby(['create = CreateWebhooks.new; create.post(\''+ organization.to_s + '\',\'<branch>\',\''+reponame.to_s+'\',
			\''+user.to_s+'\',\''+pass.to_s+'\',\''+url.to_s+'\')'])

end

#all_remotes_listObject

returns a list of students



138
139
140
141
142
143
144
145
# File 'lib/tbgit.rb', line 138

def all_remotes_list
	remote_file = Tempfile.new("remotes")
	system "git remote >> " + remote_file.path

	puts "git remote >> " + remote_file.path

	return IO.readlines(remote_file)
end

#confirm(flag, message) ⇒ Object

confirms a given message



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tbgit.rb', line 14

def confirm(flag,message)
	if !flag
		print message + " (y/n)  "
		response = $stdin.gets.chomp
		if response == 'y'
			#do nothing
		else
			exit
		end
	end
end

#create_local_tracking_remotesObject

create local branches to track remote student repositories



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tbgit.rb', line 123

def create_local_tracking_remotes
    students = IO.readlines(@students_file)
	students.each do |username|
	username.delete!("\n")

	puts "Creating Local Branch to Track Remote: " + username 
	checkout_command = "git checkout --track -b " + username + " remotes/" + username + "/master"
	puts checkout_command
	system  checkout_command

end

end

#gather(command, args) ⇒ Object

gather necessary information



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
# File 'lib/tbgit.rb', line 41

def gather(command, args)
options = {}
opt_parser = OptionParser.new do |opts|
	if command == "setup"
  		opts.banner = "Usage: tbgit setup [options]"
  	elsif command == "add-remotes"
  		opts.banner = "Usage: tbgit add-remotes [options]"
  	else
  		opts.banner = "Usage: tbgit create-locals [options]"
  	end


  opts.on("-s", "--studentfile FILEPATH","Specify the file containing the list of students.") do |f|
    options[:studentfile] = f
  end
  
  opts.on("-o", "--organization NAME","Specify the name of the GitHub organization.") do |o|
    options[:organization] = o
  end

  opts.on("-r", "--repo NAME","Specify the name of the student repositories.") do |r|
    options[:repo] = r
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end

opt_parser.parse!(args)

if options[:studentfile] == nil
	puts 'Students file |../students|:'
	@students_file = $stdin.gets.chomp
	if @students_file == ""
		@students_file = "../students"
	end
else
	@students_file = options[:studentfile]
end

if options[:organization] == nil
	puts 'Organization name |yale-stc-developer-curriculum|:'
	@organization = $stdin.gets.chomp
	if @organization == ""
		@organization = "yale-stc-developer-curriculum"
	end
else
	@organization = options[:organization]
end

if options[:repo] == nil
	puts 'Student Repo Name |TechBootcampHomework|:'
	@reponame = $stdin.gets.chomp
	if @reponame == ""
		@reponame = "TechBootcampHomework"
	end
else
	@reponame = options[:repo]
end
end

#git_branchObject



35
36
37
38
# File 'lib/tbgit.rb', line 35

def git_branch
puts "git branch"
system "git branch"
end

#git_remoteObject



31
32
33
34
# File 'lib/tbgit.rb', line 31

def git_remote
puts "git remote"
system "git remote"
end

#git_statusObject



327
328
329
# File 'lib/tbgit.rb', line 327

def git_status
	on_each_exec(["git status <branch>"])
end

#git_updateObject

update remotes



105
106
107
108
# File 'lib/tbgit.rb', line 105

def git_update
puts "git remote update"
	system "git remote update"
end

#merge_and_commit(args) ⇒ Object

merges from master (or another branch) to each student branch and commits the changes



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/tbgit.rb', line 203

def merge_and_commit(args)

options = {}
opt_parser = OptionParser.new do |opts|
  	opts.banner = "Usage: tbgit push-origin [options]"

  opts.on("-y", "--yes","Proceed without asking for confirmation") do |y|
    options[:yes] = y
  end

  opts.on("-b", "--branch BRANCH", "Specify the branch to merge from") do |b|
  	options[:branch] = b
  end

  opts.on("-m", "--message MESSAGE", "Specify the commit message for the merge.") do |m|
  	options[:message] = m 
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end
opt_parser.parse!(args)
	
	if options[:branch] == nil
		puts "Merge from branch: "
		merge_branch = $stdin.gets.chomp
	else
		merge_branch = options[:branch]
	end

	if options[:message] == nil
		puts "Commit Message: "
		message = $stdin.gets.chomp
	else
		message = options[:message]
	end

	confirm(options[:yes],"A merge and commit will be performed on each local student branch (from the branch you specify). Continue?")

commands = ["git merge --no-commit " + merge_branch.to_s,
		  		"git add --all",
				"git commit -am '" + message + "'"]
	on_each_exec(commands)

end

#on_each_exec(input) ⇒ Object

takes an array of commands, and executes a system command on each student branch



322
323
324
325
# File 'lib/tbgit.rb', line 322

def on_each_exec(input)
	input.map! { |a| "system '" + a.gsub("'"){"\\'"} + "'"}
	on_each_ruby(input)
end

#on_each_gather(args) ⇒ Object

gathers the commands to be executed, and then calls on_each_exec(input)



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/tbgit.rb', line 252

def on_each_gather(args)

	options = {}
	opt_parser = OptionParser.new do |opts|

	  opts.on("-f", "--file FILENAME", "Execute the commands specified in a certain file. One command per line. '<branch>' will be replaced by the name of the current student branch checked out.") do |f|
	  	options[:file] = f
	  end

	  opts.on("-y", "--yes","Proceed without asking for confirmation") do |y|
	    options[:yes] = y
	  end

	  opts.on_tail("-h", "--help", "Show this message") do
	    puts opts
	    exit
	  end

	end

	opt_parser.parse!(args)

	if options[:file] == nil
		puts "Enter the commands you would like to have executed on each branch, one on each line."
		puts "'<branch>' will be replaced by the current checked-out branch. Enter a blank line to finish."
		done = false
		input = Array.new
		while !done
			text = $stdin.gets.chomp
			if text == ""
				done = true
			else
				input << text
			end
		end
	else
		input = IO.readlines(options[:file])
	end

	confirm(options[:yes], "The commands specified will be executed on each student branch. Continue?")

	on_each_exec(input)

end

#on_each_ruby(input) ⇒ Object

takes an array of commands, and executes a ruby command on each student branch



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/tbgit.rb', line 298

def on_each_ruby(input)   
	all_remotes = all_remotes_list
	all_remotes.each do |branch|
		branch.delete!("\n")

		if branch != "origin"
  		checkout_command = "git checkout " + branch 

  		puts checkout_command
  		system checkout_command

			input.each do |command|
				final_command = command.gsub("<branch>", branch)

				puts final_command
				eval(final_command)
			end
		end

	end
	switch_to_master
end

#push_origin(args) ⇒ Object

push all student branches to our origin



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/tbgit.rb', line 181

def push_origin(args) #push all student branches to our origin

	options = {}
opt_parser = OptionParser.new do |opts|
  	opts.banner = "Usage: tbgit push-origin [options]"

  opts.on("-y", "--yes","Proceed without asking for confirmation") do |y|
    options[:yes] = y
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end
opt_parser.parse!(args)

	confirm(options[:yes],"Each local student branch will be pushed to the the origin remote. Continue?")
	on_each_exec(["git push origin <branch>"])
end

#spacerObject



9
10
11
# File 'lib/tbgit.rb', line 9

def spacer
	puts "********************************************************************************"
end

#switch_to_masterObject

three simple git methods



27
28
29
30
# File 'lib/tbgit.rb', line 27

def switch_to_master
puts "git checkout master"
system "git checkout master"
end

#update_repos(pushpull, args) ⇒ Object

used for push / pull



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tbgit.rb', line 148

def update_repos(pushpull,args)

	options = {}
opt_parser = OptionParser.new do |opts|
	if pushpull == "push"
  		opts.banner = "Usage: tbgit push [options]"
  	else
  		opts.banner = "Usage: tbgit pull [options]"
  	end

  opts.on("-y", "--yes","Proceed without asking for confirmation") do |y|
    options[:yes] = y
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end

end

opt_parser.parse!(args)


	if pushpull == "push"
		confirm(options[:yes],"Each local student branch will be pushed to their remote master branch. Continue?")
		on_each_exec(["git push <branch> <branch>:master"])
	else
		confirm(options[:yes],"Each remote student master branch will be pulled to the local branch. Continue?")
		on_each_exec(["git pull <branch> master"])
end
end