~edwargix/git.sr.ht

92d4adde7cc7ec4036730f2f1ab43cfde887f67c — Drew DeVault 8 years ago a8bb8e5
Add hooks
2 files changed, 51 insertions(+), 0 deletions(-)

M git/blueprints/manage.py
A hooks/update
M git/blueprints/manage.py => git/blueprints/manage.py +6 -0
@@ 5,6 5,7 @@ from srht.database import db
from srht.validation import Validation
from git.types import Repository, RepoVisibility
from git.decorators import loginrequired
import shutil
import subprocess
import os
import re


@@ 62,6 63,11 @@ def create():

    db.session.commit()

    subprocess.run(["git", "config", "srht.repo-id", str(repo.id)],
            cwd=os.path.join(path, repo_name))
    hook_src = os.path.join(os.path.dirname(__file__), "..", "..", "hooks", "update")
    shutil.copy(hook_src, os.path.join(path, repo_name, "hooks", "update"))

    if another == "on":
        return redirect("/manage?another")
    else:

A hooks/update => hooks/update +45 -0
@@ 0,0 1,45 @@
#!/usr/bin/env python3
from srht.config import cfg, cfgi, load_config
load_config("git")
from srht.database import DbSession
db = DbSession(cfg("sr.ht", "connection-string"))
from git.types import User, Repository
db.init()
from configparser import ConfigParser
from datetime import datetime
import shlex
import subprocess
import sys

config = ConfigParser()
with open("config") as f:
    config.readfp(f)

repo_id = config.get("srht", "repo-id")
if not repo_id:
    sys.exit(0)
repo_id = int(repo_id)

repo = Repository.query.filter(Repository.id == repo_id).first()
if not repo:
    sys.exit(0)

# Might do this later:
#result = subprocess.run([
#        "git", "ls-tree", "--full-tree", "-r", "-l", shlex.quote(sys.argv[3]), "|",
#        "sort", "-k", "4", "-n", "-r", "|", "head", "-1"
#    ], shell=True)
#
#if result.returncode != 0:
#    sys.exit(0)
#parts = result.stdout.decode().split(' ')
#size = int(parts[3])
#max_size = cfg("git.sr.ht", "max-file-size", default=52428800)
#if size > max_size:
#    print("{} is over the maximum file size ({} MiB).", parts[4], max_size // 1048576)
#    sys.exit(1)

repo.updated = datetime.utcnow()
db.session.commit()

# TODO: fire webhooks