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

fixes for paths on Windows #4188

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ maplit = "1.0.2"
minus = { version = "5.6.1", features = ["dynamic_output", "search"] }
num_cpus = "1.16.0"
once_cell = "1.19.0"
path-slash = "0.2.1"
pest = "2.7.11"
pest_derive = "2.7.11"
pollster = "0.3.0"
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jj-lib = { workspace = true }
maplit = { workspace = true }
minus = { workspace = true }
once_cell = { workspace = true }
path-slash = { workspace = true }
pest = { workspace = true }
pest_derive = { workspace = true }
pollster = { workspace = true }
Expand Down
21 changes: 2 additions & 19 deletions cli/src/commands/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::{fs, io};

use jj_lib::git::{self, GitFetchError, GitFetchStats};
use jj_lib::git::{self, sanitize_git_url_if_path, GitFetchError, GitFetchStats};
use jj_lib::repo::Repo;
use jj_lib::str_util::StringPattern;
use jj_lib::workspace::Workspace;
Expand Down Expand Up @@ -47,23 +47,6 @@ pub struct GitCloneArgs {
colocate: bool,
}

fn absolute_git_source(cwd: &Path, source: &str) -> String {
// Git appears to turn URL-like source to absolute path if local git directory
// exits, and fails because '$PWD/https' is unsupported protocol. Since it would
// be tedious to copy the exact git (or libgit2) behavior, we simply assume a
// source containing ':' is a URL, SSH remote, or absolute path with Windows
// drive letter.
if !source.contains(':') && Path::new(source).exists() {
// It's less likely that cwd isn't utf-8, so just fall back to original source.
cwd.join(source)
.into_os_string()
.into_string()
.unwrap_or_else(|_| source.to_owned())
} else {
source.to_owned()
}
}

fn clone_destination_for_source(source: &str) -> Option<&str> {
let destination = source.strip_suffix(".git").unwrap_or(source);
let destination = destination.strip_suffix('/').unwrap_or(destination);
Expand All @@ -89,7 +72,7 @@ pub fn cmd_git_clone(
return Err(cli_error("--at-op is not respected"));
}
let remote_name = "origin";
let source = absolute_git_source(command.cwd(), &args.source);
let source = sanitize_git_url_if_path(command.cwd(), &args.source);
let wc_path_str = args
.destination
.as_deref()
Expand Down
8 changes: 6 additions & 2 deletions cli/src/commands/git/remote/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jj_lib::git;
use jj_lib::git::{self, sanitize_git_url_if_path};
use jj_lib::repo::Repo;

use crate::cli_util::CommandHelper;
Expand All @@ -37,6 +37,10 @@ pub fn cmd_git_remote_add(
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
git::add_remote(&git_repo, &args.remote, &args.url)?;
git::add_remote(
&git_repo,
&args.remote,
&sanitize_git_url_if_path(command.cwd(), &args.url),
)?;
Ok(())
}
2 changes: 1 addition & 1 deletion cli/src/commands/git/remote/set_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ pub fn cmd_git_remote_set_url(
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
git::set_remote_url(&git_repo, &args.remote, &args.url)?;
git::set_remote_url(&git_repo, &args.remote, &args.url, command.cwd())?;
Ok(())
}
17 changes: 13 additions & 4 deletions cli/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

use std::cell::RefCell;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use itertools::Itertools as _;
use path_slash::PathBufExt;
use regex::{Captures, Regex};
use tempfile::TempDir;

Expand Down Expand Up @@ -53,7 +55,7 @@ impl Default for TestEnvironment {
testutils::hermetic_libgit2();

let tmp_dir = testutils::new_temp_dir();
let env_root = tmp_dir.path().canonicalize().unwrap();
let env_root = dunce::canonicalize(tmp_dir.path()).unwrap();
let home_dir = env_root.join("home");
std::fs::create_dir(&home_dir).unwrap();
let config_dir = env_root.join("config");
Expand Down Expand Up @@ -320,13 +322,20 @@ impl TestEnvironment {
pub fn normalize_output(&self, text: &str) -> String {
let text = text.replace("jj.exe", "jj");
let regex = Regex::new(&format!(
r"{}(\S+)",
regex::escape(&self.env_root.display().to_string())
r"({}|{}|{})(\S+)",
regex::escape(&self.env_root.display().to_string()),
regex::escape(&self.env_root.to_slash_lossy()),
regex::escape(
&fs::canonicalize(&self.env_root)
.unwrap()
.display()
.to_string()
)
))
.unwrap();
regex
.replace_all(&text, |caps: &Captures| {
format!("$TEST_ENV{}", caps[1].replace('\\', "/"))
format!("$TEST_ENV{}", caps[2].replace('\\', "/"))
})
.to_string()
}
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ itertools = { workspace = true }
jj-lib-proc-macros = { workspace = true }
maplit = { workspace = true }
once_cell = { workspace = true }
path-slash = { workspace = true }
pest = { workspace = true }
pest_derive = { workspace = true }
pollster = { workspace = true }
Expand Down
38 changes: 36 additions & 2 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::default::Default;
use std::io::Read;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{fmt, str};

use git2::Oid;
use itertools::Itertools;
use path_slash::PathBufExt;
use tempfile::NamedTempFile;
use thiserror::Error;

Expand Down Expand Up @@ -1143,10 +1144,43 @@ pub fn rename_remote(
Ok(())
}

fn looks_like_a_path_to_git(source: &str) -> bool {
source.chars().nth(1) == Some(':') || // Looks like a Windows C:... path
// Match Git's behavior, the URL syntax [is only recognized if there are no
// slashes before the first
// colon](https://git-scm.com/docs/git-clone#_git_urls)
!source
.split_once("/")
.map_or(source, |(first, _)| first)
.contains(":")
}

pub fn sanitize_git_url_if_path(cwd: &Path, source: &str) -> String {
// Git appears to turn URL-like source to absolute path if local git directory
// exits, and fails because '$PWD/https' is unsupported protocol. Since it would
// be tedious to copy the exact git (or libgit2) behavior, we simply assume a
// source containing ':' is a URL, SSH remote, or absolute path with Windows
// drive letter.
if looks_like_a_path_to_git(source) && Path::new(source).exists() {
// TODO: This won't work for Windows UNC path or (less importantly) if the
// original source is a non-UTF Windows path. For Windows UNC paths, we
// could use dunce, though see also https://gitlab.com/kornelski/dunce/-/issues/7
// TODO: double-check about UNC paths; what does Git do?
cwd.join(source).to_slash().map_or_else(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply do replace MAIN_SEPARATOR to slash (if cfg!(windows))? path-slash might be optimized if we have to deal with Path/OsStr, but here we build String anyway.

Copy link
Collaborator Author

@ilyagr ilyagr Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on my list to try and see if it makes things saner (that is, makes it easier to make the merge with #4080 work).

My main worry (and the reason I didn't start with search-and-replace) is about UNC paths, especially the ones appearing in our tests. I would have to find out experimentally whether I can get rid of them via dunce, and/or I'd have to find out how libgit2 deals with paths like:

  • \\?\C:/Path/
  • //?/C:/Path/

Neither of these is correct according to Microsoft, but I think libgit2 definitely chokes on the correct \\?\C:\Path\ and I have a mild suspicion that it might prefer one f the incorrect options. I wish I knew what Git did; I think we'd have to ask or read the source to find out.

(As for dependency on path-slash, that library hasn't been updated in years and it's probably pretty simple, so we could re-implement it if needed)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like dunce::simplified(..).replace(MAIN_SEPARATOR, "/") is good as a workaround. It might not work if the path couldn't be simplified, but that wouldn't be worse than the current state.

// It's less likely that cwd isn't utf-8, so just fall back to original source.
|| source.to_owned(),
|s| s.to_string(),
)
} else {
source.to_owned()
}
}

pub fn set_remote_url(
git_repo: &git2::Repository,
remote_name: &str,
new_remote_url: &str,
cwd: &Path,
) -> Result<(), GitRemoteManagementError> {
if remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO {
return Err(GitRemoteManagementError::RemoteReservedForLocalGitRepo);
Expand All @@ -1164,7 +1198,7 @@ pub fn set_remote_url(
})?;

git_repo
.remote_set_url(remote_name, new_remote_url)
.remote_set_url(remote_name, &sanitize_git_url_if_path(cwd, new_remote_url))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this should be handled by CLI because it's UI issue to resolve relative path.

(BTW, I think it's good idea to move the path conversion function to library.)

.map_err(GitRemoteManagementError::InternalGitError)?;
Ok(())
}
Expand Down
Loading