Skip to content

🎉 Add support for --amend #23

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
174 changes: 158 additions & 16 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ readme = "readme.md"
license = "MIT"

[dependencies]
termion = "1.0"
termion = "4"
log-update = "~0.1.0"
default-editor = "~0.1.0"
emoji-commit-type = "~0.1.1"
git2 = "~0.15.0"
structopt = "~0.3"
ansi_term = "~0.12"

[dev-dependencies]
tempfile = "~3.14.0"

[profile.release]
codegen-units = 1
lto = true
Expand Down
39 changes: 19 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::env;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader, Seek, Write, stderr, stdin};
use std::io::{Seek, Write, stderr, stdin};
use std::process::{Command, exit};
use std::path::PathBuf;
use std::str::FromStr;
Expand All @@ -12,12 +12,14 @@ use termion::input::TermRead;
use termion::raw::IntoRawMode;

use emoji_commit_type::CommitType;
use message::{git_parse_existing_message, git_message_is_empty};
use log_update::LogUpdate;
use structopt::StructOpt;
use ansi_term::Colour::{RGB, Green, Red, White};

mod commit_rules;
mod git;
mod message;

impl fmt::Display for commit_rules::CommitRuleValidationResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -42,14 +44,14 @@ fn commit_type_at_index (index: u8) -> Option<CommitType> {
CommitType::iter_variants().nth(index as usize)
}

fn select_emoji() -> Option<&'static str> {
fn select_emoji(initial_selected: Option<CommitType>) -> Option<&'static str> {
let mut log_update = LogUpdate::new(stderr()).unwrap();
let mut raw_output = stderr().into_raw_mode().unwrap();

let mut key_stream = stdin().keys();

let mut aborted = false;
let mut selected = CommitType::Breaking;
let mut selected = initial_selected.unwrap_or(CommitType::Breaking);

// Clear possibly printed "hint" from git
log_update.render("").unwrap();
Expand All @@ -73,14 +75,14 @@ fn select_emoji() -> Option<&'static str> {
if aborted { None } else { Some(selected.emoji()) }
}

fn collect_commit_message(selected_emoji: &'static str, launch_editor: &mut bool) -> Option<String> {
fn collect_commit_message(selected_emoji: &'static str, initial_message: Option<String>, launch_editor: &mut bool) -> Option<String> {
let mut log_update = LogUpdate::new(stderr()).unwrap();
let mut raw_output = stderr().into_raw_mode().unwrap();

let mut key_stream = stdin().keys();

let mut aborted = false;
let mut input = String::new();
let mut input = initial_message.unwrap_or(String::new());

loop {
let rule_text = commit_rules::check_message(&input)
Expand Down Expand Up @@ -143,33 +145,30 @@ fn launch_git_with_self_as_editor() {
run_cmd(Command::new("git").arg("commit").env("GIT_EDITOR", self_path))
}

fn git_message_is_empty(file: &mut File) -> bool {
for line in BufReader::new(file).lines() {
let line = line.expect("Failed to read line from git message file");

if !line.starts_with('#') && !line.is_empty() {
return false;
}
}
true
}

fn collect_information_and_write_to_file(out_path: PathBuf) {
let mut file = File::options().read(true).write(true).create(true).open(&out_path).unwrap();
let mut initial_message: Option<String> = None;
let mut initial_commit_type: Option<CommitType> = None;

if !git_message_is_empty(&mut file) {
launch_default_editor(out_path);
return;
file.rewind().unwrap();
if let Some((commit_type, message)) = git_parse_existing_message(&mut file) {
initial_commit_type = Some(commit_type);
initial_message = Some(message);
} else {
launch_default_editor(out_path);
return;
}
}

let maybe_emoji = select_emoji();
let maybe_emoji = select_emoji(initial_commit_type);
if maybe_emoji == None {
abort();
}

if let Some(emoji) = maybe_emoji {
let mut launch_editor = false;
let maybe_message = collect_commit_message(emoji, &mut launch_editor);
let maybe_message = collect_commit_message(emoji, initial_message, &mut launch_editor);
if maybe_message == None {
abort();
}
Expand Down
Loading