Created
February 21, 2025 17:43
-
-
Save Mathspy/2a1c07d5e290ca517eea9b772127c30c to your computer and use it in GitHub Desktop.
Use gitoxide to get a specific file out of a repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn get_file_from_repo(remote: &str, branch: &str, path: &str) -> String { | |
let tempdir = TempDir::new("why-am-i-like-this").expect("tempdir can be created"); | |
let repo = gix::init_bare(tempdir.path()).expect("can init repo"); | |
let remote = repo | |
.remote_at(remote) | |
.expect("remote can be created") | |
.with_fetch_tags(remote::fetch::Tags::None) | |
.with_refspecs( | |
[format!("+refs/heads/{branch}:refs/heads/{branch}").as_str()], | |
Direction::Fetch, | |
) | |
.expect("refspecs can be added"); | |
let connection = remote | |
.connect(Direction::Fetch) | |
.expect("can connect to remote"); | |
let prepared_fetch = connection | |
.prepare_fetch(progress::Discard, remote::ref_map::Options::default()) | |
.expect("preparing fetch succeeds") | |
.with_shallow(remote::fetch::Shallow::DepthAtRemote( | |
NonZero::new(1).unwrap(), | |
)); | |
let outcome = prepared_fetch | |
.receive(progress::Discard, &AtomicBool::new(false)) | |
.expect("fetch succeeds"); | |
if matches!(outcome.status, remote::fetch::Status::NoPackReceived { .. }) { | |
panic!("received nothing") | |
} | |
let commit = repo.head_commit().expect("head exists"); | |
let tree = commit.tree().expect("can parse commit into tree"); | |
let entry = tree | |
.lookup_entry_by_path(path) | |
.expect("can find entry by path") | |
.expect("entry to exist"); | |
let mut object = entry.object().expect("entry relates to an object"); | |
String::from_utf8(std::mem::take(&mut object.data)).expect("all files are utf8 totally!!!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At least
["blocking-network-client", "blocking-http-transport-curl"]
features are required (for http transport)