summaryrefslogtreecommitdiffstats
path: root/contrib/git-hooks
diff options
context:
space:
mode:
authorChris Laplante <chris.laplante@agilent.com>2020-12-25 13:16:49 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2020-12-30 14:01:07 +0000
commit53afafe42930aada4924cfa2fb5b6be587713312 (patch)
treee42bd17d74190ce4e838ef7e0a9246f6a3b2a013 /contrib/git-hooks
parent2421a2ca5dde8eb571b40a0c455f545695593d64 (diff)
downloadpoky-53afafe42930aada4924cfa2fb5b6be587713312.tar.gz
contrib/git-hooks: add a sendemail-validate example hook that adds FROM: lines to outgoing patch emails
This is useful for people using Microsoft Exchange / Office 360, which butchers patches causing author identity to be lost. (From OE-Core rev: 8dc690fddc1bace6b7af3f5e8af60c1625b067db) Signed-off-by: Chris Laplante <chris.laplante@agilent.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'contrib/git-hooks')
-rwxr-xr-xcontrib/git-hooks/sendemail-validate.sample78
1 files changed, 78 insertions, 0 deletions
diff --git a/contrib/git-hooks/sendemail-validate.sample b/contrib/git-hooks/sendemail-validate.sample
new file mode 100755
index 0000000000..af5d55cb00
--- /dev/null
+++ b/contrib/git-hooks/sendemail-validate.sample
@@ -0,0 +1,78 @@
1#!/usr/bin/env python3
2
3# Copyright (C) 2020 Agilent Technologies, Inc.
4# Author: Chris Laplante <chris.laplante@agilent.com>
5
6# This sendemail-validate hook injects 'From: ' header lines into outgoing
7# emails sent via 'git send-email', to ensure that accurate commit authorship
8# information is present. It was created because some email servers
9# (notably Microsoft Exchange / Office 360) seem to butcher outgoing patches,
10# resulting in incorrect authorship.
11
12# Current limitations:
13# 1. Assumes one per patch per email
14# 2. Minimal error checking
15#
16# Installation:
17# 1. Copy to .git/hooks/sendemail-validate
18# 2. chmod +x .git/hooks/sendemail-validate
19
20
21import enum
22import re
23import subprocess
24import sys
25
26
27class Subject(enum.IntEnum):
28 NOT_SEEN = 0
29 CONSUMING = 1
30 SEEN = 2
31
32
33def make_from_line():
34 cmd = ["git", "var", "GIT_COMMITTER_IDENT"]
35 proc = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, universal_newlines=True)
36 regex = re.compile(r"^(.*>).*$")
37 match = regex.match(proc.stdout)
38 assert match is not None
39 return "From: {0}".format(match.group(1))
40
41
42def main():
43 email = sys.argv[1]
44
45 with open(email, "r") as f:
46 email_lines = f.read().split("\n")
47
48 subject_seen = Subject.NOT_SEEN
49 first_body_line = None
50 for i, line in enumerate(email_lines):
51 if (subject_seen == Subject.NOT_SEEN) and line.startswith("Subject: "):
52 subject_seen = Subject.CONSUMING
53 continue
54 if subject_seen == Subject.CONSUMING:
55 if not line.strip():
56 subject_seen = Subject.SEEN
57 continue
58 if subject_seen == Subject.SEEN:
59 first_body_line = i
60 break
61
62 assert subject_seen == Subject.SEEN
63 assert first_body_line is not None
64
65 from_line = make_from_line()
66 # Only add FROM line if it is not already there
67 if email_lines[first_body_line] != from_line:
68 email_lines.insert(first_body_line, from_line)
69 email_lines.insert(first_body_line + 1, "")
70 with open(email, "w") as f:
71 f.write("\n".join(email_lines))
72
73 return 0
74
75
76if __name__ == "__main__":
77 sys.exit(main())
78