-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetNthCommitSha.sh
More file actions
executable file
·36 lines (31 loc) · 880 Bytes
/
getNthCommitSha.sh
File metadata and controls
executable file
·36 lines (31 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# shell script that, given some kind of output from git log like "git log" or "git mylog", and
# a positional parameter (1 means appears first in output, 2 means appears second, etc.),
# will return the SHA for that commit.
#
# generally interesting as an example of a script that expects data from stdin, probably via pipe
#
# I use this in combination with other scripts/commands, like:
#
# # show the files from my second most recent commit
# git showfiles `git mylog | getNthCommitSha.sh 2`
if [ -n $1 ]; then
let target=$(($1))
else
let target=0
fi
if [ $target -le 0 ]; then
exit 1
fi
let counter=0
regex="^commit [a-z0-9]{40}$"
while read LINE; do
if [[ "${LINE}" =~ $regex ]]; then
let counter=$counter+1
if [ $counter -eq $target ]; then
echo "${LINE}" | awk '{print $2}' # do something with it here
exit 0
fi
fi
done
exit 1