def test_commmit_nomessage(self): transaction.begin() trans = transaction.current() trans.set_blob(['foo'], 'bar'.encode('utf-8')) self.assert_commit_count(0) assert_raises(GitError, transaction.commit) self.assert_commit_count(0) transaction.rollback()
def test_commit(self): transaction.begin() trans = transaction.current() trans.set_blob(['foo'], 'bar'.encode('utf-8')) trans.add_message('foobar') self.assert_commit_count(0) transaction.commit() self.assert_commit_count(1) self.assert_file_exists('foo')
def save(self): for field in self._meta.writable_fields: try: field.validate(getattr(self, field.attname)) except ValueError as e: raise self.InvalidObject(e) trans = transaction.current() serialized = self.dumps(include_hidden=True, include_pk=False) trans.set_blob(self.path, serialized.encode('utf-8')) # TODO: create informative commit message trans.add_message('Edit {}'.format(self))
def get_filename(self, fullname, prefix=PATH_PREFIX): trans = transaction.current() shortname = fullname.rpartition('.')[2] base = '/'.join([self.path, shortname]) for ext in ('/__init__.py', '.py'): filename = base + ext if trans.exists(filename.strip('/').split('/')): if prefix: filename = prefix + filename return filename raise ImportError()
def __getitem__(self, pk): if not pk in self.cache: obj = self.model(pk=pk) try: trans = transaction.current() content = trans.get_blob(obj.path).decode('utf-8') except GitError: raise self.model.DoesNotExist( 'object with pk {} does not exist'.format(pk)) obj.loads(content) self.cache[pk] = obj return self.cache[pk]
def get_source(self, fullname): path = self.get_filename(fullname, prefix=None).strip('/').split('/') trans = transaction.current() return trans.get_blob(path).decode('utf-8') + '\n'
def test_current(self): assert_raises(GitError, transaction.current) with transaction.wrap(): transaction.current() assert_raises(GitError, transaction.current)
def post(self, request, problem_code, revision_slug, *args, **kwargs): if "_commit_id" not in request.POST: raise PermissionDenied if settings.DISABLE_BRANCHES: edited_on_commit_id = request.POST["_commit_id"] patch_prefix = "{}_patch".format(request.user.username) patch_number = 0 for x in NewProblemBranch.objects.filter( name__startswith=patch_prefix): try: patch_number = max(patch_number, int(x.name[len(patch_prefix):])) except ValueError: pass new_branch_name = patch_prefix + str(patch_number + 1) new_branch = self.problem.branches.create(name=new_branch_name, head=edited_on_commit_id) overriding_transaction = git_transaction.Transaction( repository_path=self.problem.repository_path, branch_name=new_branch_name) previous_transaction = git_transaction.current() else: raise NotImplementedError git_transaction.set_default_transaction(overriding_transaction) try: instance = self.get_instance(request, *args, **kwargs) except ObjectDoesNotExist: raise Http404 form = self.model_form(request.POST, request.FILES, problem=self.problem, revision=self.revision, owner=request.user, instance=instance) if form.is_valid(): obj = form.save() if hasattr(obj, "_transaction"): new_commit = obj._transaction.commit( author_signature=Signature(request.user.get_full_name(), request.user.email)) if settings.DISABLE_BRANCHES: try: previous_transaction.merge( new_commit, squash=True, author_signature=Signature( request.user.get_full_name(), request.user.email)) except GitError as e: messages.error( request, _("Unable to merge automatically. " "Your changes can be found in commit {commit_id}. " "The merge should be done manually.").format( commit_id=str(new_commit))) return HttpResponseRedirect( self.request.get_full_path()) new_branch.delete() messages.success(request, _("Saved successfully")) return HttpResponseRedirect( self.get_success_url(request, problem_code, revision_slug, obj)) return self._show_form(request, form, instance)
def __init__(self, model): self.model = model self.cache = {} pks = transaction.current().list_blobs([model._meta.storage_name]) self.pks = set(map(model._meta.pk.loads, pks)) self.pk_names = ('pk', model._meta.pk.attname)