blob: f17585869bc0675ff7ecb798c0d7b890c860e9a0 [file] [log] [blame]
Joanna Wang0a590f32023-04-07 17:51:12 +00001#!/usr/bin/env python3
2# Copyright (c) 2023 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""
Joanna Wang4a224312023-04-13 21:04:36 +00006Migrates an infra or infra_internal gclient to an infra_superproject one.
Joanna Wang0a590f32023-04-07 17:51:12 +00007
Joanna Wang4a224312023-04-13 21:04:36 +00008Does an in-place migration of the cwd's infra or infra_internal gclient
9checkout to an infra_superproject checkout, preserving all repo branches
10and commits. Should be called in the gclient root directory (the one that
11contains the .gclient file).
Joanna Wang0a590f32023-04-07 17:51:12 +000012
Joanna Wang4a224312023-04-13 21:04:36 +000013By default creates a backup dir of the original gclient checkout in
14`<dir>_backup`. If something goes wrong during the migration, this can
15be used to restore your environment to its original state.
Joanna Wang0a590f32023-04-07 17:51:12 +000016"""
17
Joanna Wang4a224312023-04-13 21:04:36 +000018import argparse
Joanna Wang0a590f32023-04-07 17:51:12 +000019import subprocess
20import os
21import sys
Joanna Wang4a224312023-04-13 21:04:36 +000022import shutil
Joanna Wang0a590f32023-04-07 17:51:12 +000023
24
25def main(argv):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000026 source = os.getcwd()
Joanna Wang0a590f32023-04-07 17:51:12 +000027
Mike Frysinger124bb8e2023-09-06 05:48:55 +000028 parser = argparse.ArgumentParser(
29 description=__doc__.strip().splitlines()[0],
30 epilog=' '.join(__doc__.strip().splitlines()[1:]))
31 parser.add_argument('-n',
32 '--no-backup',
33 action='store_true',
34 help='NOT RECOMMENDED. Skips copying the current '
35 'checkout (which can take up to ~15 min) to '
36 'a backup before starting the migration.')
37 args = parser.parse_args(argv)
Joanna Wang0a590f32023-04-07 17:51:12 +000038
Mike Frysinger124bb8e2023-09-06 05:48:55 +000039 if not args.no_backup:
40 backup = source + '_backup'
41 print(f'Creating backup in {backup}')
42 print('May take up to ~15 minutes...')
43 shutil.copytree(source, backup, symlinks=True, dirs_exist_ok=True)
44 print('backup complete')
Joanna Wang0a590f32023-04-07 17:51:12 +000045
Mike Frysinger124bb8e2023-09-06 05:48:55 +000046 print(f'Deleting old {source}/.gclient file')
47 gclient_file = os.path.join(source, '.gclient')
48 with open(gclient_file, 'r') as file:
49 data = file.read()
50 internal = "infra_internal" in data
51 os.remove(gclient_file)
Joanna Wang0a590f32023-04-07 17:51:12 +000052
Mike Frysinger124bb8e2023-09-06 05:48:55 +000053 print('Migrating to infra/infra_superproject')
54 cmds = ['fetch', '--force']
55 if internal:
56 cmds.append('infra_internal')
57 print('including internal code in checkout')
58 else:
59 cmds.append('infra')
60 shell = sys.platform == 'win32'
61 fetch = subprocess.Popen(cmds, cwd=source, shell=shell)
62 fetch.wait()
Joanna Wang0a590f32023-04-07 17:51:12 +000063
64
65if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000066 sys.exit(main(sys.argv[1:]))