From 319e59a115b6f26ba85ae745b44d40d2e552dd48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Thu, 13 Aug 2020 17:19:01 +0200 Subject: [PATCH] Add migration for receive.advertisePushOptions=true, previously missing pre-receive hooks, pruning useless files --- ...a45_clean_up_samples_allow_push_options.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 gitsrht/alembic/versions/dacab1dcba45_clean_up_samples_allow_push_options.py diff --git a/gitsrht/alembic/versions/dacab1dcba45_clean_up_samples_allow_push_options.py b/gitsrht/alembic/versions/dacab1dcba45_clean_up_samples_allow_push_options.py new file mode 100755 index 0000000..45cbaa2 --- /dev/null +++ b/gitsrht/alembic/versions/dacab1dcba45_clean_up_samples_allow_push_options.py @@ -0,0 +1,66 @@ +"""Clean up samples, allow push options + +Revision ID: dacab1dcba45 +Revises: 9f72f0dea908 +Create Date: 2020-08-12 18:45:59.390269 + +""" + +# revision identifiers, used by Alembic. +revision = 'dacab1dcba45' +down_revision = '9f72f0dea908' + +import glob +import os.path +from alembic import op +from sqlalchemy.orm import sessionmaker +from pygit2 import Repository as GitRepository +from gitsrht.types import Repository +from srht.config import cfg +try: + from tqdm import tqdm +except ImportError: + def tqdm(iterable): + yield from iterable + +Session = sessionmaker() + +post_update = cfg("git.sr.ht", "post-update-script") + + +def upgrade(): + bind = op.get_bind() + session = Session(bind=bind) + print("Allowing push options, fixing repositories with missing hooks, pruning samples") + for repo in tqdm(session.query(Repository).all()): + git_repo = GitRepository(repo.path) + git_repo.config["receive.advertisePushOptions"] = True + + try: + # pre-receive wasn't linked for autocreated repositories + os.symlink(post_update, os.path.join(repo.path, "hooks", "pre-receive")) + except FileExistsError: + pass + + try: + os.unlink(os.path.join(repo.path, "description")) + except FileNotFoundError: + pass + try: + os.unlink(os.path.join(repo.path, "info", "exclude")) + except FileNotFoundError: + pass + + for samp in glob.glob(os.path.join(repo.path, "hooks", "*.sample")): + os.unlink(samp) + + +def downgrade(): + bind = op.get_bind() + session = Session(bind=bind) + for repo in tqdm(session.query(Repository).all()): + git_repo = GitRepository(repo.path) + try: + del git_repo.config["receive.advertisePushOptions"] + except KeyError: + pass -- 2.38.4