{"thread":{"id":"2668","subject":"simple git repository browser with vim","startedAt":"2005-11-24T09:33:22Z","lastAt":"2005-11-26T22:05:17Z","messageCount":3,"participants":["Eric Wong","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"12690","messageId":"20051124093322.GA3899@mail.yhbt.net","threadId":"2668","inReplyTo":null,"subject":"simple git repository browser with vim","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2005-11-24T09:33:22Z","receivedAt":"2005-11-24T09:33:22Z","isPatch":false,"sender":{"key":"e@80x24.org","avatar":null},"body":"Here's a really quick and easy way to browse git repositories for vim\nusers.  Hopefully somebody else finds this useful, I know I do.\n\nThe idea is just to open a temporary file with git sha1sums (say git-log\noutput) in vim, move your cursor over any one of the object sha1sums,\nand then hit ,G (or any shortcut of your choice) in normal mode to\nshow what one of those was.\n\nIt relies on a simple shell script I wrote called 'git-show' that picks\na reasonable way to display each of the blob, tree, or commit object\ntypes.  I'm fairly sure somebody else has written something like\ngit-show before, I just couldn't find it.  I'd imagine it's pretty\nuseful standalone without vim, too.\n\nIf you :set foldmethod=marker in vim, you can pass the -f\nflag to git-show and it'll enclose the output with the the default\nvim fold markers: {{{  }}}\n\nThis is the line I've added to my .vimrc:\n\n\tmap ,G yaw:.!git-show -f <c-r>\" <CR>\n\nAnd here is git-show in all it's glory:\n---\n\n#!/bin/sh\n\nwhile : ; do\n\tcase \"$1\" in\n\t-f|--fold-marker)\n\t\tfold_marker=1\n\t\t;;\n\t*)\n\t\tif [ -z \"$sha1sum\" ]; then\n\t\t\tsha1sum=\"$1\"\n\t\tfi\n\t\tbreak\n\t\t;;\n\tesac\n\tshift\ndone\n\ntype=`git-cat-file -t $sha1sum`\n\nif [ -n \"$fold_marker\" ]; then\n\techo \"$type($1) {{{\"\nfi\n\ncase \"$type\" in\n\ttree)\n\t\tgit-ls-tree -r $1\n\t\t;;\n\tblob)\n\t\tgit-cat-file blob $1\n\t\t;;\n\tcommit)\n\t\tgit-cat-file commit $1\n\t\techo ''\n\t\tgit-whatchanged --max-count=1 -C -p -r $1\n\t\t;;\nesac\n\nif [ -n \"$fold_marker\" ]; then\n\techo '}}}'\nfi\n\n-- \nEric Wong\n"},{"id":"12691","messageId":"7v8xveuyq0.fsf@assigned-by-dhcp.cox.net","threadId":"2668","inReplyTo":"20051124093322.GA3899@mail.yhbt.net","subject":"Re: simple git repository browser with vim","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-11-24T10:28:07Z","receivedAt":"2005-11-24T10:28:07Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Wong <normalperson@yhbt.net> writes:\n\n> It relies on a simple shell script I wrote called 'git-show' that picks\n> a reasonable way to display each of the blob, tree, or commit object\n> types.  I'm fairly sure somebody else has written something like\n> git-show before, I just couldn't find it.  I'd imagine it's pretty\n> useful standalone without vim, too.\n\nAha.\n\nIt strikes me that the blob case could be enhanced to do\ngit-unpack-file, run \"file -i\" on it to figure out the file's\nmimetype, and then give it to an appropriate MIME type viewer\n;-).\n\nFor a tag object, you might want to deref it, and also if it is\na signed kind, you may want to do git-verify-tag it.\n\nThe following should not taken too seriously, but you might find\nit instructive to learn how to unwrap tag objects, verify them\nfor signature, and pretty-print a single commit without running\nwhatchanged.  I suspect the script as posted by you shows the\ncommit message twice, BTW.\n\n-- >8 --\n#!/bin/sh\n\nextract_tag_info='\n\ts/^object /sha1=/p\n        /^type /{\n        \ts//type=/\n                p\n                q\n\t}\n'\nfor sha1\ndo\n    type=`git-cat-file -t \"$sha1\"` || continue\n    while case \"$type\" in tag) ;; *) break ;; esac\n    do\n\techo \"*** tag $sha1 ***\"\n        git-cat-file tag \"$sha1\"\n\tgit-verify-tag \"$sha1\"\n\techo\n\teval `git-cat-file tag \"$sha1\" | sed -n -e \"$extract_tag_info\"`\n    done\n    case \"$type\" in\n    tree)\n        echo \"*** $type $sha1 ***\"\n        git-ls-tree -r \"$sha1\"\n        ;;\n    blob)\n        temp=`git-unpack-file \"$sha1\"`\n\tmimetype=`file -i \"$temp\" | sed -e 's/^[^:]*: *//'`\n        echo \"*** $type ($mimetype) $sha1 ***\"\n\tcase \"$mimetype\" in\n        image/*)\n        \tdisplay \"$temp\" ;;\n\taudio/*)\n        \tcat \"$temp\" >/dev/audio ;; # sorry I do not do audio\n        text/html)\n        \tlinks -dump \"$temp\" ;; # hello, Pasky\n\t*)\n\t\tcat \"$temp\" ;;\n\tesac\n        rm -f $temp\n        ;;\n    commit)\n        echo \"*** $type $sha1 ***\"\n        git-diff-tree --pretty -m -C --find-copies-harder -p \"$sha1\"\n        ;;\n    esac\n    echo\ndone\n"},{"id":"12778","messageId":"20051126220517.GC3899@mail.yhbt.net","threadId":"2668","inReplyTo":"7v8xveuyq0.fsf@assigned-by-dhcp.cox.net","subject":"Re: simple git repository browser with vim","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2005-11-26T22:05:17Z","receivedAt":"2005-11-26T22:05:17Z","isPatch":false,"sender":{"key":"e@80x24.org","avatar":null},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> Eric Wong <normalperson@yhbt.net> writes:\n> \n> > It relies on a simple shell script I wrote called 'git-show' that picks\n> > a reasonable way to display each of the blob, tree, or commit object\n> > types.  I'm fairly sure somebody else has written something like\n> > git-show before, I just couldn't find it.  I'd imagine it's pretty\n> > useful standalone without vim, too.\n> \n> Aha.\n> \n> It strikes me that the blob case could be enhanced to do\n> git-unpack-file, run \"file -i\" on it to figure out the file's\n> mimetype, and then give it to an appropriate MIME type viewer\n> ;-).\n\nMaybe somebody else will find this useful, overkill for me.\n\n> For a tag object, you might want to deref it, and also if it is\n> a signed kind, you may want to do git-verify-tag it.\n\nThis is pretty useful, I've added it to my version, and also added\nextra vim foldmarkers for the dereferenced tag.\n\n> The following should not taken too seriously, but you might find\n> it instructive to learn how to unwrap tag objects, verify them\n> for signature, and pretty-print a single commit without running\n> whatchanged.  I suspect the script as posted by you shows the\n> commit message twice, BTW.\n\nThat was semi-intentional, I wanted the short diff format with the\nsha1sums of the blobs so I could further expand those inside vim.\nFortunately, I've figured out that diff-tree is better for this.\n\nHere's what I'm currently using:\n--- \n\n#!/bin/sh\n\nextract_tag_info='\n        s/^object /sha1=/p\n        /^type /{\n                s//type=/\n                p\n                q\n        }\n'\nsha1sums=\nwhile : ; do\n\tcase \"$1\" in\n\t-f|--fold-marker)\n\t\tfold_marker=1\n\t\t;;\n\t*)\n\t\tsha1sums=\"$sha1sums $1\"\n\t\tbreak\n\t\t;;\n\tesac\n\tshift\ndone\n\nfor sha1 in $sha1sums\ndo\n\ttype=`git-cat-file -t $sha1` || continue\n\t\n\ttest -n \"$fold_marker\" && echo \"$type($1) {{{\"\n\t\n\twhile case \"$type\" in tag) ;; *) break ;; esac\n\tdo\n\t\tgit-cat-file tag \"$sha1\"\n\t\tgit-verify-tag \"$sha1\"\n\t\ttest -n \"$fold_marker\" && echo '}}}'\n \t\t\n\t\teval `git-cat-file tag \"$sha1\" | sed -n -e \"$extract_tag_info\"`\n\t\t\n\t\ttest -n \"$fold_marker\" && echo \"$type($1) {{{\"\n\tdone\n\t\n\tcase \"$type\" in\n\ttree)\n\t\tgit-ls-tree -r \"$sha1\"\n\t\t;;\n\tblob)\n\t\tgit-cat-file blob \"$sha1\"\n\t\t;;\n\tcommit)\n         \tgit-diff-tree --pretty=raw -r -m -C --find-copies-harder \"$sha1\"\n\t\t\n\t\ttest -n \"$fold_marker\" && echo \"commit_diff($1) {{{\"\n\n        \tgit-diff-tree -m -C --find-copies-harder -p \"$sha1\"\n\t\t\n\t\ttest -n \"$fold_marker\" && echo '}}}'\n\t\t;;\n\tesac\n\n\ttest -n \"$fold_marker\" && echo '}}}'\ndone\n"}]}