Skip to content

Commit 9c6221b

Browse files
fix(turborepo): Switching back to git command instead of git2 (vercel#4606)
### Description Once vercel#4604 is merged and we release 1.9.2, we can merge this with additional tests ### Testing Instructions Added test for shallow cloned repo that fails on main
1 parent 636f584 commit 9c6221b

File tree

9 files changed

+517
-256
lines changed

9 files changed

+517
-256
lines changed

cli/internal/ffi/ffi.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ func stringToRef(s string) *string {
116116
}
117117

118118
// ChangedFiles returns the files changed in between two commits, the workdir and the index, and optionally untracked files
119-
func ChangedFiles(repoRoot string, monorepoRoot string, fromCommit string, toCommit string) ([]string, error) {
119+
func ChangedFiles(gitRoot string, turboRoot string, fromCommit string, toCommit string) ([]string, error) {
120120
fromCommitRef := stringToRef(fromCommit)
121121
toCommitRef := stringToRef(toCommit)
122122

123123
req := ffi_proto.ChangedFilesReq{
124-
RepoRoot: repoRoot,
125-
FromCommit: fromCommitRef,
126-
ToCommit: toCommitRef,
127-
MonorepoRoot: monorepoRoot,
124+
GitRoot: gitRoot,
125+
FromCommit: fromCommitRef,
126+
ToCommit: toCommitRef,
127+
TurboRoot: turboRoot,
128128
}
129129

130130
reqBuf := Marshal(&req)
@@ -144,9 +144,9 @@ func ChangedFiles(repoRoot string, monorepoRoot string, fromCommit string, toCom
144144
}
145145

146146
// PreviousContent returns the content of a file at a previous commit
147-
func PreviousContent(repoRoot, fromCommit, filePath string) ([]byte, error) {
147+
func PreviousContent(gitRoot, fromCommit, filePath string) ([]byte, error) {
148148
req := ffi_proto.PreviousContentReq{
149-
RepoRoot: repoRoot,
149+
GitRoot: gitRoot,
150150
FromCommit: fromCommit,
151151
FilePath: filePath,
152152
}

cli/internal/ffi/proto/messages.pb.go

Lines changed: 88 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/internal/scm/git_go.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build go || !rust
2+
// +build go !rust
3+
14
// Package scm abstracts operations on various tools like git
25
// Currently, only git is supported.
36
//
@@ -8,6 +11,7 @@ package scm
811

912
import (
1013
"fmt"
14+
"github.com/vercel/turbo/cli/internal/turbopath"
1115
"os/exec"
1216
"path/filepath"
1317
"strings"
@@ -17,13 +21,13 @@ import (
1721

1822
// git implements operations on a git repository.
1923
type git struct {
20-
repoRoot string
24+
repoRoot turbopath.AbsoluteSystemPath
2125
}
2226

2327
// ChangedFiles returns a list of modified files since the given commit, optionally including untracked files.
2428
func (g *git) ChangedFiles(fromCommit string, toCommit string, relativeTo string) ([]string, error) {
2529
if relativeTo == "" {
26-
relativeTo = g.repoRoot
30+
relativeTo = g.repoRoot.ToString()
2731
}
2832
relSuffix := []string{"--", relativeTo}
2933
command := []string{"diff", "--name-only", toCommit}

cli/internal/scm/git_rust.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Package scm abstracts operations on various tools like git
2+
// Currently, only git is supported.
3+
//
4+
// Adapted from https://github.com/thought-machine/please/tree/master/src/scm
5+
// Copyright Thought Machine, Inc. or its affiliates. All Rights Reserved.
6+
// SPDX-License-Identifier: Apache-2.0
7+
//go:build rust
8+
// +build rust
9+
10+
package scm
11+
12+
import (
13+
"fmt"
14+
"github.com/vercel/turbo/cli/internal/ffi"
15+
"github.com/vercel/turbo/cli/internal/turbopath"
16+
)
17+
18+
// git implements operations on a git repository.
19+
type git struct {
20+
repoRoot turbopath.AbsoluteSystemPath
21+
}
22+
23+
// ChangedFiles returns a list of modified files since the given commit, optionally including untracked files.
24+
func (g *git) ChangedFiles(fromCommit string, toCommit string, monorepoRoot string) ([]string, error) {
25+
return ffi.ChangedFiles(g.repoRoot.ToString(), monorepoRoot, fromCommit, toCommit)
26+
}
27+
28+
func (g *git) PreviousContent(fromCommit string, filePath string) ([]byte, error) {
29+
if fromCommit == "" {
30+
return nil, fmt.Errorf("Need commit sha to inspect file contents")
31+
}
32+
33+
return ffi.PreviousContent(g.repoRoot.ToString(), fromCommit, filePath)
34+
}

cli/internal/scm/scm.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
package scm
88

99
import (
10-
"path/filepath"
11-
1210
"github.com/pkg/errors"
1311

14-
"github.com/vercel/turbo/cli/internal/fs"
1512
"github.com/vercel/turbo/cli/internal/turbopath"
1613
)
1714

@@ -27,16 +24,16 @@ type SCM interface {
2724

2825
// newGitSCM returns a new SCM instance for this repo root.
2926
// It returns nil if there is no known implementation there.
30-
func newGitSCM(repoRoot string) SCM {
31-
if fs.PathExists(filepath.Join(repoRoot, ".git")) {
27+
func newGitSCM(repoRoot turbopath.AbsoluteSystemPath) SCM {
28+
if repoRoot.UntypedJoin(".git").Exists() {
3229
return &git{repoRoot: repoRoot}
3330
}
3431
return nil
3532
}
3633

3734
// newFallback returns a new SCM instance for this repo root.
3835
// If there is no known implementation it returns a stub.
39-
func newFallback(repoRoot string) (SCM, error) {
36+
func newFallback(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) {
4037
if scm := newGitSCM(repoRoot); scm != nil {
4138
return scm, nil
4239
}
@@ -52,5 +49,5 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) {
5249
if err != nil {
5350
return nil, err
5451
}
55-
return newFallback(dotGitDir.Dir().ToStringDuringMigration())
52+
return newFallback(dotGitDir.Dir())
5653
}

crates/turborepo-ffi/messages.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ message GlobRespList {
2424
}
2525

2626
message ChangedFilesReq {
27-
string repo_root = 1;
28-
string monorepo_root = 2;
27+
string git_root = 1;
28+
string turbo_root = 2;
2929
optional string from_commit = 3;
30-
optional string to_commit = 4;
30+
string to_commit = 4;
3131
}
3232

3333
message ChangedFilesResp {
@@ -42,7 +42,7 @@ message ChangedFilesList {
4242
}
4343

4444
message PreviousContentReq {
45-
string repo_root = 1;
45+
string git_root = 1;
4646
string from_commit = 2;
4747
string file_path = 3;
4848
}

crates/turborepo-ffi/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ pub extern "C" fn changed_files(buffer: Buffer) -> Buffer {
7474
}
7575
};
7676

77-
let commit_range = req.from_commit.as_deref().zip(req.to_commit.as_deref());
7877
let response = match turborepo_scm::git::changed_files(
79-
req.repo_root.into(),
80-
req.monorepo_root.into(),
81-
commit_range,
78+
req.git_root.into(),
79+
req.turbo_root.into(),
80+
req.from_commit.as_deref(),
81+
&req.to_commit,
8282
) {
8383
Ok(files) => {
8484
let files: Vec<_> = files.into_iter().collect();
@@ -108,7 +108,7 @@ pub extern "C" fn previous_content(buffer: Buffer) -> Buffer {
108108
};
109109

110110
let response = match turborepo_scm::git::previous_content(
111-
req.repo_root.into(),
111+
req.git_root.into(),
112112
&req.from_commit,
113113
PathBuf::from(req.file_path),
114114
) {

0 commit comments

Comments
 (0)