blob: 4eab72f97dae1478860cb495218ff704f1128ccd [file] [log] [blame]
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +00001#!/usr/bin/env python
2# Copyright 2013 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#
6# Usage:
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -07007# gclient-new-workdir.py [options] <repository> <new_workdir>
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +00008#
9
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070010import argparse
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000011import os
12import shutil
13import subprocess
14import sys
Wei-Yin Chen (陳威尹)56e4ad92017-05-23 01:05:38 -070015import textwrap
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000016
sammc@chromium.org900a33f2015-09-29 06:57:09 +000017import git_common
18
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000019
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000020def parse_options():
21 if sys.platform == 'win32':
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070022 print('ERROR: This script cannot run on Windows because it uses symlinks.')
23 sys.exit(1)
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000024
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070025 parser = argparse.ArgumentParser(description='''\
26 Clone an existing gclient directory, taking care of all sub-repositories.
27 Works similarly to 'git new-workdir'.''')
28 parser.add_argument('repository', type=os.path.abspath,
29 help='should contain a .gclient file')
30 parser.add_argument('new_workdir', help='must not exist')
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070031 parser.add_argument('--reflink', action='store_true', default=None,
32 help='''force to use "cp --reflink" for speed and disk
33 space. need supported FS like btrfs or ZFS.''')
34 parser.add_argument('--no-reflink', action='store_false', dest='reflink',
35 help='''force not to use "cp --reflink" even on supported
36 FS like btrfs or ZFS.''')
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070037 args = parser.parse_args()
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000038
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070039 if not os.path.exists(args.repository):
40 parser.error('Repository "%s" does not exist.' % args.repository)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000041
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070042 gclient = os.path.join(args.repository, '.gclient')
43 if not os.path.exists(gclient):
44 parser.error('No .gclient file at "%s".' % gclient)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000045
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070046 if os.path.exists(args.new_workdir):
47 parser.error('New workdir "%s" already exists.' % args.new_workdir)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000048
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070049 return args
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000050
51
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070052def support_cow(src, dest):
53 try:
54 subprocess.check_output(['cp', '-a', '--reflink', src, dest],
55 stderr=subprocess.STDOUT)
56 except subprocess.CalledProcessError:
57 return False
58 finally:
59 os.remove(dest)
60 return True
61
62
Wei-Yin Chen (陳威尹)56664462017-05-23 18:37:23 -070063def try_vol_snapshot(src, dest):
64 try:
65 subprocess.check_call(['btrfs', 'subvol', 'snapshot', src, dest],
66 stderr=subprocess.STDOUT)
67 except subprocess.CalledProcessError:
68 return False
69 return True
70
71
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000072def main():
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070073 args = parse_options()
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000074
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070075 gclient = os.path.join(args.repository, '.gclient')
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070076 new_gclient = os.path.join(args.new_workdir, '.gclient')
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000077
Wei-Yin Chen (陳威尹)56664462017-05-23 18:37:23 -070078 if try_vol_snapshot(args.repository, args.new_workdir):
79 args.reflink = True
80 else:
81 os.makedirs(args.new_workdir)
82 if args.reflink is None:
83 args.reflink = support_cow(gclient, new_gclient)
84 if args.reflink:
85 print('Copy-on-write support is detected.')
86 os.symlink(gclient, new_gclient)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000087
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070088 for root, dirs, _ in os.walk(args.repository):
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000089 if '.git' in dirs:
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070090 workdir = root.replace(args.repository, args.new_workdir, 1)
sammc@chromium.org900a33f2015-09-29 06:57:09 +000091 print('Creating: %s' % workdir)
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070092
93 if args.reflink:
94 if not os.path.exists(workdir):
95 print('Copying: %s' % workdir)
96 subprocess.check_call(['cp', '-a', '--reflink', root, workdir])
97 shutil.rmtree(os.path.join(workdir, '.git'))
98
sammc@chromium.org900a33f2015-09-29 06:57:09 +000099 git_common.make_workdir(os.path.join(root, '.git'),
100 os.path.join(workdir, '.git'))
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -0700101 if args.reflink:
102 subprocess.check_call(['cp', '-a', '--reflink',
103 os.path.join(root, '.git', 'index'),
104 os.path.join(workdir, '.git', 'index')])
105 else:
106 subprocess.check_call(['git', 'checkout', '-f'], cwd=workdir)
107
Wei-Yin Chen (陳威尹)56e4ad92017-05-23 01:05:38 -0700108 if args.reflink:
109 print(textwrap.dedent('''\
110 The repo was copied with copy-on-write, and the artifacts were retained.
111 More details on http://crbug.com/721585.
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +0000112
Wei-Yin Chen (陳威尹)56e4ad92017-05-23 01:05:38 -0700113 Depending on your usage pattern, you might want to do "gn gen"
114 on the output directories. More details: http://crbug.com/723856.'''))
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +0000115
116if __name__ == '__main__':
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +0000117 sys.exit(main())