blob: 96fccf700dc15b609761c36512b9667e29954023 [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
15
sammc@chromium.org900a33f2015-09-29 06:57:09 +000016import git_common
17
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000018
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000019def parse_options():
20 if sys.platform == 'win32':
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070021 print('ERROR: This script cannot run on Windows because it uses symlinks.')
22 sys.exit(1)
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000023
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070024 parser = argparse.ArgumentParser(description='''\
25 Clone an existing gclient directory, taking care of all sub-repositories.
26 Works similarly to 'git new-workdir'.''')
27 parser.add_argument('repository', type=os.path.abspath,
28 help='should contain a .gclient file')
29 parser.add_argument('new_workdir', help='must not exist')
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070030 parser.add_argument('--reflink', action='store_true', default=None,
31 help='''force to use "cp --reflink" for speed and disk
32 space. need supported FS like btrfs or ZFS.''')
33 parser.add_argument('--no-reflink', action='store_false', dest='reflink',
34 help='''force not to use "cp --reflink" even on supported
35 FS like btrfs or ZFS.''')
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070036 args = parser.parse_args()
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000037
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070038 if not os.path.exists(args.repository):
39 parser.error('Repository "%s" does not exist.' % args.repository)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000040
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070041 gclient = os.path.join(args.repository, '.gclient')
42 if not os.path.exists(gclient):
43 parser.error('No .gclient file at "%s".' % gclient)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000044
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070045 if os.path.exists(args.new_workdir):
46 parser.error('New workdir "%s" already exists.' % args.new_workdir)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000047
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070048 return args
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000049
50
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070051def support_cow(src, dest):
52 try:
53 subprocess.check_output(['cp', '-a', '--reflink', src, dest],
54 stderr=subprocess.STDOUT)
55 except subprocess.CalledProcessError:
56 return False
57 finally:
58 os.remove(dest)
59 return True
60
61
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000062def main():
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070063 args = parse_options()
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000064
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070065 gclient = os.path.join(args.repository, '.gclient')
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070066 new_gclient = os.path.join(args.new_workdir, '.gclient')
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000067
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070068 os.makedirs(args.new_workdir)
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070069 if args.reflink is None:
70 args.reflink = support_cow(gclient, new_gclient)
71 if args.reflink:
72 print('Copy-on-write is supported. Using reflink to copy the repo.')
73 os.symlink(gclient, new_gclient)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000074
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070075 for root, dirs, _ in os.walk(args.repository):
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +000076 if '.git' in dirs:
Wei-Yin Chen (陳威尹)05203492017-05-11 00:07:53 -070077 workdir = root.replace(args.repository, args.new_workdir, 1)
sammc@chromium.org900a33f2015-09-29 06:57:09 +000078 print('Creating: %s' % workdir)
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070079
80 if args.reflink:
81 if not os.path.exists(workdir):
82 print('Copying: %s' % workdir)
83 subprocess.check_call(['cp', '-a', '--reflink', root, workdir])
84 shutil.rmtree(os.path.join(workdir, '.git'))
85
sammc@chromium.org900a33f2015-09-29 06:57:09 +000086 git_common.make_workdir(os.path.join(root, '.git'),
87 os.path.join(workdir, '.git'))
Wei-Yin Chen (陳威尹)704be872017-05-11 00:58:26 -070088 if args.reflink:
89 subprocess.check_call(['cp', '-a', '--reflink',
90 os.path.join(root, '.git', 'index'),
91 os.path.join(workdir, '.git', 'index')])
92 else:
93 subprocess.check_call(['git', 'checkout', '-f'], cwd=workdir)
94
95 if args.reflink:
96 subprocess.check_call(['git', 'clean', '-df'], cwd=workdir)
adam.treat@samsung.com62d817c2013-11-05 15:18:13 +000097
98
99if __name__ == '__main__':
pgervais@chromium.orgf13c2052013-11-15 20:09:23 +0000100 sys.exit(main())