From 8806cc14f6d064e8043d73c5a4ad24a01f4ef4cd Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Fri, 4 Feb 2022 11:12:35 -0500 Subject: [PATCH] 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 --- api/graph/schema.graphqls | 6 ++++++ api/graph/schema.resolvers.go | 23 +++++++++++++++++++++++ gitsrht/blueprints/manage.py | 14 +------------- gitsrht/templates/settings_info.html | 12 ++++++------ 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/api/graph/schema.graphqls b/api/graph/schema.graphqls index 1ed6a2d..5d55e42 100644 --- a/api/graph/schema.graphqls +++ b/api/graph/schema.graphqls @@ -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 { diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index b13a61c..7c05b22 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -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) diff --git a/gitsrht/blueprints/manage.py b/gitsrht/blueprints/manage.py index 54000fe..c6e2d4c 100644 --- a/gitsrht/blueprints/manage.py +++ b/gitsrht/blueprints/manage.py @@ -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 } diff --git a/gitsrht/templates/settings_info.html b/gitsrht/templates/settings_info.html index 72434c3..79fe42b 100644 --- a/gitsrht/templates/settings_info.html +++ b/gitsrht/templates/settings_info.html @@ -77,16 +77,16 @@
-