last day (17 days later) » 

20:40
0
A: How to generate (git) diffs with <n> characters of context?

doakThis script will do the job, at least as long escape sequences are as expected. Call it e.g. as git-diff-word-context -U 5. It accepts all arguments git diff would accept and intercepts -U (respectively --unified=) to set the number of characters. It just utilises grep to fulfil the task: #!/usr...

Knu
Knu
My BSD grep 2.5.1 doesn't have a P option so git-diff-word-context -U 5 file.ext failed.
It is quite easy to convert the used Perl regular expression to POSIX confirming ones. Please state your requirements in your question.
You seem to use FreeBSD, is this correct? ("This option is not supported in FreeBSD.") It is hard to find a solution without knowledge of constraints.
This is the line without Perl regular expressions: ok grep -o ".\\{0,$CONTEXT\\}\\($START_DIFF[^$ESCAPE]\\+$END_DIFF\\)\\+‌​.\\{0,$CONTEXT\\}"
Knu
Knu
I tried the version "without Perl regular expressions" and it doesn't diff at a char level: it's not as granular as --color-words=..
"You seem to use FreeBSD, is this correct?" I am not. I am using macOS X
The solution is generic enough. You can just tell Git what a word ist. Have a look at option --word-diff-regex for git diff and friends.
To be more specific: For example, --word-diff-regex=. will treat each character as a word and, correspondingly, show differences character by character.
Knu, I added two characters to the script to fullfill the by character requirement.
To be honest, I would limit words to not cross lines (i.e. to use --color-words='[^\n]') instead. I just used . in the updated answer because you did the same in your question, thus I imply it's your intention.
Knu
Knu
Alright it's almost there. I just need to replace the separator (\n) by something custom. BTW I don't have enough reputation to upvote.
20:40
You already can do. Just call the script like this: git-diff-word-context --color-words=<arg>. This will overwrite script default. The script just passes all arguments to git diff with the exception of -U. Or you use it as a filter: git show HEAD~5 --color-words=<arg> | git-diff-word-context.
I have found a bug if changed words (currently characters) overlaps with context. I will fix it in the answer.
Do you want to get the changes listed one by one or combined in that case?
Example with `-U5`: `TestTextByMe` -> `TestTEXTByME`
a) One change, i.e. both changes combined.
b) Two changes: one for TEXT, one for ME.

  last day (17 days later) »