| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 # | 5 # |
| 6 # Usage: | 6 # Usage: |
| 7 # gclient-new-workdir.py <repository> <new_workdir> [<branch>] | 7 # gclient-new-workdir.py <repository> <new_workdir> [<branch>] |
| 8 # | 8 # |
| 9 | 9 |
| 10 import os | 10 import os |
| 11 import shutil | 11 import shutil |
| 12 import subprocess | 12 import subprocess |
| 13 import sys | 13 import sys |
| 14 import textwrap | 14 import textwrap |
| 15 | 15 |
| 16 import git_common |
| 17 |
| 16 | 18 |
| 17 def print_err(msg): | 19 def print_err(msg): |
| 18 print >> sys.stderr, msg | 20 print >> sys.stderr, msg |
| 19 | 21 |
| 20 | 22 |
| 21 def usage(msg=None): | 23 def usage(msg=None): |
| 22 | 24 |
| 23 if msg is not None: | 25 if msg is not None: |
| 24 print_err('\n' + textwrap.dedent(msg) + '\n') | 26 print_err('\n' + textwrap.dedent(msg) + '\n') |
| 25 usage_msg = 'Run without arguments to get usage help.' | 27 usage_msg = 'Run without arguments to get usage help.' |
| 26 else: | 28 else: |
| 27 usage_msg = '''\ | 29 usage_msg = '''\ |
| 28 usage: %s <repository> <new_workdir> | 30 usage: %s <repository> <new_workdir> |
| 29 | 31 |
| 30 Clone an existing gclient directory, taking care of all sub-repositories | 32 Clone an existing gclient directory, taking care of all sub-repositories |
| 31 Works similarly to 'git new-workdir'. | 33 Works similarly to 'git new-workdir'. |
| 32 | 34 |
| 33 <repository> should contain a .gclient file | 35 <repository> should contain a .gclient file |
| 34 <new_workdir> must not exist | 36 <new_workdir> must not exist |
| 35 ''' % os.path.basename(sys.argv[0]) | 37 '''% os.path.basename(sys.argv[0]) |
| 36 | 38 |
| 37 print_err(textwrap.dedent(usage_msg)) | 39 print_err(textwrap.dedent(usage_msg)) |
| 38 sys.exit(1) | 40 sys.exit(1) |
| 39 | 41 |
| 40 | 42 |
| 41 def parse_options(): | 43 def parse_options(): |
| 42 if sys.platform == 'win32': | 44 if sys.platform == 'win32': |
| 43 usage('This script cannot run on Windows because it uses symlinks.') | 45 usage('This script cannot run on Windows because it uses symlinks.') |
| 44 | 46 |
| 45 if len(sys.argv) != 3: | 47 if len(sys.argv) != 3: |
| (...skipping 17 matching lines...) Expand all Loading... |
| 63 gclient = os.path.join(repository, '.gclient') | 65 gclient = os.path.join(repository, '.gclient') |
| 64 if not os.path.exists(gclient): | 66 if not os.path.exists(gclient): |
| 65 print_err('No .gclient file: ' + gclient) | 67 print_err('No .gclient file: ' + gclient) |
| 66 | 68 |
| 67 os.makedirs(new_workdir) | 69 os.makedirs(new_workdir) |
| 68 os.symlink(gclient, os.path.join(new_workdir, '.gclient')) | 70 os.symlink(gclient, os.path.join(new_workdir, '.gclient')) |
| 69 | 71 |
| 70 for root, dirs, _ in os.walk(repository): | 72 for root, dirs, _ in os.walk(repository): |
| 71 if '.git' in dirs: | 73 if '.git' in dirs: |
| 72 workdir = root.replace(repository, new_workdir, 1) | 74 workdir = root.replace(repository, new_workdir, 1) |
| 73 make_workdir(os.path.join(root, '.git'), | 75 print('Creating: %s' % workdir) |
| 74 os.path.join(workdir, '.git')) | 76 git_common.make_workdir(os.path.join(root, '.git'), |
| 75 | 77 os.path.join(workdir, '.git')) |
| 76 | 78 subprocess.check_call(['git', 'checkout', '-f'], |
| 77 def make_workdir(repository, new_workdir): | 79 cwd=new_workdir.rstrip('.git')) |
| 78 print('Creating: ' + new_workdir) | |
| 79 os.makedirs(new_workdir) | |
| 80 | |
| 81 GIT_DIRECTORY_WHITELIST = [ | |
| 82 'config', | |
| 83 'info', | |
| 84 'hooks', | |
| 85 'logs/refs', | |
| 86 'objects', | |
| 87 'packed-refs', | |
| 88 'refs', | |
| 89 'remotes', | |
| 90 'rr-cache', | |
| 91 'svn' | |
| 92 ] | |
| 93 | |
| 94 for entry in GIT_DIRECTORY_WHITELIST: | |
| 95 make_symlink(repository, new_workdir, entry) | |
| 96 | |
| 97 shutil.copy2(os.path.join(repository, 'HEAD'), | |
| 98 os.path.join(new_workdir, 'HEAD')) | |
| 99 subprocess.check_call(['git', 'checkout', '-f'], | |
| 100 cwd=new_workdir.rstrip('.git')) | |
| 101 | |
| 102 | |
| 103 def make_symlink(repository, new_workdir, link): | |
| 104 if not os.path.exists(os.path.join(repository, link)): | |
| 105 return | |
| 106 link_dir = os.path.dirname(os.path.join(new_workdir, link)) | |
| 107 if not os.path.exists(link_dir): | |
| 108 os.makedirs(link_dir) | |
| 109 os.symlink(os.path.join(repository, link), os.path.join(new_workdir, link)) | |
| 110 | 80 |
| 111 | 81 |
| 112 if __name__ == '__main__': | 82 if __name__ == '__main__': |
| 113 sys.exit(main()) | 83 sys.exit(main()) |
| OLD | NEW |