fix: git (write index to file)

This commit is contained in:
qwjyh 2023-08-28 21:45:05 +09:00
parent db09897b5a
commit 4bacba7ff9

View file

@ -242,11 +242,13 @@ fn main() -> Result<()> {
Path::new(".gitignore"), Path::new(".gitignore"),
"Add devname to gitignore.", "Add devname to gitignore.",
)?; )?;
full_status(&repo)?;
} }
is_first_device = true; is_first_device = true;
repo repo
} }
}; };
full_status(&repo)?;
// set device name // set device name
let device = set_device_name()?; let device = set_device_name()?;
@ -258,6 +260,7 @@ fn main() -> Result<()> {
let writer = BufWriter::new(f); let writer = BufWriter::new(f);
serde_yaml::to_writer(writer, &device.name).unwrap(); serde_yaml::to_writer(writer, &device.name).unwrap();
}; };
full_status(&repo)?;
// Add new device to devices.yml // Add new device to devices.yml
{ {
@ -273,6 +276,7 @@ fn main() -> Result<()> {
trace!("Devices: {:?}", devices); trace!("Devices: {:?}", devices);
write_devices(&config_dir, devices)?; write_devices(&config_dir, devices)?;
} }
full_status(&repo)?;
// commit // commit
add_and_commit( add_and_commit(
@ -280,7 +284,8 @@ fn main() -> Result<()> {
&Path::new(DEVICESFILE), &Path::new(DEVICESFILE),
&format!("Add new devname: {}", &device.name), &format!("Add new devname: {}", &device.name),
)?; )?;
} full_status(&repo)?;
},
Commands::Storage(storage) => match storage.command { Commands::Storage(storage) => match storage.command {
StorageCommands::Add { storage_type } => { StorageCommands::Add { storage_type } => {
trace!("Storage Add {:?}", storage_type); trace!("Storage Add {:?}", storage_type);
@ -332,11 +337,12 @@ fn main() -> Result<()> {
}, },
Commands::Path {} => { Commands::Path {} => {
println!("{}", &config_dir.display()); println!("{}", &config_dir.display());
} },
Commands::Sync {} => { Commands::Sync {} => {
unimplemented!("Sync is not implemented") unimplemented!("Sync is not implemented")
} },
} }
full_status(&Repository::open(&config_dir)?)?;
Ok(()) Ok(())
} }
@ -516,9 +522,13 @@ fn find_last_commit(repo: &Repository) -> Result<Option<Commit>, git2::Error> {
/// Add file and commit /// Add file and commit
fn add_and_commit(repo: &Repository, path: &Path, message: &str) -> Result<Oid, git2::Error> { fn add_and_commit(repo: &Repository, path: &Path, message: &str) -> Result<Oid, git2::Error> {
trace!("repo state: {:?}", repo.state()); trace!("repo state: {:?}", repo.state());
full_status(repo).unwrap();
let mut index = repo.index()?; let mut index = repo.index()?;
index.add_path(path)?; index.add_path(path)?;
full_status(repo).unwrap();
index.write()?;
let oid = index.write_tree()?; let oid = index.write_tree()?;
let tree = repo.find_tree(oid)?;
let config = git2::Config::open_default()?; let config = git2::Config::open_default()?;
let signature = git2::Signature::now( let signature = git2::Signature::now(
config.get_entry("user.name")?.value().unwrap(), config.get_entry("user.name")?.value().unwrap(),
@ -526,7 +536,6 @@ fn add_and_commit(repo: &Repository, path: &Path, message: &str) -> Result<Oid,
)?; )?;
trace!("git signature: {}", signature); trace!("git signature: {}", signature);
let parent_commit = find_last_commit(&repo)?; let parent_commit = find_last_commit(&repo)?;
let tree = repo.find_tree(oid)?;
let result = match parent_commit { let result = match parent_commit {
Some(parent_commit) => repo.commit( Some(parent_commit) => repo.commit(
Some("HEAD"), Some("HEAD"),
@ -539,5 +548,17 @@ fn add_and_commit(repo: &Repository, path: &Path, message: &str) -> Result<Oid,
None => repo.commit(Some("HEAD"), &signature, &signature, message, &tree, &[]), None => repo.commit(Some("HEAD"), &signature, &signature, message, &tree, &[]),
}; };
trace!("repo state: {:?}", repo.state()); trace!("repo state: {:?}", repo.state());
full_status(repo).unwrap();
result result
} }
/// Print git repo status
fn full_status(repo: &Repository) -> Result<()> {
trace!("status: ");
for status in repo.statuses(None)?.iter() {
let path = status.path().unwrap_or("");
let st = status.status();
trace!(" {}: {:?}", path, st);
}
Ok(())
}