From 2b8d4b103e5fc6a274543bac2848b83f0c7c5e28 Mon Sep 17 00:00:00 2001 From: rzmk Date: Fri, 1 Dec 2023 20:59:42 -0500 Subject: [PATCH 01/12] feat: add -p flag for git push after commit --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 8 ++++++++ src/main.rs | 19 +++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c204ec..edbd684 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "commit-helper" -version = "0.1.3" +version = "0.1.4" dependencies = [ "inquire", ] diff --git a/Cargo.toml b/Cargo.toml index f6235f3..7520859 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "commit-helper" -version = "0.1.3" +version = "0.1.4" edition = "2021" [[bin]] diff --git a/README.md b/README.md index 3abdae3..978b1d1 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,14 @@ If you want to run `git add -A` before committing, use the `-a` flag ch -a ``` +### `-p` + +If you want to run `git push` after committing, use the `-p` flag + +```bash +ch -p +``` + ### `--dry-run` or `-d` If you want to do a dry run without actually adding or committing, use the `-d` or `--dry-run` flag. diff --git a/src/main.rs b/src/main.rs index 396cd3e..212b592 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ fn main() { if dry_run { println!("Running in dry run mode\n"); } + // Check if -p flag is passed to run git push after commit + let run_git_push = args.len() > 1 && args.contains(&String::from("-p")); // Check if --debug flag is passed to run in debug mode let debug = args.len() > 1 && args.contains(&String::from("--debug")); @@ -89,6 +91,23 @@ fn main() { println!("Exit status:\n{}", output.status); } } + + if run_git_push { + println!("Running git push"); + if !dry_run { + let output = Command::new("git") + .args(["push"]) + .output() + .expect("failed to execute process"); + + if debug { + println!("Debug info:"); + println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); + println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); + println!("Exit status:\n{}", output.status); + } + } + } } _ => { println!("Exiting"); From c48139ebc6d705679bc5af9f1b7456c4fedfe052 Mon Sep 17 00:00:00 2001 From: rzmk Date: Fri, 1 Dec 2023 21:02:26 -0500 Subject: [PATCH 02/12] docs: modify punctuation and show --dry-run instead of -d --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 978b1d1..a5908a8 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ cargo install --git https://github.com/rzmk/commit-helper ## Usage -To run the tool, simply run `ch` in your terminal. +To run the tool, simply run `ch` in your terminal: ```bash ch @@ -22,7 +22,7 @@ ch ### `-a` -If you want to run `git add -A` before committing, use the `-a` flag +If you want to run `git add -A` before committing, use the `-a` flag: ```bash ch -a @@ -30,7 +30,7 @@ ch -a ### `-p` -If you want to run `git push` after committing, use the `-p` flag +If you want to run `git push` after committing, use the `-p` flag: ```bash ch -p @@ -38,15 +38,15 @@ ch -p ### `--dry-run` or `-d` -If you want to do a dry run without actually adding or committing, use the `-d` or `--dry-run` flag. +If you want to do a dry run without actually adding or committing, use the `-d` or `--dry-run` flag: ```bash -ch -d +ch --dry-run ``` ### `--debug` -If you want to see the debug output, use the `--debug` flag. +If you want to see the debug output, use the `--debug` flag: ```bash ch --debug From 0d5ebd26ac451646f3b5e43863895d92f864c86f Mon Sep 17 00:00:00 2001 From: rzmk Date: Fri, 1 Dec 2023 23:54:52 -0500 Subject: [PATCH 03/12] refactor: consolidate debug print statements to a single function --- src/main.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 212b592..ebbb2dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,13 @@ use inquire::{Confirm, Select}; use std::{env, process::Command}; +fn print_debug_info(output: std::process::Output) { + println!("Debug info:"); + println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); + println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); + println!("Exit status:\n{}", output.status); +} + fn main() { // Check if -a flag is passed to run git add -A before commit let args: Vec = env::args().collect(); @@ -69,10 +76,7 @@ fn main() { .expect("failed to execute process"); if debug { - println!("Debug info:"); - println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); - println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); - println!("Exit status:\n{}", output.status); + print_debug_info(output); } } } @@ -85,10 +89,7 @@ fn main() { .expect("failed to execute process"); if debug { - println!("Debug info:"); - println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); - println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); - println!("Exit status:\n{}", output.status); + print_debug_info(output); } } @@ -101,10 +102,7 @@ fn main() { .expect("failed to execute process"); if debug { - println!("Debug info:"); - println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); - println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); - println!("Exit status:\n{}", output.status); + print_debug_info(output); } } } From 86ee66c7beec0a1a207f8122ccbbfc2b1fcb31ae Mon Sep 17 00:00:00 2001 From: rzmk Date: Sun, 24 Dec 2023 22:15:11 -0500 Subject: [PATCH 04/12] docs: add link to memo --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a5908a8..d289423 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A simple tool to help write commit messages and run git commands. ![Demo](demo.gif) +You may read a brief post about commit-helper on my website: [mueezkhan.com/memos/commit-helper](https://www.mueezkhan.com/memos/commit-helper) + ## Installation Make sure you have [Cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html) installed, then run the following command in your terminal: From 07e03335cca5ccf9ad5d6e2180c146ca57e33f3a Mon Sep 17 00:00:00 2001 From: rzmk Date: Mon, 1 Jan 2024 04:32:51 -0500 Subject: [PATCH 05/12] feat: use clap crate, add -m flag, change -d to --debug --- Cargo.lock | 203 +++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 3 +- README.md | 61 +++++++++++---- src/main.rs | 130 +++++++++++--------------------- src/parser.rs | 50 +++++++++++++ src/util.rs | 40 ++++++++++ 6 files changed, 374 insertions(+), 113 deletions(-) create mode 100644 src/parser.rs create mode 100644 src/util.rs diff --git a/Cargo.lock b/Cargo.lock index edbd684..ba43259 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,54 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "anstream" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -21,9 +69,56 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "commit-helper" -version = "0.1.4" +name = "clap" +version = "4.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d" dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb7fb5e4e979aec3be7791562fcba452f94ad85e954da024396433e0e25a79e9" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + +[[package]] +name = "commit-helper" +version = "0.2.0" +dependencies = [ + "clap", "inquire", ] @@ -58,6 +153,12 @@ version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "545b22097d44f8a9581187cdf93de7a71e4722bf51200cfaba810865b49a495d" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "inquire" version = "0.6.2" @@ -111,7 +212,7 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -143,7 +244,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -215,6 +316,12 @@ version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "2.0.39" @@ -264,6 +371,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -298,7 +411,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", ] [[package]] @@ -307,13 +429,28 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] [[package]] @@ -322,38 +459,80 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/Cargo.toml b/Cargo.toml index 7520859..c7207c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "commit-helper" -version = "0.1.4" +version = "0.2.0" edition = "2021" [[bin]] @@ -8,4 +8,5 @@ name = "ch" path = "src/main.rs" [dependencies] +clap = { version = "4.4.12", features = ["derive"] } inquire = { version = "0.6.2" } diff --git a/README.md b/README.md index d289423..f5a752b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,9 @@ # commit-helper (ch) -A simple tool to help write commit messages and run git commands. +A simple tool to help run commands related to `git commit` in one go. ![Demo](demo.gif) -You may read a brief post about commit-helper on my website: [mueezkhan.com/memos/commit-helper](https://www.mueezkhan.com/memos/commit-helper) - ## Installation Make sure you have [Cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html) installed, then run the following command in your terminal: @@ -14,49 +12,82 @@ Make sure you have [Cargo](https://doc.rust-lang.org/cargo/getting-started/insta cargo install --git https://github.com/rzmk/commit-helper ``` -## Usage +## Reference -To run the tool, simply run `ch` in your terminal: +To run the interactive workflow, simply run `ch` in your terminal: ```bash ch ``` -### `-a` +### `--help` or `-h` -If you want to run `git add -A` before committing, use the `-a` flag: +To get the help message, run `ch --help` or `ch -h`: + +```bash +ch -h +``` + +### `--add` or `-a` + +If you want to run `git add -A` before committing, use the `--add` or `-a` flag: ```bash ch -a ``` -### `-p` +### `--push` or `-p` -If you want to run `git push` after committing, use the `-p` flag: +If you want to run `git push` after committing, use the `--push` or `-p` flag: ```bash ch -p ``` -### `--dry-run` or `-d` +### `--message` or `-m` -If you want to do a dry run without actually adding or committing, use the `-d` or `--dry-run` flag: +If you want to pass in a custom commit message (therefore skipping the interactive steps), use the `--message` or `-m` flag: + +```bash +ch -m "feat: add new feature" +``` + +### `--debug` or `-d` + +If you want to see the debug output, use the `--debug` or `-d` flag: + +```bash +ch -d +``` + +> Note: The debug output will not be printed if you use the `--dry-run` flag. + +### `--dry-run` + +If you want to do a dry run without executing any commands, use the `--dry-run` flag: ```bash ch --dry-run ``` -### `--debug` +## Example -If you want to see the debug output, use the `--debug` flag: +If I want to run `git add -A`, then `git commit -m "feat: add new feature"`, then `git push` all in one go, I could run the following command: ```bash -ch --debug +ch -m "feat: add new feature" -a -p ``` -Note that the debug output will not be printed if you use the `--dry-run` flag. +Equivalently to the above command, I may instead combine the short flags: + +```bash +ch -apm "feat: add new feature" +``` + +> Note: The order of the combined short flags does not matter, except for the `-m` flag, which must be the last flag if you want to pass in a custom commit message after a combination of flags. ## Tech Stack - [Rust](https://www.rust-lang.org/) +- [clap](https://github.com/clap-rs/clap) - [inquire](https://github.com/mikaelmello/inquire) diff --git a/src/main.rs b/src/main.rs index ebbb2dd..9b23530 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,55 +1,53 @@ -use inquire::{Confirm, Select}; -use std::{env, process::Command}; +use clap::Parser; +use inquire::{formatter::OptionFormatter, Confirm, InquireError, Select}; -fn print_debug_info(output: std::process::Output) { - println!("Debug info:"); - println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); - println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); - println!("Exit status:\n{}", output.status); -} +pub mod parser; +use parser::{Cli, CommitType}; +pub mod util; +use util::orchestrate_commit; fn main() { - // Check if -a flag is passed to run git add -A before commit - let args: Vec = env::args().collect(); - let run_git_add = args.len() > 1 && args.contains(&String::from("-a")); - // Check if -d or --dry-run flag is passed to run in dry run mode - let dry_run = args.len() > 1 - && (args.contains(&String::from("-d")) || args.contains(&String::from("--dry-run"))); - if dry_run { - println!("Running in dry run mode\n"); - } - // Check if -p flag is passed to run git push after commit - let run_git_push = args.len() > 1 && args.contains(&String::from("-p")); - // Check if --debug flag is passed to run in debug mode - let debug = args.len() > 1 && args.contains(&String::from("--debug")); + // Parse command line arguments + let cli: Cli = Cli::parse(); - // Commit Type + // If a message was provided, run the commands in succession + if let Some(message) = &cli.message { + orchestrate_commit(&cli, &message); + return; + } + + // Otherwise, prompt for a message let commit_type_options = vec![ - "build - build system and dependencies", - "ci - continuous integration", - "chore - misc/maintenance not related to core code", - "docs - documentation changes (e.g., README.md, comments)", - "feat - new feature or significant enhancement", - "fix - bug fix or error correction", - "perf - performance improvement", - "refactor - code restructuring or cleanup", - "test - add or update tests", + CommitType::new("build", "build system and dependencies"), + CommitType::new("ci", "continuous integration"), + CommitType::new("chore", "misc/maintenance not related to core code"), + CommitType::new("docs", "documentation changes (e.g., README.md, comments)"), + CommitType::new("feat", "new feature or significant enhancement"), + CommitType::new("fix", "bug fix or error correction"), + CommitType::new("perf", "performance improvement"), + CommitType::new("refactor", "code restructuring or cleanup"), + CommitType::new("test", "add or update tests"), ]; - let commit_type = Select::new("Type:", commit_type_options).prompt(); - let commit_type = match commit_type { - Ok(commit_type) => - // Get the first word of the commit type - { - let commit_type = commit_type.split_whitespace().next().unwrap(); - commit_type - } - Err(_) => { - println!("No commit type selected, exiting"); + + // Format the commit type options for display + let formatter: OptionFormatter = + &|ct| format!("{}: {}", ct.value.name, ct.value.description); + + // Prompt for the commit type + let selected_type: Result = Select::new("Type:", commit_type_options) + .with_formatter(formatter) + .prompt(); + + // Get the name of the selected commit type + let commit_type = match selected_type { + Ok(ct) => ct.name, + Err(e) => { + println!("Error: {:?}", e); return; } }; - // Summary + // Prompt for the commit summary (the message after the commit type) let commit_summary = inquire::Text::new("Summary:").prompt(); let commit_summary = match commit_summary { Ok(commit_summary) => commit_summary, @@ -59,59 +57,21 @@ fn main() { } }; + // Format the commit message to include the commit type and summary let result_message = format!("{}: {}", commit_type, commit_summary); - // Confirm + // Confirm the commit message let confirm = - Confirm::new(format!("Result:\n\n{}\n\nCommit? (y/n):", result_message,).as_str()).prompt(); + Confirm::new(format!("Result:\n\n{}\n\nCommit? (y/n):", result_message).as_str()).prompt(); + // If confirmed, run the commands in succession match confirm { Ok(true) => { - if run_git_add { - println!("Running git add -A"); - if !dry_run { - let output = Command::new("git") - .args(["add", "-A"]) - .output() - .expect("failed to execute process"); - - if debug { - print_debug_info(output); - } - } - } - - println!("Running git commit -m \"{}\"", result_message); - if !dry_run { - let output = Command::new("git") - .args(["commit", "-m", result_message.as_str()]) - .output() - .expect("failed to execute process"); - - if debug { - print_debug_info(output); - } - } - - if run_git_push { - println!("Running git push"); - if !dry_run { - let output = Command::new("git") - .args(["push"]) - .output() - .expect("failed to execute process"); - - if debug { - print_debug_info(output); - } - } - } + orchestrate_commit(&cli, &result_message); } _ => { println!("Exiting"); return; } } - - println!("Done 🎉"); } diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..f0892c1 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,50 @@ +use clap::Parser; + +#[derive(Parser)] +#[command( + name = "commit-helper", + author = "Mueez Khan", + about = "Run multiple commands related to `git commit` in succession" +)] +pub struct Cli { + /// Run `git add -A` before committing + #[arg(short = 'a', long = "add")] + pub add: bool, + + /// Run `git push` after committing + #[arg(short = 'p', long = "push")] + pub push: bool, + + /// Run in debug mode (print output of each command) + #[arg(short = 'd', long = "debug")] + pub debug: bool, + + /// Run in dry run mode (disallow executing commands) + #[arg(long = "dry-run")] + pub dry_run: bool, + + /// Run `git commit` with the given message + /// (if not provided, will prompt for message) + #[arg(short = 'm', long = "message")] + pub message: Option, +} + +pub struct CommitType { + pub name: String, + pub description: String, +} + +impl CommitType { + pub fn new(name: &str, description: &str) -> Self { + Self { + name: name.to_owned(), + description: description.to_owned(), + } + } +} + +impl std::fmt::Display for CommitType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}: {}", self.name, self.description) + } +} diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..e453f1f --- /dev/null +++ b/src/util.rs @@ -0,0 +1,40 @@ +use super::parser::Cli; +use std::process::Command; + +// Run the commands in succession +pub fn orchestrate_commit(cli: &Cli, message: &str) { + if cli.add { + println!("Running git add -A"); + run(&cli, "git", &["add", "-A"]); + } + + println!("Running git commit -m \"{}\"", message); + run(&cli, "git", &["commit", "-m", message]); + + if cli.push { + println!("Running git push"); + run(&cli, "git", &["push"]); + } + + println!("Done 🎉"); +} + +pub fn print_debug_info(output: std::process::Output) { + println!("Debug info:"); + println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout)); + println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr)); + println!("Exit status:\n{}", output.status); +} + +pub fn run(cli: &Cli, command: &str, args: &[&str]) { + if !cli.dry_run { + let output = Command::new(command) + .args(args) + .output() + .expect("failed to execute process"); + + if cli.debug { + print_debug_info(output); + } + } +} From f3e227ac2ca7e8c6376ea9916c8862fece6bbfe2 Mon Sep 17 00:00:00 2001 From: rzmk Date: Mon, 1 Jan 2024 05:44:55 -0500 Subject: [PATCH 06/12] refactor: fix lint checks by cargo clippy --- src/main.rs | 3 +-- src/util.rs | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9b23530..880e84f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ fn main() { // If a message was provided, run the commands in succession if let Some(message) = &cli.message { - orchestrate_commit(&cli, &message); + orchestrate_commit(&cli, message); return; } @@ -71,7 +71,6 @@ fn main() { } _ => { println!("Exiting"); - return; } } } diff --git a/src/util.rs b/src/util.rs index e453f1f..887fdd7 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,15 +5,15 @@ use std::process::Command; pub fn orchestrate_commit(cli: &Cli, message: &str) { if cli.add { println!("Running git add -A"); - run(&cli, "git", &["add", "-A"]); + run(cli, "git", &["add", "-A"]); } println!("Running git commit -m \"{}\"", message); - run(&cli, "git", &["commit", "-m", message]); + run(cli, "git", &["commit", "-m", message]); if cli.push { println!("Running git push"); - run(&cli, "git", &["push"]); + run(cli, "git", &["push"]); } println!("Done 🎉"); From 8a959c6cd5ebebf630c511349c9384f64e03b6bd Mon Sep 17 00:00:00 2001 From: rzmk Date: Mon, 1 Jan 2024 16:47:11 -0500 Subject: [PATCH 07/12] docs: clarify CLI tool in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f5a752b..2918ba5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # commit-helper (ch) -A simple tool to help run commands related to `git commit` in one go. +A command-line tool to help run commands related to `git commit` in one go. ![Demo](demo.gif) From b67ef9914ea32dcc1966641ce909343fe94108d0 Mon Sep 17 00:00:00 2001 From: rzmk Date: Mon, 1 Jan 2024 16:48:04 -0500 Subject: [PATCH 08/12] docs: remove 'simply' in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2918ba5..3a30810 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ cargo install --git https://github.com/rzmk/commit-helper ## Reference -To run the interactive workflow, simply run `ch` in your terminal: +To run the interactive workflow, run `ch` in your terminal: ```bash ch From 1757cb99ff56fbcbcad39887a56771c1c0a23d4d Mon Sep 17 00:00:00 2001 From: rzmk Date: Thu, 4 Jan 2024 08:07:04 -0500 Subject: [PATCH 09/12] docs: include mention of interactive functionality --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a30810..b01ffbb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # commit-helper (ch) -A command-line tool to help run commands related to `git commit` in one go. +A command-line tool to help run commands related to `git commit` in one go or use an interactive commit message builder. ![Demo](demo.gif) From 3e0b97d1720a47b1efdc3d6b99ce70ac21be2a90 Mon Sep 17 00:00:00 2001 From: rzmk <30333942+rzmk@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:22:04 -0500 Subject: [PATCH 10/12] feat: add -c (copy to clipboard) and -s (sign) --- Cargo.lock | 399 +++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + README.md | 8 + src/main.rs | 3 +- src/parser.rs | 8 + src/util.rs | 24 ++- 6 files changed, 433 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba43259..d87635a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" [[package]] name = "anstream" @@ -50,6 +56,24 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "arboard" +version = "3.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4" +dependencies = [ + "clipboard-win", + "core-graphics", + "image", + "log", + "objc2", + "objc2-app-kit", + "objc2-foundation", + "parking_lot", + "windows-sys 0.48.0", + "x11rb", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -62,6 +86,33 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "block2" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f" +dependencies = [ + "objc2", +] + +[[package]] +name = "bytemuck" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a" + +[[package]] +name = "byteorder-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" + [[package]] name = "cfg-if" version = "1.0.0" @@ -108,6 +159,15 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +[[package]] +name = "clipboard-win" +version = "5.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15efe7a882b08f34e38556b14f2fb3daa98769d06c7f0c1b076dfd0d983bc892" +dependencies = [ + "error-code", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -118,17 +178,67 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" name = "commit-helper" version = "0.2.0" dependencies = [ + "arboard", "clap", "inquire", ] +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "core-graphics" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-graphics-types", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossterm" version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" dependencies = [ - "bitflags", + "bitflags 1.3.2", "crossterm_winapi", "libc", "mio", @@ -153,19 +263,104 @@ version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "545b22097d44f8a9581187cdf93de7a71e4722bf51200cfaba810865b49a495d" +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "error-code" +version = "3.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d9305ccc6942a704f4335694ecd3de2ea531b114ac2d51f5f843750787a92f" + +[[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "flate2" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + +[[package]] +name = "gethostname" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818" +dependencies = [ + "libc", + "windows-targets 0.48.5", +] + [[package]] name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "image" +version = "0.25.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6f44aed642f18953a158afeb30206f4d50da59fbc66ecb53c66488de73563b" +dependencies = [ + "bytemuck", + "byteorder-lite", + "num-traits", + "png", + "tiff", +] + [[package]] name = "inquire" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c33e7c1ddeb15c9abcbfef6029d8e29f69b52b6d6c891031b88ed91b5065803b" dependencies = [ - "bitflags", + "bitflags 1.3.2", "crossterm", "dyn-clone", "lazy_static", @@ -175,6 +370,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "jpeg-decoder" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0" + [[package]] name = "lazy_static" version = "1.4.0" @@ -187,6 +388,12 @@ version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "lock_api" version = "0.4.11" @@ -203,6 +410,16 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "mio" version = "0.8.9" @@ -224,6 +441,114 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "objc-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb91bdd390c7ce1a8607f35f3ca7151b65afc0ff5ff3b34fa350f7d7c7e4310" + +[[package]] +name = "objc2" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46a785d4eeff09c14c487497c162e92766fbb3e4059a71840cecc03d9a50b804" +dependencies = [ + "objc-sys", + "objc2-encode", +] + +[[package]] +name = "objc2-app-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff" +dependencies = [ + "bitflags 2.6.0", + "block2", + "libc", + "objc2", + "objc2-core-data", + "objc2-core-image", + "objc2-foundation", + "objc2-quartz-core", +] + +[[package]] +name = "objc2-core-data" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef" +dependencies = [ + "bitflags 2.6.0", + "block2", + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-core-image" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80" +dependencies = [ + "block2", + "objc2", + "objc2-foundation", + "objc2-metal", +] + +[[package]] +name = "objc2-encode" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7891e71393cd1f227313c9379a26a584ff3d7e6e7159e988851f0934c993f0f8" + +[[package]] +name = "objc2-foundation" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8" +dependencies = [ + "bitflags 2.6.0", + "block2", + "libc", + "objc2", +] + +[[package]] +name = "objc2-metal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6" +dependencies = [ + "bitflags 2.6.0", + "block2", + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a" +dependencies = [ + "bitflags 2.6.0", + "block2", + "objc2", + "objc2-foundation", + "objc2-metal", +] + [[package]] name = "parking_lot" version = "0.12.1" @@ -247,6 +572,19 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "png" +version = "0.17.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67582bd5b65bdff614270e2ea89a1cf15bef71245cc1e5f7ea126977144211d" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide", +] + [[package]] name = "proc-macro2" version = "1.0.70" @@ -271,7 +609,20 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" dependencies = [ - "bitflags", + "bitflags 1.3.2", +] + +[[package]] +name = "rustix" +version = "0.38.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", ] [[package]] @@ -310,6 +661,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simd-adler32" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" + [[package]] name = "smallvec" version = "1.11.2" @@ -353,6 +710,17 @@ dependencies = [ "syn", ] +[[package]] +name = "tiff" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e" +dependencies = [ + "flate2", + "jpeg-decoder", + "weezl", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -383,6 +751,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "weezl" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" + [[package]] name = "winapi" version = "0.3.9" @@ -536,3 +910,20 @@ name = "windows_x86_64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" + +[[package]] +name = "x11rb" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d91ffca73ee7f68ce055750bf9f6eca0780b8c85eff9bc046a3b0da41755e12" +dependencies = [ + "gethostname", + "rustix", + "x11rb-protocol", +] + +[[package]] +name = "x11rb-protocol" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec107c4503ea0b4a98ef47356329af139c0a4f7750e621cf2973cd3385ebcb3d" diff --git a/Cargo.toml b/Cargo.toml index c7207c5..16120b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ name = "ch" path = "src/main.rs" [dependencies] +arboard = "3.4.1" clap = { version = "4.4.12", features = ["derive"] } inquire = { version = "0.6.2" } diff --git a/README.md b/README.md index b01ffbb..615e66a 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,14 @@ If you want to pass in a custom commit message (therefore skipping the interacti ch -m "feat: add new feature" ``` +### `--clipboard` or `-c` + +If you want to copy the commit message to your clipboard **and skip committing and pushing**: + +```bash +ch -c +``` + ### `--debug` or `-d` If you want to see the debug output, use the `--debug` or `-d` flag: diff --git a/src/main.rs b/src/main.rs index 880e84f..a23361c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,8 @@ fn main() { // Confirm the commit message let confirm = - Confirm::new(format!("Result:\n\n{}\n\nCommit? (y/n):", result_message).as_str()).prompt(); + Confirm::new(format!("Result:\n\n{}\n\nRun commands? (y/n):", result_message).as_str()) + .prompt(); // If confirmed, run the commands in succession match confirm { diff --git a/src/parser.rs b/src/parser.rs index f0892c1..48c4743 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -11,10 +11,18 @@ pub struct Cli { #[arg(short = 'a', long = "add")] pub add: bool, + /// Copy commit message to clipboard instead of committing + #[arg(short = 'c', long = "clipboard")] + pub clipboard: bool, + /// Run `git push` after committing #[arg(short = 'p', long = "push")] pub push: bool, + /// Add the `-S` flag when running `git commit` for signing + #[arg(short = 's', long = "sign")] + pub sign: bool, + /// Run in debug mode (print output of each command) #[arg(short = 'd', long = "debug")] pub debug: bool, diff --git a/src/util.rs b/src/util.rs index 887fdd7..02bbcac 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,5 @@ use super::parser::Cli; +use arboard::Clipboard; use std::process::Command; // Run the commands in succession @@ -8,12 +9,25 @@ pub fn orchestrate_commit(cli: &Cli, message: &str) { run(cli, "git", &["add", "-A"]); } - println!("Running git commit -m \"{}\"", message); - run(cli, "git", &["commit", "-m", message]); + if cli.clipboard { + println!("Copying commit message to clipboard"); + if !cli.dry_run { + let mut clipboard = Clipboard::new().unwrap(); + clipboard.set_text(message).unwrap(); + } + } else { + println!("Running git commit -m \"{}\"", message); + let args = if cli.sign { + vec!["commit", "-m", message] + } else { + vec!["commit", "-S", "-m", message] + }; + run(cli, "git", &args); - if cli.push { - println!("Running git push"); - run(cli, "git", &["push"]); + if cli.push { + println!("Running git push"); + run(cli, "git", &["push"]); + } } println!("Done 🎉"); From 6ffb55be752ef8340cf354b5d7e6ab0d765f057a Mon Sep 17 00:00:00 2001 From: rzmk <30333942+rzmk@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:25:15 -0500 Subject: [PATCH 11/12] docs: mention -s flag in README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 615e66a..a3dc099 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,14 @@ If you want to do a dry run without executing any commands, use the `--dry-run` ch --dry-run ``` +### `--sign` or `-s` + +If you want to run the `-S` flag when using `git commit`: + +```bash +ch -s +``` + ## Example If I want to run `git add -A`, then `git commit -m "feat: add new feature"`, then `git push` all in one go, I could run the following command: From 490148727d7f656730007fc9316d2072d539e0fc Mon Sep 17 00:00:00 2001 From: rzmk <30333942+rzmk@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:30:40 -0500 Subject: [PATCH 12/12] docs: remove tech stack section from README --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index a3dc099..80e2faf 100644 --- a/README.md +++ b/README.md @@ -101,9 +101,3 @@ ch -apm "feat: add new feature" ``` > Note: The order of the combined short flags does not matter, except for the `-m` flag, which must be the last flag if you want to pass in a custom commit message after a combination of flags. - -## Tech Stack - -- [Rust](https://www.rust-lang.org/) -- [clap](https://github.com/clap-rs/clap) -- [inquire](https://github.com/mikaelmello/inquire)