GoogleGit

blob: 6c241c50b204480a10c1ed14bee441a1e41b774b [file] [log] [blame]
  1. #!/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:
  7. # gclient-new-workdir.py <repository> <new_workdir> [<branch>]
  8. #
  9. import os
  10. import shutil
  11. import subprocess
  12. import sys
  13. import textwrap
  14. def print_err(msg):
  15. print >> sys.stderr, msg
  16. def usage(msg=None):
  17. if msg is not None:
  18. print_err('\n' + textwrap.dedent(msg) + '\n')
  19. usage_msg = 'Run without arguments to get usage help.'
  20. else:
  21. usage_msg = '''\
  22. usage: %s <repository> <new_workdir>
  23. Clone an existing gclient directory, taking care of all sub-repositories
  24. Works similarly to 'git new-workdir'.
  25. <repository> should contain a .gclient file
  26. <new_workdir> must not exist
  27. ''' % os.path.basename(sys.argv[0])
  28. print_err(textwrap.dedent(usage_msg))
  29. sys.exit(1)
  30. def parse_options():
  31. if sys.platform == 'win32':
  32. usage('This script cannot run on Windows because it uses symlinks.')
  33. if len(sys.argv) != 3:
  34. usage()
  35. repository = os.path.abspath(sys.argv[1])
  36. new_workdir = sys.argv[2]
  37. if not os.path.exists(repository):
  38. usage('Repository does not exist: ' + repository)
  39. if os.path.exists(new_workdir):
  40. usage('New workdir already exists: ' + new_workdir)
  41. return repository, new_workdir
  42. def main():
  43. repository, new_workdir = parse_options()
  44. gclient = os.path.join(repository, '.gclient')
  45. if not os.path.exists(gclient):
  46. print_err('No .gclient file: ' + gclient)
  47. os.makedirs(new_workdir)
  48. os.symlink(gclient, os.path.join(new_workdir, '.gclient'))
  49. for root, dirs, _ in os.walk(repository):
  50. if '.git' in dirs:
  51. workdir = root.replace(repository, new_workdir, 1)
  52. make_workdir(os.path.join(root, '.git'),
  53. os.path.join(workdir, '.git'))
  54. def make_workdir(repository, new_workdir):
  55. print('Creating: ' + new_workdir)
  56. os.makedirs(new_workdir)
  57. GIT_DIRECTORY_WHITELIST = [
  58. 'config',
  59. 'info',
  60. 'hooks',
  61. 'logs/refs',
  62. 'objects',
  63. 'packed-refs',
  64. 'refs',
  65. 'remotes',
  66. 'rr-cache',
  67. 'svn'
  68. ]
  69. for entry in GIT_DIRECTORY_WHITELIST:
  70. make_symlink(repository, new_workdir, entry)
  71. shutil.copy2(os.path.join(repository, 'HEAD'),
  72. os.path.join(new_workdir, 'HEAD'))
  73. subprocess.check_call(['git', 'checkout', '-f'],
  74. cwd=new_workdir.rstrip('.git'))
  75. def make_symlink(repository, new_workdir, link):
  76. if not os.path.exists(os.path.join(repository, link)):
  77. return
  78. link_dir = os.path.dirname(os.path.join(new_workdir, link))
  79. if not os.path.exists(link_dir):
  80. os.makedirs(link_dir)
  81. os.symlink(os.path.join(repository, link), os.path.join(new_workdir, link))
  82. if __name__ == '__main__':
  83. sys.exit(main())