Skip to content

Commit

Permalink
feat(cli): improve git statuses display
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 27, 2020
1 parent ba4a2cf commit cf380e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,10 @@ impl CocoGitto {

// Fail if repo contains un-staged or un-committed changes
if !statuses.is_empty() {
let statuses = statuses
.iter()
.map(|status| format!("{} : {:?}\n", status.path().unwrap(), status.status()))
.collect::<String>();
return Err(anyhow!(
"Repository contains unstaged change :\n{}",
statuses
"{}\n{}",
"repository contains unstaged change (use `git add` to track)",
self.repository.statuses_display()?,
));
}

Expand Down
20 changes: 12 additions & 8 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ impl Repository {
.commit(Some("HEAD"), &sig, &sig, &message, &tree, &[])
.map_err(|err| anyhow!(err))
} else {
self.print_statues()?;
Err(anyhow!("nothing to commit (use \"git add\" to track)"))
Err(anyhow!(
"{}, nothing to commit (use \"git add\" to track)",
self.statuses_display()?
))
}
}

Expand Down Expand Up @@ -147,9 +149,10 @@ impl Repository {

pub(crate) fn create_tag(&self, name: &str) -> Result<()> {
if self.get_diff(true).is_some() {
self.print_statues()?;
return Err(anyhow!(
"cannot create tag : changes needs to be commited".red()
"{}{}",
self.statuses_display()?,
"Cannot create tag : changes needs to be commited".red()
));
}

Expand Down Expand Up @@ -207,8 +210,10 @@ impl Repository {
Ok(Some(tree))
}

fn print_statues(&self) -> Result<()> {
pub fn statuses_display(&self) -> Result<String> {
let statuses = self.get_statuses()?;
//TODO : implement fmt display and use a proper statuses wrapper struct
let mut out = String::new();
statuses.iter().for_each(|entry| {
let status = match entry.status() {
s if s.contains(git2::Status::WT_NEW) => "Untracked: ",
Expand All @@ -223,10 +228,9 @@ impl Repository {
s if s.contains(git2::Status::INDEX_TYPECHANGE) => "Typechange:",
_ => "unknown git status",
};
println!("{} {}", status.red(), entry.path().unwrap());
out.push_str(&format!("{} {}\n", status.red(), entry.path().unwrap()));
});
println!();
Ok(())
Ok(out)
}
}

Expand Down

0 comments on commit cf380e6

Please sign in to comment.