Add repository name and current tag to gstat (#692)

* Add repository name to gstat

* Fix getting repo name; Add tag as well
This commit is contained in:
Jakub Žádník 2022-01-07 13:44:05 +02:00 committed by GitHub
parent f016a5cb72
commit f964ce9bc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
use git2::{Branch, BranchType, Repository}; use git2::{Branch, BranchType, DescribeOptions, Repository};
use nu_plugin::LabeledError; use nu_plugin::LabeledError;
use nu_protocol::{Span, Spanned, Value}; use nu_protocol::{Span, Spanned, Value};
use std::fmt::Write; use std::fmt::Write;
@ -126,14 +126,26 @@ impl GStat {
} }
}; };
let stats = Repository::discover(repo_path).map(|mut repo| (Stats::new(&mut repo))); let (stats, repo) = if let Ok(mut repo) = Repository::discover(repo_path) {
let stats = match stats { (Stats::new(&mut repo), repo)
Ok(s) => s, } else {
Err(_) => { return Ok(self.create_empty_git_status(span));
// Since we really never want this to fail, lets return an empty record so };
// that one can check it in a script and do something with it.
return Ok(self.create_empty_git_status(span)); let repo_name = repo
} .path()
.parent()
.and_then(|p| p.file_name())
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|| "".to_string());
let mut desc_opts = DescribeOptions::new();
desc_opts.describe_tags();
let tag = if let Ok(Ok(s)) = repo.describe(&desc_opts).map(|d| d.format(None)) {
s
} else {
"no_tag".to_string()
}; };
let mut cols = vec![]; let mut cols = vec![];
@ -214,6 +226,16 @@ impl GStat {
val: stats.stashes as i64, val: stats.stashes as i64,
span: *span, span: *span,
}); });
cols.push("repo_name".into());
vals.push(Value::String {
val: repo_name,
span: *span,
});
cols.push("tag".into());
vals.push(Value::String {
val: tag,
span: *span,
});
cols.push("branch".into()); cols.push("branch".into());
vals.push(Value::String { vals.push(Value::String {
val: stats.branch, val: stats.branch,
@ -321,6 +343,16 @@ impl GStat {
val: -1, val: -1,
span: *span, span: *span,
}); });
cols.push("repo_name".into());
vals.push(Value::String {
val: "no_repository".to_string(),
span: *span,
});
cols.push("tag".into());
vals.push(Value::String {
val: "no_tag".to_string(),
span: *span,
});
cols.push("branch".into()); cols.push("branch".into());
vals.push(Value::String { vals.push(Value::String {
val: "no_branch".to_string(), val: "no_branch".to_string(),
@ -467,7 +499,7 @@ impl Stats {
} else { } else {
"HEAD".to_string() "HEAD".to_string()
} }
// Grab the branch from the reference // Grab the branch from the reference
} else { } else {
let branch = name.to_string(); let branch = name.to_string();
// Since we have a branch name, look for the name of the upstream branch // Since we have a branch name, look for the name of the upstream branch