git2-rs icon indicating copy to clipboard operation
git2-rs copied to clipboard

path matching inconsistent error

Open lxl66566 opened this issue 1 year ago • 0 comments

Unexpected behavior happened when using get_attr_bytes. Here's a minimal example:

in src/main.rs:

use git2::{AttrCheckFlags, Repository};
use std::path::PathBuf;
use std::process::Command;
fn main() {
    let repo = Repository::open(".").unwrap();
    let git2_get = |file| {
        repo.get_attr_bytes(file, "crypt", AttrCheckFlags::default())
            .unwrap()
            .map(String::from_utf8_lossy)
    };
    let file = PathBuf::from(".").join("src").join("main.rs");
    let file2 = file.clone();
    dbg!(&file, git2_get(&file));
    let file = PathBuf::from(&file.to_str().unwrap().replace('\\', "/"));
    dbg!(&file, git2_get(&file));
    let file = PathBuf::from(&file.to_str().unwrap().trim_start_matches("./"));
    dbg!(&file, git2_get(&file));
    let file2 = PathBuf::from(&file2.to_str().unwrap().trim_start_matches(".\\"));
    dbg!(&file2, git2_get(&file2));
    let cli = Command::new("git")
        .args(["check-attr", "crypt", "--", "./src/main.rs"])
        .output()
        .unwrap()
        .stdout;
    dbg!(String::from_utf8_lossy(&cli));
}

in .gitattributes:

src/** crypt=1

output:

[src/main.rs:13:5] &file = ".\\src\\main.rs"
[src/main.rs:13:5] git2_get(&file) = None
[src/main.rs:15:5] &file = "./src/main.rs"
[src/main.rs:15:5] git2_get(&file) = None
[src/main.rs:17:5] &file = "src/main.rs"
[src/main.rs:17:5] git2_get(&file) = Some(
    "1",
)
[src/main.rs:19:5] &file2 = "src\\main.rs"
[src/main.rs:19:5] git2_get(&file2) = Some(
    "1",
)
[src/main.rs:25:5] String::from_utf8_lossy(&cli) = "./src/main.rs: crypt: 1\n"

Seems like the starting ./ caused this inconsistency. I expect that no matter whether the starting ./ exists, the output should be the same.

additional infomation

system: Windows11 rustc: 1.79.0-nightly (ccfcd950b 2024-04-15) git2 = "0.18.3"

lxl66566 avatar May 03 '24 06:05 lxl66566