Skip to contentSkip to conversation

simple git repository browser with vim

DISCUSSION Eric Wong 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore

Eric Wong

author
#0 lore raw

Here's a really quick and easy way to browse git repositories for vim users. Hopefully somebody else finds this useful, I know I do.

The idea is just to open a temporary file with git sha1sums (say git-log output) in vim, move your cursor over any one of the object sha1sums, and then hit ,G (or any shortcut of your choice) in normal mode to show what one of those was.

It relies on a simple shell script I wrote called 'git-show' that picks a reasonable way to display each of the blob, tree, or commit object types. I'm fairly sure somebody else has written something like git-show before, I just couldn't find it. I'd imagine it's pretty useful standalone without vim, too.

If you :set foldmethod=marker in vim, you can pass the -f flag to git-show and it'll enclose the output with the the default vim fold markers: {{{ }}}

This is the line I've added to my .vimrc:

	map ,G yaw:.!git-show -f <c-r>" <CR>

And here is git-show in all it's glory:
---

#!/bin/sh

while : ; do

	case "$1" in
	-f|--fold-marker)
		fold_marker=1
		;;
	*)
		if [ -z "$sha1sum" ]; then
			sha1sum="$1"
		fi
		break
		;;
	esac
	shift

done

type=git-cat-file -t $sha1sum

if [ -n "$fold_marker" ]; then

	echo "$type($1) {{{"

fi

case "$type" in

	tree)
		git-ls-tree -r $1
		;;
	blob)
		git-cat-file blob $1
		;;
	commit)
		git-cat-file commit $1
		echo ''
		git-whatchanged --max-count=1 -C -p -r $1
		;;

esac

if [ -n "$fold_marker" ]; then

	echo '}}}'

fi

Signature · 1 line
Eric Wong
#1 lore raw
Quoted 5 lines · Eric Wong

It relies on a simple shell script I wrote called 'git-show' that picks a reasonable way to display each of the blob, tree, or commit object types. I'm fairly sure somebody else has written something like git-show before, I just couldn't find it. I'd imagine it's pretty useful standalone without vim, too.

Aha.

It strikes me that the blob case could be enhanced to do git-unpack-file, run "file -i" on it to figure out the file's mimetype, and then give it to an appropriate MIME type viewer ;-).

For a tag object, you might want to deref it, and also if it is a signed kind, you may want to do git-verify-tag it.

The following should not taken too seriously, but you might find it instructive to learn how to unwrap tag objects, verify them for signature, and pretty-print a single commit without running whatchanged. I suspect the script as posted by you shows the commit message twice, BTW.

-- >8 --
#!/bin/sh

extract_tag_info='

	s/^object /sha1=/p
        /^type /{
        	s//type=/
                p
                q
	}
'
for sha1
do
    type=`git-cat-file -t "$sha1"` || continue
    while case "$type" in tag) ;; *) break ;; esac
    do
	echo "*** tag $sha1 ***"
        git-cat-file tag "$sha1"
	git-verify-tag "$sha1"
	echo
	eval `git-cat-file tag "$sha1" | sed -n -e "$extract_tag_info"`
    done
    case "$type" in
    tree)
        echo "*** $type $sha1 ***"
        git-ls-tree -r "$sha1"
        ;;
    blob)
        temp=`git-unpack-file "$sha1"`
	mimetype=`file -i "$temp" | sed -e 's/^[^:]*: *//'`
        echo "*** $type ($mimetype) $sha1 ***"
	case "$mimetype" in
        image/*)
        	display "$temp" ;;
	audio/*)
        	cat "$temp" >/dev/audio ;; # sorry I do not do audio
        text/html)
        	links -dump "$temp" ;; # hello, Pasky
	*)
		cat "$temp" ;;
	esac
        rm -f $temp
        ;;
    commit)
        echo "*** $type $sha1 ***"
        git-diff-tree --pretty -m -C --find-copies-harder -p "$sha1"
        ;;
    esac
    echo

done

author
#2 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Eric Wong

It relies on a simple shell script I wrote called 'git-show' that picks a reasonable way to display each of the blob, tree, or commit object types. I'm fairly sure somebody else has written something like git-show before, I just couldn't find it. I'd imagine it's pretty useful standalone without vim, too.

Quoted 6 lines

Aha.

It strikes me that the blob case could be enhanced to do git-unpack-file, run "file -i" on it to figure out the file's mimetype, and then give it to an appropriate MIME type viewer ;-).

Maybe somebody else will find this useful, overkill for me.

Quoted 2 lines

For a tag object, you might want to deref it, and also if it is a signed kind, you may want to do git-verify-tag it.

This is pretty useful, I've added it to my version, and also added extra vim foldmarkers for the dereferenced tag.

Quoted 5 lines

The following should not taken too seriously, but you might find it instructive to learn how to unwrap tag objects, verify them for signature, and pretty-print a single commit without running whatchanged. I suspect the script as posted by you shows the commit message twice, BTW.

That was semi-intentional, I wanted the short diff format with the sha1sums of the blobs so I could further expand those inside vim. Fortunately, I've figured out that diff-tree is better for this.

Here's what I'm currently using:
---

#!/bin/sh

extract_tag_info='

        s/^object /sha1=/p
        /^type /{
                s//type=/
                p
                q
        }
'
sha1sums=
while : ; do
	case "$1" in
	-f|--fold-marker)
		fold_marker=1
		;;
	*)
		sha1sums="$sha1sums $1"
		break
		;;
	esac
	shift

done

for sha1 in $sha1sums
do

	type=`git-cat-file -t $sha1` || continue
	
	test -n "$fold_marker" && echo "$type($1) {{{"
	
	while case "$type" in tag) ;; *) break ;; esac
	do
		git-cat-file tag "$sha1"
		git-verify-tag "$sha1"
		test -n "$fold_marker" && echo '}}}'
 		
		eval `git-cat-file tag "$sha1" | sed -n -e "$extract_tag_info"`
		
		test -n "$fold_marker" && echo "$type($1) {{{"
	done
	
	case "$type" in
	tree)
		git-ls-tree -r "$sha1"
		;;
	blob)
		git-cat-file blob "$sha1"
		;;
	commit)
         	git-diff-tree --pretty=raw -r -m -C --find-copies-harder "$sha1"
		
		test -n "$fold_marker" && echo "commit_diff($1) {{{"

        	git-diff-tree -m -C --find-copies-harder -p "$sha1"
		
		test -n "$fold_marker" && echo '}}}'
		;;
	esac

	test -n "$fold_marker" && echo '}}}'

done