Get last commit message, author and hash using git ls-remote like command

gitremote

I'm looking for a way to get three informations from a remote repository using git ls-remote like command. I would like to use it in a bash script running in a cron. Currently, if I do

git ls-remote https://github.com/torvalds/linux.git master

I get the last commit hash on the master branch :

54e514b91b95d6441c12a7955addfb9f9d2afc65    refs/heads/master

Is there any way to get commit message and commit author ?

Best Answer

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.

Related Question