While there is not any utilities that come with git that lets you do what you want, it is rather easy to write a python script that parses a git object and then outputs the author and commit message.
Here is a sample one that expects a git commit object on stdin
and then prints the author followed by the commit message:
from parse import parse
import sys, zlib
raw_commit = sys.stdin.buffer.read()
commit = zlib.decompress(raw_commit).decode('utf-8').split('\x00')[1]
(headers, body) = commit.split('\n\n')
for line in headers.splitlines():
# `{:S}` is a type identifier meaning 'non-whitespace', so that
# the fields will be captured successfully.
p = parse('author {name} <{email:S}> {time:S} {tz:S}', line)
if (p):
print("Author: {} <{}>\n".format(p['name'], p['email']))
print(body)
break
To make a full utility like you want the server needs to support the dumb git transport protocol over HTTP, as you cannot get a single commit using the smart protocol.
GitHub doesn’t support the dumb transport protocol anymore however, so I will be using my self-hosted copy of Linus’ tree as an example.
If the remote server supports the dump http git transport you could just use curl to get the object and then pipe it to the above python script. Let’s say that we want to see the author and commit message of commit c3fe5872eb
, then we’d execute the following shell script:
baseurl=http://git.kyriasis.com/kyrias/linux.git/objects
curl "$baseurl"/c3/fe5872eb3f5f9e027d61d8a3f5d092168fdbca | python parse.py
Which will print the following output:
Author: Sanidhya Kashyap <sanidhya.gatech@gmail.com>
bfs: correct return values
In case of failed memory allocation, the return should be ENOMEM instead
of ENOSPC.
...
The full commit SHA of commit c3fe5872eb
is c3fe5872eb3f5f9e027d61d8a3f5d092168fdbca
, and as you can see in the above shell script the SHA is split after the second character with a slash inbetween. This is because git stores objects namespaced under the first two characters of the SHA, presumably due to legacy filesystem having low limits on the number of files that can reside in a single directory.
While this answer doesn’t give a full working implementation of a remote git-show
command, it gives the basic parts needed to make a simple one.
Keep it simple; look for the directory .git
and run your commands from within its containing directory. Also throw in a -print
to see what dir it is running in:
find . -type d -name .git -print -execdir git symbolic-ref --short HEAD \;
(Okay, actually -print
shows the dir it found—./path/to/repo/.git
, not the ./path/to/repo/
itself. But that's a minor inconvenience.)
EDIT: You can produce the EXACT output requested in your OP by the following:
paste <(ls -ld *) <(for i in *; do if [ -d "$i"/.git ] ; then echo "($(git --git-dir="$i"/.git symbolic-ref --short HEAD))"; else echo; fi; done)
This doesn't have any real drawback that I can see.
By design it only finds top level git repos, i.e. if your "Data" directory (in your example ls
output) has subdirectories which are git repos they won't be listed, whereas with the find
command I gave earlier they would be. But as I say that's by design.
This doesn't preserve colors in the output of ls
. You can do this by adding a --color=always
to the embedded ls
command:
paste <(ls -ld --color=always *) <(for i in *; do if [ -d "$i"/.git ] ; then echo "($(git --git-dir="$i"/.git symbolic-ref --short HEAD))"; else echo; fi; done)
Though, for some reason, this produces an extra newline at the end of all the output. Oh well; not a big deal IMO.
For Mac OS, ls
doesn't have a --color
flag, so use the following to force color output:
paste <(CLICOLOR_FORCE=true ls -ld *) <(for i in *; do if [ -d "$i"/.git ] ; then echo "($(git --git-dir="$i"/.git symbolic-ref --short HEAD))"; else echo; fi; done)
Best Answer
Try this.
$1
should be the parent dir containing all of your repositories (or use "." for the current dir):You can make this its own script (but replace
$FUNCNAME
with$0
), or keep it inside a function and use it in your scripts.