Skip to content

Commit

Permalink
git clone: Add depth argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 19, 2024
1 parent d222829 commit 361b203
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* Initial support for shallow git repositories has been implemented. However
deepening the history of a shallow repository is not yet supported.

* `jj git clone` now accepts a `--depth <DEPTH>` option, which
allows to clone the repository with a given depth.

### Fixed bugs

* Fixed panic when parsing invalid conflict markers of a particular form.
Expand Down
8 changes: 7 additions & 1 deletion cli/src/commands/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::fs;
use std::io;
use std::io::Write;
use std::num::NonZeroU32;
use std::path::Path;
use std::path::PathBuf;

Expand Down Expand Up @@ -60,6 +61,9 @@ pub struct GitCloneArgs {
/// Whether or not to colocate the Jujutsu repo with the git repo
#[arg(long)]
colocate: bool,
/// Create a shallow clone of the given depth
#[arg(long)]
depth: Option<NonZeroU32>,
}

fn absolute_git_source(cwd: &Path, source: &str) -> String {
Expand Down Expand Up @@ -132,6 +136,7 @@ pub fn cmd_git_clone(
ui,
command,
args.colocate,
args.depth,
remote_name,
&source,
&canonical_wc_path,
Expand Down Expand Up @@ -195,6 +200,7 @@ fn do_git_clone(
ui: &mut Ui,
command: &CommandHelper,
colocate: bool,
depth: Option<NonZeroU32>,
remote_name: &str,
source: &str,
wc_path: &Path,
Expand All @@ -219,11 +225,11 @@ fn do_git_clone(
git::fetch(
fetch_tx.repo_mut(),
&git_repo,
None,
remote_name,
&[StringPattern::everything()],
cb,
&command.settings().git_settings(),
depth,
)
})
.map_err(|err| match err {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ pub fn cmd_git_fetch(
git::fetch(
tx.repo_mut(),
&git_repo,
None,
remote,
&args.branch,
cb,
&command.settings().git_settings(),
None,
)
})
.map_err(|err| match err {
Expand Down
1 change: 1 addition & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ The Git repo will be a bare git repo stored inside the `.jj/` directory.
Default value: `origin`
* `--colocate` — Whether or not to colocate the Jujutsu repo with the git repo
* `--depth <DEPTH>` — Create a shallow clone of the given depth
Expand Down
20 changes: 20 additions & 0 deletions cli/tests/test_git_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,26 @@ fn test_git_clone_with_remote_name() {
"#);
}

#[test]
fn test_git_clone_with_depth() {
let test_env = TestEnvironment::default();
test_env.add_config("git.auto-local-branch = true");
let git_repo_path = test_env.env_root().join("source");
let git_repo = git2::Repository::init(git_repo_path).unwrap();
set_up_non_empty_git_repo(&git_repo);

// local transport does not support shallow clones so we just test that the
// depth arg is passed on here
let stderr = test_env.jj_cmd_failure(
test_env.env_root(),
&["git", "clone", "--depth", "1", "source", "clone"],
);
insta::assert_snapshot!(stderr, @r#"
Fetching into new repo in "$TEST_ENV/clone"
Error: shallow fetch is not supported by the local transport; class=Net (12)
"#);
}

fn get_bookmark_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(repo_path, &["bookmark", "list", "--all-remotes"])
}
6 changes: 3 additions & 3 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,11 +1241,11 @@ pub struct GitFetchStats {
pub fn fetch(
mut_repo: &mut MutableRepo,
git_repo: &git2::Repository,
depth: Option<NonZeroU32>,
remote_name: &str,
branch_names: &[StringPattern],
callbacks: RemoteCallbacks<'_>,
git_settings: &GitSettings,
depth: Option<NonZeroU32>,
) -> Result<GitFetchStats, GitFetchError> {
// Perform a `git fetch` on the local git repo, updating the remote-tracking
// branches in the git repo.
Expand All @@ -1262,8 +1262,8 @@ pub fn fetch(
fetch_options.proxy_options(proxy_options);
let callbacks = callbacks.into_git();
fetch_options.remote_callbacks(callbacks);
if let Some(depth) = depth.and_then(|depth| depth.get().try_into().ok()) {
fetch_options.depth(depth);
if let Some(depth) = depth {
fetch_options.depth(depth.get().try_into().unwrap_or(i32::MAX));
}
// At this point, we are only updating Git's remote tracking branches, not the
// local branches.
Expand Down
20 changes: 10 additions & 10 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2266,11 +2266,11 @@ fn test_fetch_empty_repo() {
let stats = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// No default bookmark and no refs
Expand All @@ -2293,11 +2293,11 @@ fn test_fetch_initial_commit() {
let stats = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// No default bookmark because the origin repo's HEAD wasn't set
Expand Down Expand Up @@ -2344,11 +2344,11 @@ fn test_fetch_success() {
git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
test_data.repo = tx.commit("test");
Expand All @@ -2368,11 +2368,11 @@ fn test_fetch_success() {
let stats = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// The default bookmark is "main"
Expand Down Expand Up @@ -2426,11 +2426,11 @@ fn test_fetch_prune_deleted_ref() {
git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// Test the setup
Expand All @@ -2450,11 +2450,11 @@ fn test_fetch_prune_deleted_ref() {
let stats = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
assert_eq!(stats.import_stats.abandoned_commits, vec![jj_id(&commit)]);
Expand All @@ -2478,11 +2478,11 @@ fn test_fetch_no_default_branch() {
git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();

Expand All @@ -2502,11 +2502,11 @@ fn test_fetch_no_default_branch() {
let stats = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// There is no default bookmark
Expand All @@ -2524,11 +2524,11 @@ fn test_fetch_empty_refspecs() {
git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"origin",
&[],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
assert!(tx
Expand All @@ -2551,11 +2551,11 @@ fn test_fetch_no_such_remote() {
let result = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
None,
"invalid-remote",
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
);
assert!(matches!(result, Err(GitFetchError::NoSuchRemote(_))));
}
Expand Down

0 comments on commit 361b203

Please sign in to comment.