From 63ca832b1b8963e0da5c20449ecb5cf16157412e Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Mon, 26 Feb 2024 22:33:46 +0100 Subject: [PATCH 1/4] git: adding basic function to checkout a branch Signed-off-by: Vincenzo Palazzo --- coffee_github/src/utils.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/coffee_github/src/utils.rs b/coffee_github/src/utils.rs index 269354a..ac90d3b 100644 --- a/coffee_github/src/utils.rs +++ b/coffee_github/src/utils.rs @@ -1,8 +1,10 @@ +use log::debug; +use tokio::process::Command; + use coffee_lib::errors::CoffeeError; use coffee_lib::macros::error; use coffee_lib::url::URL; use coffee_lib::{commit_id, get_repo_info, sh}; -use log::debug; use coffee_lib::types::response::UpgradeStatus; @@ -30,8 +32,6 @@ pub async fn git_upgrade( branch: &str, verbose: bool, ) -> Result { - use tokio::process::Command; - let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?; let (local_commit, _) = get_repo_info!(repo); @@ -48,3 +48,25 @@ pub async fn git_upgrade( Ok(UpgradeStatus::Updated(upstream_commit, date)) } } + +pub async fn git_checkout( + path: &str, + branch: &str, + verbose: bool, +) -> Result { + let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?; + let (local_commit, _) = get_repo_info!(repo); + + let mut cmd = format!("git fetch origin\n"); + cmd += &format!("git reset --hard\n"); + cmd += &format!("git checkout origin/{branch}"); + sh!(path, cmd, verbose); + + let (upstream_commit, date) = get_repo_info!(repo); + + if local_commit == upstream_commit { + Ok(UpgradeStatus::UpToDate(upstream_commit, date)) + } else { + Ok(UpgradeStatus::Updated(upstream_commit, date)) + } +} From 817066181afb17d3abb3796d93c9b12009cd7333 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Mon, 26 Feb 2024 22:45:17 +0100 Subject: [PATCH 2/4] cli: pass down the cli the branch arg Signed-off-by: Vincenzo Palazzo --- coffee_cmd/src/cmd.rs | 5 ++++- coffee_cmd/src/main.rs | 3 ++- coffee_core/src/coffee.rs | 3 ++- coffee_core/src/lib.rs | 4 ++-- coffee_httpd/src/httpd/server.rs | 2 +- coffee_lib/src/plugin_manager.rs | 1 + coffee_plugin/src/plugin/plugin_mod.rs | 2 +- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/coffee_cmd/src/cmd.rs b/coffee_cmd/src/cmd.rs index 702b90c..c171095 100644 --- a/coffee_cmd/src/cmd.rs +++ b/coffee_cmd/src/cmd.rs @@ -29,6 +29,8 @@ pub enum CoffeeCommand { verbose: bool, #[arg(short, long, action = clap::ArgAction::SetTrue)] dynamic: bool, + #[arg(short, long)] + branch: Option, }, /// upgrade a single repository. #[clap(arg_required_else_help = true)] @@ -96,9 +98,10 @@ impl From<&CoffeeCommand> for coffee_core::CoffeeOperation { match value { CoffeeCommand::Install { plugin, + branch, verbose, dynamic, - } => Self::Install(plugin.to_owned(), *verbose, *dynamic), + } => Self::Install(plugin.to_owned(), branch.clone(), *verbose, *dynamic), CoffeeCommand::Upgrade { repo, verbose } => Self::Upgrade(repo.to_owned(), *verbose), CoffeeCommand::List {} => Self::List, CoffeeCommand::Setup { cln_conf } => Self::Setup(cln_conf.to_owned()), diff --git a/coffee_cmd/src/main.rs b/coffee_cmd/src/main.rs index c64106e..ef61a74 100644 --- a/coffee_cmd/src/main.rs +++ b/coffee_cmd/src/main.rs @@ -18,6 +18,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr match args.command { CoffeeCommand::Install { plugin, + branch, verbose, dynamic, } => { @@ -26,7 +27,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr } else { None }; - match coffee.install(&plugin, verbose, dynamic).await { + match coffee.install(&plugin, branch, verbose, dynamic).await { Ok(_) => { spinner.and_then(|spinner| Some(spinner.finish())); term::success!("Plugin {plugin} Compiled and Installed") diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 1f0f01c..6694a57 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -255,6 +255,7 @@ impl PluginManager for CoffeeManager { async fn install( &mut self, plugin: &str, + branch: Option, verbose: bool, try_dynamic: bool, ) -> Result<(), CoffeeError> { @@ -407,7 +408,7 @@ impl PluginManager for CoffeeManager { UpgradeStatus::Updated(_, _) => { for plugins in status.plugins_effected.iter() { self.remove(plugins).await?; - self.install(plugins, verbose, false).await?; + self.install(plugins, None, verbose, false).await?; } } _ => {} diff --git a/coffee_core/src/lib.rs b/coffee_core/src/lib.rs index 9629f58..e4bafd4 100644 --- a/coffee_core/src/lib.rs +++ b/coffee_core/src/lib.rs @@ -7,8 +7,8 @@ pub use coffee_lib as lib; #[derive(Clone, Debug)] pub enum CoffeeOperation { - /// Install(plugin name, verbose run, dynamic installation) - Install(String, bool, bool), + /// Install(plugin name, branch, verbose run, dynamic installation) + Install(String, Option, bool, bool), /// List List, // Upgrade(name of the repository, verbose run) diff --git a/coffee_httpd/src/httpd/server.rs b/coffee_httpd/src/httpd/server.rs index dc429fd..320e11a 100644 --- a/coffee_httpd/src/httpd/server.rs +++ b/coffee_httpd/src/httpd/server.rs @@ -93,7 +93,7 @@ async fn coffee_install( let try_dynamic = body.try_dynamic; let mut coffee = data.coffee.lock().await; - let result = coffee.install(plugin, false, try_dynamic).await; + let result = coffee.install(plugin, None, false, try_dynamic).await; handle_httpd_response!(result, "Plugin '{plugin}' installed successfully") } diff --git a/coffee_lib/src/plugin_manager.rs b/coffee_lib/src/plugin_manager.rs index 103b77e..38f4a4e 100644 --- a/coffee_lib/src/plugin_manager.rs +++ b/coffee_lib/src/plugin_manager.rs @@ -14,6 +14,7 @@ pub trait PluginManager { async fn install( &mut self, plugins: &str, + branch: Option, verbose: bool, try_dynamic: bool, ) -> Result<(), CoffeeError>; diff --git a/coffee_plugin/src/plugin/plugin_mod.rs b/coffee_plugin/src/plugin/plugin_mod.rs index 4f3fa40..22a4902 100644 --- a/coffee_plugin/src/plugin/plugin_mod.rs +++ b/coffee_plugin/src/plugin/plugin_mod.rs @@ -91,7 +91,7 @@ fn coffee_install(plugin: &mut Plugin, request: Value) -> Result Date: Mon, 26 Feb 2024 22:51:53 +0100 Subject: [PATCH 3/4] core: allow to install a branch Signed-off-by: Vincenzo Palazzo --- coffee_core/src/coffee.rs | 5 +++++ coffee_github/src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index 6694a57..ae05af6 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -15,6 +15,7 @@ use serde_json::json; use tokio::process::Command; use coffee_github::repository::Github; +use coffee_github::utils::git_checkout; use coffee_lib::errors::CoffeeError; use coffee_lib::plugin_manager::PluginManager; use coffee_lib::repository::Repository; @@ -316,6 +317,10 @@ impl PluginManager for CoffeeManager { plugin.root_path = new_root_path; plugin.exec_path = new_exec_path; + if let Some(branch) = branch { + let _ = git_checkout(&plugin.root_path, &branch, verbose).await?; + } + log::debug!("plugin: {:?}", plugin); let path = plugin.configure(verbose).await?; log::debug!("runnable plugin path {path}"); diff --git a/coffee_github/src/lib.rs b/coffee_github/src/lib.rs index 987ca5e..b312f8c 100644 --- a/coffee_github/src/lib.rs +++ b/coffee_github/src/lib.rs @@ -1,7 +1,7 @@ //! Github repository implementation pub mod repository; -mod utils; +pub mod utils; #[cfg(test)] mod tests { From 4d7920d621f989885a4c7810ac32cc840a2d0374 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Mon, 26 Feb 2024 23:10:30 +0100 Subject: [PATCH 4/4] fixup! core: allow to install a branch --- coffee_core/src/coffee.rs | 5 ++++- coffee_github/src/utils.rs | 12 +++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/coffee_core/src/coffee.rs b/coffee_core/src/coffee.rs index ae05af6..70fd5b6 100644 --- a/coffee_core/src/coffee.rs +++ b/coffee_core/src/coffee.rs @@ -318,7 +318,10 @@ impl PluginManager for CoffeeManager { plugin.exec_path = new_exec_path; if let Some(branch) = branch { - let _ = git_checkout(&plugin.root_path, &branch, verbose).await?; + // FIXME: Where we store the date? how we manage it? + let (commit, _) = + git_checkout(&plugin.root_path, &branch, verbose).await?; + plugin.commit = Some(commit); } log::debug!("plugin: {:?}", plugin); diff --git a/coffee_github/src/utils.rs b/coffee_github/src/utils.rs index ac90d3b..d578e74 100644 --- a/coffee_github/src/utils.rs +++ b/coffee_github/src/utils.rs @@ -53,20 +53,14 @@ pub async fn git_checkout( path: &str, branch: &str, verbose: bool, -) -> Result { - let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?; - let (local_commit, _) = get_repo_info!(repo); - +) -> Result<(String, String), CoffeeError> { let mut cmd = format!("git fetch origin\n"); cmd += &format!("git reset --hard\n"); cmd += &format!("git checkout origin/{branch}"); sh!(path, cmd, verbose); + let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?; let (upstream_commit, date) = get_repo_info!(repo); - if local_commit == upstream_commit { - Ok(UpgradeStatus::UpToDate(upstream_commit, date)) - } else { - Ok(UpgradeStatus::Updated(upstream_commit, date)) - } + Ok((upstream_commit, date)) }