Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions desktop/internal/core/git_workspace_branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package core

import "context"

// WorkspaceRepositoryReferences is one repository of a folder with several, with the references
// its git.references call returned, passed back to Core unchanged.
type WorkspaceRepositoryReferences struct {
// ID is the repository's workspace.repositories ID.
ID string `json:"id"`
References []GitReference `json:"references"`
}

// WorkspaceCommonReferences are the branches every repository of a folder has, as IntelliJ IDEA's
// Common Local Branches and Common Remote Branches list them, in the first repository's order.
// Each is a git.references entry: IsCurrent when every repository has it checked out,
// UpstreamShortName when every repository tracks the same remote branch with it, and Ahead and
// Behind added up over the repositories.
type WorkspaceCommonReferences struct {
Local []GitReference `json:"local"`
Remote []GitReference `json:"remote"`
}

// GitWorkspaceCommonReferences asks Core which local and remote branches every repository has
// (git.workspaceCommonReferences). It runs no process.
func (c *Client) GitWorkspaceCommonReferences(ctx context.Context, repositories []WorkspaceRepositoryReferences) (WorkspaceCommonReferences, error) {
request := struct {
Repositories []WorkspaceRepositoryReferences `json:"repositories"`
}{workspaceRepositoryReferences(repositories)}
var common WorkspaceCommonReferences
if err := c.Execute(ctx, "git.workspaceCommonReferences", request, &common); err != nil {
return WorkspaceCommonReferences{}, err
}
common.Local, common.Remote = nonNil(common.Local), nonNil(common.Remote)
return common, nil
}

// WorkspaceBranchOperation is the branch action chosen on a common branch.
type WorkspaceBranchOperation string

const (
// WorkspaceBranchCheckout switches every repository to the common branch.
WorkspaceBranchCheckout WorkspaceBranchOperation = "checkout"
// WorkspaceBranchCreate creates a new local branch at the common branch in every repository.
WorkspaceBranchCreate WorkspaceBranchOperation = "createBranch"
)

// WorkspaceBranchPlanRequest asks Core how a branch action on a common branch runs across the
// repositories.
type WorkspaceBranchPlanRequest struct {
Operation WorkspaceBranchOperation `json:"operation"`
// Reference is the full name of the common local or remote branch.
Reference string `json:"reference"`
// Name is the new branch's name for WorkspaceBranchCreate, as git.branchNamePreview cleaned it.
Name string `json:"name,omitempty"`
// Checkout switches every repository to the new branch of WorkspaceBranchCreate.
Checkout bool `json:"checkout,omitempty"`
// Repositories are the repositories the action runs in, in the order it runs.
Repositories []WorkspaceRepositoryReferences `json:"repositories"`
}

// WorkspaceBranchPlan is Core's plan of a branch action across the repositories.
type WorkspaceBranchPlan struct {
// Steps are the writes in the order they run; the first that fails stops the action.
Steps []WorkspaceBranchStep `json:"steps"`
// Skipped are the IDs of the repositories a checkout leaves alone because they are already on
// the branch.
Skipped []string `json:"skipped"`
}

// WorkspaceBranchStep is the write of one repository: a checkout of Reference, or a new branch
// at it.
type WorkspaceBranchStep struct {
Repository string `json:"repository"`
// Reference is this repository's fullName, shortName and kind of the common branch.
Reference GitReference `json:"reference"`
// Rollback are the writes that undo the step, in the order they run; nil when nothing can,
// because HEAD was detached or unborn before the step switched it.
Rollback []WorkspaceBranchRollback `json:"rollback"`
}

// WorkspaceBranchRollbackOperation is the git.write operation of a rollback action.
type WorkspaceBranchRollbackOperation string

const (
// WorkspaceRollbackCheckout checks the previous local branch out again.
WorkspaceRollbackCheckout WorkspaceBranchRollbackOperation = "checkout"
// WorkspaceRollbackDeleteBranch deletes the branch the step created.
WorkspaceRollbackDeleteBranch WorkspaceBranchRollbackOperation = "deleteBranch"
)

// WorkspaceBranchRollback is one write that undoes part of a step.
type WorkspaceBranchRollback struct {
Operation WorkspaceBranchRollbackOperation `json:"operation"`
Reference GitReference `json:"reference"`
}

// GitWorkspaceBranchPlan asks Core which writes a branch action on a common branch runs in each
// repository, which repositories it skips, and what undoes each write (git.workspaceBranchPlan).
// It runs no process; the caller runs the writes.
func (c *Client) GitWorkspaceBranchPlan(ctx context.Context, request WorkspaceBranchPlanRequest) (WorkspaceBranchPlan, error) {
request.Repositories = workspaceRepositoryReferences(request.Repositories)
var plan WorkspaceBranchPlan
if err := c.Execute(ctx, "git.workspaceBranchPlan", request, &plan); err != nil {
return WorkspaceBranchPlan{}, err
}
plan.Steps, plan.Skipped = nonNil(plan.Steps), nonNil(plan.Skipped)
return plan, nil
}

// workspaceRepositoryReferences is repositories with every list present, as Core requires.
func workspaceRepositoryReferences(repositories []WorkspaceRepositoryReferences) []WorkspaceRepositoryReferences {
out := make([]WorkspaceRepositoryReferences, len(repositories))
for i, repository := range repositories {
out[i] = WorkspaceRepositoryReferences{ID: repository.ID, References: nonNil(repository.References)}
}
return out
}
9 changes: 8 additions & 1 deletion desktop/internal/core/gitlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ type GraphRequest struct {
// CurrentBranch is the branch whose commits get the current-branch background, empty
// for none.
CurrentBranch string `json:"currentBranch,omitempty"`
// CheckedOutHeads gives the current-branch background to the commits of every checked-out
// branch instead, for a list that merges several repositories: each repository's own
// current branch. It cannot be combined with CurrentBranch.
CheckedOutHeads bool `json:"-"`
}

type graphRequestWire struct {
Expand All @@ -209,6 +213,7 @@ type graphRequestWire struct {
VisibleHashes []string `json:"visibleHashes"`
DisplayMode string `json:"displayMode"`
CurrentBranch string `json:"currentBranch,omitempty"`
CheckedOutHeads bool `json:"checkedOutHeads,omitempty"`
}

// GraphEdge is one edge from a row's commit to a parent.
Expand Down Expand Up @@ -264,7 +269,8 @@ type Graph struct {
// visible rows are.
RecommendedLaneCount int `json:"recommendedLaneCount"`
HasMissingParents bool `json:"hasMissingParents"`
// CurrentBranchHashes are the commits of the current branch.
// CurrentBranchHashes are the commits of the current branch, or of every checked-out branch
// for CheckedOutHeads.
CurrentBranchHashes []string `json:"currentBranchHashes"`
}

Expand All @@ -281,6 +287,7 @@ func (c *Client) GitGraph(ctx context.Context, request GraphRequest) (Graph, err
VisibleHashes: request.VisibleHashes,
DisplayMode: mode,
CurrentBranch: request.CurrentBranch,
CheckedOutHeads: request.CheckedOutHeads,
}
var graph Graph
if err := c.Execute(ctx, "git.graphProjection", wire, &graph); err != nil {
Expand Down
30 changes: 30 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 35 additions & 5 deletions desktop/internal/workbench/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const (
branchDialogTag
// branchDialogTagDelete confirms deleting a tag.
branchDialogTagDelete
// branchDialogRollback offers to undo a branch action on a common branch that failed after
// it succeeded in some repositories (branch_common.go).
branchDialogRollback
)

// branchSession is the state of the branch popup while it is open and of the dialog it
Expand Down Expand Up @@ -88,6 +91,14 @@ type branchSession struct {
busy bool
// annotated says the new tag takes the typed message; otherwise it is lightweight.
annotated bool

// common are the branches every repository of a project folder with several has
// (branch_common.go).
common commonBranches
// across says the open dialog acts on a common branch in every repository: the new branch
// dialog, the conflict dialog and the rollback dialog. write is that action.
across bool
write commonWrite
}

// branchOperationTimeout bounds one branch write and the status read after it.
Expand All @@ -114,6 +125,7 @@ func (a *App) openBranches() {
b.collapsed, b.expanded = map[string]bool{}, map[string]bool{}
}
a.loadBranches(p)
a.loadCommonBranches(p)
}

// closeBranches closes the popup and the row menu in it.
Expand All @@ -125,6 +137,7 @@ func (a *App) closeBranches() {
// popup started it.
func (a *App) leaveBranches() {
a.branch.fromPopup = a.branch.open
a.branch.across = false
a.closeBranches()
}

Expand Down Expand Up @@ -298,10 +311,14 @@ func (a *App) noticeCheckedOut(name string, status core.GitStatus) {
}

// beginCreate opens the dialog that creates a branch from r.
func (a *App) beginCreate(p *project, r core.GitReference) {
func (a *App) beginCreate(p *project, r core.GitReference) { a.openCreate(p, r, false) }

// openCreate opens the dialog that creates a branch from r, in every repository of the folder
// when across is set and r is a common branch.
func (a *App) openCreate(p *project, r core.GitReference, across bool) {
a.leaveBranches()
b := &a.branch
b.dialog, b.target, b.checkout = branchDialogCreate, r, true
b.dialog, b.target, b.checkout, b.across = branchDialogCreate, r, true, across
b.typed = ""
b.preview = a.previewBranchName(p, "", false)
b.name.SetText(b.preview.InitialName)
Expand Down Expand Up @@ -336,15 +353,19 @@ func (a *App) beginDelete(r core.GitReference) {

// cancelBranchDialog closes the open dialog without doing what it asked.
func (a *App) cancelBranchDialog() {
a.branch.dialog = branchDialogNone
a.branch.dialog, a.branch.across = branchDialogNone, false
}

// previewBranchName asks the kernel how the typed name is cleaned and whether it can be
// created. The computation is pure and answers at once. The branch being renamed is left
// out of the references, so that its own name is not a clash.
// out of the references, so that its own name is not a clash. A branch created in every
// repository must clash with none of their references.
func (a *App) previewBranchName(p *project, raw string, require bool) core.BranchNamePreview {
b := &a.branch
refs := b.refs.References
if b.across {
refs = b.common.allReferences()
}
var from *core.GitReference
switch b.dialog {
case branchDialogCreate:
Expand Down Expand Up @@ -433,6 +454,10 @@ func (a *App) submitCreate(p *project) {
}
from, checkout := b.target, b.checkout
a.performed("git.branch.create", surfaceButton)
if b.across {
a.planCommon(p, commonWrite{operation: core.WorkspaceBranchCreate, reference: from, name: name, checkout: checkout})
return
}
a.runBranchWrite(p, "git_branch_create", a.text.Branch.CreateFailed,
func(ctx context.Context, kernel Kernel, root string) error {
return kernel.GitCreateBranch(ctx, root, name, from, checkout)
Expand Down Expand Up @@ -478,12 +503,17 @@ func (a *App) submitRevision(p *project) {
func(a *App, status core.GitStatus) { a.noticeCheckedOut(revision, status) })
}

// resolveConflict checks out the blocked target in mode, as the conflict dialog chose.
// resolveConflict checks out the blocked target in mode, as the conflict dialog chose: in every
// repository of the planned action when the dialog is about a common branch.
func (a *App) resolveConflict(p *project, mode core.CheckoutMode) {
b := &a.branch
if b.busy {
return
}
if b.across {
a.runCommon(p, b.write, mode)
return
}
a.checkoutWith(p, b.target, mode)
}

Expand Down
Loading
Loading