-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat(clone): turbo clone #9904
Open
NicholasLYang
wants to merge
5
commits into
main
Choose a base branch
from
nicholasyang/turbo-clone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−24
Open
feat(clone): turbo clone #9904
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
08be420
Trying out a turbo clone mvp
NicholasLYang afcd611
Add depth flag
NicholasLYang 8baecde
Trying to do some testing for clone. Refactored the git interface aft…
NicholasLYang d6c7934
Remove unused parameter
NicholasLYang 2538853
Fix clippy
NicholasLYang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::env::current_dir; | ||
|
||
use camino::Utf8Path; | ||
use thiserror::Error; | ||
use turbopath::AbsoluteSystemPathBuf; | ||
use turborepo_ci::is_ci; | ||
use turborepo_scm::clone::{CloneMode, Git}; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("path is not valid UTF-8")] | ||
Path(#[from] camino::FromPathBufError), | ||
|
||
#[error(transparent)] | ||
Turbopath(#[from] turbopath::PathError), | ||
|
||
#[error("failed to clone repository")] | ||
Scm(#[from] turborepo_scm::Error), | ||
} | ||
|
||
pub fn run( | ||
cwd: Option<&Utf8Path>, | ||
url: &str, | ||
dir: Option<&str>, | ||
ci: bool, | ||
local: bool, | ||
depth: Option<usize>, | ||
) -> Result<i32, Error> { | ||
// We do *not* want to use the repo root but the actual, literal cwd for clone | ||
let cwd = if let Some(cwd) = cwd { | ||
cwd.to_owned() | ||
} else { | ||
current_dir() | ||
.expect("could not get current directory") | ||
.try_into()? | ||
}; | ||
let abs_cwd = AbsoluteSystemPathBuf::from_cwd(cwd)?; | ||
|
||
let clone_mode = if ci { | ||
CloneMode::CI | ||
} else if local { | ||
CloneMode::Local | ||
} else if is_ci() { | ||
CloneMode::CI | ||
} else { | ||
CloneMode::Local | ||
}; | ||
|
||
let git = Git::find()?; | ||
git.clone(url, abs_cwd, dir, None, clone_mode, depth)?; | ||
|
||
Ok(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::{backtrace::Backtrace, process::Command}; | ||
|
||
use turbopath::AbsoluteSystemPathBuf; | ||
|
||
use crate::{Error, GitRepo}; | ||
|
||
pub enum CloneMode { | ||
/// Cloning locally, do a blobless clone (good UX and reasonably fast) | ||
Local, | ||
/// Cloning on CI, do a treeless clone (worse UX but fastest) | ||
CI, | ||
} | ||
|
||
// Provide a sane maximum depth for cloning. If a user needs more history than | ||
// this, they can override or fetch it themselves. | ||
const MAX_CLONE_DEPTH: usize = 64; | ||
|
||
/// A wrapper around the git binary that is not tied to a specific repo. | ||
pub struct Git { | ||
bin: AbsoluteSystemPathBuf, | ||
} | ||
|
||
impl Git { | ||
pub fn find() -> Result<Self, Error> { | ||
Ok(Self { | ||
bin: GitRepo::find_bin()?, | ||
}) | ||
} | ||
|
||
pub fn spawn_git_command( | ||
&self, | ||
cwd: &AbsoluteSystemPathBuf, | ||
args: &[&str], | ||
pathspec: &str, | ||
) -> Result<(), Error> { | ||
let mut command = Command::new(self.bin.as_std_path()); | ||
command | ||
.args(args) | ||
.current_dir(cwd) | ||
.env("GIT_OPTIONAL_LOCKS", "0"); | ||
|
||
if !pathspec.is_empty() { | ||
command.arg("--").arg(pathspec); | ||
} | ||
|
||
let output = command.spawn()?.wait_with_output()?; | ||
|
||
if !output.status.success() { | ||
let stderr = String::from_utf8_lossy(&output.stderr).to_string(); | ||
Err(Error::Git(stderr, Backtrace::capture())) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn clone( | ||
&self, | ||
url: &str, | ||
cwd: AbsoluteSystemPathBuf, | ||
dir: Option<&str>, | ||
branch: Option<&str>, | ||
mode: CloneMode, | ||
depth: Option<usize>, | ||
) -> Result<(), Error> { | ||
let depth = depth.unwrap_or(MAX_CLONE_DEPTH).to_string(); | ||
let mut args = vec!["clone", "--depth", &depth]; | ||
if let Some(branch) = branch { | ||
args.push("--branch"); | ||
args.push(branch); | ||
} | ||
match mode { | ||
CloneMode::Local => { | ||
args.push("--filter=blob:none"); | ||
} | ||
CloneMode::CI => { | ||
args.push("--filter=tree:0"); | ||
} | ||
} | ||
args.push(url); | ||
if let Some(dir) = dir { | ||
args.push(dir); | ||
} | ||
|
||
self.spawn_git_command(&cwd, &args, "")?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as I can tell, we don't actually use the cwd as repo root anywhere. can correct if this isn't true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a necessary change for this PR?
repo_root
looks like a bad name since it can get it's value fromcli_args.cwd
. This value will now no longer be made absolute if it is given as a relative value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it is necessary, although I can just save the old cwd instead. We don't want the repo root to be our cwd and in fact, we probably don't want to be in a repo in the first place for turbo clone. This is a pretty big departure from our other commands.