diff --git a/pkg/cmd/repo/sync/sync.go b/pkg/cmd/repo/sync/sync.go index e1b71e053..0073433e1 100644 --- a/pkg/cmd/repo/sync/sync.go +++ b/pkg/cmd/repo/sync/sync.go @@ -106,6 +106,14 @@ func syncLocalRepo(opts *SyncOptions) error { return err } + dirtyRepo, err := opts.Git.IsDirty() + if err != nil { + return err + } + if dirtyRepo { + return fmt.Errorf("can't sync because there are local changes, please commit or stash them") + } + if opts.Branch == "" { httpClient, err := opts.HttpClient() if err != nil { @@ -127,9 +135,6 @@ func syncLocalRepo(opts *SyncOptions) error { if errors.Is(err, divergingError) { return fmt.Errorf("can't sync because there are diverging changes, you can use `--force` to overwrite the changes") } - if errors.Is(err, dirtyRepoError) { - return fmt.Errorf("can't sync because there are local changes, please commit or stash them") - } return err } @@ -209,8 +214,7 @@ func syncRemoteRepo(opts *SyncOptions) error { return nil } -var divergingError = errors.New("diverging commits") -var dirtyRepoError = errors.New("dirty repo") +var divergingError = errors.New("diverging changes") func executeLocalRepoSync(srcRepo ghrepo.Interface, opts *SyncOptions) error { // Remotes precedence by name @@ -243,14 +247,6 @@ func executeLocalRepoSync(srcRepo ghrepo.Interface, opts *SyncOptions) error { } } - dirtyRepo, err := git.IsDirty() - if err != nil { - return err - } - if dirtyRepo { - return dirtyRepoError - } - startBranch, err := git.CurrentBranch() if err != nil { return err diff --git a/pkg/cmd/repo/sync/sync_test.go b/pkg/cmd/repo/sync/sync_test.go index b8ed553af..d4e953597 100644 --- a/pkg/cmd/repo/sync/sync_test.go +++ b/pkg/cmd/repo/sync/sync_test.go @@ -122,10 +122,10 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(true, nil).Once() - mgc.On("IsDirty").Return(false, nil).Once() mgc.On("CurrentBranch").Return("trunk", nil).Once() mgc.On("Merge", []string{"--ff-only", "refs/remotes/origin/trunk"}).Return(nil).Once() }, @@ -141,10 +141,10 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(true, nil).Once() - mgc.On("IsDirty").Return(false, nil).Once() mgc.On("CurrentBranch").Return("trunk", nil).Once() mgc.On("Merge", []string{"--ff-only", "refs/remotes/origin/trunk"}).Return(nil).Once() }, @@ -162,10 +162,10 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(true, nil).Once() - mgc.On("IsDirty").Return(false, nil).Once() mgc.On("CurrentBranch").Return("trunk", nil).Once() mgc.On("Merge", []string{"--ff-only", "refs/remotes/origin/trunk"}).Return(nil).Once() }, @@ -178,10 +178,10 @@ func Test_SyncRun(t *testing.T) { Branch: "test", }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/test"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"test"}).Return(true).Once() mgc.On("IsAncestor", []string{"test", "origin/test"}).Return(true, nil).Once() - mgc.On("IsDirty").Return(false, nil).Once() mgc.On("CurrentBranch").Return("test", nil).Once() mgc.On("Merge", []string{"--ff-only", "refs/remotes/origin/test"}).Return(nil).Once() }, @@ -199,10 +199,10 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(false, nil).Once() - mgc.On("IsDirty").Return(false, nil).Once() mgc.On("CurrentBranch").Return("trunk", nil).Once() mgc.On("Reset", []string{"--hard", "refs/remotes/origin/trunk"}).Return(nil).Once() }, @@ -218,6 +218,7 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(false, nil).Once() @@ -229,15 +230,7 @@ func Test_SyncRun(t *testing.T) { name: "sync local repo with parent and local changes", tty: true, opts: &SyncOptions{}, - httpStubs: func(reg *httpmock.Registry) { - reg.Register( - httpmock.GraphQL(`query RepositoryInfo\b`), - httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) - }, gitStubs: func(mgc *mockGitClient) { - mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() - mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() - mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(true, nil).Once() mgc.On("IsDirty").Return(true, nil).Once() }, wantErr: true, @@ -253,10 +246,10 @@ func Test_SyncRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`)) }, gitStubs: func(mgc *mockGitClient) { + mgc.On("IsDirty").Return(false, nil).Once() mgc.On("Fetch", []string{"origin", "+refs/heads/trunk"}).Return(nil).Once() mgc.On("HasLocalBranch", []string{"trunk"}).Return(true).Once() mgc.On("IsAncestor", []string{"trunk", "origin/trunk"}).Return(true, nil).Once() - mgc.On("IsDirty").Return(false, nil).Once() mgc.On("CurrentBranch").Return("test", nil).Once() mgc.On("Checkout", []string{"trunk"}).Return(nil).Once() mgc.On("Merge", []string{"--ff-only", "refs/remotes/origin/trunk"}).Return(nil).Once()