-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add Branch Selection for coffee_install()
#242
Changes from all commits
e91a987
9a97397
7e301a2
d77053b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 GitHub Actions / Build (nightly)
|
||
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 GitHub Actions / Build (nightly)
|
||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use tokio::process::Command; | ||
|
||
use coffee_github::repository::Github; | ||
use coffee_github::{git_upgrade_with_branch, git_upgrade_with_git_head}; | ||
use coffee_lib::errors::CoffeeError; | ||
use coffee_lib::plugin_manager::PluginManager; | ||
use coffee_lib::repository::Repository; | ||
|
@@ -246,13 +247,37 @@ | |
plugin: &str, | ||
verbose: bool, | ||
try_dynamic: bool, | ||
branch: Option<String>, | ||
) -> Result<(), CoffeeError> { | ||
log::debug!("installing plugin: {plugin}"); | ||
// keep track if the plugin is successfully installed | ||
for repo in self.repos.values() { | ||
if let Some(mut plugin) = repo.get_plugin_by_name(plugin) { | ||
log::trace!("{:?}", plugin); | ||
|
||
// if the branch is specified, we need to save current git_head | ||
// and checkout the branch | ||
let current_git_head = repo.git_head(); | ||
let current_plugin_commit = plugin.commit.clone(); | ||
if let Some(branch) = branch.clone() { | ||
log::debug!("checking out branch: {branch}"); | ||
if current_git_head.is_none() { | ||
return Err(error!("unable to get the current git head")); | ||
} | ||
let updated_status = | ||
git_upgrade_with_branch(repo.url().path_string.as_str(), &branch, verbose) | ||
.await?; | ||
log::debug!("updated status: {:?}", updated_status); | ||
|
||
let repository = git2::Repository::open(repo.url().path_string.as_str()) | ||
.map_err(|err| error!("{}", err.message()))?; | ||
|
||
// get commit id after the checkout | ||
let commit = commit_id!(repository); | ||
log::debug!("commit id after the checkout: {commit}"); | ||
plugin.commit = Some(commit); | ||
} | ||
Comment on lines
+258
to
+279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO to much code for just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see some potential issues with simply using
Do you think that any of these steps is redundant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my point is that we can do all with just a couple of git command, with your solution I do not see any fetch anywhere too and we need to make sure that your code is correct, otherwise we will have the same problem that we had with the upgrade logic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// old_root_path is the path where the plugin is cloned and currently stored | ||
// eg. ~/.coffee/repositories/<repo_name>/<plugin_name> | ||
let old_root_path = plugin.root_path.clone(); | ||
|
@@ -291,7 +316,7 @@ | |
if !try_dynamic { | ||
// mark the plugin enabled | ||
plugin.enabled = Some(true); | ||
self.config.plugins.push(plugin); | ||
self.config.plugins.push(plugin.clone()); | ||
log::debug!("path coffee conf: {}", self.coffee_cln_config.path); | ||
self.coffee_cln_config | ||
.add_conf("plugin", &path.to_owned()) | ||
|
@@ -302,6 +327,23 @@ | |
} else { | ||
self.start_plugin(&path).await?; | ||
} | ||
|
||
if branch.is_some() { | ||
// we can safely unwrap here because we have checked it before | ||
let current_git_head = current_git_head.unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the safety comment start with |
||
log::debug!("checking out to git head: {current_git_head}"); | ||
// rollback to the previous git head | ||
let updated_status = git_upgrade_with_git_head( | ||
repo.url().path_string.as_str(), | ||
¤t_git_head, | ||
verbose, | ||
) | ||
.await?; | ||
log::debug!("updated status: {:?}", updated_status); | ||
|
||
plugin.commit = current_plugin_commit; | ||
} | ||
Comment on lines
+331
to
+345
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow this install method is starting to be 2 pages long? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, my point is that the code that we are writing the code of the install is not good at all. Please see some of my PR that I am refactoring it. In addition, both install and upgrade command will run the same logic and with your code we should duplicate the checkout logic. This is error prone There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ok. how do you think I can make it better? I explained why we need the extra code in
They will have the same logic because they will do very similar tasks. The function |
||
|
||
return Ok(()); | ||
} | ||
None => return Err(error!("exec path not found")), | ||
|
@@ -360,7 +402,7 @@ | |
UpgradeStatus::Updated(_, _) => { | ||
for plugins in status.plugins_effected.iter() { | ||
self.remove(plugins).await?; | ||
self.install(plugins, verbose, false).await?; | ||
self.install(plugins, verbose, false, None).await?; | ||
} | ||
} | ||
_ => {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use std::any::Any; | ||
|
||
use async_trait::async_trait; | ||
use git2; | ||
Check warning on line 4 in coffee_github/src/repository.rs GitHub Actions / Build (nightly)
|
||
use log::debug; | ||
use tokio::fs::File; | ||
use tokio::io::AsyncReadExt; | ||
|
@@ -21,7 +21,7 @@ | |
use coffee_storage::model::repository::Repository as StorageRepository; | ||
|
||
use crate::utils::clone_recursive_fix; | ||
use crate::utils::git_upgrade; | ||
use crate::utils::git_upgrade_with_branch; | ||
|
||
pub struct Github { | ||
/// the url of the repository to be able | ||
|
@@ -259,7 +259,7 @@ | |
} | ||
} | ||
// pull the changes from the repository | ||
let status = git_upgrade(&self.url.path_string, &self.branch, verbose).await?; | ||
let status = git_upgrade_with_branch(&self.url.path_string, &self.branch, verbose).await?; | ||
self.git_head = Some(status.commit_id()); | ||
self.last_activity = Some(status.date()); | ||
Ok(CoffeeUpgrade { | ||
|
@@ -336,6 +336,11 @@ | |
None | ||
} | ||
|
||
/// return the git head of the repository. | ||
fn git_head(&self) -> Option<String> { | ||
self.git_head.clone() | ||
} | ||
Comment on lines
+339
to
+342
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this as a struct method and not an a trait implementation |
||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ pub async fn clone_recursive_fix(repo: git2::Repository, url: &URL) -> Result<() | |
Ok(()) | ||
} | ||
|
||
pub async fn git_upgrade( | ||
pub async fn git_upgrade_with_branch( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why changing the name here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because I added another function that uses the git commit to upgrade There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not see the point of changing the name, due that there is no git upgrade command in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moreover, git uses always a branch there is never a |
||
path: &str, | ||
branch: &str, | ||
verbose: bool, | ||
|
@@ -48,3 +48,27 @@ pub async fn git_upgrade( | |
Ok(UpgradeStatus::Updated(upstream_commit, date)) | ||
} | ||
} | ||
|
||
pub async fn git_upgrade_with_git_head( | ||
path: &str, | ||
git_head: &str, | ||
verbose: bool, | ||
) -> Result<UpgradeStatus, CoffeeError> { | ||
use tokio::process::Command; | ||
Comment on lines
+52
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idk if this is a good idea, the checkout is just Why we need to use all this code? |
||
|
||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cmd += &format!("git reset --hard {}", git_head); | ||
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)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,5 +40,8 @@ pub trait Repository: Any { | |
/// return the url of the repository. | ||
fn url(&self) -> URL; | ||
|
||
/// return the git head of the repository. | ||
fn git_head(&self) -> Option<String>; | ||
|
||
Comment on lines
+43
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does not make sense to have this inside the repository trait There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point. I will update it |
||
fn as_any(&self) -> &dyn Any; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,14 @@ To install a plugin statically, you simply need to run. | |
coffee install <plugin_name> | ||
``` | ||
|
||
#### Specifying a branch for installation | ||
|
||
To install a plugin from a specific branch, you simply need to run. | ||
|
||
```bash | ||
coffee install <plugin_name> --branch <branch_name> | ||
``` | ||
|
||
Comment on lines
+107
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should not document every single command possibility otherwise our documentation will be always uncompleted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I can remove this section There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but we should came up with a way to document optional parameters |
||
### Removing a Plugin | ||
|
||
> ✅ Implemented | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a user need also a git PHD to understand what is a git HEAD? Idk if this is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already print the git HEAD to the user with
coffee remote list
If we add the possibility to manage the branch for the plugin in separation from the repository, I think we have to print it in
coffee list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please call it
commit
there is no meaning to complicate a simple things with big words.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Do you want me to rename "Git HEAD" to "commit" in
coffee remote list
output too?