diff --git a/src/commands/apply.rs b/src/commands/apply.rs new file mode 100644 index 0000000..2313646 --- /dev/null +++ b/src/commands/apply.rs @@ -0,0 +1,46 @@ +use crate::{ + core::{save_note, apply}, + git::Git, + parser::{commits_to_string, instruction_from_string}, +}; +use clap::Args; + +#[derive(Debug, Args)] +pub struct Apply {} + +const COMMENTS: &str = r#" +# Here is how to use yggit +# +# Commands: +# -> add a branch to the above commit +# -> : add a branch to the above commit +# +# What happens next? +# - All branches are pushed on origin, except if you specified a custom origin +# +# It's not a rebase, you can't edit commits nor reorder them +"#; + +impl Apply { + pub fn execute(&self, git: Git) -> Result<(), ()> { + let commits = git.list_commits(); + let output = commits_to_string(commits); + + let file_path = "/tmp/yggit"; + + let output = format!("{}\n{}", output, COMMENTS); + std::fs::write(file_path, output).map_err(|_| println!("cannot write file to disk"))?; + + let content = git.edit_file(file_path)?; + + let commits = instruction_from_string(content).ok_or_else(|| { + println!("Cannot parse instructions"); + })?; + + save_note(&git, commits); + + apply(&git); + + Ok(()) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7719db6..62d5d7f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ pub mod push; pub mod show; +pub mod apply; \ No newline at end of file diff --git a/src/commands/push.rs b/src/commands/push.rs index 5169738..f63f948 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -1,5 +1,5 @@ use crate::{ - core::{push_from_notes, save_note}, + core::{apply, push_from_notes, save_note}, git::Git, parser::{commits_to_string, instruction_from_string}, }; @@ -38,7 +38,7 @@ impl Push { })?; save_note(&git, commits); - + apply(&git); push_from_notes(&git); Ok(()) diff --git a/src/core.rs b/src/core.rs index 0f2fba9..b9ae075 100644 --- a/src/core.rs +++ b/src/core.rs @@ -39,11 +39,9 @@ pub fn save_note(git: &Git, commits: Vec) { } } -/// Execute the push instructions from the notes -/// -/// Change the head of the given branches -/// Push the branches to origin -pub fn push_from_notes(git: &Git) { +/// Execute the instructions from the notes +/// to change the head of the given branches +pub fn apply(git: &Git) { let commits = git.list_commits(); // Update the commits @@ -63,7 +61,11 @@ pub fn push_from_notes(git: &Git) { // Set the head of the branch to the given commit git.set_branch_to_commit(branch, *id).unwrap(); // TODO: manage error } +} +/// Push the branches to origin +pub fn push_from_notes(git: &Git) { + let commits = git.list_commits(); // Push everything for commit in &commits { let EnhancedCommit { diff --git a/src/git/git.rs b/src/git/git.rs index 038a828..af0c3c5 100644 --- a/src/git/git.rs +++ b/src/git/git.rs @@ -281,6 +281,7 @@ impl Git { /// Set the head of the given branch to the given commit pub fn set_branch_to_commit(&self, branch: &str, oid: Oid) -> Result<(), ()> { + println!("Setting branch {} to commit {:?}", branch, oid); let Ok(commit) = self.repository.find_commit(oid) else { println!("commit does not exist"); return Err(()); diff --git a/src/main.rs b/src/main.rs index 09cb364..12efb7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use clap::Parser; use clap::Subcommand; use commands::push::Push; use commands::show::Show; +use commands::apply::Apply; use git::Git; mod commands; @@ -21,6 +22,7 @@ struct Cli { enum Commands { Push(Push), Show(Show), + Apply(Apply) } fn main() { @@ -31,5 +33,6 @@ fn main() { let _ = match args.command { Commands::Push(push) => push.execute(git), Commands::Show(show) => show.execute(git), + Commands::Apply(apply) => apply.execute(git), }; }