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
11 changes: 11 additions & 0 deletions desktop/internal/core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ type GitChange struct {
// CanToggleStaging is false for a parent gitlink whose only change is uncommitted content
// inside the submodule, which cannot be staged from the parent.
CanToggleStaging bool `json:"canToggleStaging"`
// Submodule is set on a gitlink and tells a new recorded commit from content changes
// inside the submodule.
Submodule *GitSubmoduleStatus `json:"submodule,omitempty"`
// UntrackedFileCount is the number of non-ignored files below a summarized untracked
// directory, and 0 for every other change.
UntrackedFileCount int `json:"untrackedFileCount,omitempty"`
}

// GitSubmoduleStatus is what Git porcelain v2 reports about a gitlink.
type GitSubmoduleStatus struct {
// CommitChanged reports that the submodule's HEAD is not the commit the parent records.
CommitChanged bool `json:"commitChanged"`
TrackedChanges bool `json:"trackedChanges"`
UntrackedChanges bool `json:"untrackedChanges"`
}

// GitStatus is the repository identity, branch divergence and file changes of a directory.
type GitStatus struct {
// RepositoryRoot is the repository's top-level directory, relative to the root that was
Expand Down
6 changes: 6 additions & 0 deletions desktop/internal/core/gitcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ type WorkspaceCommitRequest struct {
// Reviewed is the session whose plan the user confirmed; the kernel reports
// ReviewChanged when the repositories no longer match it. Nil asks for a first plan.
Reviewed *WorkspaceCommitSession
// WorkspaceRepositories are the discovery entries of the workspace. Core binds every
// nested one that Repositories does not already bind, under its discovery ID, unless the
// commit is scoped to a ChangeList; it never adds the workspace repository.
WorkspaceRepositories []WorkspaceRepository
}

// workspaceCommitPrepareRequest is the payload of git.workspaceCommitPrepare.
Expand All @@ -247,6 +251,7 @@ type workspaceCommitPrepareRequest struct {
PathScope *ChangeListScope `json:"pathScope,omitempty"`
Previous *WorkspaceCommitSession `json:"previous,omitempty"`
Reviewed json.RawMessage `json:"reviewed,omitempty"`
WorkspaceRepositories []WorkspaceRepository `json:"workspaceRepositories,omitempty"`
}

// WorkspaceCommitStatus is the kernel's stable key for where one repository of a workspace
Expand Down Expand Up @@ -415,6 +420,7 @@ func (c *Client) WorkspaceCommitPrepare(ctx context.Context, request WorkspaceCo
IncludeParentReferences: request.IncludeParentReferences,
PathScope: request.PathScope,
Previous: request.Previous,
WorkspaceRepositories: request.WorkspaceRepositories,
}
if request.Reviewed != nil {
payload.Reviewed = request.Reviewed.plan
Expand Down
89 changes: 85 additions & 4 deletions desktop/internal/core/repository_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func TestWorkspaceRepositoryDiscoveryUsesCoreRules(t *testing.T) {
if err != nil {
t.Fatal(err)
}
serviceA := filepath.ToSlash(filepath.Join(root, "service-a"))
want := []WorkspaceRepository{
{Path: filepath.ToSlash(filepath.Join(root, "service-a"))},
{Path: filepath.ToSlash(filepath.Join(root, "service-b"))},
{Path: serviceA, ID: "service-a", Name: "service-a", Role: RepositoryNested},
{Path: filepath.ToSlash(filepath.Join(root, "service-b")), ID: "service-b", Name: "service-b", Role: RepositoryNested},
}
if !reflect.DeepEqual(repositories, want) {
t.Fatalf("repositories = %#v, want %#v", repositories, want)
Expand All @@ -38,8 +39,88 @@ func TestWorkspaceRepositoryDiscoveryUsesCoreRules(t *testing.T) {
t.Fatal(err)
}
repositories, err = NewClient().DiscoverWorkspaceRepositories(testContext(t), nested)
if err != nil || !reflect.DeepEqual(repositories, want[:1]) {
t.Fatalf("nested workspace repositories = %#v, error = %v; want %#v", repositories, err, want[:1])
containing := []WorkspaceRepository{{Path: serviceA, ID: "..", Name: "service-a", Role: RepositoryWorkspace}}
if err != nil || !reflect.DeepEqual(repositories, containing) {
t.Fatalf("nested workspace repositories = %#v, error = %v; want %#v", repositories, err, containing)
}
}

// The bridge passes discovery entries through and decodes Core's answer; the rules are the
// fixtures' and Core's.
func TestWorkspaceRepositorySelectionUsesCoreFixtures(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("git/workspace-repository-selection-v1.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Cases []struct {
Name string `json:"name"`
Request struct {
Repositories []WorkspaceRepository `json:"repositories"`
Selected string `json:"selected"`
} `json:"request"`
Response struct {
Active *string `json:"active"`
FellBack bool `json:"fellBack"`
Others []string `json:"others"`
} `json:"response"`
} `json:"cases"`
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
if len(fixture.Cases) == 0 {
t.Fatal("the selection fixture has no cases")
}
client := NewClient()
for _, item := range fixture.Cases {
t.Run(item.Name, func(t *testing.T) {
want := WorkspaceRepositorySelection{FellBack: item.Response.FellBack, Others: item.Response.Others}
if item.Response.Active != nil {
want.Active = *item.Response.Active
}
got, err := client.SelectWorkspaceRepository(testContext(t), item.Request.Repositories, item.Request.Selected)
if err != nil || !reflect.DeepEqual(got, want) {
t.Fatalf("selection = %#v, error = %v; want %#v", got, err, want)
}
})
}
duplicate := []WorkspaceRepository{
{Path: "/w/a", ID: "a", Name: "a", Role: RepositoryNested},
{Path: "/w/b", ID: "a", Name: "a", Role: RepositoryNested},
}
_, err = client.SelectWorkspaceRepository(testContext(t), duplicate, "")
requireCode(t, err, CodeInvalidRequest)
}

func TestWorkspaceChangeGroupsUseCoreFixtures(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("git/workspace-change-groups-v1.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Cases []struct {
Name string `json:"name"`
Request struct {
Repositories []WorkspaceRepositoryChanges `json:"repositories"`
} `json:"request"`
Response WorkspaceChangeGroups `json:"response"`
} `json:"cases"`
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
if len(fixture.Cases) == 0 {
t.Fatal("the change group fixture has no cases")
}
client := NewClient()
for _, item := range fixture.Cases {
t.Run(item.Name, func(t *testing.T) {
got, err := client.GroupWorkspaceChanges(testContext(t), item.Request.Repositories)
if err != nil || !reflect.DeepEqual(got, item.Response) {
t.Fatalf("groups = %#v, error = %v; want %#v", got, err, item.Response)
}
})
}
}

Expand Down
144 changes: 144 additions & 0 deletions desktop/internal/core/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,156 @@ package core

import "context"

// WorkspaceRepositoryRole is how a discovered repository relates to the opened folder.
type WorkspaceRepositoryRole string

const (
// RepositoryWorkspace is the repository that contains the opened folder. Discovery lists
// at most one, first.
RepositoryWorkspace WorkspaceRepositoryRole = "workspace"
// RepositoryNested is a repository below the opened folder.
RepositoryNested WorkspaceRepositoryRole = "nested"
)

// WorkspaceRepository is a Git root discovered by Core. Path is an absolute
// path and can be outside the opened folder when the folder is inside a
// containing repository. Core uses forward slashes on Windows too; callers
// that compare native paths must normalize separators. Ordering belongs to Core.
// The window hands entries back to Core unchanged, so every field is Core's.
type WorkspaceRepository struct {
Path string `json:"path"`
// ID is the root relative to the opened folder with "/" separators: "." for the folder
// itself, "a/b" below it and ".." segments for a containing repository. It names the
// repository in commit plans, color assignments and selections.
ID string `json:"id"`
// Name is what views show: the folder name of the workspace repository, the ID of a
// nested one.
Name string `json:"name"`
Role WorkspaceRepositoryRole `json:"role"`
}

// WorkspaceRepositorySelection is the repository a multi-repository view acts on.
type WorkspaceRepositorySelection struct {
// Active is the ID the view acts on, empty when the workspace has no repository.
Active string
// FellBack reports that the repository the view picked is no longer listed, so the view
// drops what belonged to it.
FellBack bool
// Others are every other ID in discovery order: the repositories the view lists to
// switch to.
Others []string
}

// SelectWorkspaceRepository asks Core which repository a view acts on, given the discovery
// list and the ID the view picked before, empty when it has not picked one.
func (c *Client) SelectWorkspaceRepository(ctx context.Context, repositories []WorkspaceRepository, selected string) (WorkspaceRepositorySelection, error) {
request := struct {
Repositories []WorkspaceRepository `json:"repositories"`
Selected string `json:"selected,omitempty"`
}{nonNil(repositories), selected}
var response struct {
Active *string `json:"active"`
FellBack bool `json:"fellBack"`
Others []string `json:"others"`
}
if err := c.Execute(ctx, "git.workspaceRepositorySelection", request, &response); err != nil {
return WorkspaceRepositorySelection{}, err
}
selection := WorkspaceRepositorySelection{FellBack: response.FellBack, Others: response.Others}
if response.Active != nil {
selection.Active = *response.Active
}
return selection, nil
}

// WorkspaceRepositoryChanges is a discovery entry with the changes git.status read for it.
type WorkspaceRepositoryChanges struct {
WorkspaceRepository
Changes []GitChange `json:"changes"`
}

// StagingOperation is what a checkbox of the Commit tool window writes.
type StagingOperation string

const (
StageChanges StagingOperation = "stage"
UnstageChanges StagingOperation = "unstage"
)

// StagingPlan is the operation one checkbox applies to one repository.
type StagingPlan struct {
Operation StagingOperation `json:"operation"`
// Paths are repository-relative, both sides of a rename, in change order.
Paths []string `json:"paths"`
}

// ParentReference says whether the listed ancestor that records a nested repository as a
// submodule moved its recorded commit.
type ParentReference string

const (
// ParentReferenceMoved is a gitlink the ancestor lists with a new commit.
ParentReferenceMoved ParentReference = "moved"
// ParentReferenceUnchanged is a gitlink the ancestor lists with only content changes: a
// commit here still has to be recorded in the parent.
ParentReferenceUnchanged ParentReference = "unchanged"
)

// WorkspaceChangeGroup is one repository with changes in the Commit tool window.
type WorkspaceChangeGroup struct {
ID string `json:"id"`
Name string `json:"name"`
Role WorkspaceRepositoryRole `json:"role"`
// FileCount counts each file below a summarized untracked directory.
FileCount int `json:"fileCount"`
StagedCount int `json:"stagedCount"`
// AllStaged reports that every change that can toggle is staged in full.
AllStaged bool `json:"allStaged"`
// Toggleable reports that some change can be staged or unstaged.
Toggleable bool `json:"toggleable"`
// Staging is what the group's checkbox writes, nil when it writes nothing.
Staging *StagingPlan `json:"staging,omitempty"`
// ParentReference is empty when no listed ancestor records the repository as a submodule.
ParentReference ParentReference `json:"parentReference,omitempty"`
}

// RepositoryStaging is the paths one repository stages or unstages.
type RepositoryStaging struct {
ID string `json:"id"`
Paths []string `json:"paths"`
}

// WorkspaceStageAll is what the workspace line's checkbox writes: one operation for every
// repository with something to write.
type WorkspaceStageAll struct {
Operation StagingOperation `json:"operation"`
Repositories []RepositoryStaging `json:"repositories"`
}

// WorkspaceChangeGroups is the Commit tool window of a workspace with several repositories:
// one line for all changes and a group per repository that has changes, in discovery order.
type WorkspaceChangeGroups struct {
Groups []WorkspaceChangeGroup `json:"groups"`
FileCount int `json:"fileCount"`
StagedCount int `json:"stagedCount"`
AllStaged bool `json:"allStaged"`
// StageAll is nil when the workspace line writes nothing.
StageAll *WorkspaceStageAll `json:"stageAll,omitempty"`
}

// GroupWorkspaceChanges asks Core to group the changes of every repository and plan what
// each checkbox writes. It reads nothing from disk.
func (c *Client) GroupWorkspaceChanges(ctx context.Context, repositories []WorkspaceRepositoryChanges) (WorkspaceChangeGroups, error) {
request := make([]WorkspaceRepositoryChanges, len(repositories))
for i, repository := range repositories {
repository.Changes = nonNil(repository.Changes)
request[i] = repository
}
var groups WorkspaceChangeGroups
err := c.Execute(ctx, "git.workspaceChangeGroups", struct {
Repositories []WorkspaceRepositoryChanges `json:"repositories"`
}{request}, &groups)
return groups, err
}

// DiscoverWorkspaceRepositories returns Core's ordered repository roots. Core
Expand Down
Loading
Loading