~edwargix/git.sr.ht

8806cc14f6d064e8043d73c5a4ad24a01f4ef4cd — Adnan Maolood 2 years ago 63c4d12
api/graph: Add HEAD field to RepoInput type

Add a HEAD field to the RepoInput type to allow setting the default
branch name with the updateRepository GraphQL mutation. Also modify the
frontend to use the HEAD field for the repository settings page.

Implements: https://todo.sr.ht/~sircmpwn/git.sr.ht/358
M api/graph/schema.graphqls => api/graph/schema.graphqls +6 -0
@@ 517,6 517,12 @@ input RepoInput {
  https://man.sr.ht/markdown/#post-processing
  """
  readme: String

  """
  Updates the repository HEAD reference, which serves as the default branch.
  Must be a valid branch name.
  """
  HEAD: String
}

input UserWebhookInput {

M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +23 -0
@@ 387,6 387,29 @@ func (r *mutationResolver) UpdateRepository(ctx context.Context, id int, input m
			return err
		}

		// This must be done after the query so that repo.Path is populated
		valid.OptionalString("HEAD", func(ref string) {
			gitRepo := repo.Repo()
			gitRepo.Lock()
			defer gitRepo.Unlock()
			branchName := plumbing.NewBranchReferenceName(ref)
			// Make sure that the branch exists
			branch, err := gitRepo.Storer.Reference(branchName)
			if err != nil {
				valid.Error("%s", err.Error()).WithField("HEAD")
				return
			}
			head := plumbing.NewSymbolicReference(plumbing.HEAD, branch.Name())
			if err := gitRepo.Storer.SetReference(head); err != nil {
				valid.Error("%s", err.Error()).WithField("HEAD")
				return
			}
		})

		if !valid.Ok() {
			return errors.New("placeholder") // TODO: Avoid surfacing placeholder error
		}

		export := path.Join(repo.Path, "git-daemon-export-ok")
		if repo.Visibility() == model.VisibilityPrivate {
			err := os.Remove(export)

M gitsrht/blueprints/manage.py => gitsrht/blueprints/manage.py +1 -13
@@ 76,24 76,12 @@ def settings_info_POST(owner_name, repo_name):
    if isinstance(repo, BaseRedirectMixin):
        repo = repo.new_repo

    # TODO: GraphQL mutation to set default branch name
    valid = Validation(request)
    branch = valid.optional("default_branch_name")
    if branch:
        with GitRepository(repo.path) as git_repo:
            new_default_branch = git_repo.branches.get(branch)
            if new_default_branch:
                head_ref = git_repo.lookup_reference("HEAD")
                head_ref.set_target(new_default_branch.name)
            else:
                valid.error(f"Branch {branch} not found", field="default_branch_name")
                return render_template("settings_info.html",
                        owner=owner, repo=repo, **valid.kwargs)

    rewrite = lambda value: None if value == "" else value
    input = {
        key: rewrite(valid.source[key]) for key in [
            "description", "visibility",
            "description", "visibility", "HEAD",
        ] if valid.source.get(key) is not None
    }


M gitsrht/templates/settings_info.html => gitsrht/templates/settings_info.html +6 -6
@@ 77,16 77,16 @@
        </div>
      </fieldset>
      <div class="form-group">
        <label for="default_branch_name">
        <label for="HEAD">
          Default branch
        </label>
        <select
          class="form-control {{valid.cls('default_branch_name')}}"
          id="default_branch_name"
          name="default_branch_name"
          class="form-control {{valid.cls('HEAD')}}"
          id="HEAD"
          name="HEAD"
          {% if repo.git_repo.is_empty %}disabled{% endif %}
        >
          {% set default_branch_name = repo.git_repo.default_branch_name() or "" %}
          {% set default_branch_name = HEAD or repo.git_repo.default_branch_name() %}
          {% for branch in repo.git_repo.raw_listall_branches() %}
            {% set branch = branch.decode("utf-8", "replace") %}
            <option


@@ 98,7 98,7 @@
            <option>No branches</option>
          {% endfor %}
        </select>
        {{valid.summary('default_branch_name')}}
        {{valid.summary('HEAD')}}
      </div>
      <button type="submit" class="btn btn-primary pull-right">
        Save {{icon("caret-right")}}