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
13 changes: 9 additions & 4 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *svc) Sync(ctx context.Context, url, branch string, sparseCheckout []str
}

if isExist {
if err := s.reset(ctx); err != nil {
if err := s.reset(ctx, true); err != nil {
return fmt.Errorf("failed to reset repo %s: %w", s.targetPath, err)
}
} else {
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *svc) Sync(ctx context.Context, url, branch string, sparseCheckout []str
}

func (s *svc) Pull(ctx context.Context) error {
if err := s.reset(ctx); err != nil {
if err := s.reset(ctx, false); err != nil {
return fmt.Errorf("failed to reset repo: %w", err)
}

Expand Down Expand Up @@ -162,15 +162,20 @@ func (s *svc) GetTopLevel(ctx context.Context) (string, error) {
return strings.TrimSpace(out), nil
}

func (s *svc) reset(ctx context.Context) error {
func (s *svc) reset(ctx context.Context, removeIgnored bool) error {
_ = os.Remove(filepath.Join(s.targetPath, ".git/index.lock"))

out, err := s.runner.Run(ctx, "git", "-C", s.targetPath, "reset", "--hard")
if err != nil {
return fmt.Errorf("failed to reset: %s %w", out, err)
}

out, err = s.runner.Run(ctx, "git", "-C", s.targetPath, "clean", "-fd")
cleanFlags := "-fd"
if removeIgnored {
cleanFlags = "-fdx"
}

out, err = s.runner.Run(ctx, "git", "-C", s.targetPath, "clean", cleanFlags)
if err != nil {
return fmt.Errorf("failed to clean: %s %w", out, err)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ func TestSync(t *testing.T) {
m.EXPECT().RunWithTTY(mock.Anything, "git", "clone", "--no-checkout", "--depth", "1", "https://github.com/org/repo.git", targetPath).Return("", nil)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "sparse-checkout", "disable").Return("", nil)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "checkout", "main").Return("", nil)
// Pull's reset (removeIgnored=false)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "reset", "--hard").Return("", nil)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "clean", "-fd").Return("", nil)
m.EXPECT().RunWithTTY(mock.Anything, "git", "-C", targetPath, "pull", "--rebase").Return("", nil)
Expand All @@ -457,6 +458,7 @@ func TestSync(t *testing.T) {
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "sparse-checkout", "init", "--cone").Return("", nil)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "sparse-checkout", "set", "src", "docs").Return("", nil)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "checkout", "main").Return("", nil)
// Pull's reset (removeIgnored=false)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "reset", "--hard").Return("", nil)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "clean", "-fd").Return("", nil)
m.EXPECT().RunWithTTY(mock.Anything, "git", "-C", targetPath, "pull", "--rebase").Return("", nil)
Expand All @@ -469,14 +471,14 @@ func TestSync(t *testing.T) {
setupGit: true,
sparseCheckout: nil,
setupMock: func(m *MockCommandRunner, targetPath string) {
// reset() called first
// Sync's reset (removeIgnored=true)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "reset", "--hard").Return("", nil).Once()
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "clean", "-fd").Return("", nil).Once()
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "clean", "-fdx").Return("", nil).Once()
// sparse-checkout disable
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "sparse-checkout", "disable").Return("", nil)
// checkout
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "checkout", "main").Return("", nil)
// Pull calls reset() again
// Pull's reset (removeIgnored=false)
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "reset", "--hard").Return("", nil).Once()
m.EXPECT().Run(mock.Anything, "git", "-C", targetPath, "clean", "-fd").Return("", nil).Once()
m.EXPECT().RunWithTTY(mock.Anything, "git", "-C", targetPath, "pull", "--rebase").Return("", nil)
Expand Down
Loading