diff options
author | Drew DeVault <sir@cmpwn.com> | 2017-11-12 20:30:26 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2017-11-12 20:30:26 -0500 |
commit | 17a7f707dc3cd36606eaf6045f1c62b0680da7af (patch) | |
tree | 6c9cdbe95414fb0ec8be04127a6c75a273783a98 /gitsrht/repos.py | |
parent | ef0dde8be65c8be14cd038f8126d4d80131e5dca (diff) |
Create repos if they don't exist on push
Diffstat (limited to 'gitsrht/repos.py')
-rw-r--r-- | gitsrht/repos.py | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/gitsrht/repos.py b/gitsrht/repos.py index 1b6ef54..1f7ebd2 100644 --- a/gitsrht/repos.py +++ b/gitsrht/repos.py @@ -2,6 +2,7 @@ import subprocess from srht.database import db from srht.config import cfg from gitsrht.types import Repository, RepoVisibility, Redirect +import shutil import re import os @@ -10,14 +11,17 @@ post_update = cfg("git.sr.ht", "post-update-script") def validate_name(valid, owner, repo_name): if not valid.ok: - return + return None valid.expect(re.match(r'^[a-z._-][a-z0-9._-]*$', repo_name), "Name must match [a-z._-][a-z0-9._-]*", field="name") existing = (Repository.query .filter(Repository.owner_id == owner.id) .filter(Repository.name.ilike("%" + repo_name + "%")) .first()) + if existing and existing.visibility == RepoVisibility.autocreated: + return existing valid.expect(not existing, "This name is already in use.", field="name") + return None def create_repo(valid, owner): repo_name = valid.require("name", friendly_name="Name") @@ -25,26 +29,36 @@ def create_repo(valid, owner): visibility = valid.optional("visibility", default="public", cls=RepoVisibility) - validate_name(valid, owner, repo_name) + repo = validate_name(valid, owner, repo_name) if not valid.ok: return None - repo = Repository() - repo.name = repo_name - repo.description = description - repo.owner_id = owner.id - repo.visibility = visibility - repo.path = os.path.join(repos_path, "~" + owner.username, repo.name) - db.session.add(repo) + if not repo: + repo = Repository() + repo.name = repo_name + repo.description = description + repo.owner_id = owner.id + repo.path = os.path.join(repos_path, "~" + owner.username, repo.name) + db.session.add(repo) + db.session.flush() - subprocess.run(["mkdir", "-p", repo.path]) - subprocess.run(["git", "init", "--bare"], cwd=repo.path) + subprocess.run(["mkdir", "-p", repo.path], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["git", "init", "--bare"], cwd=repo.path, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["git", "config", "srht.repo-id", str(repo.id)], + cwd=repo.path, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["ln", "-s", + post_update, + os.path.join(repo.path, "hooks", "update") + ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(["ln", "-s", + post_update, + os.path.join(repo.path, "hooks", "post-update") + ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + repo.visibility = visibility db.session.commit() - - subprocess.run(["git", "config", "srht.repo-id", str(repo.id)], cwd=repo.path) - subprocess.run(["ln", "-s", post_update, os.path.join(repo.path, "hooks", "update")]) - subprocess.run(["ln", "-s", post_update, os.path.join(repo.path, "hooks", "post-update")]) return repo def rename_repo(owner, repo, valid): @@ -71,5 +85,9 @@ def rename_repo(owner, repo, valid): repo.path = new_path repo.name = repo_name db.session.commit() - return repo + +def delete_repo(repo): + shutil.rmtree(repo.path) + db.session.delete(repo) + db.session.commit() |