Skip to content

Commit

Permalink
commands: add 'apply' command
Browse files Browse the repository at this point in the history
  • Loading branch information
d4hines authored and Pilou97 committed Feb 18, 2024
1 parent 1f45bb2 commit 37edbcc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
46 changes: 46 additions & 0 deletions src/commands/apply.rs
Original file line number Diff line number Diff line change
@@ -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:
# -> <branch> add a branch to the above commit
# -> <origin>:<branch> 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(())
}
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod push;
pub mod show;
pub mod apply;
4 changes: 2 additions & 2 deletions src/commands/push.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -38,7 +38,7 @@ impl Push {
})?;

save_note(&git, commits);

apply(&git);
push_from_notes(&git);

Ok(())
Expand Down
12 changes: 7 additions & 5 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ pub fn save_note(git: &Git, commits: Vec<crate::parser::Commit>) {
}
}

/// 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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/git/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,7 @@ struct Cli {
enum Commands {
Push(Push),
Show(Show),
Apply(Apply)
}

fn main() {
Expand All @@ -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),
};
}

0 comments on commit 37edbcc

Please sign in to comment.