summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 6ecf0e7a2443f7ad669e0c190f5cf2751fa4e8cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Make rustc's built-in lints more strict and set clippy into a whitelist-based configuration
#![deny(
	warnings,
	nonstandard_style,
	unused,
	future_incompatible,
	rust_2018_idioms,
	unsafe_code
)]
#![deny(clippy::all, clippy::cargo, clippy::nursery, clippy::pedantic, clippy::restriction)]
#![allow(clippy::blanket_clippy_restriction_lints)]
#![allow(clippy::as_conversions)]
#![allow(clippy::blocks_in_if_conditions)] // sometimes rustfmt makes blocks out of simple statements
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::else_if_without_else)]
#![allow(clippy::expect_used)]
#![allow(clippy::float_arithmetic)]
#![allow(clippy::implicit_return)]
#![allow(clippy::indexing_slicing)]
#![allow(clippy::integer_arithmetic)]
#![allow(clippy::integer_division)]
#![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::non_ascii_literal)]
#![allow(clippy::panic)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::wildcard_enum_match_arm)]
#![allow(clippy::similar_names)]
#![allow(clippy::unreachable)]
#![allow(clippy::missing_panics_doc)]

mod components;
mod config;
mod confirm_abort;
mod confirm_rebase;
mod display;
mod external_editor;
mod input;
mod insert;
mod list;
mod process;
mod show_commit;
mod todo_file;
mod view;

#[cfg(test)]
pub mod testutil;

use clap::{App, Arg};

use crate::{
	config::Config,
	display::{CrossTerm, Display},
	input::input_handler::InputHandler,
	process::{exit_status::ExitStatus, modules::Modules, Process},
	todo_file::TodoFile,
	view::View,
};

const NAME: &str = "interactive-rebase-tool";
#[cfg(not(feature = "dev"))]
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg(feature = "dev")]
const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "-dev");

#[derive(Debug)]
pub struct Exit {
	message: String,
	status: ExitStatus,
}

// TODO use the termination trait once rust-lang/rust#43301 is stable
#[allow(clippy::exit, clippy::print_stderr)]
fn main() {
	let app = App::new(NAME)
		.version(VERSION)
		.about("Full feature terminal based sequence editor for git interactive rebase.")
		.author("Tim Oram <dev@mitmaro.ca>")
		.arg(
			Arg::with_name("license")
				.long("license")
				.help("Print license information and exit")
				.conflicts_with("<rebase-todo-filepath>"),
		)
		.arg(
			Arg::with_name("rebase-todo-filepath")
				.index(1)
				.help("The path to the git rebase todo file")
				.required_unless_one(&["license"]),
		);

	let matches = app.get_matches();

	if matches.is_present("license") {
		print_license();
		std::process::exit(ExitStatus::Good.to_code());
	}

	let filepath = matches.value_of("rebase-todo-filepath").unwrap();

	match try_main(filepath) {
		Ok(code) => std::process::exit(code.to_code()),
		Err(err) => {
			eprintln!("{}", err.message);
			std::process::exit(err.status.to_code());
		},
	}
}

#[allow(clippy::print_stdout)]
fn print_license() {
	println!(
		r#"
Sequence Editor for Git Interactive Rebase

Copyright (C) 2017-2020 Tim Oram and Contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

A list of open source software and the license terms can be found at
<https://gitrebasetool.mitmaro.ca/licenses.html>
		"#
	);
}

fn try_main(filepath: &str) -> Result<ExitStatus, Exit> {
	let config = Config::new().map_err(|err| {
		Exit {
			message: format!("{:#}", err),
			status: ExitStatus::ConfigError,
		}
	})?;

	let mut todo_file = TodoFile::new(filepath, config.undo_limit, config.git.comment_char.as_str());
	todo_file.load_file().map_err(|err| {
		Exit {
			message: err.to_string(),
			status: ExitStatus::FileReadError,
		}
	})?;

	if todo_file.is_noop() {
		return Err(Exit {
			message: String::from("A noop rebase was provided, skipping editing"),
			status: ExitStatus::Good,
		});
	}

	if todo_file.is_empty() {
		return Err(Exit {
			message: String::from("An empty rebase was provided, nothing to edit"),
			status: ExitStatus::Good,
		});
	}

	let mut crossterm = CrossTerm::new();
	let display = Display::new(&mut crossterm, &config.theme);
	let modules = Modules::new(&config);
	let mut process = Process::new(
		todo_file,
		View::new(InputHandler::new(&config.key_bindings), display, &config),
	);
	let result = process.run(modules);

	result
		.map_err(|err| {
			Exit {
				message: err.to_string(),
				status: ExitStatus::FileWriteError,
			}
		})
		.map(|exit_code| exit_code.unwrap_or(ExitStatus::Good))
}

#[cfg(all(unix, test))]
mod tests {
	use std::{env::set_var, fs::File, path::Path};

	use super::*;
	use crate::assert_exit_status;

	fn set_git_directory(repo: &str) -> String {
		let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("test").join(repo);
		set_var("GIT_DIR", path.to_str().unwrap());
		String::from(path.to_str().unwrap())
	}

	#[test]
	#[serial_test::serial]
	fn error_loading_config() {
		let path = set_git_directory("fixtures/invalid-config");
		assert_exit_status!(
			try_main("does-not-exist"),
			message = format!("Error loading git config: could not find repository from '{}'", path),
			status = ExitStatus::ConfigError
		);
	}

	#[test]
	#[serial_test::serial]
	fn error_loading_file() {
		set_git_directory("fixtures/simple");
		assert_exit_status!(
			try_main("does-not-exist"),
			message = "No such file or directory (os error 2)",
			status = ExitStatus::FileReadError
		);
	}

	#[test]
	#[serial_test::serial]
	fn error_noop() {
		let path = set_git_directory("fixtures/simple");
		let todo_file = Path::new(path.as_str()).join("rebase-todo-noop");
		assert_exit_status!(
			try_main(todo_file.to_str().unwrap()),
			message = "A noop rebase was provided, skipping editing",
			status = ExitStatus::Good
		);
	}

	#[test]
	#[serial_test::serial]
	fn error_empty_file() {
		let path = set_git_directory("fixtures/simple");
		let todo_file = Path::new(path.as_str()).join("rebase-todo-empty");
		assert_exit_status!(
			try_main(todo_file.to_str().unwrap()),
			message = "An empty rebase was provided, nothing to edit",
			status = ExitStatus::Good
		);
	}

	#[test]
	#[serial_test::serial]
	fn error_process() {
		CrossTerm::set_inputs(vec![create_key_event!('d', "Control")]);
		let path = set_git_directory("fixtures/simple");
		let todo_file_path = Path::new(path.as_str()).join("rebase-todo-readonly");
		let todo_file = File::open(todo_file_path.as_path()).unwrap();
		let mut permissions = todo_file.metadata().unwrap().permissions();
		permissions.set_readonly(true);
		todo_file.set_permissions(permissions).unwrap();
		assert_exit_status!(
			try_main(todo_file_path.to_str().unwrap()),
			message = format!("Error opening file: {}", todo_file_path.to_str().unwrap()),
			status = ExitStatus::FileWriteError
		);
	}
	#[test]
	#[serial_test::serial]
	fn success() {
		CrossTerm::set_inputs(vec![create_key_event!('d', "Control")]);
		let path = set_git_directory("fixtures/simple");
		let todo_file = Path::new(path.as_str()).join("rebase-todo");
		assert_exit_status!(try_main(todo_file.to_str().unwrap()), status = ExitStatus::Abort);
	}
}