def clean_diff(self): """Validate the diff form. This function will be invoked by django.form.Forms.is_valid(), and will raise the exception ValidationError """ self.the_patch = patch.fromstring(self.cleaned_data["diff"]) # Check that it only patches one file. if len(self.the_patch.hunks) != 1: raise ValidationError, "The patch affects more than one file." # Check that the filename it patches is correct. if self.the_patch.source[0] != self.FILE_TO_BE_PATCHED or self.the_patch.target[0] != self.FILE_TO_BE_PATCHED: raise ValidationError, "The patch affects the wrong file." # Now we need to generate a working copy to apply the patch to. # We can also use this working copy to commit the patch if it's OK. repo = controllers.SvnRepository(self.username) controllers.subproc_check_output(["svn", "co", repo.file_trunk_url(), self.wcdir]) # Check that it will apply correctly to the working copy. if not self.the_patch._match_file_hunks(self.file_to_patch, self.the_patch.hunks[0]): raise ValidationError, "The patch will not apply correctly to the lastest revision." # Check that the resulting file matches what is expected. self.new_content = "".join(self.the_patch.patch_stream(open(self.file_to_patch), self.the_patch.hunks[0])) if self.new_content != open(self.NEW_CONTENT).read(): raise ValidationError, "The file resulting from patching does not have the correct contents."
def commit_diff(self): """Commit the diff form.""" # Commit the patch. open(self.file_to_patch, "w").write(self.new_content) commit_message = """Fix a typo in %s. Thanks for reporting this, %s!""" % ( self.FILE_TO_BE_PATCHED, self.username, ) controllers.subproc_check_output(["svn", "commit", "-m", commit_message, "--username", "mr_bad", self.wcdir])