forked from dandavison/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
* upstream/master: Set a default blame palette Handle blame output Refactor: module for working with format strings Add default-language option Refactor: syntax_highlight_and_paint_line function Refactor: eliminate format module Use dotted range notation in diff.submodule = short case Refactor: select next handled event with single disjunction expression (dandavison#701) More minimalistic display of submodule (short) diffs (dandavison#700) 662 submodule diffs (dandavison#699)
- Loading branch information
Showing
16 changed files
with
721 additions
and
193 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/bin/bash | ||
REPO_ROOT=/tmp/submodule-diff-example | ||
REPO_DATE_FMT=%H:%M:%S.%N | ||
mkdir ${REPO_ROOT} && git -C ${REPO_ROOT} init && cd ${REPO_ROOT} | ||
date +${REPO_DATE_FMT} >> baserepo && git add baserepo && git commit -m "Base repo commit 1" | ||
|
||
for sub in A B C; do | ||
git init submodule${sub} | ||
for iter in $(seq 1 4); do | ||
date +${REPO_DATE_FMT} >> submodule${sub}/subcontent | ||
git -C submodule${sub} add subcontent && git -C submodule${sub} commit -m "Submodule ${sub} initial commit $iter" | ||
done | ||
# Add initial submodule, message of "Submodule submoduleX 0000000...xxxxxxx (new submodule)", no individual commits | ||
git submodule add ../bogus-url-${sub} submodule${sub} && \ | ||
git commit -m "Add submodule${sub}" # the diff handling for this is correct in delta | ||
|
||
# Create additional submodule commits | ||
for iter in $(seq 1 2); do | ||
date +${REPO_DATE_FMT} >> submodule${sub}/subcontent | ||
git -C submodule${sub} add subcontent && git -C submodule${sub} commit -m "Submodule ${sub} extra change ${iter}" | ||
done | ||
git add submodule${sub} && git commit -m "Update submodule${sub}" | ||
done | ||
|
||
git -C submoduleA reset --hard HEAD~4 | ||
git -C submoduleC reset --hard HEAD~2 | ||
|
||
for sub in B C; do | ||
for iter in $(seq 1 3); do | ||
date +${REPO_DATE_FMT} >> submodule${sub}/subcontent | ||
git -C submodule${sub} add subcontent && git -C submodule${sub} commit -m "Submodule ${sub} stage change ${iter}" | ||
done | ||
done | ||
|
||
# Add all submodule updates in single commit to test multiple submodule updates in single commit | ||
git add submodule[A-C] && git commit -m "Update all submodules" | ||
|
||
# submoduleA end state is only removed commits | ||
# submoduleB end state is only added commits | ||
# submoduleC is a mixture of removed and added commits (e.g. different branch) | ||
|
||
# Manual, inspect superproject history via: | ||
# "git -c diff.submodule=short log -p | delta --no-gitconfig" | ||
# "git -c diff.submodule=log log -p | delta --no-gitconfig" | ||
# "git -c diff.submodule=diff log -p | delta --no-gitconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use chrono::{DateTime, FixedOffset}; | ||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
|
||
use crate::config; | ||
use crate::delta; | ||
use crate::format; | ||
|
||
#[derive(Debug)] | ||
pub struct BlameLine<'a> { | ||
pub commit: &'a str, | ||
pub author: &'a str, | ||
pub time: DateTime<FixedOffset>, | ||
pub line_number: usize, | ||
pub code: &'a str, | ||
} | ||
|
||
// E.g. | ||
//ea82f2d0 (Dan Davison 2021-08-22 18:20:19 -0700 120) let mut handled_line = self.handle_commit_meta_header_line()? | ||
|
||
lazy_static! { | ||
static ref BLAME_LINE_REGEX: Regex = Regex::new( | ||
r"(?x) | ||
^ | ||
( | ||
[0-9a-f]{8} # commit hash | ||
) | ||
[\ ] | ||
\( # open ( | ||
( | ||
[^\ ].*[^\ ] # author name | ||
) | ||
[\ ]+ | ||
( # timestamp | ||
[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}\ [-+][0-9]{4} | ||
) | ||
[\ ]+ | ||
( | ||
[0-9]+ # line number | ||
) | ||
\) # close ) | ||
( | ||
.* # code, with leading space | ||
) | ||
$ | ||
" | ||
) | ||
.unwrap(); | ||
} | ||
|
||
pub fn parse_git_blame_line<'a>(line: &'a str, timestamp_format: &str) -> Option<BlameLine<'a>> { | ||
if let Some(caps) = BLAME_LINE_REGEX.captures(line) { | ||
let commit = caps.get(1).unwrap().as_str(); | ||
let author = caps.get(2).unwrap().as_str(); | ||
let timestamp = caps.get(3).unwrap().as_str(); | ||
if let Ok(time) = DateTime::parse_from_str(timestamp, timestamp_format) { | ||
let line_number_str = caps.get(4).unwrap().as_str(); | ||
if let Ok(line_number) = line_number_str.parse::<usize>() { | ||
let code = caps.get(5).unwrap().as_str(); | ||
Some(BlameLine { | ||
commit, | ||
author, | ||
time, | ||
line_number, | ||
code, | ||
}) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
lazy_static! { | ||
pub static ref BLAME_PLACEHOLDER_REGEX: Regex = | ||
format::make_placeholder_regex(&["timestamp", "author", "commit"]); | ||
} | ||
|
||
pub fn format_blame_metadata( | ||
format_data: &[format::FormatStringPlaceholderData], | ||
blame: &BlameLine, | ||
config: &config::Config, | ||
) -> String { | ||
let mut s = String::new(); | ||
let mut suffix = ""; | ||
for placeholder in format_data { | ||
s.push_str(placeholder.prefix); | ||
|
||
let alignment_spec = placeholder.alignment_spec.unwrap_or("<"); | ||
let width = placeholder.width.unwrap_or(15); | ||
|
||
let pad = |s| format::pad(s, width, alignment_spec); | ||
match placeholder.placeholder { | ||
Some("timestamp") => s.push_str(&pad( | ||
&chrono_humanize::HumanTime::from(blame.time).to_string() | ||
)), | ||
Some("author") => s.push_str(&pad(blame.author)), | ||
Some("commit") => s.push_str(&pad(&delta::format_raw_line(blame.commit, config))), | ||
None => {} | ||
Some(_) => unreachable!(), | ||
} | ||
suffix = placeholder.suffix; | ||
} | ||
s.push_str(suffix); | ||
s | ||
} | ||
|
||
#[test] | ||
fn test_blame_line_regex() { | ||
for line in &[ | ||
"ea82f2d0 (Dan Davison 2021-08-22 18:20:19 -0700 120) let mut handled_line = self.handle_commit_meta_header_line()?", | ||
"b2257cfa (Dan Davison 2020-07-18 15:34:43 -0400 1) use std::borrow::Cow;" | ||
] { | ||
let caps = BLAME_LINE_REGEX.captures(line); | ||
assert!(caps.is_some()); | ||
assert!(parse_git_blame_line(line, "%Y-%m-%d %H:%M:%S %z").is_some()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.