roundabout,
created on Saturday, 27 April 2024, 17:02:08 (1714237328),
received on Wednesday, 31 July 2024, 06:54:47 (1722408887)
Author identity: vlad <vlad.muntoiu@gmail.com>
9e25b63a546ca44cc156c2ce88e02b4620dbbf84
app.py
@@ -1460,9 +1460,13 @@ def repository_settings(username, repository):
repo = git.Repo(os.path.join(config.REPOS_PATH, username, repository)) site_link = Markup(f"<code>http{'s' if config.suggest_https else ''}://{repository}.{username}.{config.BASE_DOMAIN}/</code>") primary_site_link = Markup(f"<code>http{'s' if config.suggest_https else ''}://{username}.{config.BASE_DOMAIN}/</code>") return flask.render_template("repo-settings.html", username=username, repository=repository, repo_data=db.session.get(Repo, f"/{username}/{repository}"), branches=[branch.name for branch in repo.branches])branches=[branch.name for branch in repo.branches], site_link=site_link, primary_site_link=primary_site_link)@repositories.route("/<username>/<repository>/settings/", methods=["POST"])
@@ -1474,12 +1478,26 @@ def repository_settings_post(username, repository):
repo.visibility = flask.request.form.get("visibility", type=int) repo.info = flask.request.form.get("description") print(flask.request.form.get("default_branch"))repo.default_branch = flask.request.form.get("default_branch") print(repo.default_branch)# Update site settings had_site = repo.has_site if flask.request.form.get("site_branch"): repo.site_branch = flask.request.form.get("site_branch") if flask.request.form.get("primary_site"): repo.has_site = 2 else: repo.has_site = 1 else: repo.site_branch = None repo.has_site = 0db.session.commit() if not had_site and repo.has_site: # Deploy the newly activated site result = celery_tasks.copy_site.delay(repo.route) return flask.redirect(f"/{username}/{repository}/settings", 303)
celery_tasks.py
@@ -52,6 +52,7 @@ def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True)
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit # Otherwise, we need to fetch the head repo. remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) out, err = b"", b""
@@ -83,3 +84,18 @@ def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True)
common.git_command(server_repo_location, b"", "merge", "--abort") return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch, merge_exit @shared_task(ignore_result=False) def copy_site(route): from models import Repo # Create the site directory if it doesn't exist. if not os.path.isdir(os.path.join(config.SITE_PATH, route.lstrip("/"))): os.makedirs(os.path.join(config.SITE_PATH, route.lstrip("/"))) repo = db.session.get(Repo, route) server_repo_location = os.path.join(config.REPOS_PATH, route.lstrip("/")) site_location = os.path.join(config.SITE_PATH, route.lstrip("/")) # Get the branch to be used for the site; if it somehow doesn't exist, use the default branch. branch = repo.site_branch or repo.default_branch # Make a shallow clone of the repo; this prevents getting the full git database when it's not needed. common.git_command(site_location, b"", "clone", "--depth=1", f"--branch={branch}", os.path.join(os.getcwd(), server_repo_location))
config.py
@@ -15,7 +15,8 @@ CONTACT_EMAIL: str = "root@roundabout-host.com"
REPOS_PATH: str = "./repos" USERDATA_PATH: str = "./userdata" DEFAULT_AVATARS_PATH: str = "default_avatars"SITE_PATH: str = "./usersites" DEFAULT_AVATARS_PATH: str = "./default_avatars"BASE_DOMAIN: str = "localhost" SERVER_IPS: set = {"127.0.0.1", "localhost", "0.0.0.0"} AUTH_REALM: str = "roundabout"
git_http.py
@@ -1,5 +1,4 @@
import uuid from models import * from app import app, db, bcrypt from misc_utils import *
@@ -67,7 +66,7 @@ def git_receive_pack(username, repository):
if not push_info: return flask.Response(text, content_type="application/x-git-receive-pack-result") old_sha, new_sha, _ = push_info[4:].split() # discard first 4 characters, used for line lengthold_sha, new_sha, ref = push_info[4:].split() # discard first 4 characters, used for line length which we don't needif old_sha == "0" * 40: commits_list = subprocess.check_output(["git", "rev-list", new_sha],
@@ -92,6 +91,14 @@ def git_receive_pack(username, repository):
db.session.add(commit) db.session.commit() if ref.startswith("refs/heads/"): # if the push is to a branch ref = ref.rpartition("/")[2] # get the branch name only repo_data = db.session.get(Repo, f"/{username}/{repository}") if ref == repo_data.site_branch: # Update the site from celery_tasks import copy_site copy_site.delay(repo_data.route) return flask.Response(text, content_type="application/x-git-receive-pack-result")
models.py
@@ -161,6 +161,10 @@ with (app.app_context()):
heads = db.relationship("PullRequest", back_populates="head", foreign_keys="[PullRequest.head_route]") bases = db.relationship("PullRequest", back_populates="base", foreign_keys="[PullRequest.base_route]") has_site = db.Column(db.SmallInteger, nullable=False, default=0, # 0 means no site, 1 means it's got a site, 2 means it's the user's primary site server_default="0") # (the one accessible at username.localhost) site_branch = db.Column(db.String(64), nullable=True) last_post_id = db.Column(db.Integer, nullable=False, default=0) def __init__(self, owner, name, visibility):
templates/repository/repo-settings.html
@@ -41,6 +41,25 @@
{% trans %}This is the branch the application will redirect to automatically if one is not specified. Note that only branch names are supported, not other references like tags or commit hashes.{% endtrans %} </x-vbox> <x-vbox class="nopad"> <label for="site-branch">{% trans %}Site branch{% endtrans %}</label> <select id="site-branch" name="site_branch"> <option value="">{% trans %}No sites{% endtrans %}</option> {% for branch in branches %} <option value="{{ branch }}" {% if branch == repo_data.site_branch %}selected{% endif %}>{{ branch }}</option> {% endfor %} </select> </x-vbox> <label> <input type="checkbox" name="primary_site" value="1" {% if repo_data.site == 2 %}checked{% endif %}> {% trans %}Make this the primary site{% endtrans %} </label> <p> {% trans link=site_link, primary_link=primary_site_link %} Host static sites for your projects, for free. The files in your site branch will be served at {{ link }} or {{ primary_link }} if you make this the primary site. {% endtrans %} </p><button type="submit">Update</button> </x-vbox> </form>