ezpn
ezpn

Reputation: 1748

How to get list of git commit hashes that start with given string

Firstly I want to show you data I am working on. I have a list of commit hashes, for example:

008f1dcf984ede76c8e23c88c346fde38b6399e6
b665ceb8f06b009106eea99f296a24e338952545
ba90664a9316dedd7d2fa7ee0c7c01548f9c2b81
e36475c7cdba3dcdd5a606a328b72fa8bed5cec5
df7aec65c7c2f5efb8921ed2df6f2ce2a88507a6

User input:

b

Generated hash suggestion list:

b665ceb8f06b009106eea99f296a24e338952545
ba90664a9316dedd7d2fa7ee0c7c01548f9c2b81

First when user inputs just a few characters I must check how many hashes start with given pattern. If pattern maches only 10 hashes, I want to show him a list of suggested commit hashes.

I am looking for a git command that will help me count hashes that start with given string and git command to obtain this list. Given commands should work under Windows and Unix systems (I might use diffrent commands for every operating system).

Upvotes: 2

Views: 2983

Answers (2)

xmedeko
xmedeko

Reputation: 7820

Solution based on the @jszakmeister hint:

git rev-parse --disambiguate=4cdf| git cat-file --batch-check

Or if you have less than four characters:

git rev-list --all | grep '^b'

Upvotes: 2

meeDamian
meeDamian

Reputation: 1164

If "b" is the pattern you're looking for in unix I'll do it like so:

git log --format=oneline | awk '/^b/'

Another example (looking for hashes starting from 4cdf, and outputting only number of those):

git log --format=oneline | awk '/^4cdf/' | wc -l

I have absolutely no knowledge about Windows so someone else would have to help you with that...

Upvotes: 3

Related Questions