Class: Main::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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tbgit.rb', line 65

def add_remotes
   	confirm("Each student repository will be added as a remote. Continue?")
   	students = IO.readlines(@students_file)
   	students.each do |username|
		username.delete!("\n")
		
		puts "Adding Remote: " + username 
		remote_command = "git remote add " + username + " https://github.com/"+@organization+"/" + username + "-"+@reponame+".git" 
		puts remote_command
		system 	remote_command
	end
end

#all_remotes_listObject

returns a list of students



95
96
97
98
99
100
101
102
# File 'lib/tbgit.rb', line 95

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(message) ⇒ Object

confirms a given message



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

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

#create_local_tracking_remotesObject

create local branches to track remote student repositories



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tbgit.rb', line 79

def create_local_tracking_remotes
		confirm("Local branches will be created to track remote student repositories. Continue?")
	    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

#gatherObject

gather necessary information



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tbgit.rb', line 38

def gather
	puts 'Students file |../students|:'
	@students_file = $stdin.gets.chomp
	if @students_file == ""
		@students_file = "../students"
	end

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

	puts 'Student Repo Name |TechBootcampHomework|:'
	@reponame = $stdin.gets.chomp
	if @reponame == ""
		@reponame = "TechBootcampHomework"
	end
end

#git_branchObject



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

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

#git_remoteObject



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

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

#git_statusObject



220
221
222
# File 'lib/tbgit.rb', line 220

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

#git_updateObject

update remotes



59
60
61
62
# File 'lib/tbgit.rb', line 59

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

#helpObject



247
248
249
250
251
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 247

def help
	puts ""
	puts "TBGit is a command-line utility to facilitate the management of multiple GitHub student repositories."
	puts "			~ created by Charlie Proctor at 2014 YEI Tech Bootcamp"
	puts "						~ partly based off of https://github.com/education/teachers_pet"
	puts "	Commands:"
	puts "		~ setup   	sets up a tbgit environment. See decription below"
	puts "		~ push  	pushes all local student branches to their remote master branches"
	puts "		~ pull   	pulls all remote master branches to local student branches"
	puts "		~ merge   	merges a specified branch with each student branch and then commits the changes"
	puts "		~ status 	runs `git status` on each students branch and displays the results"
	puts "		~ each 		executes a specified series of commands on each local student branch"
	puts "		~ spec 		runs rspec on specified files in a students repo"
	puts ""
	puts "		~ add-remotes  	adds each student's repository as a remote"
	puts "		~ create-locals 	creates a local branch to track the students remote master branch"
	puts "			^----> these are both part of the setup process"
	puts ""
	puts "	TBGit Environment"
	puts "		~ it's a regular git repository -- with lots of fancy features!"
	puts "		~ there is a master branch for teachers to work off of (create hw files, etc..)"
	puts "				--> teachers can obviously create and work off other branches if desired"
	puts "		~ each student's repository is a remote of the git repo"
	puts "		~ there is a local branch to track each student's remote master branches"
	puts ""
	puts "	Setup"
	puts "		1. Teachers create the student repositories (https://github.com/education/teachers_pet works perfectly well for this)"
	puts "				--> make sure the repos are all private, but that you have access to them!"
	puts "				--> initialize these repos with a README or push out a starter file."
	puts "				--> create a file with a list of the students github usernames (one on each line)" 
	puts "						--- you will need this during the setup process"
	puts "		2. Teachers create a repo for themselves. This will serve as the base for the tbgit environment."
	puts "		3. Change to that repo's directory, execute `tbgit setup`, and follow the instructions."
	puts ""
	puts "	A Typical Workflow"
	puts "		1. Teachers create the assignment (on the master branch) and make a final commit when they're ready to deploy it"
	puts "		2. Teachers pull all the students' repos to make sure they're up to date."
	puts "				--> `tbgit pull`"
	puts "		3. Teachers merge their master branch with each student's local branch"
	puts "				--> `tbgit merge`"
	puts "		4. At this point, teachers should check to make sure their were no merge conflicts. If there were, go in and fix them."
	puts "				--> feel free to `git checkout <username>` a few different branches"
	puts "		4. Teachers push each students local branch to the student's remote master branch"
	puts "				--> `tbgit push`"
	puts "		5. Make sure it worked.  Do a victory lap."
	puts ""
	puts "		To view student solutions at any point, just `tbgit pull` and `git checkout <username>`"
	puts ""
end

#merge_and_commitObject

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



138
139
140
141
142
143
144
145
146
147
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
# File 'lib/tbgit.rb', line 138

def merge_and_commit

	confirm("A merge and commit will be performed on each local student branch (from the branch you specify). Continue?")
	puts "Merge from branch: "
	merge_branch = $stdin.gets.chomp

	puts "Commit Message: "
	message = $stdin.gets.chomp

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

		if branch != "origin"
			checkout_command = "git checkout " + branch 
			merge_command = "git merge --no-commit " + merge_branch.to_s
		  	stage_changes = "git add --all"		
			commit_command = "git commit -am '" + message + "'"

			puts checkout_command
			system checkout_command

			puts merge_command
			system merge_command

			puts stage_changes
			system stage_changes

			puts commit_command
			system commit_command
		end

	end

		switch_to_master

end

#on_each_exec(input) ⇒ Object

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/tbgit.rb', line 196

def on_each_exec(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
				system final_command
			end
		end

	end
	switch_to_master
end

#on_each_gatherObject

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/tbgit.rb', line 177

def on_each_gather
	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

	on_each_exec(input)

end

#spacerObject



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

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

#specObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/tbgit.rb', line 224

def spec
	puts "Please specify the relative path from your pwd to the rspec file you would like to spec, eg. 'hw1/spec/spec.rb'"
	specfile = $stdin.gets.chomp
	puts "Where would you like to save the master copy of all results?"
	puts "**Must be outside the student's repo directory, eg. '../results.txt'**"
	mastercopy = $stdin.gets.chomp
	puts "Where would you like to save each student's individual results?"
	puts "**Must be inside the student's repo directory, eg. 'hw1/spec/results.txt'**"
	studentcopy = $stdin.gets.chomp
	puts "Commit message (commiting each student's results to their repo):"
	commit_message = $stdin.gets.chomp
	confirm("'rspec " + specfile + "' will be executed on each student's local branch. \
		Individual results will be saved to " + studentcopy + " and master results to " + mastercopy + ". Continue?")

	on_each_exec(["rspec " +specfile + " > " + studentcopy,   	#overwrite
		"echo '<branch>' >> " + mastercopy,
		"cat " + studentcopy + " >> " + mastercopy, 			#append
	 	"git add --all",
	 	"git commit -am '" + commit_message + "'",
	 	"git push <branch> <branch>:master"])

end

#switch_to_masterObject

three simple git methods



24
25
26
27
# File 'lib/tbgit.rb', line 24

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

#update_repos(pushpull) ⇒ Object

used for push / pull



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

def update_repos(pushpull)
	if pushpull == "push"
		confirm("Each local student branch will be pushed to their remote master branch. Continue?")
	else
		confirm("Each remote student master branch will be pulled to the local branch. Continue?")
		end

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

		if branch != "origin"
			checkout_command = "git checkout " + branch 
			if pushpull == "push"
				pushpull_command = "git push " + branch + " " + branch + ":master"
			else
				pushpull_command = "git pull " + branch + " master"
			end

			puts checkout_command
			system checkout_command

			puts pushpull_command
			system pushpull_command
		end

	end
	
		switch_to_master

end