summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/lib.rs b/src/lib.rs
index ecc202f..1cf0a98 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,9 @@
/*
+ * git-cat-autosave:
+ * - select an autosave
+ * - create a tree that merges autosave and workdir
+ * - show the diff of tree with workdir
+ *
* git-merge-autosave:
* - error is workdir is dirty
* - check default merge strategy
@@ -29,6 +34,13 @@ pub use config::Config;
use utils::TreeError;
+pub const AUTOSAVE_ID_CONFIG_KEY: &str = "autosave.id";
+pub const AUTOSAVE_REF_NAMESPACE: &str = "refs/autosave/";
+pub const AUTOSAVE_ENTRIES_NAMESPACE: &str = "refs/autosave/autosaves/";
+pub const AUTOSAVE_REFSPEC: &str = "+refs/autosave/autosaves/*:refs/autosave/autosaves/*";
+pub const UNDO_RESTORE_REF: &str = "refs/autosave/undo";
+pub const RESTORED_AUTOSAVE_REF: &str = "refs/autosave/restored";
+
pub struct Autosave {
pub repo_id: String,
pub branch_name: String,
@@ -57,7 +69,7 @@ impl TryFrom<&Reference<'_>> for Autosave {
"Reference is not valid UTF-8",
))?;
let reference_name = reference_name
- .strip_prefix("refs/autosave/autosaves/")
+ .strip_prefix(AUTOSAVE_ENTRIES_NAMESPACE)
.unwrap_or(reference_name);
let Some((id, branch)) = reference_name.split_once('/') else {
return Err(git2::Error::new(
@@ -89,7 +101,7 @@ impl TryFrom<&Reference<'_>> for Autosave {
pub fn repository_id(repository: &Repository) -> Result<String, git2::Error> {
repository
.config()?
- .get_entry("autosave.id")?
+ .get_entry(AUTOSAVE_ID_CONFIG_KEY)?
.value()
.map(|s| s.to_string())
.ok_or(git2::Error::new(
@@ -104,7 +116,7 @@ pub fn init(repository: &Repository, config: Option<&mut Config>) -> Result<Uuid
let workdir = repository.workdir();
repository
.config()?
- .set_str("autosave.id", &id.to_string())?;
+ .set_str(AUTOSAVE_ID_CONFIG_KEY, &id.to_string())?;
if let Some(config) = config
&& let Some(workdir) = workdir
@@ -116,12 +128,10 @@ pub fn init(repository: &Repository, config: Option<&mut Config>) -> Result<Uuid
}
pub fn commit_autosave(repository: &Repository) -> Result<Oid, TreeError> {
- let config = repository.config()?;
let head = repository.head()?;
- let uid = config.get_entry("autosave.id")?;
- let uid = String::from_utf8_lossy(uid.value_bytes());
+ let uid = repository_id(repository)?;
let branch = utils::current_branch(repository)?;
- let refname = &format!("refs/autosave/autosaves/{uid}/{branch}");
+ let refname = &format!("{AUTOSAVE_ENTRIES_NAMESPACE}{uid}/{branch}");
let signature = repository.signature()?;
let tree = repository.find_tree(utils::workdir_to_tree(repository)?)?;
let parent = head.peel_to_commit()?;
@@ -150,7 +160,7 @@ pub fn push_autosaves(
.filter(|reference| {
reference
.name_bytes()
- .starts_with(format!("refs/autosave/autosaves/{id}").as_bytes())
+ .starts_with(format!("{AUTOSAVE_ENTRIES_NAMESPACE}{id}").as_bytes())
})
.filter_map(|reference| reference.name().map(|n| n.to_string()))
.map(|reference| format!("+{reference}:{reference}"))
@@ -167,7 +177,7 @@ pub fn fetch_autosaves(
let remote = repository.branch_upstream_remote(&utils::current_branch_ref(repository)?)?;
let mut remote = repository.find_remote(&String::from_utf8_lossy(&remote))?;
remote.fetch(
- &["+refs/autosave/autosaves/*:refs/autosave/autosaves/*"],
+ &[AUTOSAVE_REFSPEC],
Some(FetchOptions::new().remote_callbacks(callbacks)),
Some("Updated autosaves"),
)?;
@@ -182,7 +192,7 @@ pub fn autosaves(repository: &Repository) -> Result<Vec<Autosave>, git2::Error>
.filter(|reference| {
reference
.name_bytes()
- .starts_with(b"refs/autosave/autosaves")
+ .starts_with(AUTOSAVE_ENTRIES_NAMESPACE.as_bytes())
})
.filter_map(|reference| reference.try_into().ok())
.collect())
@@ -199,7 +209,7 @@ pub fn save_undo_tree(repository: &Repository, workdir: &Tree<'_>) -> Result<(),
&[&repository.head()?.peel_to_commit()?],
)?;
repository.reference(
- "refs/autosave/undo",
+ UNDO_RESTORE_REF,
commit,
true,
"Save work before restoring autosave",
@@ -228,13 +238,13 @@ pub fn saved_restored_autosave(
);
let tree = repository.find_commit(autosave.commit_id)?.tree()?;
let parent = repository
- .find_reference("refs/autosave/restored")
+ .find_reference(RESTORED_AUTOSAVE_REF)
.ok()
.and_then(|reference| reference.peel_to_commit().ok());
let parents = parent.iter().collect::<Vec<_>>();
repository.commit(
- Some("refs/autosave/restored"),
+ Some(RESTORED_AUTOSAVE_REF),
&author,
&committer,
&message,