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

15,657

Solution 1

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 <[email protected]>

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.

Solution 2

How about using git log:

git log -1

Example:

$ git log -1
commit 4a3dfcc66ca76a19052a7c0d44d5e6c315d79e07
Author: LE Manh Cuong <[email protected]>
Date:   Fri Apr 17 01:54:10 2015 +0700

    Make yanking work in OSX.
Share:
15,657

Related videos on Youtube

FunkySayu
Author by

FunkySayu

Updated on September 18, 2022

Comments

  • FunkySayu
    FunkySayu almost 2 years

    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 ?

  • William
    William about 9 years
    I think you can't do the same using the "smart" protocol. The answer should be updated.
  • clay
    clay almost 7 years
    The OP asked for from-remote command - yours is solution is to run from a cloned git folder
  • clay
    clay almost 7 years
    I just found a work around, but still using git log, stackoverflow.com/a/27145694/248616