From 545b35c0aa7a7701f58bd9740988e23fcbadf318 Mon Sep 17 00:00:00 2001 From: Veiko Aasa Date: Sat, 9 Jul 2022 12:39:47 +0300 Subject: [PATCH] gitweb: Switch default branch name to main for new repositories I tested additionally that if the root user has already configured default branch other than main, it is not changed by the gitweb app setup process. Signed-off-by: Veiko Aasa Reviewed-by: James Valleroy --- actions/gitweb | 22 ++++++++++++++++++++++ plinth/modules/gitweb/__init__.py | 2 +- plinth/modules/gitweb/tests/test_views.py | 10 +++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/actions/gitweb b/actions/gitweb index da0c9eaef..124e55650 100755 --- a/actions/gitweb +++ b/actions/gitweb @@ -142,6 +142,28 @@ def parse_arguments(): def subcommand_setup(_): """Disable default Apache2 Gitweb configuration.""" action_utils.webserver_disable('gitweb') + if not _get_global_default_branch(): + _set_global_default_branch('main') + + +def _get_global_default_branch(): + """Get globally configured default branch name.""" + try: + default_branch = subprocess.check_output( + ['git', 'config', '--global', '--get', + 'init.defaultBranch']).decode().strip() + except subprocess.CalledProcessError as exception: + if exception.returncode == 1: # Default branch not configured + return None + raise + + return default_branch + + +def _set_global_default_branch(name): + """Configure default branch name globally.""" + subprocess.check_call( + ['git', 'config', '--global', 'init.defaultBranch', name]) def _clone_with_progress_report(url, repo_dir): diff --git a/plinth/modules/gitweb/__init__.py b/plinth/modules/gitweb/__init__.py index e016fcd02..8c80dd072 100644 --- a/plinth/modules/gitweb/__init__.py +++ b/plinth/modules/gitweb/__init__.py @@ -42,7 +42,7 @@ class GitwebApp(app_module.App): app_id = 'gitweb' - _version = 1 + _version = 2 def __init__(self): """Create components for the app.""" diff --git a/plinth/modules/gitweb/tests/test_views.py b/plinth/modules/gitweb/tests/test_views.py index 11a459d7d..28a12466c 100644 --- a/plinth/modules/gitweb/tests/test_views.py +++ b/plinth/modules/gitweb/tests/test_views.py @@ -25,7 +25,7 @@ EXISTING_REPOS = [ 'owner': '', 'access': 'public', 'is_private': False, - 'default_branch': 'master', + 'default_branch': 'main', }, { 'name': 'something2', @@ -33,7 +33,7 @@ EXISTING_REPOS = [ 'owner': '', 'access': 'private', 'is_private': True, - 'default_branch': 'master', + 'default_branch': 'main', }, ] @@ -59,8 +59,8 @@ def action_run(*args, **kwargs): elif subcommand == 'get-branches': return json.dumps({ - "default_branch": "master", - "branches": ["master", "branch1"] + "default_branch": "main", + "branches": ["main", "branch1"] }) return None @@ -309,7 +309,7 @@ def test_edit_repository_failed_view(rf): 'gitweb-name': 'something_other', 'gitweb-description': 'test-description', 'gitweb-owner': 'test-owner', - 'gitweb-default_branch': 'master', + 'gitweb-default_branch': 'main', } request = rf.post( urls.reverse('gitweb:edit',