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 27, 2024
1 parent 0626501 commit 469a35c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 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
7 changes: 7 additions & 0 deletions 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 Down Expand Up @@ -223,6 +229,7 @@ fn do_git_clone(
&[StringPattern::everything()],
cb,
&command.settings().git_settings(),
depth,
)
})
.map_err(|err| match err {
Expand Down
1 change: 1 addition & 0 deletions cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn cmd_git_fetch(
&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"])
}
5 changes: 5 additions & 0 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::collections::HashSet;
use std::default::Default;
use std::fmt;
use std::io::Read;
use std::num::NonZeroU32;
use std::path::PathBuf;
use std::str;

Expand Down Expand Up @@ -1244,6 +1245,7 @@ pub fn fetch(
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 @@ -1260,6 +1262,9 @@ pub fn fetch(
fetch_options.proxy_options(proxy_options);
let callbacks = callbacks.into_git();
fetch_options.remote_callbacks(callbacks);
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.
let refspecs: Vec<_> = branch_names
Expand Down
10 changes: 10 additions & 0 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ fn test_fetch_empty_repo() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// No default bookmark and no refs
Expand All @@ -2296,6 +2297,7 @@ fn test_fetch_initial_commit() {
&[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 @@ -2346,6 +2348,7 @@ fn test_fetch_success() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
test_data.repo = tx.commit("test");
Expand All @@ -2369,6 +2372,7 @@ fn test_fetch_success() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// The default bookmark is "main"
Expand Down Expand Up @@ -2426,6 +2430,7 @@ fn test_fetch_prune_deleted_ref() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// Test the setup
Expand All @@ -2449,6 +2454,7 @@ fn test_fetch_prune_deleted_ref() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
assert_eq!(stats.import_stats.abandoned_commits, vec![jj_id(&commit)]);
Expand Down Expand Up @@ -2476,6 +2482,7 @@ fn test_fetch_no_default_branch() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();

Expand All @@ -2499,6 +2506,7 @@ fn test_fetch_no_default_branch() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
// There is no default bookmark
Expand All @@ -2520,6 +2528,7 @@ fn test_fetch_empty_refspecs() {
&[],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();
assert!(tx
Expand All @@ -2546,6 +2555,7 @@ fn test_fetch_no_such_remote() {
&[StringPattern::everything()],
git::RemoteCallbacks::default(),
&git_settings,
None,
);
assert!(matches!(result, Err(GitFetchError::NoSuchRemote(_))));
}
Expand Down

0 comments on commit 469a35c

Please sign in to comment.