Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Created April 19, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurpleBooth/8ff1b86f9a6d148b82f512059bb34059 to your computer and use it in GitHub Desktop.
Save PurpleBooth/8ff1b86f9a6d148b82f512059bb34059 to your computer and use it in GitHub Desktop.
Reading git config from global and the containing repo in rust
#!/usr/bin/env run-cargo-script
//! This is a regular crate doc comment, but it also contains a partial
//! Cargo manifest. Note the use of a *fenced* code block, and the
//! `cargo` "language".
//!
//! ```cargo
//! [dependencies]
//! git2 = "0.13"
//! ```
extern crate git2;
use git2::{Repository,Config};
use std::env;
fn main() {
let repo = match Repository::discover(env::current_dir().unwrap()) {
Ok(repo) => {
let cfg = repo.config().unwrap();
for entry in &cfg.entries(None).unwrap() {
let entry = entry.unwrap();
println!("{} => {}", entry.name().unwrap(), entry.value().unwrap());
}
},
Err(e) => panic!("failed to init: {}", e),
};
println!("-------");
let cfg = Config::open_default().unwrap();
for entry in &cfg.entries(None).unwrap() {
let entry = entry.unwrap();
println!("{} => {}", entry.name().unwrap(), entry.value().unwrap());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment