Initialize repo if fetch url is configured
This commit is contained in:
parent
546de29706
commit
5ca8fee66a
2 changed files with 89 additions and 6 deletions
42
src/git.rs
42
src/git.rs
|
|
@ -6,7 +6,8 @@ use std::{
|
|||
process::{Command, Output},
|
||||
};
|
||||
|
||||
use log::trace;
|
||||
use log::{trace, warn};
|
||||
use regex::bytes::Regex;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
|
|
@ -62,6 +63,45 @@ fn run(mut command: Command) -> Result<Output, Error> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn init_bare(path: &Path) -> Result<(), Error> {
|
||||
let mut command = Command::new("git");
|
||||
command.arg("init").arg("--bare").arg("--").arg(path);
|
||||
run(command)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn fetch_head(path: &Path, url: &str) -> Result<(), Error> {
|
||||
let mut command = Command::new("git");
|
||||
command
|
||||
.arg("-C")
|
||||
.arg(path)
|
||||
.arg("ls-remote")
|
||||
.arg("--symref")
|
||||
.arg("--")
|
||||
.arg(url)
|
||||
.arg("HEAD"); // Includes other refs like refs/foo/HEAD
|
||||
let output = run(command)?;
|
||||
|
||||
let regex = Regex::new(r"(?m)^ref: (refs/\S+)\s+HEAD$").unwrap();
|
||||
let Some(captures) = regex.captures(&output.stdout) else {
|
||||
warn!("Did not find HEAD of {url}");
|
||||
return Ok(());
|
||||
};
|
||||
let head = String::from_utf8_lossy(captures.get(1).unwrap().as_bytes());
|
||||
|
||||
let mut command = Command::new("git");
|
||||
command
|
||||
.arg("-C")
|
||||
.arg(path)
|
||||
.arg("symbolic-ref")
|
||||
.arg("--")
|
||||
.arg("HEAD")
|
||||
.arg(&head as &str);
|
||||
run(command)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn fetch(path: &Path, url: &str, refspecs: &[String]) -> Result<(), Error> {
|
||||
let mut command = Command::new("git");
|
||||
command
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue