forked from github/platform-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-append-commit-trailer
executable file
·66 lines (61 loc) · 2.1 KB
/
git-append-commit-trailer
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
#
# Append a trailer with the commit hash to every commit message.
#
# # Why would you do this?
#
# Git commit hashes change when you rewrite history. If you release your
# software with the exact build hash (you should, it eases debugging!),
# then all released hashes won't be in your repository anymore. If you
# add the original hashes to the commit messages, then you can find them
# even after a history rewrite.
#
# Another use case for the original hashes is if you keep the original
# repository as archive for reference (e.g. because your industry
# requires you to keep *exactly* every state of the repository). The
# hash in the commit message will help you to find the corresponding
# commit in the original repository if necessary.
#
# Attention: Since the commit message is part of the commit hash
# calculation, this script changes the commit hashes too.
#
# Usage:
# git-append-commit-trailer [<options>]
#
# Options:
# -f, --force git filter-branch refuses to start with an existing
# temporary directory or when there are already refs
# starting with refs/original/, unless forced.
# See `man git-filter-branch`
#
# Example:
# Purge the file `foo.dat` from the repository:
#
# Step 1: Run `git-append-commit-trailer`
# Step 2: git-purge-files foo.dat
#
# Result: The file `foo.dat` was purged from the history and every
# commit message has a link (original commit hash) to the
# original version.
#
# Author: Lars Schneider, https://github.com/larsxschneider
#
filter=$(cat <<'EOF'
perl -se '
my $last;
my $found = 0;
while (my $line = <>) {
print "$line";
$found = 1 if ($line =~ m/^Original-commit: [a-f0-9]{40}$/);
$last = "$line";
}
# Add newline if there is none in the last line
print "\n" if (!($last =~ /\x0a$/));
# Add newline if there is no previous trailer
print "\n" if (!($last =~ /^\S+: /));
# Add commit hash if there was no other before
print "Original-commit: $hash\n" if (! $found);
' -- -hash=$GIT_COMMIT
EOF
)
git filter-branch $1 --msg-filter "$filter" --tag-name-filter cat -- --all