From 8d53cc0496f2f36a212ff699de59bc6127eadacb Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 19 2014 10:32:28 +0000 Subject: Merge pull request #2 from bochecha/master Don't rely on the filesystem so much --- diff --git a/progit/app.py b/progit/app.py index 5851e15..30fb8f1 100644 --- a/progit/app.py +++ b/progit/app.py @@ -39,13 +39,11 @@ def index(): limit = APP.config['ITEM_PER_PAGE'] start = limit * (page - 1) - end = limit * page - repos = sorted(os.listdir(APP.config['GIT_FOLDER'])) - repos_length = len(repos) - total_page = int(ceil(repos_length / float(limit))) + repos = progit.lib.list_projects(SESSION, start=start, limit=limit) + num_repos = progit.lib.count_projects(SESSION) - repos = repos[start:end] + total_page = int(ceil(num_repos / float(limit))) return flask.render_template( 'index.html', @@ -159,10 +157,13 @@ def new_project(): def view_repo(repo): """ Front page of a specific repo. """ - reponame = os.path.join(APP.config['GIT_FOLDER'], repo) - if not os.path.exists(reponame): + repo = progit.lib.get_project(SESSION, repo) + + if repo is None: flask.abort(404) - repo_obj = pygit2.Repository(reponame) + + repo_obj = pygit2.Repository(os.path.join(APP.config["GIT_FOLDER"], + repo.path)) cnt = 0 last_commits = [] @@ -190,10 +191,14 @@ def view_repo(repo): def view_repo_branch(repo, branchname): """ Displays the information about a specific branch. """ - reponame = os.path.join(APP.config['GIT_FOLDER'], repo) - if not os.path.exists(reponame): + repo = progit.lib.get_project(SESSION, repo) + + if repo is None: flask.abort(404) - repo_obj = pygit2.Repository(reponame) + + repo_obj = pygit2.Repository(os.path.join(APP.config["GIT_FOLDER"], + repo.path)) + if not branchname in repo_obj.listall_branches(): flask.abort(404) @@ -223,10 +228,13 @@ def view_repo_branch(repo, branchname): def view_log(repo, branchname=None): """ Displays the logs of the specified repo. """ - reponame = os.path.join(APP.config['GIT_FOLDER'], repo) - if not os.path.exists(reponame): + repo = progit.lib.get_project(SESSION, repo) + + if repo is None: flask.abort(404) - repo_obj = pygit2.Repository(reponame) + + repo_obj = pygit2.Repository(os.path.join(APP.config["GIT_FOLDER"], + repo.path)) if branchname and not branchname in repo_obj.listall_branches(): flask.abort(404) @@ -272,10 +280,13 @@ def view_log(repo, branchname=None): def view_file(repo, identifier, filename): """ Displays the content of a file or a tree for the specified repo. """ - reponame = os.path.join(APP.config['GIT_FOLDER'], repo) - if not os.path.exists(reponame): + repo = progit.lib.get_project(SESSION, repo) + + if repo is None: flask.abort(404) - repo_obj = pygit2.Repository(reponame) + + repo_obj = pygit2.Repository(os.path.join(APP.config["GIT_FOLDER"], + repo.path)) if identifier in repo_obj.listall_branches(): branchname = identifier @@ -336,10 +347,13 @@ def view_file(repo, identifier, filename): def view_commit(repo, commitid): """ Render a commit in a repo """ - reponame = os.path.join(APP.config['GIT_FOLDER'], repo) - if not os.path.exists(reponame): + repo = progit.lib.get_project(SESSION, repo) + + if repo is None: flask.abort(404) - repo_obj = pygit2.Repository(reponame) + + repo_obj = pygit2.Repository(os.path.join(APP.config["GIT_FOLDER"], + repo.path)) try: commit = repo_obj.get(commitid) @@ -378,10 +392,13 @@ def view_commit(repo, commitid): def view_tree(repo, identifier=None): """ Render the tree of the repo """ - reponame = os.path.join(APP.config['GIT_FOLDER'], repo) - if not os.path.exists(reponame): + repo = progit.lib.get_project(SESSION, repo) + + if repo is None: flask.abort(404) - repo_obj = pygit2.Repository(reponame) + + repo_obj = pygit2.Repository(os.path.join(APP.config["GIT_FOLDER"], + repo.path)) if identifier in repo_obj.listall_branches(): branchname = identifier diff --git a/progit/lib.py b/progit/lib.py index 50f0d10..dc780a3 100644 --- a/progit/lib.py +++ b/progit/lib.py @@ -79,3 +79,23 @@ def new_project(session, user, name, folder, pygit2.init_repository(gitrepo, bare=True) return 'Project "%s" created' % name + +def list_projects(session, start=None, limit=None): + """List existing projects""" + projects = session.query(model.Project) + + if start is not None: + projects = projects.offset(start) + + if limit is not None: + projects = projects.limit(limit) + + return projects.all() + +def count_projects(session): + """Count the number of projects in the database""" + return session.query(model.Project).count() + +def get_project(session, name): + """Get a project from the database""" + return session.query(model.Project).filter_by(name=name).first() diff --git a/progit/model.py b/progit/model.py index e79cddd..1478c2f 100644 --- a/progit/model.py +++ b/progit/model.py @@ -87,6 +87,9 @@ class Project(BASE): date_created = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow) + @property + def path(self): + return "%s.git" % self.name class Comment(BASE): """ Stores the comments made on a commit/file. diff --git a/progit/templates/commit.html b/progit/templates/commit.html index 970123e..bdfbe54 100644 --- a/progit/templates/commit.html +++ b/progit/templates/commit.html @@ -1,6 +1,6 @@ {% extends "master.html" %} -{% block title %}{{ repo }} - {{ commitid }}{% endblock %} +{% block title %}{{ repo.name }} - {{ commitid }}{% endblock %} {%block tag %}home{% endblock %} @@ -8,20 +8,20 @@

{% if '/fork/' in request.url %} - + {% else %} - + {% endif %} - {{ repo.split('.git')[0] }} + {{ repo.name }} {% if '/fork/' in request.url %} (tree) {% else %} (tree) {% endif %} @@ -50,10 +50,10 @@ {% for parent in commit.parents %} {% if '/fork/' in request.url %} + repo=repo.name, commitid=parent.oid.hex) }}"> {% else %} + repo=repo.name, commitid=parent.oid.hex) }}"> {% endif %} {{ parent.oid.hex }} diff --git a/progit/templates/file.html b/progit/templates/file.html index 15c2466..d149b63 100644 --- a/progit/templates/file.html +++ b/progit/templates/file.html @@ -8,19 +8,19 @@

{% if '/fork/' in request.url %} - + {% else %} - + {% endif %} - {{ repo.split('.git')[0] }} + {{ repo.name }} : {% if '/fork/' in request.url %} + repo=repo.name, identifier=branchname) }}"> {% else %} + repo=repo.name, identifier=branchname) }}"> {% endif %} {{ branchname }}/{% for file in filename.split('/') %} @@ -32,12 +32,12 @@ {% if loop.index != loop.length %} {% if '/fork/' in request.url %}{{ file }}/ {% else %}{{ file }}/{% endif %}{% else %} @@ -61,11 +61,11 @@ else %} {% else %} {% endif %} diff --git a/progit/templates/index.html b/progit/templates/index.html index eaa1353..61e59f2 100644 --- a/progit/templates/index.html +++ b/progit/templates/index.html @@ -38,8 +38,8 @@