Rahul Raj
Rahul Raj

Reputation: 81

How to find the commit message using BLOB SHA1

I am a beginner in GIT and trying to do some validation with Bit Bucket objects.

I am using the below script to find all the objects in a branch: git ls-tree -r branch-name

This gives me the object name along with the blob SHA1 information. I want to find the commit message from the blob. We use the jira number in the commit message and I want to extract the jira no from the message.

The problem is that the file is commited multiple times to resolve merge conflict for some other object and thereby when I try to find the commitID from SHA1, it is giving me the latest commitID, which is wrong as that jira is not associated with the particular object.

If I see the object in the remote repo, I can still see the correct commit message (jira-no) and thereby would like to retrieve the object and the corresponding commit message from the target branch. Please suggest if there is a way to do so.

Another query is the way to resolve merge conflict. In case of conflict with one object, we generally pull the entire content of the target branch and then resolve the conflicting object. Due to this, the object which is not modified is also commited again. Any easy way to pull only the conflicting object.

Thanks for your help

Upvotes: 0

Views: 201

Answers (1)

eftshift0
eftshift0

Reputation: 30297

Well... the problem is that one single blob could be present in many revisions.... but you could try with a pseudo-1-liner:

git log --all --pretty=%h | while read revision; do
    lines=$( git ls-tree -r $revision | grep object-id | wc -l )
    if [ $lines -gt 0 ]; then
        # it's present here
        git show --summary --pretty="%h %s" $revision
    fi
done

Replace object-id for the blob id that you want. That should do.

Upvotes: 1

Related Questions