cli: show Git HEAD in log output

It's useful to know which commit is checked out in the underlying Git
repo (if there is one), so let's show that. This patch indicates that
commit with `HEAD@git` in the log output. It's probably not very
useful when the Git repo is "internal" (i.e. stored inside `.jj/`),
because then it's unlikely to change often. I therefore considered not
showing it when the Git repo is internal. However, it turned out that
`HEAD` points to a non-existent branch in the repo I use, so it won't
get imported anyway (by the function added in the previous patch). We
can always review this decision later.

This is part of #44.
This commit is contained in:
Martin von Zweigbergk 2021-11-28 21:33:37 -08:00
parent 8a2f630ac0
commit 626fbee0dd
5 changed files with 29 additions and 3 deletions

View file

@ -67,6 +67,13 @@ pub enum RepoRef<'a> {
}
impl<'a> RepoRef<'a> {
pub fn base_repo(&self) -> &ReadonlyRepo {
match self {
RepoRef::Readonly(repo) => repo,
RepoRef::Mutable(repo) => repo.base_repo.as_ref(),
}
}
pub fn store(&self) -> &Arc<Store> {
match self {
RepoRef::Readonly(repo) => repo.store(),

View file

@ -2195,6 +2195,7 @@ fn log_template(settings: &UserSettings) -> String {
" " label("timestamp", author.timestamp())
" " branches
" " tags
if(is_git_head, label("git_head", " HEAD@git"))
if(divergent, label("divergent", " divergent"))
if(conflict, label("conflict", " conflict"))
"\n"

View file

@ -92,6 +92,7 @@ fn config_colors(user_settings: &UserSettings) -> HashMap<String, String> {
result.insert(String::from("branches"), String::from("magenta"));
result.insert(String::from("tags"), String::from("magenta"));
result.insert(String::from("git_refs"), String::from("magenta"));
result.insert(String::from("git_head"), String::from("magenta"));
result.insert(String::from("divergent"), String::from("red"));
result.insert(String::from("conflict"), String::from("red"));

View file

@ -25,9 +25,9 @@ use crate::formatter::PlainTextFormatter;
use crate::templater::{
AuthorProperty, BranchProperty, ChangeIdProperty, CommitIdKeyword, CommitterProperty,
ConditionalTemplate, ConflictProperty, ConstantTemplateProperty, CurrentCheckoutProperty,
DescriptionProperty, DivergentProperty, DynamicLabelTemplate, GitRefsProperty, LabelTemplate,
ListTemplate, LiteralTemplate, OpenProperty, StringPropertyTemplate, TagProperty, Template,
TemplateFunction, TemplateProperty,
DescriptionProperty, DivergentProperty, DynamicLabelTemplate, GitRefsProperty,
IsGitHeadProperty, LabelTemplate, ListTemplate, LiteralTemplate, OpenProperty,
StringPropertyTemplate, TagProperty, Template, TemplateFunction, TemplateProperty,
};
#[derive(Parser)]
@ -242,6 +242,7 @@ fn parse_commit_keyword<'a>(repo: RepoRef<'a>, pair: Pair<Rule>) -> (Property<'a
"branches" => Property::String(Box::new(BranchProperty { repo })),
"tags" => Property::String(Box::new(TagProperty { repo })),
"git_refs" => Property::String(Box::new(GitRefsProperty { repo })),
"is_git_head" => Property::Boolean(Box::new(IsGitHeadProperty::new(repo))),
"divergent" => Property::Boolean(Box::new(DivergentProperty::new(repo))),
"conflict" => Property::Boolean(Box::new(ConflictProperty)),
name => panic!("unexpected identifier: {}", name),

View file

@ -288,6 +288,22 @@ impl TemplateProperty<Commit, String> for GitRefsProperty<'_> {
}
}
pub struct IsGitHeadProperty<'a> {
repo: RepoRef<'a>,
}
impl<'a> IsGitHeadProperty<'a> {
pub fn new(repo: RepoRef<'a>) -> Self {
Self { repo }
}
}
impl TemplateProperty<Commit, bool> for IsGitHeadProperty<'_> {
fn extract(&self, context: &Commit) -> bool {
self.repo.view().git_head().as_ref() == Some(context.id())
}
}
pub struct DivergentProperty {
divergent_changes: HashSet<ChangeId>,
}