summaryrefslogtreecommitdiffstats
path: root/git_superproject.py
diff options
context:
space:
mode:
authorXin Li <delphij@google.com>2021-06-16 10:19:00 -0700
committerXin Li <delphij@google.com>2021-07-01 16:27:43 +0000
commit0cb6e92ac561878cc7f6d711648e732b498f943f (patch)
tree566fe915376b95f126a48d6f1180a3aeb0103b85 /git_superproject.py
parent0e776a5837d08d58cedd1d9e4288ed7db349749c (diff)
downloadgit-repo-0cb6e92ac561878cc7f6d711648e732b498f943f.tar.gz
Add the ability to administratively enroll repo into using superproject.
Repo will remember a choice and an expiration time of the choice, per user, about whether to use superproject by default. When not specified from command line and the choice is not expired, repo would use the user default value. When a user default value is not present and when the system wide enable default is provided in git's system configuration, repo would ask the user for a confirmation which will be valid for two weeks. git_config.py: Add support for system config. When reading system config, we would use --system to avoid hardcoding a path as the value may be different on some other distributions. git_superproject.py: Add a new subroutine, _UseSuperproject(), which returns whether superproject should be used and whether it is from a user configuration. The value is determined in the following order: 1. If the user specifies either --use-superproject or --no-use-superproject, then that choice is being used. 2. If neither is specified, we would then check the saved value (upon repo init) and use that choice when there was a choice. 3. We then check if there is a saved and unexpired value for user's choice in their ~/.gitconfig, and use the unexpired choice, if available. 4. Finally, if all the above didn't give us a decision, and if the git system configuration is providing a rollout hint, present a prompt to user for their decision and save it in ~/.gitconfig. subcmds/sync.py: Make use of the new UseSuperproject() provided by git_superproject.py. While there also silent stderr from git describe when determining the version of repo. Bug: [google internal] b/190688390 Change-Id: Iad3ee03026342ee500e5d65e2f0fa600d7637613 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/309762 Reviewed-by: Mike Frysinger <vapier@google.com> Tested-by: Xin Li <delphij@google.com>
Diffstat (limited to 'git_superproject.py')
-rw-r--r--git_superproject.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/git_superproject.py b/git_superproject.py
index 0c477060..2a47847a 100644
--- a/git_superproject.py
+++ b/git_superproject.py
@@ -23,11 +23,14 @@ Examples:
23""" 23"""
24 24
25import hashlib 25import hashlib
26import functools
26import os 27import os
27import sys 28import sys
29import time
28from typing import NamedTuple 30from typing import NamedTuple
29 31
30from git_command import git_require, GitCommand 32from git_command import git_require, GitCommand
33from git_config import RepoConfig
31from git_refs import R_HEADS 34from git_refs import R_HEADS
32from manifest_xml import LOCAL_MANIFEST_GROUP_PREFIX 35from manifest_xml import LOCAL_MANIFEST_GROUP_PREFIX
33 36
@@ -343,3 +346,81 @@ class Superproject(object):
343 346
344 manifest_path = self._WriteManfiestFile() 347 manifest_path = self._WriteManfiestFile()
345 return UpdateProjectsResult(manifest_path, False) 348 return UpdateProjectsResult(manifest_path, False)
349
350
351@functools.lru_cache(maxsize=None)
352def _UseSuperprojectFromConfiguration():
353 """Returns the user choice of whether to use superproject."""
354 user_cfg = RepoConfig.ForUser()
355 system_cfg = RepoConfig.ForSystem()
356 time_now = int(time.time())
357
358 user_value = user_cfg.GetBoolean('repo.superprojectChoice')
359 if user_value is not None:
360 user_expiration = user_cfg.GetInt('repo.superprojectChoiceExpire')
361 if user_expiration is not None and (user_expiration <= 0 or user_expiration >= time_now):
362 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
363 # default value.
364 print(('You are currently enrolled in Git submodules experiment '
365 '(go/android-submodules-quickstart). Use --no-use-superproject '
366 'to override.\n'), file=sys.stderr)
367 return user_value
368
369 # We don't have an unexpired choice, ask for one.
370 system_value = system_cfg.GetBoolean('repo.superprojectChoice')
371 if system_value:
372 # The system configuration is proposing that we should enable the
373 # use of superproject. Present this to user for confirmation if we
374 # are on a TTY, or, when we are not on a TTY, accept the system
375 # default for this time only.
376 #
377 # TODO(b/190688390) - Remove prompt when we are comfortable with the new
378 # default value.
379 prompt = ('Repo can now use Git submodules (go/android-submodules-quickstart) '
380 'instead of manifests to represent the state of the Android '
381 'superproject, which results in faster syncs and better atomicity.\n\n')
382 if sys.stdout.isatty():
383 prompt += 'Would you like to opt in for two weeks (y/N)? '
384 response = input(prompt).lower()
385 time_choiceexpire = time_now + (86400 * 14)
386 if response in ('y', 'yes'):
387 userchoice = True
388 elif response in ('a', 'always'):
389 userchoice = True
390 time_choiceexpire = 0
391 elif response == 'never':
392 userchoice = False
393 time_choiceexpire = 0
394 elif response in ('n', 'no'):
395 userchoice = False
396 else:
397 # Unrecognized user response, assume the intention was no, but
398 # only for 2 hours instead of 2 weeks to balance between not
399 # being overly pushy while still retain the opportunity to
400 # enroll.
401 userchoice = False
402 time_choiceexpire = time_now + 7200
403
404 user_cfg.SetString('repo.superprojectChoiceExpire', str(time_choiceexpire))
405 user_cfg.SetBoolean('repo.superprojectChoice', userchoice)
406
407 return userchoice
408 else:
409 print('Accepting once since we are not on a TTY', file=sys.stderr)
410 return True
411
412 # For all other cases, we would not use superproject by default.
413 return False
414
415
416def UseSuperproject(opt, manifest):
417 """Returns a boolean if use-superproject option is enabled."""
418
419 if opt.use_superproject is not None:
420 return opt.use_superproject
421 else:
422 client_value = manifest.manifestProject.config.GetBoolean('repo.superproject')
423 if client_value is not None:
424 return client_value
425 else:
426 return _UseSuperprojectFromConfiguration()