bli00
bli00

Reputation: 2817

grep SHA-1 hash with possible prefix

I want to grep from git submodule status <PATH> the SHA-1 commit hash of my submodule. According to git submodule --help:

status [--cached] [--recursive] [--] [...]

Show the status of the submodules. This will print the SHA-1 of the currently checked out commit for each submodule, along with the submodule path and the output of git describe for the SHA-1. Each SHA-1 will possibly be prefixed with - if the submodule is not initialized, + if the currently checked out submodule commit does not match the SHA-1 found in the index of the containing repository and U if the submodule has merge conflicts.

So the result looks something like this:

 f1eeb6aa2a5009b5ef68b5b754499dcb3ab575d1 my-submodule (remotes/origin/HEAD)

The description mentions that each hash will be possibly prefixed with a + or a -. I'm not interested in the signs, and therefore, for whatever result it gives me, I want to get the 40 character hash without the prefix.

Example:

input: +f1eeb6aa2a5009b5ef68b5b754499dcb3ab575d1 my-submodule (remotes/origin/HEAD)
desired output: f1eeb6aa2a5009b5ef68b5b754499dcb3ab575d1

input: f1eeb6aa2a5009b5ef68b5b754499dcb3ab575d1 my-submodule (remotes/origin/HEAD)
desired output: f1eeb6aa2a5009b5ef68b5b754499dcb3ab575d1

I've tried something like awk '{print $1;}' | grep -e '[0-9a-f]\{40\}' but it doesn't seem to remove the prefix. Any elegant solutions?

Upvotes: 1

Views: 865

Answers (3)

jthill
jthill

Reputation: 60497

I want to grep from git submodule status <PATH> the SHA-1 commit hash of my submodule.

Why go through the gyrations? Just ask for what you want directly:

git rev-parse :<PATH>

Upvotes: 5

Cyrus
Cyrus

Reputation: 88889

Add option -o to your grep command.

-o: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line

Upvotes: 2

VonC
VonC

Reputation: 1328282

You can use the output of git ls-tree:

Instead of:

D:\git\git>git ls-tree master -- sha1collisiondetection
160000 commit 855827c583bc30645ba427885caa40c5b81764d2  sha1collisiondetection

You would get:

git ls-tree master -- sha1collisiondetection|awk "{print $3}"
855827c583bc30645ba427885caa40c5b81764d2

Upvotes: 1

Related Questions