summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTim Oram <dev@mitmaro.ca>2018-07-10 13:27:40 -0230
committerTim Oram <dev@mitmaro.ca>2018-07-10 13:27:40 -0230
commit4e8e950e6fe17047feca4352652a78e793d5fe66 (patch)
treeef538fa16658fd31c7c78748a9e6a53db0ee632d /src
parent973f87a2c6845805a7e59247c08f5dc7f85c9ed1 (diff)
Add support for core.commentChar
Having the configuration option core.commentChar set to anything other than # would result in the tool being unable to read the rebase tool. This change reads the configuration value and uses it over the hard-coded value.
Diffstat (limited to 'src')
-rw-r--r--src/application.rs41
-rw-r--r--src/git_config.rs44
-rw-r--r--src/git_interactive.rs4
-rw-r--r--src/main.rs15
4 files changed, 85 insertions, 19 deletions
diff --git a/src/application.rs b/src/application.rs
index 4d218e6..e5805b0 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -171,7 +171,7 @@ mod tests {
#[test]
fn application_read_all_actions() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let app = Application::new(gi, window);
assert_eq!(app.git_interactive.get_lines().len(), 12);
@@ -179,7 +179,7 @@ mod tests {
#[test]
fn application_show_help() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('?');
@@ -190,7 +190,7 @@ mod tests {
#[test]
fn application_show_commit() {
// first commit in
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-show-commit.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-show-commit.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('c');
@@ -200,7 +200,7 @@ mod tests {
#[test]
fn application_scroll_basic() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-long.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-long.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::KeyDown;
@@ -216,10 +216,10 @@ mod tests {
app.process_input();
assert_eq!(*app.git_interactive.get_selected_line_index(), 1);
}
-
+
#[test]
fn application_scroll_limits() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-short.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-short.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::KeyUp;
@@ -241,7 +241,7 @@ mod tests {
#[test]
fn application_set_pick() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
// first item is already pick
@@ -254,7 +254,7 @@ mod tests {
#[test]
fn application_set_reword() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('r');
@@ -264,7 +264,7 @@ mod tests {
#[test]
fn application_set_edit() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('e');
@@ -274,7 +274,7 @@ mod tests {
#[test]
fn application_set_squash() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('s');
@@ -284,7 +284,7 @@ mod tests {
#[test]
fn application_set_drop() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('d');
@@ -294,7 +294,7 @@ mod tests {
#[test]
fn application_swap_down() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('j');
@@ -306,7 +306,7 @@ mod tests {
#[test]
fn application_swap_up() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::KeyDown;
@@ -320,7 +320,7 @@ mod tests {
#[test]
fn application_quit() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('Q');
@@ -331,7 +331,18 @@ mod tests {
#[test]
fn application_finish() {
- let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in").unwrap();
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap();
+ let window = Window::new();
+ let mut app = Application::new(gi, window);
+ app.window.window.next_char = Input::Character('W');
+ app.process_input();
+ assert_eq!(app.exit_code.unwrap(), 0);
+ assert!(!app.git_interactive.get_lines().is_empty());
+ }
+
+ #[test]
+ fn application_alternative_comment_character() {
+ let gi = GitInteractive::new_from_filepath("test/git-rebase-alternative-comment-character.in", "%").unwrap();
let window = Window::new();
let mut app = Application::new(gi, window);
app.window.window.next_char = Input::Character('W');
diff --git a/src/git_config.rs b/src/git_config.rs
new file mode 100644
index 0000000..6487e64
--- /dev/null
+++ b/src/git_config.rs
@@ -0,0 +1,44 @@
+use git2::ConfigLevel;
+use git2::Config;
+use std::env;
+use std::path::Path;
+
+pub struct GitConfig {
+ pub comment_char: String,
+}
+
+impl GitConfig {
+ pub fn new() -> Result<Self, String> {
+ let cfg = Config::open_default();
+
+ match cfg {
+ Ok(mut config) => {
+ match env::var_os("GIT_DIR") {
+ Some(val) => match val.into_string() {
+ Ok(s) => {
+ let mut p = s.to_owned();
+ p.push_str("/config");
+ match config.add_file(Path::new(&p), ConfigLevel::Local, false) {
+ Ok(_v) => {}, Err(_e) => {}
+ }
+ },
+ Err(_e) => {}
+ },
+ None => {}
+ }
+
+ Ok(GitConfig {
+ comment_char: match config.get_string("core.commentChar") {
+ Ok(comment_char_value) => comment_char_value,
+ Err(_msg) => String::from("#")
+ }
+ })
+ },
+ Err(msg) => {
+ Err(format!(
+ "Error reading git config, Reason {}\n", msg
+ ))
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/git_interactive.rs b/src/git_interactive.rs
index e60118b..e6f3897 100644
--- a/src/git_interactive.rs
+++ b/src/git_interactive.rs
@@ -16,7 +16,7 @@ pub struct GitInteractive {
}
impl GitInteractive {
- pub fn new_from_filepath(filepath: &str) -> Result<Self, String> {
+ pub fn new_from_filepath(filepath: &str, comment_char: &str) -> Result<Self, String> {
let path = PathBuf::from(filepath);
let mut file = match File::open(&path) {
@@ -48,7 +48,7 @@ impl GitInteractive {
Some("noop") => Ok(Vec::new()),
_ => {
s.lines()
- .filter(|l| !l.starts_with('#') && !l.is_empty())
+ .filter(|l| !l.starts_with(comment_char) && !l.is_empty())
.map(|l| Line::new(l))
.collect()
}
diff --git a/src/main.rs b/src/main.rs
index 85f5632..2fc1cee 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@
#![cfg_attr(feature="clippy", plugin(clippy))]
extern crate clap;
+extern crate git2;
extern crate pad;
extern crate pancurses;
@@ -9,6 +10,7 @@ mod action;
mod application;
mod cli;
mod commit;
+mod git_config;
mod git_interactive;
mod line;
mod window;
@@ -16,6 +18,7 @@ mod window;
mod mocks;
use application::Application;
+use git_config::GitConfig;
use git_interactive::GitInteractive;
use std::process;
use window::Window;
@@ -24,8 +27,16 @@ fn main() {
let matches = cli::build_cli().get_matches();
let filepath = matches.value_of("rebase-todo-filepath").unwrap();
-
- let git_interactive = match GitInteractive::new_from_filepath(filepath) {
+
+ let git_config = match GitConfig::new() {
+ Ok(gc) => gc,
+ Err(msg) => {
+ eprintln!("{}", msg);
+ process::exit(1)
+ }
+ };
+
+ let git_interactive = match GitInteractive::new_from_filepath(filepath, &git_config.comment_char) {
Ok(gi) => gi,
Err(msg) => {
eprintln!("{}", msg);