Class: Repository

Inherits:
Grit::Repo
  • Object
show all
Extended by:
ClassMethods
Includes:
Constants
Defined in:
app/models/repository.rb

Direct Known Subclasses

Rebuild

Defined Under Namespace

Modules: ClassMethods, Constants

Constant Summary

Constants included from Constants

Constants::Error_classification, Constants::README_start_text, Constants::Root_directory, Constants::Source, Constants::Temporary

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

create_empty, create_if_missing, create_test_repository, delete_existing, replace_or_create

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.


75
76
77
78
79
80
# File 'app/models/repository.rb', line 75

def initialize(path)
	@url=path
	@path=path
  puts '@path='+@path if $VERBOSE
	@grit_repo=Grit::Repo.new(@path)
end

Instance Attribute Details

#deserving_branchObject (readonly)

Returns the value of attribute deserving_branch


74
75
76
# File 'app/models/repository.rb', line 74

def deserving_branch
  @deserving_branch
end

#grit_repoObject (readonly)

Returns the value of attribute grit_repo


74
75
76
# File 'app/models/repository.rb', line 74

def grit_repo
  @grit_repo
end

#pathObject (readonly)

Returns the value of attribute path


74
75
76
# File 'app/models/repository.rb', line 74

def path
  @path
end

#recent_testObject (readonly)

Returns the value of attribute recent_test


74
75
76
# File 'app/models/repository.rb', line 74

def recent_test
  @recent_test
end

Instance Method Details

#commit_to_branch(target_branch, tested_files) ⇒ Object

something_to_commit


207
208
209
210
211
212
213
# File 'app/models/repository.rb', line 207

def commit_to_branch(target_branch, tested_files)
	push_branch=stage(target_branch, tested_files)
	if push_branch!=target_branch then
		git_command('checkout '+push_branch.to_s).assert_post_conditions
		git_command('checkout stash apply').assert_post_conditions
	end #if
end

#confirm_branch_switch(branch) ⇒ Object

This is safe in the sense that a stash saves all files and a stash apply restores all tracked files safe is meant to mean no files or changes are lost or buried.


132
133
134
135
136
137
138
# File 'app/models/repository.rb', line 132

def confirm_branch_switch(branch)
	checkout_branch=git_command("checkout #{branch}")
	if checkout_branch.errors!="Already on '#{branch}'\n" && checkout_branch.errors!="Switched to branch '#{branch}'\n" then
		checkout_branch.assert_post_conditions
	end #if
	checkout_branch # for command chaining
end

#corruption_fsckObject

inspect


90
91
92
# File 'app/models/repository.rb', line 90

def corruption_fsck
	git_command("fsck")
end

#corruption_gcObject

corruption


96
97
98
# File 'app/models/repository.rb', line 96

def corruption_gc
	git_command("gc")
end

#corruption_rebaseObject

corruption


93
94
95
# File 'app/models/repository.rb', line 93

def corruption_rebase
#	git_command("rebase")
end

#current_branch_name?Boolean

standardize_position!

Returns:

  • (Boolean)

105
106
107
# File 'app/models/repository.rb', line 105

def current_branch_name?
	@grit_repo.head.name.to_sym
end

#edited_superset_of_testingObject

testing_superset_of_passed


217
218
219
# File 'app/models/repository.rb', line 217

def edited_superset_of_testing
	git_command("log edited..testing")
end

#error_score?(executable = @related_files.model_test_pathname?) ⇒ Boolean

current_branch_name

Returns:

  • (Boolean)

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/repository.rb', line 108

def error_score?(executable=@related_files.model_test_pathname?)
	@recent_test=shell_command("ruby "+executable)
#	@recent_test.puts if $VERBOSE
	if @recent_test.success? then
		0
	elsif @recent_test.process_status.exitstatus==1 then # 1 error or syntax error
		syntax_test=shell_command("ruby -c "+executable)
		if syntax_test.output=="Syntax OK\n" then
			initialize_test=shell_command("ruby "+executable+' -n test_initialize')
			if initialize_test.success? then
				1
			else # initialization  failure or test_initialize failure
				100 # may prevent other tests from running
			end #if
		else
			10000 # syntax error can hide many sins
		end #if
	else
		@recent_test.process_status.exitstatus # num_errors>1
	end #if
end

#force_change(content = README_start_text+Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")+"\n") ⇒ Object

edited_superset_of_testing


220
221
222
# File 'app/models/repository.rb', line 220

def force_change(content=README_start_text+Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")+"\n")
	IO.write(@path+'/README', content) # timestamp make file unique
end

#git_command(git_subcommand) ⇒ Object

shell_command


84
85
86
# File 'app/models/repository.rb', line 84

def git_command(git_subcommand)
	Repository.git_command(git_subcommand, @path)
end

#inspectObject

git_command


87
88
89
# File 'app/models/repository.rb', line 87

def inspect
	git_command('status --short --branch').output
end

#revert_changesObject

force_change


223
224
225
# File 'app/models/repository.rb', line 223

def revert_changes
	git_command('reset --hard')
end

#safely_visit_branch(target_branch, &block) ⇒ Object

confirm_branch_switch


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
# File 'app/models/repository.rb', line 139

def safely_visit_branch(target_branch, &block)
	push_branch=current_branch_name?
	changes_branch=push_branch # 
	push=something_to_commit? # remember
	if push then
#		[email protected]_repo.status
#		puts "status.added=#{status.added.inspect}"
#		puts "status.changed=#{status.changed.inspect}"
#		puts "status.deleted=#{status.deleted.inspect}"
#		puts "something_to_commit?=#{something_to_commit?.inspect}"
		git_command('stash save --include-untracked').assert_post_conditions
		changes_branch=:stash
	end #if

	if push_branch!=target_branch then
		confirm_branch_switch(target_branch)
		ret=block.call(changes_branch)
		confirm_branch_switch(push_branch)
	else
		ret=block.call(changes_branch)
	end #if
	if push then
		git_command('stash apply --quiet').assert_post_conditions
	end #if
	ret
end

#shell_command(command, working_directory = @path) ⇒ Object

initialize


81
82
83
# File 'app/models/repository.rb', line 81

def shell_command(command, working_directory=@path)
	ShellCommands.new(command, :chdir=>working_directory)
end

#something_to_commit?Boolean

validate_commit

Returns:

  • (Boolean)

197
198
199
200
201
202
203
204
205
206
# File 'app/models/repository.rb', line 197

def something_to_commit?
	status=@grit_repo.status
	ret=status.added!={}||status.changed!={}||status.deleted!={}
	message="status.added=#{status.added.inspect}"
	message+="\nstatus.changed=#{status.changed.inspect}"
	message+="\nstatus.deleted=#{status.deleted.inspect}"
	message+="\nstatus.added!={}||status.changed!={}||status.deleted!={}==#{ret}"
	puts message if $VERBOSE
	ret
end

#stage_files(branch, files) ⇒ Object

safely_visit_branch


165
166
167
168
169
# File 'app/models/repository.rb', line 165

def stage_files(branch, files)
	safely_visit_branch(branch) do |changes_branch|
		validate_commit(changes_branch, files)
	end #safely_visit_branch
end

#standardize_position!Object

corruption


99
100
101
102
103
104
# File 'app/models/repository.rb', line 99

def standardize_position!
	git_command("rebase --abort")
	git_command("merge --abort")
	git_command("stash save").assert_post_conditions
	git_command("checkout master")
end

#testing_superset_of_passedObject

commit_to_branch


214
215
216
# File 'app/models/repository.rb', line 214

def testing_superset_of_passed
	git_command("log testing..master")
end

#unit_names?(files) ⇒ Boolean

stage_files

Returns:

  • (Boolean)

170
171
172
173
174
# File 'app/models/repository.rb', line 170

def unit_names?(files)
	files.map do |f|
		FilePattern.path2model_name?(f).to_s
	end #map
end

#validate_commit(changes_branch, files, interact = :interactive) ⇒ Object

unit_names?


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/repository.rb', line 175

def validate_commit(changes_branch, files, interact=:interactive)
	puts files.inspect if $VERBOSE
	files.each do |p|
		puts p.inspect  if $VERBOSE
		git_command(['checkout', changes_branch.to_s, p])
	end #each
	if something_to_commit? then
		commit_message= 'fixup! '+unit_names?(files).uniq.join(',')
		if !@recent_test.nil? then
			commit_message+= "\n"+@recent_test.errors if !@recent_test.errors.empty?
		end #if
		IO.binwrite('.git/GIT_COLA_MSG', commit_message)	
		case interact
		when :interactive then
			git_command('cola').assert_post_conditions
		when :echo then
			puts "changes_branch="+changes_branch.to_s
			puts "files="+files.inspect
		end #case
#		git_command('rebase --autosquash --interactive')
	end #if
end