OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2014 Google Inc. | 2 # Copyright 2014 Google Inc. |
3 # | 3 # |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 | 7 |
8 """Parse a DEPS file and git checkout all of the dependencies. | 8 """Parse a DEPS file and git checkout all of the dependencies. |
9 | 9 |
10 Args: | 10 Args: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 def git_repository_sync_is_disabled(git, directory): | 58 def git_repository_sync_is_disabled(git, directory): |
59 try: | 59 try: |
60 disable = subprocess.check_output( | 60 disable = subprocess.check_output( |
61 [git, 'config', 'sync-deps.disable'], cwd=directory) | 61 [git, 'config', 'sync-deps.disable'], cwd=directory) |
62 return disable.lower().strip() in ['true', '1', 'yes', 'on'] | 62 return disable.lower().strip() in ['true', '1', 'yes', 'on'] |
63 except subprocess.CalledProcessError: | 63 except subprocess.CalledProcessError: |
64 return False | 64 return False |
65 | 65 |
66 | 66 |
| 67 def is_git_toplevel(git, directory): |
| 68 """Return true iff the directory is the top level of a Git repository. |
| 69 |
| 70 Args: |
| 71 git (string) the git executable |
| 72 |
| 73 directory (string) the path into which the repository |
| 74 is expected to be checked out. |
| 75 """ |
| 76 try: |
| 77 toplevel = subprocess.check_output( |
| 78 [git, 'rev-parse', '--show-toplevel'], cwd=directory).strip() |
| 79 return os.path.abspath(directory) == os.path.abspath(toplevel) |
| 80 except subprocess.CalledProcessError: |
| 81 return False |
| 82 |
| 83 |
67 def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): | 84 def git_checkout_to_directory(git, repo, checkoutable, directory, verbose): |
68 """Checkout (and clone if needed) a Git repository. | 85 """Checkout (and clone if needed) a Git repository. |
69 | 86 |
70 Args: | 87 Args: |
71 git (string) the git executable | 88 git (string) the git executable |
72 | 89 |
73 repo (string) the location of the repository, suitable | 90 repo (string) the location of the repository, suitable |
74 for passing to `git clone`. | 91 for passing to `git clone`. |
75 | 92 |
76 checkoutable (string) a tag, branch, or commit, suitable for | 93 checkoutable (string) a tag, branch, or commit, suitable for |
77 passing to `git checkout` | 94 passing to `git checkout` |
78 | 95 |
79 directory (string) the path into which the repository | 96 directory (string) the path into which the repository |
80 should be checked out. | 97 should be checked out. |
81 | 98 |
82 verbose (boolean) | 99 verbose (boolean) |
83 | 100 |
84 Raises an exception if any calls to git fail. | 101 Raises an exception if any calls to git fail. |
85 """ | 102 """ |
86 if not os.path.isdir(directory): | 103 if not os.path.isdir(directory): |
87 subprocess.check_call( | 104 subprocess.check_call( |
88 [git, 'clone', '--quiet', repo, directory]) | 105 [git, 'clone', '--quiet', repo, directory]) |
89 | 106 |
| 107 if not is_git_toplevel(git, directory): |
| 108 # if the directory exists, but isn't a git repo, you will modify |
| 109 # the parent repostory, which isn't what you want. |
| 110 sys.stdout.write('%s\n IS NOT TOP-LEVEL GIT DIRECTORY.\n' % directory) |
| 111 return |
| 112 |
90 # Check to see if this repo is disabled. Quick return. | 113 # Check to see if this repo is disabled. Quick return. |
91 if git_repository_sync_is_disabled(git, directory): | 114 if git_repository_sync_is_disabled(git, directory): |
92 sys.stdout.write('%s\n SYNC IS DISABLED.\n' % directory) | 115 sys.stdout.write('%s\n SYNC IS DISABLED.\n' % directory) |
93 return | 116 return |
94 | 117 |
95 subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory) | 118 subprocess.check_call([git, 'fetch', '--quiet'], cwd=directory) |
96 | 119 |
97 subprocess.check_call( | 120 subprocess.check_call( |
98 [git, 'checkout', '--quiet', checkoutable], cwd=directory) | 121 [git, 'checkout', '--quiet', checkoutable], cwd=directory) |
99 | 122 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 try: | 196 try: |
174 git_sync_deps(deps_file_path, argv, verbose) | 197 git_sync_deps(deps_file_path, argv, verbose) |
175 return 0 | 198 return 0 |
176 except DepsError: | 199 except DepsError: |
177 usage(deps_file_path) | 200 usage(deps_file_path) |
178 return 1 | 201 return 1 |
179 | 202 |
180 | 203 |
181 if __name__ == '__main__': | 204 if __name__ == '__main__': |
182 exit(main(sys.argv[1:])) | 205 exit(main(sys.argv[1:])) |
OLD | NEW |