Skip to content

Commit 6dde3eb

Browse files
authored
Fix more Windows paths. (#557)
1 parent 4ab58cc commit 6dde3eb

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

src/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl<'cb> RepoBuilder<'cb> {
267267
}
268268

269269
let url = CString::new(url)?;
270+
// Normal file path OK (does not need Windows conversion).
270271
let into = into.into_c_string()?;
271272
let mut raw = ptr::null_mut();
272273
unsafe {
@@ -511,6 +512,7 @@ impl<'cb> CheckoutBuilder<'cb> {
511512

512513
/// Set the directory to check out to
513514
pub fn target_dir(&mut self, dst: &Path) -> &mut CheckoutBuilder<'cb> {
515+
// Normal file path OK (does not need Windows conversion).
514516
self.target_dir = Some(dst.into_c_string().unwrap());
515517
self
516518
}

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl Config {
4646
pub fn open(path: &Path) -> Result<Config, Error> {
4747
crate::init();
4848
let mut raw = ptr::null_mut();
49+
// Normal file path OK (does not need Windows conversion).
4950
let path = path.into_c_string()?;
5051
unsafe {
5152
try_call!(raw::git_config_open_ondisk(&mut raw, path));
@@ -122,6 +123,7 @@ impl Config {
122123
/// file instances in order (instances with a higher priority level will be
123124
/// accessed first).
124125
pub fn add_file(&mut self, path: &Path, level: ConfigLevel, force: bool) -> Result<(), Error> {
126+
// Normal file path OK (does not need Windows conversion).
125127
let path = path.into_c_string()?;
126128
unsafe {
127129
try_call!(raw::git_config_add_file_ondisk(

src/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ impl DiffOptions {
828828

829829
/// Add to the array of paths/fnmatch patterns to constrain the diff.
830830
pub fn pathspec<T: IntoCString>(&mut self, pathspec: T) -> &mut DiffOptions {
831-
let s = pathspec.into_c_string().unwrap();
831+
let s = util::cstring_to_repo_path(pathspec).unwrap();
832832
self.pathspec_ptrs.push(s.as_ptr());
833833
self.pathspec.push(s);
834834
self

src/index.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::{CStr, CString, OsString};
1+
use std::ffi::{CStr, CString};
22
use std::marker;
33
use std::ops::Range;
44
use std::path::Path;
@@ -7,7 +7,7 @@ use std::slice;
77

88
use libc::{c_char, c_int, c_uint, c_void, size_t};
99

10-
use crate::util::{self, Binding};
10+
use crate::util::{self, path_to_repo_path, Binding};
1111
use crate::IntoCString;
1212
use crate::{panic, raw, Error, IndexAddOption, IndexTime, Oid, Repository, Tree};
1313

@@ -94,6 +94,7 @@ impl Index {
9494
pub fn open(index_path: &Path) -> Result<Index, Error> {
9595
crate::init();
9696
let mut raw = ptr::null_mut();
97+
// Normal file path OK (does not need Windows conversion).
9798
let index_path = index_path.into_c_string()?;
9899
unsafe {
99100
try_call!(raw::git_index_open(&mut raw, index_path));
@@ -220,15 +221,7 @@ impl Index {
220221
/// no longer be marked as conflicting. The data about the conflict will be
221222
/// moved to the "resolve undo" (REUC) section.
222223
pub fn add_path(&mut self, path: &Path) -> Result<(), Error> {
223-
// Git apparently expects '/' to be separators for paths
224-
let mut posix_path = OsString::new();
225-
for (i, comp) in path.components().enumerate() {
226-
if i != 0 {
227-
posix_path.push("/");
228-
}
229-
posix_path.push(comp.as_os_str());
230-
}
231-
let posix_path = posix_path.into_c_string()?;
224+
let posix_path = path_to_repo_path(path)?;
232225
unsafe {
233226
try_call!(raw::git_index_add_bypath(self.raw, posix_path));
234227
Ok(())
@@ -364,7 +357,7 @@ impl Index {
364357

365358
/// Get one of the entries in the index by its path.
366359
pub fn get_path(&self, path: &Path, stage: i32) -> Option<IndexEntry> {
367-
let path = path.into_c_string().unwrap();
360+
let path = path_to_repo_path(path).unwrap();
368361
unsafe {
369362
let ptr = call!(raw::git_index_get_bypath(self.raw, path, stage as c_int));
370363
if ptr.is_null() {
@@ -419,7 +412,7 @@ impl Index {
419412

420413
/// Remove an entry from the index
421414
pub fn remove(&mut self, path: &Path, stage: i32) -> Result<(), Error> {
422-
let path = path.into_c_string()?;
415+
let path = path_to_repo_path(path)?;
423416
unsafe {
424417
try_call!(raw::git_index_remove(self.raw, path, stage as c_int));
425418
}
@@ -435,7 +428,7 @@ impl Index {
435428
/// no longer be marked as conflicting. The data about the conflict will be
436429
/// moved to the "resolve undo" (REUC) section.
437430
pub fn remove_path(&mut self, path: &Path) -> Result<(), Error> {
438-
let path = path.into_c_string()?;
431+
let path = path_to_repo_path(path)?;
439432
unsafe {
440433
try_call!(raw::git_index_remove_bypath(self.raw, path));
441434
}
@@ -444,7 +437,7 @@ impl Index {
444437

445438
/// Remove all entries from the index under a given directory.
446439
pub fn remove_dir(&mut self, path: &Path, stage: i32) -> Result<(), Error> {
447-
let path = path.into_c_string()?;
440+
let path = path_to_repo_path(path)?;
448441
unsafe {
449442
try_call!(raw::git_index_remove_directory(
450443
self.raw,

src/pathspec.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ops::Range;
55
use std::path::Path;
66
use std::ptr;
77

8-
use crate::util::Binding;
8+
use crate::util::{path_to_repo_path, Binding};
99
use crate::{raw, Diff, DiffDelta, Error, Index, IntoCString, PathspecFlags, Repository, Tree};
1010

1111
/// Structure representing a compiled pathspec used for matching against various
@@ -45,6 +45,7 @@ impl Pathspec {
4545
T: IntoCString,
4646
I: IntoIterator<Item = T>,
4747
{
48+
crate::init();
4849
let (_a, _b, arr) = crate::util::iter2cstrs_paths(specs)?;
4950
unsafe {
5051
let mut ret = ptr::null_mut();
@@ -158,7 +159,7 @@ impl Pathspec {
158159
/// explicitly pass flags to control case sensitivity or else this will fall
159160
/// back on being case sensitive.
160161
pub fn matches_path(&self, path: &Path, flags: PathspecFlags) -> bool {
161-
let path = path.into_c_string().unwrap();
162+
let path = path_to_repo_path(path).unwrap();
162163
unsafe { raw::git_pathspec_matches_path(&*self.raw, flags.bits(), path.as_ptr()) == 1 }
163164
}
164165
}

src/repo.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl Repository {
6464
/// The path can point to either a normal or bare repository.
6565
pub fn open<P: AsRef<Path>>(path: P) -> Result<Repository, Error> {
6666
init();
67+
// Normal file path OK (does not need Windows conversion).
6768
let path = path.as_ref().into_c_string()?;
6869
let mut ret = ptr::null_mut();
6970
unsafe {
@@ -77,6 +78,7 @@ impl Repository {
7778
/// The path can point to only a bare repository.
7879
pub fn open_bare<P: AsRef<Path>>(path: P) -> Result<Repository, Error> {
7980
init();
81+
// Normal file path OK (does not need Windows conversion).
8082
let path = path.as_ref().into_c_string()?;
8183
let mut ret = ptr::null_mut();
8284
unsafe {
@@ -142,6 +144,7 @@ impl Repository {
142144
I: IntoIterator<Item = O>,
143145
{
144146
init();
147+
// Normal file path OK (does not need Windows conversion).
145148
let path = path.as_ref().into_c_string()?;
146149
let ceiling_dirs_os = env::join_paths(ceiling_dirs)?;
147150
let ceiling_dirs = ceiling_dirs_os.into_c_string()?;
@@ -165,6 +168,7 @@ impl Repository {
165168
// TODO: this diverges significantly from the libgit2 API
166169
init();
167170
let buf = Buf::new();
171+
// Normal file path OK (does not need Windows conversion).
168172
let path = path.as_ref().into_c_string()?;
169173
unsafe {
170174
try_call!(raw::git_repository_discover(
@@ -201,6 +205,7 @@ impl Repository {
201205
opts: &RepositoryInitOptions,
202206
) -> Result<Repository, Error> {
203207
init();
208+
// Normal file path OK (does not need Windows conversion).
204209
let path = path.as_ref().into_c_string()?;
205210
let mut ret = ptr::null_mut();
206211
unsafe {
@@ -393,6 +398,7 @@ impl Repository {
393398
/// and set config "core.worktree" (if workdir is not the parent of the .git
394399
/// directory).
395400
pub fn set_workdir(&self, path: &Path, update_gitlink: bool) -> Result<(), Error> {
401+
// Normal file path OK (does not need Windows conversion).
396402
let path = path.into_c_string()?;
397403
unsafe {
398404
try_call!(raw::git_repository_set_workdir(
@@ -856,7 +862,7 @@ impl Repository {
856862
/// directory containing the file, would it be added or not?
857863
pub fn status_should_ignore(&self, path: &Path) -> Result<bool, Error> {
858864
let mut ret = 0 as c_int;
859-
let path = path.into_c_string()?;
865+
let path = util::cstring_to_repo_path(path)?;
860866
unsafe {
861867
try_call!(raw::git_status_should_ignore(&mut ret, self.raw, path));
862868
}
@@ -950,7 +956,7 @@ impl Repository {
950956
flags: AttrCheckFlags,
951957
) -> Result<Option<&[u8]>, Error> {
952958
let mut ret = ptr::null();
953-
let path = path.into_c_string()?;
959+
let path = util::cstring_to_repo_path(path)?;
954960
let name = CString::new(name)?;
955961
unsafe {
956962
try_call!(raw::git_attr_get(
@@ -991,6 +997,7 @@ impl Repository {
991997
/// The Oid returned can in turn be passed to `find_blob` to get a handle to
992998
/// the blob.
993999
pub fn blob_path(&self, path: &Path) -> Result<Oid, Error> {
1000+
// Normal file path OK (does not need Windows conversion).
9941001
let path = path.into_c_string()?;
9951002
let mut raw = raw::git_oid {
9961003
id: [0; raw::GIT_OID_RAWSZ],
@@ -1545,7 +1552,7 @@ impl Repository {
15451552
use_gitlink: bool,
15461553
) -> Result<Submodule<'_>, Error> {
15471554
let url = CString::new(url)?;
1548-
let path = path.into_c_string()?;
1555+
let path = path_to_repo_path(path)?;
15491556
let mut raw = ptr::null_mut();
15501557
unsafe {
15511558
try_call!(raw::git_submodule_add_setup(
@@ -2069,7 +2076,7 @@ impl Repository {
20692076
path: &Path,
20702077
opts: Option<&mut BlameOptions>,
20712078
) -> Result<Blame<'_>, Error> {
2072-
let path = path.into_c_string()?;
2079+
let path = path_to_repo_path(path)?;
20732080
let mut raw = ptr::null_mut();
20742081

20752082
unsafe {
@@ -2800,12 +2807,13 @@ impl RepositoryInitOptions {
28002807
self
28012808
}
28022809

2803-
/// The path do the working directory.
2810+
/// The path to the working directory.
28042811
///
28052812
/// If this is a relative path it will be evaulated relative to the repo
28062813
/// path. If this is not the "natural" working directory, a .git gitlink
28072814
/// file will be created here linking to the repo path.
28082815
pub fn workdir_path(&mut self, path: &Path) -> &mut RepositoryInitOptions {
2816+
// Normal file path OK (does not need Windows conversion).
28092817
self.workdir_path = Some(path.into_c_string().unwrap());
28102818
self
28112819
}
@@ -2823,6 +2831,7 @@ impl RepositoryInitOptions {
28232831
/// If this is not configured, then the default locations will be searched
28242832
/// instead.
28252833
pub fn template_path(&mut self, path: &Path) -> &mut RepositoryInitOptions {
2834+
// Normal file path OK (does not need Windows conversion).
28262835
self.template_path = Some(path.into_c_string().unwrap());
28272836
self
28282837
}

0 commit comments

Comments
 (0)