Set repo HEAD on gerrit project creation
By default we set repo HEAD to master but add configuration that allows
us to set it to some other value for new projects. Note this shouldn't
be used until tools for other systems like Gitea are ready.
We explicitly set the default to master as we rely on Gitea and Gerrit
and Git to all be in sync with the default branch. That is the case
today but may not be going forward. Being explicit allows us to avoid
early unexpected change but would also allow us to swap before all tools
update their defaults as well.
Note that we configure our local operating copy with the default head as
well to ensure there isn't additional confusion between the local repo
used to create .gitreview files and the gerrit canonical copy.
Depends-On: https://review.opendev.org/741277
Change-Id: Ie064056ba1722b1bca309dd9f96b61aae0a2fef9
diff --git a/jeepyb/cmd/manage_projects.py b/jeepyb/cmd/manage_projects.py
index caa9388..4b29e56 100644
--- a/jeepyb/cmd/manage_projects.py
+++ b/jeepyb/cmd/manage_projects.py
@@ -354,7 +354,7 @@
return created
-# TODO(mordred): Inspect repo_dir:master for a description
+# TODO(mordred): Inspect repo_dir:default-branch for a description
# override
def find_description_override(repo_path):
return None
@@ -388,14 +388,15 @@
raise ProcessACLException()
finally:
u.git_command(repo_path, 'reset --hard')
- u.git_command(repo_path, 'checkout master')
+ default_branch = section.get('default-branch', 'master')
+ u.git_command(repo_path, 'checkout %s' % default_branch)
u.git_command(repo_path, 'branch -D config')
-def create_gerrit_project(project, project_list, gerrit):
+def create_gerrit_project(project, default_branch, project_list, gerrit):
if project not in project_list:
try:
- gerrit.createProject(project)
+ gerrit.createProject(project, branches=[default_branch])
return True
except Exception:
log.exception(
@@ -487,6 +488,7 @@
description = section.get('description', None)
homepage = section.get('homepage', DEFAULT_HOMEPAGE)
upstream = section.get('upstream', None)
+ default_branch = section.get('default-branch', 'master')
repo_path = os.path.join(JEEPYB_CACHE_DIR, project)
# If this project doesn't want to use gerrit, exit cleanly.
@@ -514,7 +516,7 @@
if not project_created:
try:
project_created = create_gerrit_project(
- project, project_list, gerrit)
+ project, default_branch, project_list, gerrit)
project_cache[project]['project-created'] = True
except Exception:
project_cache[project]['project-created'] = False
@@ -529,7 +531,7 @@
# Make Local repo
push_string = u.make_local_copy(
- repo_path, project, project_list,
+ repo_path, project, default_branch, project_list,
git_opts, ssh_env, upstream, GITREVIEW_GERRIT_HOST,
GITREVIEW_GERRIT_PORT, project_git, GERRIT_GITID)
@@ -558,9 +560,10 @@
if not os.path.exists(repo_path):
u.make_local_copy(
- repo_path, project, project_list,
- git_opts, ssh_env, upstream, GERRIT_HOST,
- GERRIT_PORT, project_git, GERRIT_GITID)
+ repo_path, project, default_branch,
+ project_list, git_opts, ssh_env, upstream,
+ GERRIT_HOST, GERRIT_PORT, project_git,
+ GERRIT_GITID)
process_acls(
acl_config, project, ACL_DIR, section,
remote_url, repo_path, ssh_env, gerrit,
diff --git a/jeepyb/cmd/track_upstream.py b/jeepyb/cmd/track_upstream.py
index 65751b7..846dc96 100644
--- a/jeepyb/cmd/track_upstream.py
+++ b/jeepyb/cmd/track_upstream.py
@@ -192,6 +192,7 @@
upstream = section.get('upstream', None)
upstream_prefix = section.get('upstream-prefix', None)
+ default_branch = section.get('default-branch', 'master')
repo_path = os.path.join(IMPORT_DIR, project)
project_git = "%s.git" % project
@@ -209,7 +210,7 @@
# Make Local repo
if not os.path.exists(repo_path):
u.make_local_copy(
- repo_path, project, project_list,
+ repo_path, project, default_branch, project_list,
git_opts, ssh_env, upstream, GERRIT_HOST,
GERRIT_PORT, project_git, GERRIT_GITID)
else:
diff --git a/jeepyb/utils.py b/jeepyb/utils.py
index 8e6ab34..31510cf 100644
--- a/jeepyb/utils.py
+++ b/jeepyb/utils.py
@@ -92,7 +92,7 @@
return dict(GIT_SSH=name)
-def make_local_copy(repo_path, project, project_list,
+def make_local_copy(repo_path, project, default_branch, project_list,
git_opts, ssh_env, upstream, GERRIT_HOST, GERRIT_PORT,
project_git, GERRIT_GITID):
@@ -148,8 +148,11 @@
# Neither gerrit has it, nor does it have an upstream,
# just create a whole new one
else:
+ ref_str = 'refs/heads/%s' % default_branch
run_command("git init %s" % repo_path)
git_command(
+ repo_path, "symbolic-ref HEAD " + ref_str)
+ git_command(
repo_path,
"remote add origin %(remote_url)s" % git_opts)
with open(os.path.join(repo_path,
@@ -164,7 +167,7 @@
cmd = ("commit -a -m'Added .gitreview' --author='%s'"
% GERRIT_GITID)
git_command(repo_path, cmd)
- return "push %s HEAD:refs/heads/master"
+ return "push %s HEAD:" + ref_str
def fsck_repo(repo_path):