+
+
+def GitGetRepoAlternates(directory):
+ """Gets the list of git alternates for a local git repo.
+
+ Args:
+ directory: Local git repository to get the git alternate for.
+
+ Returns:
+ List of git alternates set for the local git repository.
+ """
+ git_alternates_file = os.path.join(directory, GIT_ALTERNATES_PATH)
+ if os.path.isfile(git_alternates_file):
+ with open(git_alternates_file, 'rt') as f:
+ alternates_list = []
+ for line in f.readlines():
+ line = line.strip()
+ if line:
+ if posixpath.basename(line) == 'objects':
+ line = posixpath.dirname(line)
+ alternates_list.append(line)
+
+ return alternates_list
+
+ return []
+
+
+def GitSetRepoAlternates(directory, alternates_list, append=True, logger=None):
+ """Sets the list of git alternates for a local git repo.
+
+ Args:
+ directory: Local git repository.
+ alternates_list: List of local git repositories for the git alternates.
+ append: If True, will append the list to currently set list of alternates.
+ """
+ if logger is None:
+ logger = log_tools.GetConsoleLogger()
+ git_alternates_file = os.path.join(directory, GIT_ALTERNATES_PATH)
+ git_alternates_dir = os.path.dirname(git_alternates_file)
+ if not os.path.isdir(git_alternates_dir):
+ raise InvalidRepoException(directory,
+ 'Invalid local git repo: %s', directory)
+
+ original_alternates_list = GitGetRepoAlternates(directory)
+ if append:
+ alternates_list.extend(original_alternates_list)
+ alternates_list = sorted(set(alternates_list))
+
+ if set(original_alternates_list) != set(alternates_list):
+ lines = [posixpath.join(line, 'objects') + '\n' for line in alternates_list]
+ logger.info('Setting git alternates:\n\t%s', '\t'.join(lines))
+
+ with open(git_alternates_file, 'wb') as f:
+ f.writelines(lines)