-
Notifications
You must be signed in to change notification settings - Fork 9
/
format-subst
executable file
·32 lines (22 loc) · 781 Bytes
/
format-subst
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
#!/bin/bash
# format-subst <infile> [ <outfile> ]
# Expands git's export-subst formatting placeholders in infile to outfile,
# thus producing the same result as if infile had been git-archived.
# Example:
# $ ./format-subst LICENSE.txt
# The MIT License (MIT)
# Copyright (c) 1985-2019...Applied Logic Systems, Inc.
# ...
set -Eeuo pipefail
INFILE=$1
OUTFILE=${2:-/dev/stdout}
# Only attempt substitution in a git tree, otherwise just cat-copy
if git rev-parse --is-inside-work-tree >/dev/null 2>&1
then
# Extract text from infile, striped of export-subt $Format:...$ syntax
TEXT=$(sed 's/\$Format:\(.*\)\$/\1/g' "$INFILE")
# Use text as git-log formatting template to produce outfile
git log -1 --format="$TEXT" > "$OUTFILE"
else
cat "$INFILE" > "$OUTFILE"
fi