def post(self, request, *args, **kwargs):
     form = MergeRequestAddForm(request.POST)
     if form.is_valid():
         title = form.cleaned_data["title"]
         first_comment = form.cleaned_data["description"]
         merge_request = MergeRequest.objects.create(
             problem=self.problem,
             title=title,
             source_branch=self.branch,
             destination_branch=self.problem.get_master_branch(),
             requester=self.request.user,
         )
         merge_request.participants.add(self.request.user)
         comment = Comment.objects.create(
             topic=merge_request,
             author=self.request.user,
             text=first_comment,
         )
         return HttpResponseRedirect(reverse("problems:merge_request", kwargs={
             "problem_code": self.problem.code,
             "merge_request_id": merge_request.id,
             "revision_slug": self.revision_slug,
         }))
     else:
         differences = get_revision_difference(
             base=self.problem.get_master_branch().head,
             new=self.revision
         )
         return render(request, "problems/create_merge_request.html", context={
             "differences": differences,
             "form": form,
         })
    def get(self, request, *args, **kwargs):
        master = self.problem.get_master_branch()
        result = assert_no_open_merge_request(
            request,
            self.problem,
            self.revision_slug,
            source=self.branch,
            destination=master
        )
        if result:
            return result
        differences = get_revision_difference(
            base=master.head,
            new=self.revision
        )
        master_merge_base = self.revision.find_merge_base(master.head)
        default_description = "\n".join(
            ["* " + revision.commit_message for revision in
             self.revision.path_to_parent(
                 master_merge_base
             )]
        )

        form = MergeRequestAddForm(initial={
            "title": self.branch.name.replace("_", " ").capitalize(),
            "description": default_description
        })
        return render(request, "problems/create_merge_request.html", context={
            "differences": differences,
            "form": form
        })
    def get(self, request, problem_code, revision_slug):
        if settings.DISABLE_BRANCHES:
            commit_form_class = CommitForm
        else:
            commit_form_class = CommitFormPullChoice
        if not self.branch.working_copy_has_changed():
            messages.error(request, _("Nothing to commit"))
            return HttpResponseRedirect(reverse("problems:overview", kwargs={
                "problem_code": self.problem.code,
                "revision_slug": self.revision_slug,
            }))
        if self.branch.working_copy.has_unresolved_conflicts():
            messages.error(request, _("You must resolve all conflicts"))
            return HttpResponseRedirect(reverse("problems:conflicts", kwargs={
                "problem_code": self.problem.code,
                "revision_slug": self.branch.get_slug()
            }))
        # TODO: Optimize the process of calculating changes

        changes = get_revision_difference(self.branch.head, self.revision)

        if len(changes) == 1:
            initial_commit_message = changes[0][0]
        else:
            initial_commit_message = ""

        commit_form = commit_form_class(instance=self.branch.working_copy, initial={
            "commit_message": initial_commit_message
        })

        return render(request, "problems/confirm_commit.html", context={
            "changes": changes,
            "commit_form": commit_form
        })
 def get(self, request, *args, **kwargs):
     if self.branch.has_working_copy() and self.revision == self.branch.working_copy:
         changes = get_revision_difference(self.branch.head, self.revision)
         return render(request, "problems/confirm_discard.html", context={
             "changes": changes,
         })
     else:
         messages.error(request, _("Nothing to discard"))
         return HttpResponseRedirect(reverse("problems:overview", kwargs={
             "problem_code": self.problem.code,
             "revision_slug": self.revision_slug
         }))
 def get(self, request, problem_code, revision_slug, merge_request_id):
     merge_request = get_object_or_404(
         MergeRequest,
         source_branch__problem_id=self.problem.id,
         id=merge_request_id,
     )
     differences = get_revision_difference(
         base=merge_request.destination_branch.head,
         new=merge_request.source_branch.head,
     )
     return render(request, "problems/merge_request_changes.html", context={
         "merge_request": merge_request,
         "differences": differences,
     })
    def post(self, request, problem_code, revision_slug):
        if settings.DISABLE_BRANCHES:
            commit_form_class = CommitForm
        else:
            commit_form_class = CommitFormPullChoice
        if self.branch.working_copy_has_changed() and self.revision == self.branch.working_copy:
            if self.branch.working_copy.has_unresolved_conflicts():
                messages.error(request, _("You must resolve all conflicts"))
                return HttpResponseRedirect(reverse("problems:conflicts", kwargs={
                    "problem_code": self.problem.code,
                    "revision_slug": self.branch.get_slug()
                }))
            commit_form = commit_form_class(request.POST, instance=self.branch.working_copy)
            if commit_form.is_valid():
                commit_form.save()
                self.branch.set_working_copy_as_head()
                if settings.DISABLE_BRANCHES:
                    if branch_pull(request, source=self.problem.get_master_branch(), destination=self.branch):
                        self.problem.get_master_branch().set_as_head(self.branch.head)
                elif commit_form.cleaned_data["pull_from_master"]:
                    branch_pull(request, source=self.problem.get_master_branch(), destination=self.branch)

                messages.success(request, _("Committed successfully"))

                if "create_merge_request" in request.POST and not settings.DISABLE_BRANCHES:
                    return HttpResponseRedirect(reverse("problems:create_merge_request", kwargs={
                        "problem_code": self.problem.code,
                        "revision_slug": self.branch.get_slug()
                    }))
                else:
                    return HttpResponseRedirect(reverse("problems:overview", kwargs={
                        "problem_code": self.problem.code,
                        "revision_slug": self.branch.get_slug()
                    }))
            else:
                changes = get_revision_difference(self.branch.head, self.revision)
                return render(request, "problems/confirm_commit.html", context={
                    "changes": changes,
                    "commit_form": commit_form
                })

        else:
            messages.error(request, _("Nothing to commit"))
            return HttpResponseRedirect(reverse("problems:overview", kwargs={
                    "problem_code": self.problem.code,
                    "revision_slug": self.revision_slug,
                }))