summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Willemsen <dwillemsen@google.com>2015-10-06 15:23:19 -0700
committerDan Willemsen <dwillemsen@google.com>2015-10-07 15:43:22 -0700
commit745b4ad660f8050045b521c4e15b7d3ac0b3d70e (patch)
tree643e836eafdde0c08e1eec540eab6ae23b2ef29e
parent4c5f74e4521679d1dcc31c575d417387b9c51c87 (diff)
downloadgit-repo-1.12.32.tar.gz
Fix gitc-init behaviorv1.12.32
With gitc-init, a gitc client may be specified using '-c'. If we're not currently in that client, we need to change directories so that we don't affect the local checkout, and to ensure that repo is checked out in the new client. This also makes '-c' optional if already in a gitc client, to match the rest of the init options. Change-Id: Ib514ad9fd101698060ae89bb035499800897e9bd
-rw-r--r--gitc_utils.py12
-rwxr-xr-xrepo38
-rw-r--r--subcmds/gitc_init.py18
-rw-r--r--tests/fixtures/gitc_config1
-rw-r--r--tests/test_wrapper.py75
5 files changed, 119 insertions, 25 deletions
diff --git a/gitc_utils.py b/gitc_utils.py
index dd38f890..0f3e1818 100644
--- a/gitc_utils.py
+++ b/gitc_utils.py
@@ -24,23 +24,13 @@ import git_command
24import git_config 24import git_config
25import wrapper 25import wrapper
26 26
27GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
28NUM_BATCH_RETRIEVE_REVISIONID = 300 27NUM_BATCH_RETRIEVE_REVISIONID = 300
29 28
30def get_gitc_manifest_dir(): 29def get_gitc_manifest_dir():
31 return wrapper.Wrapper().get_gitc_manifest_dir() 30 return wrapper.Wrapper().get_gitc_manifest_dir()
32 31
33def parse_clientdir(gitc_fs_path): 32def parse_clientdir(gitc_fs_path):
34 """Parse a path in the GITC FS and return its client name. 33 return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
35
36 @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
37
38 @returns: The GITC client name
39 """
40 if (gitc_fs_path == GITC_FS_ROOT_DIR or
41 not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)):
42 return None
43 return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
44 34
45def _set_project_revisions(projects): 35def _set_project_revisions(projects):
46 """Sets the revisionExpr for a list of projects. 36 """Sets the revisionExpr for a list of projects.
diff --git a/repo b/repo
index 502bb324..d1c5427f 100755
--- a/repo
+++ b/repo
@@ -109,6 +109,7 @@ S_manifests = 'manifests' # special manifest repository
109REPO_MAIN = S_repo + '/main.py' # main script 109REPO_MAIN = S_repo + '/main.py' # main script
110MIN_PYTHON_VERSION = (2, 6) # minimum supported python version 110MIN_PYTHON_VERSION = (2, 6) # minimum supported python version
111GITC_CONFIG_FILE = '/gitc/.config' 111GITC_CONFIG_FILE = '/gitc/.config'
112GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
112 113
113 114
114import errno 115import errno
@@ -221,7 +222,7 @@ def _GitcInitOptions(init_optparse):
221 help='Optional manifest file to use for this GITC client.') 222 help='Optional manifest file to use for this GITC client.')
222 g.add_option('-c', '--gitc-client', 223 g.add_option('-c', '--gitc-client',
223 dest='gitc_client', 224 dest='gitc_client',
224 help='The name for the new gitc_client instance.') 225 help='The name of the gitc_client instance to create or modify.')
225 226
226_gitc_manifest_dir = None 227_gitc_manifest_dir = None
227def get_gitc_manifest_dir(): 228def get_gitc_manifest_dir():
@@ -238,6 +239,28 @@ def get_gitc_manifest_dir():
238 pass 239 pass
239 return _gitc_manifest_dir 240 return _gitc_manifest_dir
240 241
242def gitc_parse_clientdir(gitc_fs_path):
243 """Parse a path in the GITC FS and return its client name.
244
245 @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
246
247 @returns: The GITC client name
248 """
249 if gitc_fs_path == GITC_FS_ROOT_DIR:
250 return None
251 if not gitc_fs_path.startswith(GITC_FS_ROOT_DIR):
252 manifest_dir = get_gitc_manifest_dir()
253 if manifest_dir == '':
254 return None
255 if manifest_dir[-1] != '/':
256 manifest_dir += '/'
257 if gitc_fs_path == manifest_dir:
258 return None
259 if not gitc_fs_path.startswith(manifest_dir):
260 return None
261 return gitc_fs_path.split(manifest_dir)[1].split('/')[0]
262 return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
263
241class CloneFailure(Exception): 264class CloneFailure(Exception):
242 """Indicate the remote clone of repo itself failed. 265 """Indicate the remote clone of repo itself failed.
243 """ 266 """
@@ -276,10 +299,13 @@ def _Init(args, gitc_init=False):
276 _print('fatal: GITC filesystem is not available. Exiting...', 299 _print('fatal: GITC filesystem is not available. Exiting...',
277 file=sys.stderr) 300 file=sys.stderr)
278 sys.exit(1) 301 sys.exit(1)
279 if not opt.gitc_client: 302 gitc_client = opt.gitc_client
303 if not gitc_client:
304 gitc_client = gitc_parse_clientdir(os.getcwd())
305 if not gitc_client:
280 _print('fatal: GITC client (-c) is required.', file=sys.stderr) 306 _print('fatal: GITC client (-c) is required.', file=sys.stderr)
281 sys.exit(1) 307 sys.exit(1)
282 client_dir = os.path.join(gitc_manifest_dir, opt.gitc_client) 308 client_dir = os.path.join(gitc_manifest_dir, gitc_client)
283 if not os.path.exists(client_dir): 309 if not os.path.exists(client_dir):
284 os.makedirs(client_dir) 310 os.makedirs(client_dir)
285 os.chdir(client_dir) 311 os.chdir(client_dir)
@@ -772,9 +798,13 @@ def _SetDefaultsTo(gitdir):
772 798
773 799
774def main(orig_args): 800def main(orig_args):
775 repo_main, rel_repo_dir = _FindRepo()
776 cmd, opt, args = _ParseArguments(orig_args) 801 cmd, opt, args = _ParseArguments(orig_args)
777 802
803 repo_main, rel_repo_dir = None, None
804 # Don't use the local repo copy, make sure to switch to the gitc client first.
805 if cmd != 'gitc-init':
806 repo_main, rel_repo_dir = _FindRepo()
807
778 wrapper_path = os.path.abspath(__file__) 808 wrapper_path = os.path.abspath(__file__)
779 my_main, my_git = _RunSelf(wrapper_path) 809 my_main, my_git = _RunSelf(wrapper_path)
780 810
diff --git a/subcmds/gitc_init.py b/subcmds/gitc_init.py
index 4f9d7344..2726eaec 100644
--- a/subcmds/gitc_init.py
+++ b/subcmds/gitc_init.py
@@ -21,6 +21,7 @@ import gitc_utils
21from command import GitcAvailableCommand 21from command import GitcAvailableCommand
22from manifest_xml import GitcManifest 22from manifest_xml import GitcManifest
23from subcmds import init 23from subcmds import init
24import wrapper
24 25
25 26
26class GitcInit(init.Init, GitcAvailableCommand): 27class GitcInit(init.Init, GitcAvailableCommand):
@@ -55,18 +56,15 @@ use for this GITC client.
55 help='Optional manifest file to use for this GITC client.') 56 help='Optional manifest file to use for this GITC client.')
56 g.add_option('-c', '--gitc-client', 57 g.add_option('-c', '--gitc-client',
57 dest='gitc_client', 58 dest='gitc_client',
58 help='The name for the new gitc_client instance.') 59 help='The name of the gitc_client instance to create or modify.')
59 60
60 def Execute(self, opt, args): 61 def Execute(self, opt, args):
61 if not opt.gitc_client: 62 gitc_client = gitc_utils.parse_clientdir(os.getcwd())
62 print('fatal: gitc client (-c) is required', file=sys.stderr) 63 if not gitc_client or (opt.gitc_client and gitc_client != opt.gitc_client):
64 print('fatal: Please update your repo command. See go/gitc for instructions.', file=sys.stderr)
63 sys.exit(1) 65 sys.exit(1)
64 self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(), 66 self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
65 opt.gitc_client) 67 gitc_client)
66 if not os.path.exists(gitc_utils.get_gitc_manifest_dir()):
67 os.makedirs(gitc_utils.get_gitc_manifest_dir())
68 if not os.path.exists(self.client_dir):
69 os.mkdir(self.client_dir)
70 super(GitcInit, self).Execute(opt, args) 68 super(GitcInit, self).Execute(opt, args)
71 69
72 manifest_file = self.manifest.manifestFile 70 manifest_file = self.manifest.manifestFile
@@ -77,8 +75,8 @@ use for this GITC client.
77 sys.exit(1) 75 sys.exit(1)
78 manifest_file = opt.manifest_file 76 manifest_file = opt.manifest_file
79 77
80 manifest = GitcManifest(self.repodir, opt.gitc_client) 78 manifest = GitcManifest(self.repodir, gitc_client)
81 manifest.Override(manifest_file) 79 manifest.Override(manifest_file)
82 gitc_utils.generate_gitc_manifest(None, manifest) 80 gitc_utils.generate_gitc_manifest(None, manifest)
83 print('Please run `cd %s` to view your GITC client.' % 81 print('Please run `cd %s` to view your GITC client.' %
84 os.path.join(gitc_utils.GITC_FS_ROOT_DIR, opt.gitc_client)) 82 os.path.join(wrapper.Wrapper().GITC_FS_ROOT_DIR, gitc_client))
diff --git a/tests/fixtures/gitc_config b/tests/fixtures/gitc_config
new file mode 100644
index 00000000..a7f3d1c9
--- /dev/null
+++ b/tests/fixtures/gitc_config
@@ -0,0 +1 @@
gitc_dir=/test/usr/local/google/gitc
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py
new file mode 100644
index 00000000..fb32e38a
--- /dev/null
+++ b/tests/test_wrapper.py
@@ -0,0 +1,75 @@
1#
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import unittest
18
19import wrapper
20
21def fixture(*paths):
22 """Return a path relative to tests/fixtures.
23 """
24 return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
25
26class RepoWrapperUnitTest(unittest.TestCase):
27 """Tests helper functions in the repo wrapper
28 """
29 def setUp(self):
30 """Load the wrapper module every time
31 """
32 wrapper._wrapper_module = None
33 self.wrapper = wrapper.Wrapper()
34
35 def test_get_gitc_manifest_dir_no_gitc(self):
36 """
37 Test reading a missing gitc config file
38 """
39 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
40 val = self.wrapper.get_gitc_manifest_dir()
41 self.assertEqual(val, '')
42
43 def test_get_gitc_manifest_dir(self):
44 """
45 Test reading the gitc config file and parsing the directory
46 """
47 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
48 val = self.wrapper.get_gitc_manifest_dir()
49 self.assertEqual(val, '/test/usr/local/google/gitc')
50
51 def test_gitc_parse_clientdir_no_gitc(self):
52 """
53 Test parsing the gitc clientdir without gitc running
54 """
55 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
56 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
57 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
58
59 def test_gitc_parse_clientdir(self):
60 """
61 Test parsing the gitc clientdir
62 """
63 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
64 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
65 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
66 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
67 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
68 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
69 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
70 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'), 'test')
71 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
72 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
73
74if __name__ == '__main__':
75 unittest.main()