Skip to content

Commit 478bc5b

Browse files
author
K
committed
Improvements
1 parent 0736d2c commit 478bc5b

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

libgit2-sys/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ pub struct git_merge_file_input {
13411341
/// File name of the conflicted file, or `NULL` to not merge the path.
13421342
pub path: *const c_char,
13431343
/// File mode of the conflicted file, or `0` to not merge the mode.
1344-
pub mode: c_uint,
1344+
pub mode: git_filemode_t,
13451345
}
13461346

13471347
#[repr(C)]
@@ -1356,7 +1356,7 @@ pub struct git_merge_file_result {
13561356
pub path: *const c_char,
13571357

13581358
/// The mode that the resultant merge file should use.
1359-
pub mode: c_uint,
1359+
pub mode: git_filemode_t,
13601360

13611361
/// The contents of the merge.
13621362
pub ptr: *const c_char,

src/call.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod impls {
5757
use libc;
5858

5959
use crate::call::Convert;
60-
use crate::{raw, BranchType, ConfigLevel, Direction, ObjectType, ResetType};
60+
use crate::{raw, BranchType, ConfigLevel, Direction, FileMode, ObjectType, ResetType};
6161
use crate::{AutotagOption, DiffFormat, FetchPrune, FileFavor, SubmoduleIgnore};
6262

6363
impl<T: Copy> Convert<T> for T {
@@ -229,4 +229,17 @@ mod impls {
229229
}
230230
}
231231
}
232+
233+
impl Convert<raw::git_filemode_t> for FileMode {
234+
fn convert(&self) -> raw::git_filemode_t {
235+
match *self {
236+
FileMode::Unreadable => raw::GIT_FILEMODE_UNREADABLE,
237+
FileMode::Tree => raw::GIT_FILEMODE_TREE,
238+
FileMode::Blob => raw::GIT_FILEMODE_BLOB,
239+
FileMode::BlobExecutable => raw::GIT_FILEMODE_BLOB_EXECUTABLE,
240+
FileMode::Link => raw::GIT_FILEMODE_LINK,
241+
FileMode::Commit => raw::GIT_FILEMODE_COMMIT,
242+
}
243+
}
244+
}
232245
}

src/lib.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -1052,22 +1052,9 @@ pub enum FileMode {
10521052
}
10531053

10541054
impl FileMode {
1055-
#[cfg(target_os = "windows")]
1056-
fn from(mode: i32) -> Self {
1057-
match mode {
1058-
raw::GIT_FILEMODE_UNREADABLE => FileMode::Unreadable,
1059-
raw::GIT_FILEMODE_TREE => FileMode::Tree,
1060-
raw::GIT_FILEMODE_BLOB => FileMode::Blob,
1061-
raw::GIT_FILEMODE_BLOB_EXECUTABLE => FileMode::BlobExecutable,
1062-
raw::GIT_FILEMODE_LINK => FileMode::Link,
1063-
raw::GIT_FILEMODE_COMMIT => FileMode::Commit,
1064-
mode => panic!("unknown file mode: {}", mode),
1065-
}
1066-
}
1067-
1068-
#[cfg(not(target_os = "windows"))]
1069-
fn from(mode: u32) -> Self {
1070-
match mode {
1055+
/// Convert git_filemode_t to FileMode
1056+
pub fn from_raw(raw: raw::git_filemode_t) -> Self {
1057+
match raw {
10711058
raw::GIT_FILEMODE_UNREADABLE => FileMode::Unreadable,
10721059
raw::GIT_FILEMODE_TREE => FileMode::Tree,
10731060
raw::GIT_FILEMODE_BLOB => FileMode::Blob,

src/merge.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::call::Convert;
77
use crate::util::Binding;
88
use crate::{raw, Commit, FileFavor, FileMode, IntoCString, Oid};
99
use core::{ptr, slice};
10-
use std::convert::TryInto;
1110
use std::ffi::{CStr, CString};
1211

1312
/// A structure to represent an annotated commit, the input to merge and rebase.
@@ -346,7 +345,7 @@ impl<'a> MergeFileInput<'a> {
346345
/// File mode of the conflicted file, or `0` to not merge the mode.
347346
pub fn mode(&mut self, mode: Option<FileMode>) -> &mut MergeFileInput<'a> {
348347
if let Some(mode) = mode {
349-
self.raw.mode = mode as u32;
348+
self.raw.mode = mode as raw::git_filemode_t;
350349
}
351350

352351
self
@@ -378,9 +377,9 @@ impl std::fmt::Debug for MergeFileInput<'_> {
378377
if let Some(path) = &self.path {
379378
ds.field("path", path);
380379
}
381-
ds.field("mode", &FileMode::from(self.raw.mode.try_into().unwrap()));
380+
ds.field("mode", &FileMode::from_raw(self.raw.mode));
382381

383-
match FileMode::from(self.raw.mode.try_into().unwrap()) {
382+
match FileMode::from_raw(self.raw.mode) {
384383
FileMode::Unreadable => {}
385384
FileMode::Tree => {}
386385
FileMode::Blob => {
@@ -428,7 +427,7 @@ impl MergeFileResult {
428427

429428
/// The mode that the resultant merge file should use.
430429
pub fn mode(&self) -> FileMode {
431-
FileMode::from(self.raw.mode.try_into().unwrap())
430+
FileMode::from_raw(self.raw.mode)
432431
}
433432

434433
/// The contents of the merge.

src/repo.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3072,7 +3072,6 @@ mod tests {
30723072
use crate::build::CheckoutBuilder;
30733073
use crate::{CherrypickOptions, FileMode, MergeFileInput};
30743074
use crate::{ObjectType, Oid, Repository, ResetType};
3075-
use std::convert::TryInto;
30763075
use std::ffi::OsStr;
30773076
use std::fs;
30783077
use std::io::Write;
@@ -3443,7 +3442,7 @@ mod tests {
34433442
let ancestor_content = ancestor_blob.content();
34443443
let mut input = MergeFileInput::new();
34453444
input.path(String::from_utf8(ancestor.path).unwrap());
3446-
input.mode(Some(FileMode::from(ancestor.mode.try_into().unwrap())));
3445+
input.mode(Some(FileMode::from_raw(ancestor.mode)));
34473446
input.content(Some(&ancestor_content));
34483447
ancestor_input = Some(input);
34493448
} else {
@@ -3456,7 +3455,7 @@ mod tests {
34563455
let ours_content = ours_blob.content();
34573456
let mut input = MergeFileInput::new();
34583457
input.path(String::from_utf8(ours.path).unwrap());
3459-
input.mode(Some(FileMode::from(ours.mode.try_into().unwrap())));
3458+
input.mode(Some(FileMode::from_raw(ours.mode)));
34603459
input.content(Some(&ours_content));
34613460
ours_input = Some(input);
34623461
} else {
@@ -3469,7 +3468,7 @@ mod tests {
34693468
let theirs_content = theirs_blob.content();
34703469
let mut input = MergeFileInput::new();
34713470
input.path(String::from_utf8(theirs.path).unwrap());
3472-
input.mode(Some(FileMode::from(theirs.mode.try_into().unwrap())));
3471+
input.mode(Some(FileMode::from_raw(theirs.mode)));
34733472
input.content(Some(&theirs_content));
34743473
theirs_input = Some(input);
34753474
} else {

0 commit comments

Comments
 (0)