aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
authorGravatar BanceDev 2026-03-02 14:22:55 -0500
committerGravatar BanceDev 2026-03-02 14:22:55 -0500
commitf76baa98acffc6a0d637ff360dd890a5fa10a973 (patch)
treed153197e38c5f26c0398a296259f0b1a98ce2ab4 /src/util.rs
parentdont fail on empty command (diff)
use lockfile for update tracking instead of trusting repo head
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/util.rs b/src/util.rs
index 9d44e70..a829327 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,4 +1,4 @@
-use git2::{Buf, Cred, FetchOptions, RemoteCallbacks, Repository, build::CheckoutBuilder};
+use git2::{Buf, Cred, FetchOptions, Oid, RemoteCallbacks, Repository, build::CheckoutBuilder};
use std::env;
use std::fs;
use std::io;
@@ -71,7 +71,15 @@ pub fn dir_size(path: &Path) -> std::io::Result<u64> {
Ok(size)
}
-pub fn get_commit_hash(path: &Path) -> Result<Buf, git2::Error> {
+pub fn get_commit_hash_full(path: &Path) -> Result<Oid, git2::Error> {
+ let repo = Repository::open(path)?;
+ let head = repo.head()?;
+
+ let commit = head.peel_to_commit()?;
+ Ok(commit.id())
+}
+
+pub fn get_commit_hash_short(path: &Path) -> Result<Buf, git2::Error> {
let repo = Repository::open(path)?;
let head = repo.head()?;
@@ -85,6 +93,18 @@ pub fn get_editor() -> String {
.unwrap_or_else(|_| "nano".to_string())
}
+pub fn get_remote_url(path: &Path) -> Result<String, git2::Error> {
+ let repo = Repository::open(path)?;
+
+ let remote = repo.find_remote("origin")?;
+
+ if let Some(url) = remote.url() {
+ Ok(url.to_string())
+ } else {
+ Err(git2::Error::from_str("Remote 'origin' has no URL"))
+ }
+}
+
pub fn open_in_editor(editor: &str, file: &str) -> Result<(), String> {
let status = Command::new(editor)
.arg(file)
@@ -110,7 +130,7 @@ pub fn print_collected_packages(packages: &PackageList, message: &str) {
);
}
-pub fn pull_repo(path: &Path) -> Result<bool, git2::Error> {
+pub fn pull_repo(path: &Path) -> Result<(), git2::Error> {
let repo = Repository::open(path)?;
let head = repo.head()?;
@@ -140,13 +160,10 @@ pub fn pull_repo(path: &Path) -> Result<bool, git2::Error> {
reference.set_target(fetch_commit.id(), "Fast-Forward")?;
repo.set_head(&refname)?;
repo.checkout_head(Some(CheckoutBuilder::default().force()))?;
- Ok(true)
- } else if analysis.is_up_to_date() {
- Ok(false)
- } else {
+ } else if !analysis.is_up_to_date() {
println!("Non fast-forward merge required (manual merge needed).");
- Ok(false)
}
+ Ok(())
}
pub fn yn_prompt(prompt: &str) -> bool {