Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: adding basic function to checkout a branch #248

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion coffee_cmd/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum CoffeeCommand {
verbose: bool,
#[arg(short, long, action = clap::ArgAction::SetTrue)]
dynamic: bool,
#[arg(short, long)]
branch: Option<String>,
},
/// upgrade a single repository.
#[clap(arg_required_else_help = true)]
Expand Down Expand Up @@ -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()),
Expand Down
3 changes: 2 additions & 1 deletion coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async fn run(args: CoffeeArgs, mut coffee: CoffeeManager) -> Result<(), CoffeeEr
match args.command {
CoffeeCommand::Install {
plugin,
branch,
verbose,
dynamic,
} => {
Expand All @@ -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")
Expand Down
11 changes: 10 additions & 1 deletion coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
//! Coffee mod implementation
use std::collections::HashMap;
use std::fmt::Debug;
use std::vec::Vec;

Check warning on line 4 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `Vec` is imported redundantly

Check warning on line 4 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `Vec` is imported redundantly
use tokio::fs;

use async_trait::async_trait;
use clightningrpc_common::client::Client;
use clightningrpc_common::json_utils;
use clightningrpc_conf::{CLNConf, SyncCLNConf};
use log;

Check warning on line 11 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `log` is imported redundantly

Check warning on line 11 in coffee_core/src/coffee.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `log` is imported redundantly
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
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;
Expand Down Expand Up @@ -255,6 +256,7 @@
async fn install(
&mut self,
plugin: &str,
branch: Option<String>,
verbose: bool,
try_dynamic: bool,
) -> Result<(), CoffeeError> {
Expand Down Expand Up @@ -315,6 +317,13 @@
plugin.root_path = new_root_path;
plugin.exec_path = new_exec_path;

if let Some(branch) = branch {
// FIXME: Where we store the date? how we manage it?
let (commit, _) =
git_checkout(&plugin.root_path, &branch, verbose).await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We switch the whole repository to the required branch but we don't switch back to the original branch of the repository after installing the plugin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice point

plugin.commit = Some(commit);
}

log::debug!("plugin: {:?}", plugin);
let path = plugin.configure(verbose).await?;
log::debug!("runnable plugin path {path}");
Expand Down Expand Up @@ -407,7 +416,7 @@
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?;
}
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions coffee_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, bool, bool),
/// List
List,
// Upgrade(name of the repository, verbose run)
Expand Down
2 changes: 1 addition & 1 deletion coffee_github/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Github repository implementation

pub mod repository;
mod utils;
pub mod utils;

#[cfg(test)]
mod tests {
Expand Down
22 changes: 19 additions & 3 deletions coffee_github/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use log::debug;
use tokio::process::Command;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong import place


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;

Expand Down Expand Up @@ -30,8 +32,6 @@ pub async fn git_upgrade(
branch: &str,
verbose: bool,
) -> Result<UpgradeStatus, CoffeeError> {
use tokio::process::Command;

let repo = git2::Repository::open(path).map_err(|err| error!("{}", err.message()))?;

let (local_commit, _) = get_repo_info!(repo);
Expand All @@ -48,3 +48,19 @@ pub async fn git_upgrade(
Ok(UpgradeStatus::Updated(upstream_commit, date))
}
}

pub async fn git_checkout(
path: &str,
branch: &str,
verbose: bool,
) -> 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);

Ok((upstream_commit, date))
}
2 changes: 1 addition & 1 deletion coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions coffee_lib/src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub trait PluginManager {
async fn install(
&mut self,
plugins: &str,
branch: Option<String>,
verbose: bool,
try_dynamic: bool,
) -> Result<(), CoffeeError>;
Expand Down
2 changes: 1 addition & 1 deletion coffee_plugin/src/plugin/plugin_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn coffee_install(plugin: &mut Plugin<State>, request: Value) -> Result<Value, P
let rt = Runtime::new().unwrap();

let request: InstallReq = serde_json::from_value(request)?;
rt.block_on(coffee.install(&request.name, false, true))
rt.block_on(coffee.install(&request.name, None, false, true))
.map_err(from)?;
Ok(json!({}))
}
Expand Down
Loading