Add status command

This commit is contained in:
2021-11-19 22:12:31 +01:00
parent c0172f9af5
commit 5f0ec0fec8
6 changed files with 542 additions and 2 deletions

View File

@@ -26,6 +26,8 @@ pub enum SubCommand {
Sync(Sync),
#[clap(about = "Generate a repository configuration from an existing file tree")]
Find(Find),
#[clap(about = "Show status of configured repositories")]
Status(OptionalConfig),
}
#[derive(Parser)]
@@ -40,6 +42,17 @@ pub struct Sync {
pub config: String,
}
#[derive(Parser)]
#[clap()]
pub struct OptionalConfig {
#[clap(
short,
long,
about = "Path to the configuration file"
)]
pub config: Option<String>,
}
#[derive(Parser)]
pub struct Find {
#[clap(about = "The path to search through")]

View File

@@ -10,7 +10,12 @@ mod repo;
use config::{Config, Tree};
use output::*;
use repo::{clone_repo, detect_remote_type, init_repo, open_repo, Remote, Repo};
use comfy_table::{Table, Cell};
use repo::{
clone_repo, detect_remote_type, get_repo_status, init_repo, open_repo, Remote, Repo,
RepoErrorKind, RemoteTrackingStatus
};
fn path_as_string(path: &Path) -> String {
path.to_path_buf().into_os_string().into_string().unwrap()
@@ -358,6 +363,137 @@ fn find_in_tree(path: &Path) -> Option<Tree> {
})
}
fn add_table_header(table: &mut Table) {
table
.load_preset(comfy_table::presets::UTF8_FULL)
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS)
.set_header(vec![
Cell::new("Repo"),
Cell::new("Status"),
Cell::new("Branches"),
Cell::new("HEAD"),
Cell::new("Remotes"),
]);
}
fn add_repo_status(table: &mut Table, repo_name: &String, repo_handle: &git2::Repository) {
let repo_status = get_repo_status(repo_handle);
table.add_row(vec![
repo_name,
&match repo_status.changes {
Some(changes) => {
let mut out = Vec::new();
if changes.files_new > 0 {
out.push(format!("New: {}\n", changes.files_new))
}
if changes.files_modified > 0 {
out.push(format!("Modified: {}\n", changes.files_modified))
}
if changes.files_deleted > 0 {
out.push(format!("Deleted: {}\n", changes.files_deleted))
}
out.into_iter().collect::<String>().trim().to_string()
},
None => String::from("No changes"),
},
&repo_status.branches.iter().map(|(branch_name, remote_branch)| {
format!("branch: {}{}\n",
&branch_name,
&match remote_branch {
None => String::from(" <!local>"),
Some((remote_branch_name, remote_tracking_status)) => {
format!(" <{}>{}",
remote_branch_name,
&match remote_tracking_status {
RemoteTrackingStatus::UpToDate => String::from(" \u{2714}"),
RemoteTrackingStatus::Ahead(d) => format!(" [+{}]", &d),
RemoteTrackingStatus::Behind(d) => format!(" [-{}]", &d),
RemoteTrackingStatus::Diverged(d1, d2) => format!(" [-{}/+{}]", &d1,&d2),
}
)
}
}
)
}).collect::<String>().trim().to_string(),
&match repo_status.head {
Some(head) => head,
None => String::from("Empty"),
},
&repo_status.remotes.iter().map(|r| format!("{}\n", r)).collect::<String>().trim().to_string(),
]);
}
fn show_single_repo_status(path: &PathBuf) {
let mut table = Table::new();
add_table_header(&mut table);
let repo_handle = open_repo(path);
if let Err(error) = repo_handle {
if error.kind == RepoErrorKind::NotFound {
print_error(&"Directory is not a git directory".to_string());
} else {
print_error(&format!("Opening repository failed: {}", error));
}
process::exit(1);
};
let repo_name = match path.file_name() {
None => {
print_warning("Cannot detect repo name. Are you working in /?");
String::from("unknown")
},
Some(file_name) => match file_name.to_str() {
None => {
print_warning("Name of current directory is not valid UTF-8");
String::from("invalid")
},
Some(name) => name.to_string(),
}
};
add_repo_status(&mut table, &repo_name, &repo_handle.unwrap());
println!("{}", table);
}
fn show_status(config: Config) {
for tree in config.trees {
let repos = tree.repos.unwrap_or_default();
let root_path = expand_path(Path::new(&tree.root));
let mut table = Table::new();
add_table_header(&mut table);
for repo in &repos {
let repo_path = root_path.join(&repo.name);
if !repo_path.exists() {
print_repo_error(&repo.name, &"Repository does not exist. Run sync?".to_string());
continue;
}
let repo_handle = open_repo(&repo_path);
if let Err(error) = repo_handle {
if error.kind == RepoErrorKind::NotFound {
print_repo_error(&repo.name, &"No git repository found. Run sync?".to_string());
} else {
print_repo_error(&repo.name, &format!("Opening repository failed: {}", error));
}
continue;
};
let repo_handle = repo_handle.unwrap();
add_repo_status(&mut table, &repo.name, &repo_handle);
}
println!("{}", table);
}
}
pub fn run() {
let opts = cmd::parse();
@@ -372,6 +508,31 @@ pub fn run() {
};
sync_trees(config);
}
cmd::SubCommand::Status(args) => {
match &args.config {
Some(config_path) => {
let config = match config::read_config(config_path) {
Ok(c) => c,
Err(e) => {
print_error(&e);
process::exit(1);
}
};
show_status(config);
},
None => {
let dir = match std::env::current_dir(){
Ok(d) => d,
Err(e) => {
print_error(&format!("Could not open current directory: {}", e));
process::exit(1);
},
};
show_single_repo_status(&dir);
}
}
}
cmd::SubCommand::Find(find) => {
let path = Path::new(&find.path);
if !path.exists() {

View File

@@ -53,6 +53,44 @@ pub struct Repo {
pub remotes: Option<Vec<Remote>>,
}
pub struct RepoChanges {
pub files_new: usize,
pub files_modified: usize,
pub files_deleted: usize,
}
pub enum SubmoduleStatus {
Clean,
Uninitialized,
Changed,
OutOfDate,
}
pub enum RemoteTrackingStatus {
UpToDate,
Ahead(usize),
Behind(usize),
Diverged(usize, usize),
}
pub struct RepoStatus {
pub operation: Option<git2::RepositoryState>,
pub empty: bool,
pub remotes: Vec<String>,
pub head: Option<String>,
pub changes: Option<RepoChanges>,
pub worktrees: usize,
pub submodules: Vec<(String, SubmoduleStatus)>,
pub branches: Vec<(String, Option<(String, RemoteTrackingStatus)>)>,
}
#[cfg(test)]
mod tests {
use super::*;
@@ -178,3 +216,130 @@ pub fn clone_repo(remote: &Remote, path: &Path) -> Result<(), Box<dyn std::error
}
}
}
pub fn get_repo_status(repo: &git2::Repository) -> RepoStatus {
let operation = match repo.state() {
git2::RepositoryState::Clean => None,
state => Some(state),
};
let empty = repo.is_empty().unwrap();
let remotes = repo
.remotes()
.unwrap()
.iter()
.map(|repo_name| repo_name.unwrap().to_string())
.collect::<Vec<String>>();
let head = match empty {
true => None,
false => Some(repo.head().unwrap().shorthand().unwrap().to_string()),
};
let statuses = repo.statuses(None).unwrap();
let changes = match statuses.is_empty() {
true => None,
false => {
let mut files_new = 0;
let mut files_modified = 0;
let mut files_deleted = 0;
for status in statuses.iter() {
let status_bits = status.status();
if status_bits.intersects(
git2::Status::INDEX_MODIFIED
| git2::Status::INDEX_RENAMED
| git2::Status::INDEX_TYPECHANGE
| git2::Status::WT_MODIFIED
| git2::Status::WT_RENAMED
| git2::Status::WT_TYPECHANGE,
) {
files_modified += 1;
} else if status_bits.intersects(git2::Status::INDEX_NEW | git2::Status::WT_NEW) {
files_new += 1;
} else if status_bits
.intersects(git2::Status::INDEX_DELETED | git2::Status::WT_DELETED)
{
files_deleted += 1;
}
}
Some(RepoChanges {
files_new,
files_modified,
files_deleted,
})
}
};
let worktrees = repo.worktrees().unwrap().len();
let mut submodules = Vec::new();
for submodule in repo.submodules().unwrap() {
let submodule_name = submodule.name().unwrap().to_string();
let submodule_status;
let status = repo
.submodule_status(submodule.name().unwrap(), git2::SubmoduleIgnore::None)
.unwrap();
if status.intersects(
git2::SubmoduleStatus::WD_INDEX_MODIFIED
| git2::SubmoduleStatus::WD_WD_MODIFIED
| git2::SubmoduleStatus::WD_UNTRACKED,
) {
submodule_status = SubmoduleStatus::Changed;
} else if status.is_wd_uninitialized() {
submodule_status = SubmoduleStatus::Uninitialized;
} else if status.is_wd_modified() {
submodule_status = SubmoduleStatus::OutOfDate;
} else {
submodule_status = SubmoduleStatus::Clean;
}
submodules.push((submodule_name, submodule_status));
}
let mut branches = Vec::new();
for (local_branch, _) in repo
.branches(Some(git2::BranchType::Local))
.unwrap()
.map(|branch_name| branch_name.unwrap())
{
let branch_name = local_branch.name().unwrap().unwrap().to_string();
let remote_branch = match local_branch.upstream() {
Ok(remote_branch) => {
let remote_branch_name = remote_branch.name().unwrap().unwrap().to_string();
let (ahead, behind) = repo
.graph_ahead_behind(
local_branch.get().peel_to_commit().unwrap().id(),
remote_branch.get().peel_to_commit().unwrap().id(),
)
.unwrap();
let remote_tracking_status = match (ahead, behind) {
(0, 0) => RemoteTrackingStatus::UpToDate,
(0, d) => RemoteTrackingStatus::Behind(d),
(d, 0) => RemoteTrackingStatus::Ahead(d),
(d1, d2) => RemoteTrackingStatus::Diverged(d1, d2),
};
Some((remote_branch_name, remote_tracking_status))
}
// Err => no remote branch
Err(_) => None,
};
branches.push((branch_name, remote_branch));
}
RepoStatus {
operation,
empty,
remotes,
head,
changes,
worktrees,
submodules,
branches,
}
}