~edwargix/git.sr.ht

6d7c9eb06d79b8655c8d805eb9d5a3735f65e8ac — наб 4 years ago e22daa3
Allow setting the default branch from the info settings tab
3 files changed, 78 insertions(+), 0 deletions(-)

M gitsrht/app.py
A gitsrht/blueprints/manage.py
A gitsrht/templates/settings_info.html
M gitsrht/app.py => gitsrht/app.py +2 -0
@@ 25,6 25,7 @@ class GitApp(ScmSrhtFlask):
        from gitsrht.blueprints.api import plumbing, porcelain
        from gitsrht.blueprints.artifacts import artifacts
        from gitsrht.blueprints.email import mail
        from gitsrht.blueprints.manage import manage
        from gitsrht.blueprints.repo import repo
        from gitsrht.blueprints.stats import stats
        from srht.graphql import gql_blueprint


@@ 32,6 33,7 @@ class GitApp(ScmSrhtFlask):
        self.register_blueprint(plumbing)
        self.register_blueprint(porcelain)
        self.register_blueprint(mail)
        self.register_blueprint(manage)
        self.register_blueprint(repo)
        self.register_blueprint(stats)
        self.register_blueprint(webhooks_notify)

A gitsrht/blueprints/manage.py => gitsrht/blueprints/manage.py +47 -0
@@ 0,0 1,47 @@
import pygit2
from flask import Blueprint, request, render_template
from flask import redirect, url_for
from gitsrht.git import Repository as GitRepository
from srht.database import db
from srht.oauth import loginrequired
from srht.validation import Validation
from scmsrht.access import check_access, UserAccess
from scmsrht.repos.redirect import BaseRedirectMixin
from scmsrht.repos.repository import RepoVisibility
from scmsrht.webhooks import UserWebhook

manage = Blueprint('manage_git', __name__)

@manage.route("/<owner_name>/<repo_name>/settings/info_git", methods=["POST"])
@loginrequired
def settings_info_git_POST(owner_name, repo_name):
    owner, repo = check_access(owner_name, repo_name, UserAccess.manage)
    if isinstance(repo, BaseRedirectMixin):
        repo = repo.new_repo
    valid = Validation(request)
    desc = valid.optional("description", default=repo.description)
    visibility = valid.optional("visibility",
            cls=RepoVisibility,
            default=repo.visibility)
    branch = valid.optional("default_branch_name")
    with GitRepository(repo.path) as git_repo:
        new_default_branch = None
        if branch:
            try:
                new_default_branch = git_repo.branches.get(branch)
            except pygit2.InvalidSpecError:
                valid.error(f"Branch {branch} not found", field="default_branch_name")
        if not valid.ok:
            return render_template("settings_info.html",
                    owner=owner, repo=repo, **valid.kwargs)
        if new_default_branch:
            head_ref = git_repo.lookup_reference("HEAD")
            head_ref.set_target(new_default_branch.name)

        repo.visibility = visibility
        repo.description = desc
        UserWebhook.deliver(UserWebhook.Events.repo_update,
                repo.to_dict(), UserWebhook.Subscription.user_id == repo.owner_id)
        db.session.commit()
        return redirect(url_for("manage.settings_info",
            owner_name=owner_name, repo_name=repo_name))

A gitsrht/templates/settings_info.html => gitsrht/templates/settings_info.html +29 -0
@@ 0,0 1,29 @@
{% set info_action=url_for("manage_git.settings_info_git_POST",
                    owner_name=owner.canonical_name, repo_name=repo.name) %}
{% extends "bases/scmsettings_info.html" %}

{% block extrafields %}
<div class="form-group">
  <label for="default_branch_name">
    Default branch
  </label>
  <select
    class="form-control {{valid.cls('default_branch_name')}}"
    id="default_branch_name"
    name="default_branch_name"
    {% if repo.git_repo.is_empty %}disabled{% endif %}
  >
    {% set default_branch_name = repo.git_repo.default_branch_name() or "" %}
    {% for branch in repo.git_repo.branches %}
      <option
        value="{{branch}}"
        {% if branch == default_branch_name %}
          selected
        {% endif %}>{{branch}}</option>
    {% else %}
      <option>No branches</option>
    {% endfor %}
  </select>
  {{valid.summary('default_branch_name')}}
</div>
{% endblock %}