Skip to content

Instantly share code, notes, and snippets.

@gabro
Last active March 27, 2023 09:47
Show Gist options
  • Save gabro/5883819 to your computer and use it in GitHub Desktop.
Save gabro/5883819 to your computer and use it in GitHub Desktop.
Github-like working directory history
#!/bin/bash
FILES=`git ls-tree --name-only HEAD .`
MAXLEN=0
IFS=$(echo -en "\n\b")
for f in $FILES; do
if [ ${#f} -gt $MAXLEN ]; then
MAXLEN=${#f}
fi
done
for f in $FILES; do
str=$(git log -1 --pretty=format:"%C(green)%cr%Creset %x09 %C(cyan)%h%Creset %s %C(yellow)(%cn)%Creset" $f)
printf "%-${MAXLEN}s -- %s\n" "$f" "$str"
done
@bturner
Copy link

bturner commented Jan 13, 2015

A couple of small tweaks could make this run substantially faster for non-trivial directories. I'd recommend:

  • Drop --decorate. You're not actually using the results from that, you're just paying the performance cost. On my machine removing --decorate takes the process from ~100ms to ~40ms
  • Combine your git log commands into one. You're running two, as far as I can see, so that you can format some whitespace in. That's not necessary; %x09 will print a tab

My version would look like this:

#!/bin/bash

FILES=`git ls-tree --name-only HEAD .`
MAXLEN=0
for f in $FILES; do
    if [ ${#f} -gt $MAXLEN ]; then
        MAXLEN=${#f}
    fi
done
for f in $FILES; do
    str=$(git log -1 --pretty=format:"%C(green)%cr%Creset %x09 %C(cyan)%h%Creset %s %C(yellow)(%cn)%Creset" $f)
    printf "%-${MAXLEN}s -- %s\n" "$f" "$str"
done

For a directory with 29 files, these changes reduce the execution time for the script from ~5.6s to ~1.2s.

@gabro
Copy link
Author

gabro commented May 6, 2015

Thank you @bturner, that's very helpful!
I wish gist notified me of your comment months ago

@coldcandor
Copy link

It would probably be useful to commit this as an executable file, rather than force the downloader to change permissions on it (and thus have a dirty local copy).

I also noticed that tabbing gets messed up if you have a combination of file that were edited, say "3 years, 3 months ago" and some that were edited "3 years ago". Would be cool to either round the dates if older than a year ago, or add an option to print the dates as a datestamp, rather than relative.

Other than those minor annoyances, this is exactly what I was looking for, thank you so much for posting it!

@gabro
Copy link
Author

gabro commented Jul 9, 2015

@coldcandor sorry for the late response, but gist doesn't send out notifications :(

For the spacing, I agree it can be improved, I just admittedly never spent too much time on this.

I'll think of making this a full-fledged utility, rather than a gist, thanks for the feedback.

@mzavoloka
Copy link

@grimreaper
Copy link

change /bin/bash to /bin/sh for portability
change echo -en to printf for portability and functionality (most echo implementations don't support -e)
change the use of backticks to $() to be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment