Skip to content

Conversation

@tototomate123
Copy link

Summary

This adds a per-repository default PR base branch and wires it through PR entry points. It updates compare links and recently pushed branch prompts to respect the configured base branch, and prevents auto-merge cleanup from deleting the configured base branch on same-repo PRs.

Behavior changes

  • New PR compare links on repo home/issue list and branch list honor the configured default PR base branch.
  • The "recently pushed new branches" prompt now compares against the configured base branch.
  • Auto-merge branch cleanup skips deleting the configured base branch (same-repo PRs only).

Testing

Not run (local UI verification only).


Reviewer question: I added a guard in services/repository/branch.go (DeleteBranchAfterMerge flow) to avoid auto-deleting the configured default PR base branch for same-repo PRs. Is that desired, or is branch protection expected to cover this?

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 21, 2026
@github-actions github-actions bot added modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files modifies/migrations labels Jan 21, 2026
@wxiaoguang
Copy link
Contributor

Is there a reason why "default" branch can't be used as PR base branch?

@TheFox0x7
Copy link
Contributor

The linked issue explains it a bit - the idea is to support a setup where you have main branch, staging branch and the dev ones and while the repository main is the first one from the list, the staging is the base one for PRs.
You could set the default to staging branch as a workaround but it's not really the main one for the workflow.

@tototomate123
Copy link
Author

Is there a reason why "default" branch can't be used as PR base branch?

Yeah, changing the default branch changes more than just PR targets. Plus, when the PR UI tries to load the diff between the "default" branch and the staging branch (which your team normally PRs to), it tends to get really laggy (making the workflow of PRing the staging branch rather tedious).

@wxiaoguang
Copy link
Contributor

The linked issue explains it a bit

Sorry but I didn't find the linked issue ... Did I miss something? What I saw on my side:

Details image

@TheFox0x7
Copy link
Contributor

it's in the title - #36412

@wxiaoguang
Copy link
Contributor

it's in the title - #36412

Hmm, totally missed that.

But still don't understand the details ... in that case, why not set dev to be the default branch, and only keep main for release purpose?

changing the default branch changes more than just PR targets.

What else would it affect?

when the PR UI tries to load the diff between the "default" branch and the staging branch (which your team normally PRs to), it tends to get really laggy

If the default branch is dev, then it isn't a problem anymore?

@tototomate123
Copy link
Author

But still don't understand the details ... in that case, why not set dev to be the default branch, and only keep main for release purpose?

Because the default branch is the repo's canonical ref, not just "the default PR target". It affects what git clone checks out, what the web UI shows by default (file tree/README), and what various tools/integrations assume when they operate on "the repo" without an explicit ref.

changing the default branch changes more than just PR targets.

What else would it affect?

Clone defaults, the repository home view/default ref in the UI, and integrations/automation that implicitly use the default branch (for example links that omit a ref, docs references, or CI setups that follow the default branch unless configured otherwise). Many repos also want the default branch to represent the stable/releasable line for external consumers and casual viewers.

If the default branch is dev, then it isn't a problem anymore?

It avoids the PR-base mismatch, but at the cost of making an integration branch the repo's canonical branch. A common workflow keeps main stable for releases while PRs land in dev/staging. Flipping the default branch to dev has broader side effects than desired and changes what the repo "looks like" to everyone by default.

A configurable default PR base branch solves the actual pain point: it reduces mistakes (accidentally opening PRs against main), fixes compare links/prompts, and supports multi-branch workflows without redefining the repo's canonical branch.

@wxiaoguang
Copy link
Contributor

Thank you for the clarification, it makes sense.

@github-actions github-actions bot added the modifies/api This PR adds API routes or modifies them label Jan 21, 2026
@tototomate123 tototomate123 marked this pull request as ready for review January 21, 2026 19:08
@tototomate123
Copy link
Author

Thank you for the clarification, it makes sense.

Thanks for taking a look, glad the motivation makes sense.

Just to confirm: this is fully opt-in (per-repo default PR base branch), so there’s no behavior change unless the setting is configured. The changes mostly wire that base through the existing PR entry points (compare links / recently pushed prompt), and add a small safety guard to avoid auto-deleting the configured base branch in same-repo PR cleanup.

Do you have any concerns with the current approach, or would you like any adjustments (naming/UI wording, docs, tests, or the branch-cleanup guard behavior)?

@lunny
Copy link
Member

lunny commented Jan 21, 2026

If we want to add an option for pull request ,it should be in the repo_unit table and the migration is unnecessary.

tototomate123 and others added 2 commits January 22, 2026 17:33
Signed-off-by: Louis <116039387+tototomate123@users.noreply.github.com>
@tototomate123
Copy link
Author

If we want to add an option for pull request ,it should be in the repo_unit table and the migration is unnecessary.

Done - moved the setting into the PR repo_unit config and removed the migration.

@silverwind silverwind requested a review from Copilot January 23, 2026 13:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds per-repository configuration for the default pull request base branch, allowing repositories to specify a different target branch for PRs instead of always using the repository's default branch. This is useful for repositories that use a branching strategy where PRs target a development branch rather than the main/master branch.

Changes:

  • Added DefaultPRBaseBranch configuration field to the PR settings unit with database validation
  • Updated PR creation links and branch compare URLs throughout templates to use the configured base branch
  • Added fallback logic when comparing the configured base branch against itself
  • Implemented protection against auto-deleting the configured base branch after merge for same-repo PRs

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
models/repo/pull_request_default.go New file implementing core logic for retrieving, validating, and checking existence of the configured default PR base branch
models/repo/pull_request_default_test.go Test coverage for the default PR base branch selection and validation
models/repo/repo_unit.go Added DefaultPRBaseBranch field to PullRequestsConfig struct
models/repo/repo.go Updated ComposeBranchCompareURL to accept context and use the configured base branch
models/git/branch.go Updated recently pushed branches to use ComposeBranchCompareURL with context
modules/structs/repo.go Added DefaultPRBaseBranch to API Repository struct
services/repository/branch.go Added protection to prevent auto-deletion of configured base branch in same-repo PRs
services/forms/repo_form.go Added DefaultPRBaseBranch form field
services/convert/repository.go Populated DefaultPRBaseBranch in API responses
services/context/repo.go Set DefaultPRBaseBranch in template context data, preferring base repo's configuration
routers/web/repo/setting/setting.go Added validation and handling for default PR base branch setting
routers/web/repo/compare.go Updated compare page to use configured base branch when none specified
templates/repo/view_content.tmpl Updated PR creation link to use configured base branch
templates/repo/settings/options.tmpl Added UI dropdown for selecting default PR base branch
templates/repo/issue/list.tmpl Updated new PR and compare buttons to use configured base branch
templates/repo/branch/list.tmpl Updated branch compare links with fallback logic when branch matches configured base
templates/swagger/v1_json.tmpl Added API documentation for default_pr_base_branch field
options/locale/locale_en-US.json Added localization strings for the new setting

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// 4 get base and head refs
baseRefName := util.IfZero(compareReq.BaseOriRef, baseRepo.DefaultBranch)
baseRefName := util.IfZero(compareReq.BaseOriRef, baseRepo.GetDefaultPRBaseBranch(ctx))
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API endpoint for comparing branches (routers/api/v1/repo/pull.go) also uses a default base branch when none is specified, but it's not updated to use the configured default PR base branch. For consistency, consider whether the API compare endpoint should also respect the configured default PR base branch setting when no base branch is explicitly provided in the request.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API compare handler intentionally defaults to DefaultBranch. Keeping it as-is avoids changing API semantics; opinions welcome.

return nil
}

func isBranchNameExists(ctx context.Context, repoID int64, branchName string) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't there some similar functions?

Copy link
Author

@tototomate123 tototomate123 Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's git_model.IsBranchExist, but importing models/git from models/repo creates an import cycle. I kept a small local helper here to avoid that.

@tototomate123
Copy link
Author

Following up on the Copilot API compare comment:
I intentionally kept the API default behavior unchanged (still defaulting to DefaultBranch) to avoid altering API semantics and potentially breaking existing consumers that rely on the current behavior.

The configurable default PR base branch is currently treated as a UI / workflow concern (PR creation, compare links, prompts), and only applies when explicitly opening PRs, not when calling the compare API without a base.

Before changing anything there, I wanted to ask:
Do we want the API compare endpoint to also respect the configured default PR base branch when no base is provided, or should it continue to default to DefaultBranch for stability?

Happy to adjust either way, but wanted alignment before touching API defaults. The PR should otherwise be ready for another review round.

@silverwind
Copy link
Member

silverwind commented Jan 24, 2026

Small remark regarding naming: I would rename DefaultPRBaseBranch to DefaultTargetBranch. Shorter and imho also a clearer name.

Do we want the API compare endpoint to also respect the configured default PR base branch when no base is provided, or should it continue to default to DefaultBranch for stability?

I would say all APIs that previously used DefaultBranch should be updated to support DefaultTargetBranch. If a PR is opened on a repo (which uses the compare page), the target should be set to DefaultTargetBranch if it exists, otherwise DefaultBranch.

@tototomate123
Copy link
Author

tototomate123 commented Jan 24, 2026

Thanks! I've renamed DefaultPRBaseBranch to DefaultTargetBranch and updated the compare API default to use DefaultTargetBranch when no base is provided (falling back to DefaultBranch).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants