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
18 changes: 3 additions & 15 deletions desktop/internal/core/git_push.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package core

import (
"context"
"encoding/json"
)
import "context"

// GitPushTags says which tags a push sends along with the branch.
type GitPushTags string
Expand Down Expand Up @@ -88,18 +85,9 @@ type expectedPush struct {
// GitPush starts git and blocks; call it from a background goroutine and observe its progress
// with WithGitProgress.
func (c *Client) GitPush(ctx context.Context, root string, preview GitPushPreview, tags GitPushTags, target PushTargetChoice, force bool) error {
reviewed := expectedPush{
LocalBranch: preview.LocalBranch, LocalHead: preview.LocalHead, Remote: preview.Remote,
RemoteBranch: preview.RemoteBranch, TargetKind: preview.TargetKind,
RemoteTrackingOID: preview.RemoteTrackingOID, Tags: nonNil(preview.Tags),
}
raw, err := json.Marshal(reviewed)
request, err := pushRequest(root, preview, tags, target, force)
if err != nil {
return err
}
return c.gitWrite(ctx, gitWriteRequest{
Root: root, Operation: "push", Force: force, PushTags: string(tags), PushTarget: string(target),
ExpectedPush: raw,
GitReference: &gitReferenceRequest{FullName: "refs/heads/" + preview.LocalBranch, ShortName: preview.LocalBranch, Kind: "local"},
})
return c.gitWrite(ctx, request)
}
103 changes: 103 additions & 0 deletions desktop/internal/core/git_workspace_push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package core

import (
"context"
"encoding/json"
)

// WorkspacePushState is whether a repository of a workspace push can push, and why not.
type WorkspacePushState string

const (
// WorkspacePushReady has commits to send, or a remote branch to create.
WorkspacePushReady WorkspacePushState = "ready"
// WorkspacePushUpToDate is a branch its remote already has.
WorkspacePushUpToDate WorkspacePushState = "upToDate"
// WorkspacePushNoRemote is a repository without a remote.
WorkspacePushNoRemote WorkspacePushState = "noRemote"
// WorkspacePushEmpty is a branch without a commit yet.
WorkspacePushEmpty WorkspacePushState = "emptyRepository"
// WorkspacePushDetached is a repository whose HEAD names a commit instead of a branch.
WorkspacePushDetached WorkspacePushState = "detachedHead"
// WorkspacePushFailed is a repository Git could not read or resolve; Detail says why.
WorkspacePushFailed WorkspacePushState = "failed"
)

// WorkspacePushRepository is Core's push review of one repository of a workspace push.
type WorkspacePushRepository struct {
ID string `json:"id"`
State WorkspacePushState `json:"state"`
// Branch is the checked-out branch, empty when HEAD is detached or the repository could
// not be read.
Branch string `json:"branch"`
// Preview is the push review of a ready or up-to-date repository, nil otherwise.
Preview *GitPushPreview `json:"preview"`
// NeedsTarget reports a divergent upstream no destination was picked for.
NeedsTarget bool `json:"needsTarget"`
// Records are the listed repositories this one records as submodules. They come earlier
// in the list, and this one should not push after one of them failed to.
Records []string `json:"records"`
Detail string `json:"detail"`
}

type workspacePushPreviewRequest struct {
Repositories []WorkspaceCommitRepository `json:"repositories"`
Limit int `json:"limit,omitempty"`
PushTags GitPushTags `json:"pushTags,omitempty"`
PushTargets map[string]PushTargetChoice `json:"pushTargets,omitempty"`
}

// WorkspacePushPreview reviews pushing the checked-out branch of every repository, as IDEA's
// Push dialog lists them. The answer is in push order: a submodule before every listed
// repository that records it. targets answers divergent upstreams by repository ID.
//
// WorkspacePushPreview starts git and blocks; call it from a background goroutine.
func (c *Client) WorkspacePushPreview(ctx context.Context, repositories []WorkspaceCommitRepository, tags GitPushTags, targets map[string]PushTargetChoice) ([]WorkspacePushRepository, error) {
request := workspacePushPreviewRequest{Repositories: nonNil(repositories), Limit: 100, PushTags: tags, PushTargets: targets}
var response struct {
Repositories []WorkspacePushRepository `json:"repositories"`
}
if err := c.Execute(ctx, "git.workspacePushPreview", request, &response); err != nil {
return nil, err
}
for i := range response.Repositories {
r := &response.Repositories[i]
r.Records = nonNil(r.Records)
if r.Preview != nil {
r.Preview.Tags = nonNil(r.Preview.Tags)
r.Preview.Commits = nonNil(r.Preview.Commits)
}
}
return nonNil(response.Repositories), nil
}

// GitPushInWorkspace is GitPush for one repository of a workspace push: Git also refuses the
// push while a submodule commit it records is on no remote (--recurse-submodules=check).
//
// GitPushInWorkspace starts git and blocks; call it from a background goroutine.
func (c *Client) GitPushInWorkspace(ctx context.Context, root string, preview GitPushPreview, tags GitPushTags, target PushTargetChoice) error {
request, err := pushRequest(root, preview, tags, target, false)
if err != nil {
return err
}
request.CheckSubmodules = true
return c.gitWrite(ctx, request)
}

// pushRequest is the git.write request of a push validated against preview.
func pushRequest(root string, preview GitPushPreview, tags GitPushTags, target PushTargetChoice, force bool) (gitWriteRequest, error) {
reviewed := expectedPush{
LocalBranch: preview.LocalBranch, LocalHead: preview.LocalHead, Remote: preview.Remote,
RemoteBranch: preview.RemoteBranch, TargetKind: preview.TargetKind,
RemoteTrackingOID: preview.RemoteTrackingOID, Tags: nonNil(preview.Tags),
}
raw, err := json.Marshal(reviewed)
if err != nil {
return gitWriteRequest{}, err
}
return gitWriteRequest{
Root: root, Operation: "push", Force: force, PushTags: string(tags), PushTarget: string(target),
ExpectedPush: raw,
GitReference: &gitReferenceRequest{FullName: "refs/heads/" + preview.LocalBranch, ShortName: preview.LocalBranch, Kind: "local"},
}, nil
}
28 changes: 17 additions & 11 deletions desktop/internal/core/gitcommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type gitWriteRequest struct {
PushTags string `json:"pushTags,omitempty"`
PushTarget string `json:"pushTarget,omitempty"`
ExpectedPush json.RawMessage `json:"expectedPush,omitempty"`
// CheckSubmodules has Git refuse a push while a submodule commit it records is on no remote.
CheckSubmodules bool `json:"checkSubmodules,omitempty"`
// NoCheckout registers a created worktree without filling its directory.
NoCheckout bool `json:"noCheckout,omitempty"`
// Lightweight makes createTag a lightweight tag, which takes no message.
Expand Down Expand Up @@ -303,9 +305,11 @@ type WorkspaceCommitSession struct {
// OrderedIDs are the repositories of the plan in the order they commit: a child before the
// parent that records it.
OrderedIDs []string
Message string
Amend bool
Push bool
// Repositories are the repositories the plan binds, as the request and Core listed them.
Repositories []WorkspaceCommitRepository
Message string
Amend bool
Push bool
// Author is the "Name <email>" the plan records its commits under, empty for the
// configured identity.
Author string
Expand Down Expand Up @@ -352,14 +356,15 @@ func (s *WorkspaceCommitSession) UnmarshalJSON(data []byte) error {
return err
}
var plan struct {
Message string `json:"message"`
Amend bool `json:"amend"`
Push bool `json:"push"`
Author string `json:"author"`
SignOff bool `json:"signOff"`
IsRetry bool `json:"isRetry"`
OrderedIDs []string `json:"orderedIds"`
States map[string]WorkspaceCommitState `json:"states"`
Message string `json:"message"`
Amend bool `json:"amend"`
Push bool `json:"push"`
Author string `json:"author"`
SignOff bool `json:"signOff"`
IsRetry bool `json:"isRetry"`
OrderedIDs []string `json:"orderedIds"`
Repositories []WorkspaceCommitRepository `json:"repositories"`
States map[string]WorkspaceCommitState `json:"states"`
}
if len(wire.Plan) == 0 {
return errors.New("workspace commit session has no plan")
Expand All @@ -371,6 +376,7 @@ func (s *WorkspaceCommitSession) UnmarshalJSON(data []byte) error {
raw: append(json.RawMessage(nil), data...),
plan: append(json.RawMessage(nil), wire.Plan...),
OrderedIDs: plan.OrderedIDs,
Repositories: plan.Repositories,
Message: plan.Message,
Amend: plan.Amend,
Push: plan.Push,
Expand Down
13 changes: 11 additions & 2 deletions desktop/internal/core/gitlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type GitHistoryPage struct {
// NextCursor continues the walk with GitHistoryPage; empty when the page reaches the end.
NextCursor string `json:"nextCursor,omitempty"`
HasMore bool `json:"hasMore"`
// LimitReached says the walk stopped at the most commits its first page asked for while
// the history goes on; HasMore is then false.
LimitReached bool `json:"limitReached,omitempty"`
}

// GitCommitFile is one path a commit changed.
Expand Down Expand Up @@ -85,6 +88,8 @@ type gitHistoryPageRequest struct {
Order string `json:"order"`
Cursor string `json:"cursor,omitempty"`
Limit int `json:"limit"`
// MaxCommits is the most commits the walk reads in all; zero leaves it to the kernel.
MaxCommits int `json:"maxCommits,omitempty"`
}

type gitCursorRequest struct {
Expand Down Expand Up @@ -122,9 +127,13 @@ func (c *Client) GitReferences(ctx context.Context, root string) (GitReferences,
// minutes and keeps at most eight, so a stale cursor fails and the history is reloaded.
// A walk that is not continued to its end should be released with CloseGitHistoryCursor.
//
// maxCommits, 1 to 100,000, is the most commits the walk reads in all, fixed by its first
// page; zero is the kernel's 5,000 on a first page and the walk's own bound on a later one.
// The page that stops at the bound while the history goes on says LimitReached.
//
// GitHistoryPage starts git and blocks; call it from a background goroutine.
func (c *Client) GitHistoryPage(ctx context.Context, root, reference, cursor string, limit int) (GitHistoryPage, error) {
request := gitHistoryPageRequest{Root: root, Reference: reference, Order: "date", Cursor: cursor, Limit: limit}
func (c *Client) GitHistoryPage(ctx context.Context, root, reference, cursor string, limit, maxCommits int) (GitHistoryPage, error) {
request := gitHistoryPageRequest{Root: root, Reference: reference, Order: "date", Cursor: cursor, Limit: limit, MaxCommits: maxCommits}
var page GitHistoryPage
if err := c.Execute(ctx, "git.historyPage", request, &page); err != nil {
return GitHistoryPage{}, err
Expand Down
60 changes: 47 additions & 13 deletions desktop/internal/core/repository_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ func TestWorkspaceRepositorySelectionUsesCoreFixtures(t *testing.T) {
Request struct {
Repositories []WorkspaceRepository `json:"repositories"`
Selected string `json:"selected"`
Chosen []string `json:"chosen"`
// Chosen is nil when the case picks one repository, not several.
Chosen *[]string `json:"chosen"`
} `json:"request"`
Response struct {
Active *string `json:"active"`
FellBack bool `json:"fellBack"`
Others []string `json:"others"`
Chosen []string `json:"chosen"`
} `json:"response"`
} `json:"cases"`
}
Expand All @@ -74,28 +76,24 @@ func TestWorkspaceRepositorySelectionUsesCoreFixtures(t *testing.T) {
t.Fatal("the selection fixture has no cases")
}
client := NewClient()
checked := 0
for _, item := range fixture.Cases {
if item.Request.Chosen != nil {
// A view acting on several repositories at once sends chosen instead of selected;
// the bridge does not, until the Git Log takes several repositories (#2013).
continue
}
checked++
t.Run(item.Name, func(t *testing.T) {
want := WorkspaceRepositorySelection{FellBack: item.Response.FellBack, Others: item.Response.Others}
want := WorkspaceRepositorySelection{FellBack: item.Response.FellBack, Others: item.Response.Others, Chosen: item.Response.Chosen}
if item.Response.Active != nil {
want.Active = *item.Response.Active
}
got, err := client.SelectWorkspaceRepository(testContext(t), item.Request.Repositories, item.Request.Selected)
var got WorkspaceRepositorySelection
var err error
if item.Request.Chosen != nil {
got, err = client.SelectWorkspaceRepositories(testContext(t), item.Request.Repositories, *item.Request.Chosen)
} else {
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)
}
})
}
if checked == 0 {
t.Fatal("the selection fixture has no case with selected")
}
duplicate := []WorkspaceRepository{
{Path: "/w/a", ID: "a", Name: "a", Role: RepositoryNested},
{Path: "/w/b", ID: "a", Name: "a", Role: RepositoryNested},
Expand All @@ -104,6 +102,42 @@ func TestWorkspaceRepositorySelectionUsesCoreFixtures(t *testing.T) {
requireCode(t, err, CodeInvalidRequest)
}

// The bridge sends each repository's loaded history and decodes Core's merged rows; the order and
// the keys are the fixtures' and Core's.
func TestWorkspaceHistoryMergeUsesCoreFixtures(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("git/workspace-history-merge-v1.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Cases []struct {
Name string `json:"name"`
Request struct {
Repositories []WorkspaceHistory `json:"repositories"`
} `json:"request"`
Response WorkspaceHistoryMerge `json:"response"`
} `json:"cases"`
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
if len(fixture.Cases) == 0 {
t.Fatal("the merge fixture has no cases")
}
client := NewClient()
for _, item := range fixture.Cases {
t.Run(item.Name, func(t *testing.T) {
got, err := client.MergeWorkspaceHistories(testContext(t), item.Request.Repositories)
if err != nil || !reflect.DeepEqual(got, item.Response) {
t.Fatalf("merge = %#v, error = %v; want %#v", got, err, item.Response)
}
})
}
repeated := []WorkspaceHistory{{ID: "a", Complete: true}, {ID: "a", Complete: true}}
_, err = client.MergeWorkspaceHistories(testContext(t), repeated)
requireCode(t, err, CodeInvalidRequest)
}

func TestWorkspaceChangeGroupsUseCoreFixtures(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("git/workspace-change-groups-v1.json"))
if err != nil {
Expand Down
Loading
Loading