HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashgitMinor

Adding trusted timestamps to Git commits

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
trustedaddingcommitsgittimestamps

Problem

I don't write too many bash scripts and I can usually struggle my way through getting the odd thing I need working, but my scripts always seem to feel a bit brittle. Below is a script I wrote for adding trusted timestamps to commits in a Git repository. I would appreciate any feedback on ways I could improve it.

```
#!/bin/bash

# Exit immediately if any commands return non-zero
set -e

# Config variables.
url=
cafile="${HOME}/time-stamping-cert-chain.crt"
request_delay=15 # COMODO asks for this in scripts.
blobref="tsa-blobs" # tsa = time stamp authority

# Make sure the script was called from within a Git repo. Ignore
# stdout.
git rev-parse --show-toplevel > /dev/null

# Start with flags set to false
delay_next=false
verbose=false
ltime=false

prep() {
# Assume we start with no note.
note=false

# The revision should be the first argument.
rev="$1"

# If no revision is specified, assume HEAD.
if [ -z "$rev" ]; then
rev="HEAD"
fi

# Run git rev-parse since it will expand HEAD and shortend hashes for
# us.
rev="$(git rev-parse "$rev")"

# Figure out if the timestamp note exists.
git notes --ref="$blobref" show "$rev" > /dev/null 2>&1 && note=true \
|| true # Make sure the command exits 0
}

print_rev() {
if $verbose; then
echo "$rev"
else
echo "$(git rev-parse --short "$rev")"
fi
}

print_signed_timestamp() {
tsatime="$(echo "$1" | grep -i "time stamp:" | cut -c13-)"
echo -e "\t$(date -d "$tsatime" +"SIGNED-%d-%b-%Y")"
#echo "$(date -d "$tsatime" --iso-8601=minutes)"
#echo "$(date -d "$tsatime" +"%Y-%m-%d-%T-%Z")"
}

print_local_timestamp() {
if $ltime; then
ctime="$(git log --pretty=format:"%ad" --date=iso "$rev" -1)"
echo -e "\t$(date -d "$ctime" +"COMMITTED-%d-%b-%Y")"
fi
}

# This outputs the text version of the TSA reply for the current
# revision.
examine() {
if $note; then
timestamp="$(git notes --ref="$blobref"

Solution

You can use a here document to avoid all those calls to echo:

cat <<END
Usage: git-timestamp [options] command [revision]

 options
  -h - Show this usage info.  All other options are ignored.
  -v - Output long revision instead of short.
  -l - Also show the local commit time of the specified revision.

 ...etc...
END

Code Snippets

cat <<END
Usage: git-timestamp [options] command [revision]

 options
  -h - Show this usage info.  All other options are ignored.
  -v - Output long revision instead of short.
  -l - Also show the local commit time of the specified revision.

 ...etc...
END

Context

StackExchange Code Review Q#15380, answer score: 3

Revisions (0)

No revisions yet.