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

Add apply command #36

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.direnv
.vscode
60 changes: 60 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {
self,
nixpkgs,
flake-utils,
}: let
pkgs = import nixpkgs {system = "x86_64-linux";};
in
flake-utils.lib.eachDefaultSystem
(system: let
pkgs = import nixpkgs {inherit system;};
in {
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
rustc
cargo
cargo-tauri
rustfmt
rust-analyzer
pkg-config
openssl_3
];
};
});
}
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),
};
}
Loading