Pathspec support for magic words

Aperta
#1,132 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
git, rust
Ambito
tooling

Direzione di ricerca

Inizia eseguendo l’esempio fornito con git2-rs e confronta DiffOptions::pathspec con repo.diff_tree_to_tree per i magic-word pathspecs. Segui il modo in cui la Rust API passa i pathspecs a libgit2, quindi verifica che l’esempio documentato di exclude e l’esempio misto di include/exclude producano gli elenchi di file previsti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

upstream

Hi all, I'm using git2-rs's diff API interface and I'm trying to filter out some paths using pathspec.

From Git's user manual, pathspecs should support "magic words", which would allow me to write something like this :(exclude)*.toml to exclude all toml files when computing the diff. This is important to me because it allows to save a lot of computation time wasted on files I do not care about (and have to filter out manually after the diff).

When using Git, this works as expected, however, I can't replicate the same behaviour using git2-rs from my Rust application.

Any idea on how to workaround on this issue? Am I using the APIs wrongly? Or even pointers on how to implement this in the library (I'm a newbie to Rust, but I might give it a try...).

Here's an example Rust application that showcases the problem.

use std::{env::args, error::Error};

use git2::{DiffFindOptions, DiffOptions, ObjectType, Repository};

pub fn diff(
    repo: &Repository,
    parent: &str,
    child: &str,
    pathspecs: &[String],
) -> Result<(), git2::Error> {
    let parent = repo.revparse_single(parent)?;
    let child = repo.revparse_single(child)?;
    let p_obj = parent.peel(ObjectType::Tree)?;
    let c_obj = child.peel(ObjectType::Tree)?;
    let p_tree = p_obj.as_tree().unwrap();
    let c_tree = c_obj.as_tree().unwrap();
    let mut rename_opts = DiffFindOptions::new();
    rename_opts.renames(true);
    rename_opts.rename_limit(200);
    rename_opts.renames_from_rewrites(true);
    let mut opts = DiffOptions::new();
    opts.update_index(false);
    opts.context_lines(0);
    for p in pathspecs {
        opts.pathspec(p);
    }
    let mut diff = repo.diff_tree_to_tree(Some(p_tree), Some(c_tree), Some(&mut opts))?;
    diff.find_similar(Some(&mut rename_opts))?;
    for delta in diff.deltas() {
        println!("{}", delta.old_file().path().unwrap().to_str().unwrap());
    }
    Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut args = args();
    if args.len() <= 3 {
        println!("Usage: <repo> <sha> <sha> [pathspec...]");
        return Err("Wrong number of arguments".into());
    }
    args.next();
    let repo = args.next().expect("Repository must be provided");
    let sha_parent = args.next().expect("Parent sha must be provided");
    let sha_child = args.next().expect("Child sha must be provided");
    let pathspecs = args.collect::<Vec<String>>();
    let repo = Repository::open(repo)?;
    diff(&repo, &sha_parent, &sha_child, pathspecs.as_slice())?;
    Ok(())
}

To illustrate the problem, you can follow these steps (after setting up the sample cargo app above):

  1. Run Git on an example repository
git clone https://github.com/rust-lang/git2-rs /tmp/git2-rs
git -C /tmp/git2-rs/ diff --name-only d1ae3b6c 7b7be439 ":(exclude)*.toml"

Which outputs

libgit2-sys/lib.rs
src/repo.rs

Note that also Cargo.toml and git2-curl/Cargo.toml have changed, but they are not part of the diff.

  1. Run sample app
cargo run -- /tmp/git2-rs/ d1ae3b6c 7b7be439 ":(exclude)*.toml"

No output is display at all if you provide a pathspec with any magic word. In this case, the expected output is the two .rs files seen above.

Morover, combinations like these seem to fail too:

cargo run -- /tmp/git2-rs/ d1ae3b6c 7b7be439 "*.toml" ":(exclude)*.toml"
# Outputs
Cargo.toml
git2-curl/Cargo.toml

Git correctly prints out nothing.

Thank you! Looking forward to your answers :)

Lingua principale
Rust
Stelle
2.1k
Fork
470
Merge medio
2g 17h
PR unite (30g)
18

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/git2-rs

Tutte le issue di rust-lang/git2-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.