Compare commits

..

No commits in common. "87fe70bb98ca9f34eead748979218fab85a34df0" and "851c0259a1088703fd68e7a1e1c5e5aa24ebe25d" have entirely different histories.

6 changed files with 30 additions and 110 deletions

View file

@ -2,9 +2,6 @@
## [Unreleased]
### Added
- Option to use `git` cli in `sync` subcommand. This is now the default (#27)
## [0.4.0] - 2025-03-01
### Added

View file

@ -1,7 +1,7 @@
//! CLI arguments
use crate::PathBuf;
use crate::path;
use crate::PathBuf;
use clap::Args;
use clap::{Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
@ -63,9 +63,6 @@ pub(crate) enum Commands {
Sync {
/// Remote name to sync.
remote_name: Option<String>,
/// Use custom git implementation.
#[arg(short, long)]
use_libgit2: bool,
/// Whether to use ssh-agent
#[arg(long)]
use_sshagent: bool,

View file

@ -2,13 +2,11 @@
//! Initialize xdbm for the device.
use crate::backups::Backups;
use crate::storages::{STORAGESFILE, Storages};
use crate::storages::{Storages, STORAGESFILE};
use crate::{
DEVICESFILE, Device, add_and_commit, backups,
devices::{get_devices, write_devices},
full_status,
add_and_commit, backups, full_status, get_devices, write_devices, Device, DEVICESFILE,
};
use anyhow::{Context, Ok, Result, anyhow};
use anyhow::{anyhow, Context, Ok, Result};
use core::panic;
use git2::{Cred, RemoteCallbacks, Repository};
use inquire::Password;

View file

@ -1,77 +1,16 @@
use std::{
io::{self, Write},
path::{Path, PathBuf},
process,
};
use anyhow::{Context, Result, anyhow};
use git2::{Cred, FetchOptions, PushOptions, RemoteCallbacks, Repository, build::CheckoutBuilder};
use anyhow::{anyhow, Context, Result};
use git2::{build::CheckoutBuilder, Cred, FetchOptions, PushOptions, RemoteCallbacks, Repository};
pub(crate) fn cmd_sync(
config_dir: &PathBuf,
remote_name: Option<String>,
use_sshagent: bool,
ssh_key: Option<PathBuf>,
use_libgit2: bool,
) -> Result<()> {
if use_libgit2 {
cmd_sync_custom(config_dir, remote_name, use_sshagent, ssh_key)
} else {
cmd_sync_cl(config_dir, remote_name, ssh_key)
}
}
fn cmd_sync_cl(
config_dir: &PathBuf,
remote_name: Option<String>,
ssh_key: Option<PathBuf>,
) -> Result<()> {
info!("cmd_sync (command line version)");
trace!("pull");
let args = |cmd| {
let mut args = vec![cmd];
if let Some(ref remote_name) = remote_name {
args.push(remote_name.clone());
}
if let Some(ref ssh_key) = ssh_key {
args.push("-i".to_string());
args.push(ssh_key.to_str().unwrap().to_owned());
}
args
};
let git_pull_result = process::Command::new("git")
.args(args("pull".to_owned()))
.current_dir(config_dir)
.status()
.context("error while executing git pull")?
.success();
if git_pull_result {
eprintln!("git pull completed");
} else {
return Err(anyhow!("failed to complete git pull"));
}
trace!("push");
let git_push_result = process::Command::new("git")
.args(args("push".to_owned()))
.current_dir(config_dir)
.status()
.context("error while executing git push")?
.success();
if git_push_result {
eprintln!("git push completed");
} else {
return Err(anyhow!("failed to complete git push"));
}
Ok(())
}
fn cmd_sync_custom(
config_dir: &PathBuf,
remote_name: Option<String>,
use_sshagent: bool,
ssh_key: Option<PathBuf>,
) -> Result<()> {
info!("cmd_sync");
let repo = Repository::open(config_dir)?;
@ -142,13 +81,13 @@ where
})
.transfer_progress(|progress| {
if progress.received_objects() == progress.total_objects() {
eprint!(
print!(
"Resolving deltas {}/{}\r",
progress.indexed_deltas(),
progress.total_deltas()
);
} else {
eprint!(
print!(
"Received {}/{} objects ({}) in {} bytes\r",
progress.received_objects(),
progress.total_objects(),
@ -156,7 +95,7 @@ where
progress.received_bytes(),
);
}
io::stderr().flush().unwrap();
io::stdout().flush().unwrap();
true
})
.sideband_progress(|text| {
@ -210,7 +149,7 @@ fn pull(
.context("Failed to fetch (pull)")?;
let stats = remote.stats();
if stats.local_objects() > 0 {
eprintln!(
println!(
"\rReceived {}/{} objects in {} bytes (used {} local objects)",
stats.indexed_objects(),
stats.total_objects(),
@ -218,7 +157,7 @@ fn pull(
stats.local_objects(),
);
} else {
eprintln!(
println!(
"\rReceived {}/{} objects in {} bytes",
stats.indexed_objects(),
stats.total_objects(),
@ -259,7 +198,7 @@ fn pull(
None => String::from_utf8_lossy(ref_remote.name_bytes()).to_string(),
};
let msg = format!("Fast-Forward: Setting {} to id: {}", name, fetch_head.id());
eprintln!("{}", msg);
println!("{}", msg);
ref_remote
.set_target(fetch_head.id(), &msg)
.context("failed to set target")?;
@ -299,7 +238,7 @@ fn push(
ssh_key: Option<&PathBuf>,
) -> Result<()> {
debug!("push");
let callbacks = remote_callback(use_sshagent, ssh_key);
let callbacks = remote_callback(&use_sshagent, ssh_key);
let mut push_options = PushOptions::new();
push_options.remote_callbacks(callbacks);
let num_push_refspecs = remote

View file

@ -23,7 +23,7 @@ use std::path::{self, PathBuf};
use storages::Storages;
use crate::cmd_args::{BackupSubCommands, Cli, Commands, StorageCommands};
use devices::{DEVICESFILE, Device};
use devices::{Device, DEVICESFILE, *};
mod backups;
mod cmd_args;
@ -94,10 +94,9 @@ fn main() -> Result<()> {
}
Commands::Sync {
remote_name,
use_libgit2,
use_sshagent,
ssh_key,
} => cmd_sync::cmd_sync(&config_dir, remote_name, use_sshagent, ssh_key, use_libgit2)?,
} => cmd_sync::cmd_sync(&config_dir, remote_name, use_sshagent, ssh_key)?,
Commands::Status {
path,
storage,

View file

@ -5,8 +5,8 @@ mod integrated_test {
path,
};
use anyhow::{Context, Ok, Result, anyhow};
use assert_cmd::{Command, assert::OutputAssertExt};
use anyhow::{anyhow, Context, Ok, Result};
use assert_cmd::{assert::OutputAssertExt, Command};
use git2::Repository;
use log::{debug, trace};
use predicates::{boolean::PredicateBooleanExt, prelude::predicate};
@ -73,22 +73,13 @@ mod integrated_test {
Ok(())
}
fn run_sync_cmd(config_dir: &path::Path, use_cl: bool) -> Result<()> {
if use_cl {
fn run_sync_cmd(config_dir: &path::Path) -> Result<()> {
Command::cargo_bin("xdbm")?
.arg("-c")
.arg(config_dir)
.args(["sync", "-vvvv"])
.assert()
.success();
} else {
Command::cargo_bin("xdbm")?
.arg("-c")
.arg(config_dir)
.args(["sync", "-vvvv", "-u"])
.assert()
.success();
}
Ok(())
}
@ -338,7 +329,6 @@ mod integrated_test {
.arg(config_dir_2.path())
.arg("sync")
.arg("-vvvv")
.arg("-u")
.assert()
.success()
.stderr(predicate::str::contains("successfully pushed"));
@ -401,8 +391,8 @@ mod integrated_test {
std::fs::read_to_string(config_dir_1.join("storages.yml"))?.contains("parent: gdrive1")
);
run_sync_cmd(&config_dir_1, false)?;
run_sync_cmd(&config_dir_2, false)?;
run_sync_cmd(&config_dir_1)?;
run_sync_cmd(&config_dir_2)?;
// bind
//
@ -616,8 +606,8 @@ mod integrated_test {
.and(predicate::str::contains("foodoc").not()),
);
run_sync_cmd(&config_dir_2, true)?;
run_sync_cmd(&config_dir_1, true)?;
run_sync_cmd(&config_dir_2)?;
run_sync_cmd(&config_dir_1)?;
// bind
//
@ -732,8 +722,8 @@ mod integrated_test {
.assert()
.success();
run_sync_cmd(&config_dir_1, false)?;
run_sync_cmd(&config_dir_2, false)?;
run_sync_cmd(&config_dir_1)?;
run_sync_cmd(&config_dir_2)?;
// backup add
//