forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_pr
executable file
·62 lines (52 loc) · 2.44 KB
/
merge_pr
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
#!/usr/bin/env python3
import sys
import argparse
from subprocess import check_call, call, check_output
def main():
parser = argparse.ArgumentParser(
description="Creates a PR for merging two branches")
parser.add_argument("from_branch",
help="From branch (e.g 1.1)")
parser.add_argument("to_branch",
help="To branch (e.g master)")
parser.add_argument("--yes", action="store_true",
help="Assume yes. Warning: discards local changes.")
parser.add_argument("--continue", action="store_true",
help="Continue after fixing merging errors.")
args = parser.parse_args()
tmp_branch = "automatic_merge_from_{}_to_{}_branch".format(
args.from_branch, args.to_branch)
if not vars(args)["continue"]:
if not args.yes and raw_input("This will destroy all local changes. " +
"Continue? [y/n]: ") != "y":
return 1
check_call("git reset --hard", shell=True)
check_call("git clean -df", shell=True)
check_call("git fetch", shell=True)
check_call("git checkout {}".format(args.from_branch), shell=True)
check_call("git pull", shell=True)
check_call("git checkout {}".format(args.to_branch), shell=True)
check_call("git pull", shell=True)
call("git branch -D {} > /dev/null".format(tmp_branch), shell=True)
check_call("git checkout -b {}".format(tmp_branch), shell=True)
if call("git merge {}".format(args.from_branch), shell=True) != 0:
print("Looks like you have merge errors.")
print("Fix them, commit, then run: {} --continue"
.format(" ".join(sys.argv)))
return 1
if len(check_output("git status -s", shell=True).strip()) > 0:
print("Looks like you have uncommitted changes")
return 1
if len(check_output("git log HEAD...{}".format(args.to_branch),
shell=True).strip()) == 0:
print("No commit to push")
return 1
print("Ready to push branch.")
remote = raw_input("To which remote should I push? (your fork): ")
call("git push {} :{} > /dev/null".format(remote, tmp_branch),
shell=True)
check_call("git push --set-upstream {} {}"
.format(remote, tmp_branch), shell=True)
print("Done. Go to Github and open the PR")
if __name__ == "__main__":
sys.exit(main())