Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(turborepo): Switching back to git command instead of git2 #4606

Merged
merged 10 commits into from Apr 20, 2023
14 changes: 7 additions & 7 deletions cli/internal/ffi/ffi.go
Expand Up @@ -116,15 +116,15 @@ func stringToRef(s string) *string {
}

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

req := ffi_proto.ChangedFilesReq{
RepoRoot: repoRoot,
FromCommit: fromCommitRef,
ToCommit: toCommitRef,
MonorepoRoot: monorepoRoot,
GitRoot: gitRoot,
FromCommit: fromCommitRef,
ToCommit: toCommitRef,
TurboRoot: turboRoot,
}

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

// PreviousContent returns the content of a file at a previous commit
func PreviousContent(repoRoot, fromCommit, filePath string) ([]byte, error) {
func PreviousContent(gitRoot, fromCommit, filePath string) ([]byte, error) {
req := ffi_proto.PreviousContentReq{
RepoRoot: repoRoot,
GitRoot: gitRoot,
FromCommit: fromCommit,
FilePath: filePath,
}
Expand Down
176 changes: 88 additions & 88 deletions cli/internal/ffi/proto/messages.pb.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions cli/internal/scm/git_go.go
@@ -1,3 +1,6 @@
//go:build go || !rust
// +build go !rust

// Package scm abstracts operations on various tools like git
// Currently, only git is supported.
//
Expand All @@ -8,6 +11,7 @@ package scm

import (
"fmt"
"github.com/vercel/turbo/cli/internal/turbopath"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -17,13 +21,13 @@ import (

// git implements operations on a git repository.
type git struct {
repoRoot string
repoRoot turbopath.AbsoluteSystemPath
}

// ChangedFiles returns a list of modified files since the given commit, optionally including untracked files.
func (g *git) ChangedFiles(fromCommit string, toCommit string, relativeTo string) ([]string, error) {
if relativeTo == "" {
relativeTo = g.repoRoot
relativeTo = g.repoRoot.ToString()
}
relSuffix := []string{"--", relativeTo}
command := []string{"diff", "--name-only", toCommit}
Expand Down
34 changes: 34 additions & 0 deletions cli/internal/scm/git_rust.go
@@ -0,0 +1,34 @@
// Package scm abstracts operations on various tools like git
// Currently, only git is supported.
//
// Adapted from https://github.com/thought-machine/please/tree/master/src/scm
// Copyright Thought Machine, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//go:build rust
// +build rust

package scm

import (
"fmt"
"github.com/vercel/turbo/cli/internal/ffi"
"github.com/vercel/turbo/cli/internal/turbopath"
)

// git implements operations on a git repository.
type git struct {
repoRoot turbopath.AbsoluteSystemPath
}

// ChangedFiles returns a list of modified files since the given commit, optionally including untracked files.
func (g *git) ChangedFiles(fromCommit string, toCommit string, monorepoRoot string) ([]string, error) {
return ffi.ChangedFiles(g.repoRoot.ToString(), monorepoRoot, fromCommit, toCommit)
}

func (g *git) PreviousContent(fromCommit string, filePath string) ([]byte, error) {
if fromCommit == "" {
return nil, fmt.Errorf("Need commit sha to inspect file contents")
}

return ffi.PreviousContent(g.repoRoot.ToString(), fromCommit, filePath)
}
11 changes: 4 additions & 7 deletions cli/internal/scm/scm.go
Expand Up @@ -7,11 +7,8 @@
package scm

import (
"path/filepath"

"github.com/pkg/errors"

"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/turbopath"
)

Expand All @@ -27,16 +24,16 @@ type SCM interface {

// newGitSCM returns a new SCM instance for this repo root.
// It returns nil if there is no known implementation there.
func newGitSCM(repoRoot string) SCM {
if fs.PathExists(filepath.Join(repoRoot, ".git")) {
func newGitSCM(repoRoot turbopath.AbsoluteSystemPath) SCM {
if repoRoot.UntypedJoin(".git").Exists() {
return &git{repoRoot: repoRoot}
}
return nil
}

// newFallback returns a new SCM instance for this repo root.
// If there is no known implementation it returns a stub.
func newFallback(repoRoot string) (SCM, error) {
func newFallback(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) {
if scm := newGitSCM(repoRoot); scm != nil {
return scm, nil
}
Expand All @@ -52,5 +49,5 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) {
if err != nil {
return nil, err
}
return newFallback(dotGitDir.Dir().ToStringDuringMigration())
return newFallback(dotGitDir.Dir())
}
8 changes: 4 additions & 4 deletions crates/turborepo-ffi/messages.proto
Expand Up @@ -24,10 +24,10 @@ message GlobRespList {
}

message ChangedFilesReq {
string repo_root = 1;
string monorepo_root = 2;
string git_root = 1;
string turbo_root = 2;
optional string from_commit = 3;
optional string to_commit = 4;
string to_commit = 4;
}

message ChangedFilesResp {
Expand All @@ -42,7 +42,7 @@ message ChangedFilesList {
}

message PreviousContentReq {
string repo_root = 1;
string git_root = 1;
string from_commit = 2;
string file_path = 3;
}
Expand Down
10 changes: 5 additions & 5 deletions crates/turborepo-ffi/src/lib.rs
Expand Up @@ -74,11 +74,11 @@ pub extern "C" fn changed_files(buffer: Buffer) -> Buffer {
}
};

let commit_range = req.from_commit.as_deref().zip(req.to_commit.as_deref());
let response = match turborepo_scm::git::changed_files(
req.repo_root.into(),
req.monorepo_root.into(),
commit_range,
req.git_root.into(),
req.turbo_root.into(),
req.from_commit.as_deref(),
&req.to_commit,
) {
Ok(files) => {
let files: Vec<_> = files.into_iter().collect();
Expand Down Expand Up @@ -108,7 +108,7 @@ pub extern "C" fn previous_content(buffer: Buffer) -> Buffer {
};

let response = match turborepo_scm::git::previous_content(
req.repo_root.into(),
req.git_root.into(),
&req.from_commit,
PathBuf::from(req.file_path),
) {
Expand Down