make the alternates path writing more robust

This commit is contained in:
Josh Junon 2024-03-14 10:37:52 +01:00 committed by GitButler
parent c2b4087e93
commit 3d475b7a3c

View File

@ -1,9 +1,13 @@
use std::{path, str}; use std::{
io::Write,
path::{self, Path},
str,
};
use git2::Submodule; use git2::Submodule;
use git2_hooks::HookResult; use git2_hooks::HookResult;
use crate::keys; use crate::{keys, path::Normalize};
use super::{ use super::{
Blob, Branch, Commit, Config, Index, Oid, Reference, Refname, Remote, Result, Signature, Tree, Blob, Branch, Commit, Config, Index, Oid, Reference, Refname, Remote, Result, Signature, Tree,
@ -50,10 +54,13 @@ impl Repository {
Ok(Repository(inner)) Ok(Repository(inner))
} }
pub fn add_disk_alternate(&self, path: &str) -> Result<()> { pub fn add_disk_alternate<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let alternates_path = self.0.path().join("objects/info/alternates"); let alternates_path = self.0.path().join("objects/info/alternates");
if !alternates_path.exists() { if !alternates_path.exists() {
std::fs::write(alternates_path, format!("{path}\n"))?; let path = path.as_ref().normalize();
let mut alternates_file = std::fs::File::create(&alternates_path)?;
alternates_file.write_all(path.as_path().as_os_str().as_encoded_bytes())?;
alternates_file.write_all(b"\n")?;
self.0.odb().and_then(|odb| odb.refresh())?; self.0.odb().and_then(|odb| odb.refresh())?;
} }