-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlepatches
executable file
·101 lines (83 loc) · 3.54 KB
/
handlepatches
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/bin/env python3
# Copyright (C) 2015 Ross Burton <[email protected]>
# MIT licensed
import re
import os
import sys
import argparse
import imapclient
import subprocess
import git
import pathlib
parser = argparse.ArgumentParser()
parser.add_argument("branch", nargs="?", help="The branch to scan (default origin/master)", default="origin/master")
parser.add_argument("-r", "--repo", help="Repository to scan", type=pathlib.Path, default=".")
parser.add_argument("-c", "--commits", help="Number of commits back to go in history", type=int, default=10000)
parser.add_argument("-l", "--list", help="List patches found", action="store_true", default=False)
parser.add_argument("-v", "--verbose", help="Verbose mode", action="store_true", default=False)
parser.add_argument("-d", "--dryrun", help="Dry-run only", action="store_true", default=False)
args = parser.parse_args()
verbose = args.verbose
def normalise(s):
return " ".join(s.split())
def get_commits(repo, branch, num_commits):
# TODO: this should use GitPython
revlist = subprocess.Popen("git -C %s log %s --format=oneline -n %d" % (repo, branch, num_commits), shell=True, stdout=subprocess.PIPE).communicate()[0]
revlist = revlist.decode("utf-8")
revdata = {}
for line in revlist.split("\n"):
if not line:
continue
rev, desc = line.split(" ", 1)
if verbose: print("Storing commit '%s'" % desc)
revdata[normalise(desc)] = rev
return revdata
def match_messages(server, folder, search=None):
server.select_folder(folder)
if search:
messages = server.gmail_search(search)
else:
messages = server.search()
print("Fetched %d messages" % len(messages))
found = []
response = server.fetch(messages, ['ENVELOPE'])
for msgid, data in response.items():
if b"ENVELOPE" not in data:
print("Skipping %s without ENVELOPE" % msgid)
continue
# Assume subject is UTF-8
subject = normalise(data[b"ENVELOPE"].subject.decode("utf-8"))
subject = re.sub(r".? patchwork:", "", subject)
if "]" in subject:
subject = subject.rsplit("]", 1)[1].strip()
if verbose: print("Searching for subject '%s'" % subject)
if subject in revdata:
found.append(msgid)
if verbose or args.list:
print("Found match for %s" % subject)
print("Found %d merged patches" % len(found))
return found
try:
repo = git.Repo(args.repo)
except git.exc.InvalidGitRepositoryError:
print("handlepatches wasn't ran inside a git clone, aborting")
sys.exit(1)
config = repo.config_reader()
for option in ("imapserver", "imapuser", "imappassword", "search"):
if not config.has_option("handlepatches", option):
print(f"Missing config option handlepatches.{option}")
sys.exit(1)
revdata = get_commits(args.repo, args.branch, args.commits)
server = imapclient.IMAPClient(config.get("handlepatches", "imapserver"), ssl=True)
server.login(config.get("handlepatches", "imapuser"), config.get("handlepatches", "imappassword"))
# Labels to remove
remove_label = config.get_value("handlepatches", "removelabel", default="x")
folder = server.find_special_folder(imapclient.imapclient.ALL)
assert folder
messages = match_messages(server, folder, config.get("handlepatches", "search"))
if not args.dryrun:
server.add_flags(messages, imapclient.SEEN)
if remove_label:
server.remove_gmail_labels(messages, [remove_label])
#server.remove_flags(messages, imapclient.FLAGGED)
#server.delete_messages(messages)