How to make git-log scroll up instead of down

Original answer, which doesn't work, so goto EDIT for the working version

The following sed solution works for me if used directly form command line. It does not use any temporary string to switch \ and / by relying on seds y command.

$ git --no-pager log --all --graph --decorate --oneline --color=always | tac | sed 'h
s!\( *[0-9a-z]\{7\} .*\)\{0,1\}$!\1!
y/\\\/_¯/\/\\¯_/
x
s!\(.*\)\( *[0-9a-z]\{7\} .*\)\{0,1\}$!\2!
x
G
s/\n//' | less -X +G -r

It assumes the SHA code to be 7 characters long and uses it to "recognize" what is not the leading sequence of \, /, |, _, *, and <space>, which I was not able to put at the beginning of the first s command's search pattern and in place of the first . in the second s command.

I don't know why I cannot get it to work when all sed commands are put in a script called in with seds -f option.

EDIT (The above code is actually faulty)

As @user1902689 pointed out, sed scripts can make anyone's eyes bleed, myself included, since they are extremely cryptic.

In my opinion, the task would be easily accomplished if the --color=always was not used, in which case the text piped out of git log would be the same as we see on screen; using --color=always, on the contrary, inserts control sequences like ^[[33m interspersed in the text to control the coloring (different colors for different branches, ...).

But having a colored output is nice, so I directed the output of git log --color=always ... to file, and looked into it, discovering that the hash always appears in between ^[[33m and ^[[m, where ^[ is a single character obtainable by hitting Ctrl+V, then Esc. These are essentially the escape sequences interpreted by bash as setting the color to yellow, and back to white, respectively (link).

The hash, which is not the only 7-alphanumeric characters-string in the line (e.g. thiswrd can be in the commit main message), is almost certainly the first one, so greedy expressions (sed has no non-greedy expressions) can be safely used after it, and not before (a .* before the hash-matching regexp would make that regexp match the last 7-alphanumeric characters-string on the line, which could be anytext, for instance, and the hash would be lost somewhere in .*). In order to allow the use of the greedy .* in a way that it doesn't devour the hash, we can enclose the hash in between newlines \n which are not matched by the . in .* (and, as such, they must be explicitly typed) with an s command, so that we can "limit" the greediness of .* in a successive s command by using some \n explicitly in the search pattern.

I think the following code (explained afterward) is not definitive, since it hardcodes the coloring escape sequences used to get a colored hash string, but it works as long as I've tried.

$ git --no-pager log --all --graph --decorate --oneline --color=always | tac | sed '
s/\(\(^[\[33m\)\([0-9a-z]\{7\}\)\(^[\[m\)\(.*\)\)/\2\n\3\n\4\5/
h
s/^.*\(\n[a-z0-9]\{7\}\n.*\|$\)/\1/
x
s/\n[a-z0-9]\{7\}\n.*$//
y/\\\/_¯/\/\\¯_/
G
s/\n\([a-z0-9]\{7\}\)\n/\1/
s/\n//' | less -X +G -r

Each line contains the three parts Graph OpeningColorTagHashClosingColorTag Message, or only the first one, Graph. The sed string consists of 9 commands that do what I intended to do with the original answer, but in a bit different way (especially in that the order of some commands is inverted, to save a x command).

  1. The first s command puts a newline \n on each side of the Hash string [0-9a-z]\{7\} (or does nothing if there is no hash in this line; note that the lines before/after a merge/diverge have no hash or message following them). This has the purpose of "isolating" Hash. Note that the capturing groups \(...\) are numbered based on the order of occurrence of the opening token \(, so in the replacement string \2\n\3\n\4\5:
    • \2 refers to ^[\[33m, which is the OpeningColorTag (NOTE: ^[ is a single character obtained by hitting Ctrl+V, then Esc, whereas the "true" [ must be escped with a backslash \);
    • \3 refers to Hash, [0-9a-z]\{7\}
    • \4 refers to the ClosingColorTag, ^[\[m (what is said for \2 holds here too);
    • \5 is whatever follows, .* (implicitly up to end-of-line).

Now the pattern space (the current line, as we've edited so far) contains the original line with two embedded newlines on each side of the hash (Graph OpeningColorTag\nHash\nClosingColorTag Message), or the unmodified original line if it contained no hash (Graph).

  1. The h command "saves" the pattern space into the hold space (think of it as a drawer).

Now the pattern and hold spaces have the same content (Graph OpeningColorTag\nHash\nClosingColorTag Message or Graph).

  1. The second s command captures and replace, with itself only (/\1/) and discarding everything before it (^.*, i.e. Graph OpeningColorTag), either (\| separates alternatives in a capturing group \(...\))
    • the newline-enclosed hash and everything following it (i.e. the commit main message) \n[a-z0-9]\{7\}\n.*,
    • or the end-of-line $,

Now the pattern space contains \nHash\nClosingColorTag Message, or the empty string if there was no hash.

  1. The x command exchanges the contents of pattern and hold spaces, so that the multiline \nHash\nClosingColorTag Message (or the empty string) is saved in the hold space, and the multiline Graph OpeningColorTag\nHash\nClosingColorTag Message is in the pattern space, ready to be re-edited.
  2. The third s command command strips off the \nHash, and everything following it, from the pattern space.

Now the pattern space contains Graph OpeningColorTag.

  1. The y substitutes each character between the first two non-escaped /, with the corresponing character between the second and third non-escaped /. Here both back and forward slashes must be escaped with a backslash. (This shoul be safe, since OpeningColorTag should not contain any of the translated characters.)

Now the pattern space contains Hparg OpeningColorTag, where Hparg is the "inverted" version of Graph (or Hparg only).

  1. The G command gets the content of the hold space and appends it to (hence the capital G; the lower case g would copy into instead of append to) the pattern space, with a newline \n in between.

Now the pattern space contains Hparg OpeningColorTag\n\nHash\nClosingColorTag Message (or Hparg\n only), and we don't care the hold space from now on.

  1. The fourth s command captures the \nHash\n part and substitutes it with Hash.

Now the pattern space contains Hparg OpeningColorTag\nHashClosingColorTag Message or Hparg\n.

  1. The last s command removes the remaining newline \n.

Finally the pattern space contains Hparg OpeningColorTagHashClosingColorTag Message or Hparg.

Steps 8. and 9. cannot be fused together (e.g. s/\n\n\([a-z0-9]\{7\}\)\n/\1/), since the two \n enclosing the hash are there only if the line contains the hash (the first s of point 1. does nothing if there's no hash), whereas the first \n is always present, since it comes with the G command.


Actually the most external group \(...\) is not needed (it's not used, indeed), so it can be removed, and all numeric references to the other capturing groups can be diminished by 1, e.g. s/\(\(^[\[33m\)\([0-9a-z]\{7\}\)\(^[\[m\)\(.*\)\)/\2\n\3\n\4\5/ can be changed to s/\(^[\[33m\)\([0-9a-z]\{7\}\)\(^[\[m\)\(.*\)/\1\n\2\n\3\4/; but I'll keep the unnecessary group in the answer, since it gives the chance to mention the not-so-widely-known numbering of capturing groups.


This is an answer that seems to catch most edge cases. Not tested thoroughly.

[alias]
    rlog = !"git --no-pager log --all --graph --decorate --oneline --color=always | tac | awk -f ~/switchslashes.awk | less -X +G -r"

where the file ~/switchslashes.awk contains

{
    match($0,/([[:space:][:cntrl:]|*\/\\]+)(.*)/,a) # find the segment of the graph
    tgt = substr($0,RSTART,RLENGTH)     # save that segment in a variable tgt
    gsub(/\//,RS,tgt)                   # change all /s to newlines in tgt
    gsub(/\\/,"/",tgt)                  # change all \s to /s in tgt
    gsub(RS,"\\",tgt)                   # change all newlines to \s in tgt
    gsub(/_/,"¯",tgt)                   # change all _ to ¯ in tgt
    print tgt substr($0,RSTART+RLENGTH) # print tgt plus rest of the line
}

which is a modified version of this script. It replaces underscores with overlines and swaps slashes with backslashes and vice versa. This fixes the graph after the text has been reversed by tac.

Disclaimer

I never started using this, since it is slow with larger repositories. It needs to load everything and then apply the substitutions, which take too much time for my taste.