move all git method in crate, reduce main deps

This commit is contained in:
Stephan Dilly 2020-03-23 00:14:57 +01:00
parent 491fca6926
commit 9580f8b489
9 changed files with 47 additions and 27 deletions

1
Cargo.lock generated
View File

@ -290,7 +290,6 @@ dependencies = [
"crossbeam-channel",
"crossterm 0.15.0",
"dirs",
"git2",
"itertools 0.9.0",
"log",
"scopetime",

View File

@ -8,7 +8,6 @@ homepage = "https://gitui.org"
edition = "2018"
[dependencies]
git2 = "0.10"
crossterm = "0.15"
itertools = "0.9"
log = "0.4"

View File

@ -1,7 +1,13 @@
mod diff;
mod status;
mod utils;
pub use crate::{
diff::{get_diff, Diff, DiffLine, DiffLineType},
status::{get_index, StatusItem, StatusItemType, StatusType},
utils::{commit, index_reset, stage_add, stage_reset},
};
use crossbeam_channel::Sender;
pub use diff::{get_diff, Diff, DiffLine, DiffLineType};
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},

View File

@ -1,7 +1,8 @@
use crate::git_utils;
use crate::utils;
use git2::{Status, StatusOptions, StatusShow};
use scopetime::scope_time;
///
#[derive(PartialEq, Copy, Clone)]
pub enum StatusItemType {
New,
@ -27,21 +28,39 @@ impl From<Status> for StatusItemType {
}
}
///
#[derive(Default, PartialEq, Clone)]
pub struct StatusItem {
pub path: String,
pub status: Option<StatusItemType>,
}
pub fn get_index(show: StatusShow) -> Vec<StatusItem> {
///
#[derive(Copy, Clone)]
pub enum StatusType {
WorkingDir,
Stage,
}
impl Into<StatusShow> for StatusType {
fn into(self) -> StatusShow {
match self {
StatusType::WorkingDir => StatusShow::Workdir,
StatusType::Stage => StatusShow::Index,
}
}
}
///
pub fn get_index(status_type: StatusType) -> Vec<StatusItem> {
scope_time!("get_index");
let repo = git_utils::repo();
let repo = utils::repo();
let statuses = repo
.statuses(Some(
StatusOptions::default()
.show(show)
.show(status_type.into())
.include_untracked(true)
.renames_head_to_index(true)
.recurse_untracked_dirs(true),

View File

@ -46,6 +46,7 @@ pub fn commit(msg: &String) {
.unwrap();
}
///
pub fn stage_add(path: &Path) -> bool {
scope_time!("stage_add");
@ -73,6 +74,7 @@ pub fn stage_add(path: &Path) -> bool {
false
}
///
pub fn stage_reset(path: &Path) -> bool {
scope_time!("stage_reset");
@ -93,6 +95,7 @@ pub fn stage_reset(path: &Path) -> bool {
false
}
///
pub fn index_reset(path: &Path) -> bool {
scope_time!("index_reset");

View File

@ -3,12 +3,11 @@ use crate::{
CommandInfo, CommitComponent, Component, DiffComponent,
IndexComponent,
},
git_utils, keys, strings,
keys, strings,
};
use asyncgit::AsyncDiff;
use asyncgit::{AsyncDiff, StatusType};
use crossbeam_channel::Sender;
use crossterm::event::Event;
use git2::StatusShow;
use itertools::Itertools;
use log::trace;
use std::{borrow::Cow, path::Path};
@ -57,12 +56,12 @@ impl App {
commit: CommitComponent::default(),
index_wd: IndexComponent::new(
strings::TITLE_STATUS,
StatusShow::Workdir,
StatusType::WorkingDir,
true,
),
index: IndexComponent::new(
strings::TITLE_INDEX,
StatusShow::Index,
StatusType::Stage,
false,
),
diff: DiffComponent::default(),
@ -354,7 +353,7 @@ impl App {
if let Some(i) = self.index_wd.selection() {
let path = Path::new(i.path.as_str());
if git_utils::stage_add(path) {
if asyncgit::stage_add(path) {
self.update();
}
}
@ -362,7 +361,7 @@ impl App {
if let Some(i) = self.index.selection() {
let path = Path::new(i.path.as_str());
if git_utils::stage_reset(path) {
if asyncgit::stage_reset(path) {
self.update();
}
}
@ -374,7 +373,7 @@ impl App {
if let Some(i) = self.index_wd.selection() {
let path = Path::new(i.path.as_str());
if git_utils::index_reset(path) {
if asyncgit::index_reset(path) {
self.update();
}
}

View File

@ -1,5 +1,5 @@
use super::{CommandInfo, Component};
use crate::{git_utils, strings, ui};
use crate::{strings, ui};
use crossterm::event::{Event, KeyCode};
use std::borrow::Cow;
use tui::{
@ -100,7 +100,7 @@ impl Component for CommitComponent {
impl CommitComponent {
fn commit(&mut self) {
git_utils::commit(&self.msg);
asyncgit::commit(&self.msg);
self.msg.clear();
self.hide();

View File

@ -1,10 +1,7 @@
use crate::components::{CommandInfo, Component};
use crate::{
git_status::{self, StatusItem, StatusItemType},
ui,
};
use crate::ui;
use asyncgit::{StatusItem, StatusItemType, StatusType};
use crossterm::event::{Event, KeyCode};
use git2::StatusShow;
use std::{borrow::Cow, cmp};
use tui::{
backend::Backend,
@ -18,7 +15,7 @@ use tui::{
pub struct IndexComponent {
title: String,
items: Vec<StatusItem>,
index_type: StatusShow,
index_type: StatusType,
selection: Option<usize>,
focused: bool,
show_selection: bool,
@ -28,7 +25,7 @@ impl IndexComponent {
///
pub fn new(
title: &str,
index_type: StatusShow,
index_type: StatusType,
focus: bool,
) -> Self {
Self {
@ -42,7 +39,7 @@ impl IndexComponent {
}
///
pub fn update(&mut self) {
let new_status = git_status::get_index(self.index_type);
let new_status = asyncgit::get_index(self.index_type.into());
if self.items != new_status {
self.items = new_status;

View File

@ -1,7 +1,5 @@
mod app;
mod components;
mod git_status;
mod git_utils;
mod keys;
mod poll;
mod strings;