forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-checkout
executable file
·82 lines (66 loc) · 3.08 KB
/
make-checkout
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
#!/usr/bin/env python3
# This file is part of Cockpit.
#
# Copyright (C) 2017 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
import shutil
import subprocess
import sys
from task import github
from machine.machine_core.directories import BASE_DIR
sys.dont_write_bytecode = True
# Check out the given ref and if necessary overlay the bots
# directory on top of it as expected on non-master branches
TARGET_DIR = "make-checkout-workdir"
def main():
parser = argparse.ArgumentParser(description="Fetch and checkout specific revision")
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
parser.add_argument("--repo", nargs='?', help="Repository to check out")
parser.add_argument("--rebase", metavar="branch", help="Branch to rebase to")
parser.add_argument("ref", help="The git ref to fetch")
parser.add_argument("revision", nargs='?', help="Actual commit to check out, defaults to ref")
opts = parser.parse_args()
if not opts.revision:
opts.revision = "FETCH_HEAD"
api = github.GitHub(repo=opts.repo)
def execute(*args, cwd=BASE_DIR, error_on_fail=True):
output = None
if opts.verbose:
sys.stderr.write("+ " + " ".join(args) + "\n")
try:
output = subprocess.check_output(args, cwd=cwd, universal_newlines=True)
except subprocess.CalledProcessError as ex:
sys.exit(ex.returncode if error_on_fail else 0)
finally:
if opts.verbose and output:
sys.stderr.write("> " + output + "\n")
return output
if os.path.exists(TARGET_DIR):
shutil.rmtree(TARGET_DIR)
cache = os.getenv('XDG_CACHE_HOME', ".")
execute("git", "clone", "--reference-if-able", "{}/{}".format(cache, api.repo),
"https://github.com/{}".format(api.repo), TARGET_DIR)
execute("git", "fetch", "origin", opts.ref, cwd=TARGET_DIR, error_on_fail=False)
execute("git", "checkout", "--detach", opts.revision, cwd=TARGET_DIR, error_on_fail=False)
if opts.rebase:
remote_base = "origin/" + opts.rebase
execute("git", "fetch", "origin", opts.rebase, cwd=TARGET_DIR, error_on_fail=False)
sha = execute("git", "rev-parse", remote_base, cwd=TARGET_DIR, error_on_fail=False).strip()
sys.stderr.write("Rebasing onto {0} ({1}) ...\n".format(remote_base, sha))
execute("git", "rebase", remote_base, cwd=TARGET_DIR, error_on_fail=False)
if __name__ == '__main__':
sys.exit(main())