diff --git a/izanami/controller.py b/izanami/controller.py index 441424d..2523500 100644 --- a/izanami/controller.py +++ b/izanami/controller.py @@ -11,7 +11,6 @@ import shutil import yaml import traceback -from io import StringIO from unidiff import PatchSet @@ -22,6 +21,7 @@ def handle(self, request): return Response.render(template, { 'repos': repos }) + def create(self, request): template = self.view.get_template("repo/create.html") nodes = [ @@ -38,12 +38,12 @@ def create(self, request): if not (self.app.project_dir / 'git_template').is_dir: git.Repo.init( self.app.project_dir / 'git_template', - bare = True + bare=True ) git.Repo.init( self.app.project_dir / ('repos/' + repo.name + '.git'), - bare = True, - template = self.app.project_dir / 'git_template' + bare=True, + template=self.app.project_dir / 'git_template' ) return Response.redirect(self.app.convert_url('/'+repo.name)) except Exception as err: @@ -61,8 +61,15 @@ def create(self, request): def update(self, request): template = self.view.get_template("repo/update.html") - repo = Repo.retrieve(name = request.params['repo']) - if isinstance(repo.owner.object, Group) and not InnerPermission.is_accepted('update_repository', repo.owner.object, request.user): + repo = Repo.retrieve(name=request.params['repo']) + if ( + repo.owner != request.user and + not InnerPermission.is_accepted( + 'update_repository', + repo.owner.object, + request.user + ) + ): return Response.redirect(self.app.convert_url('/')) try: if request.method == 'POST': @@ -87,14 +94,23 @@ def update(self, request): def delete(self, request): template = self.view.get_template("repo/delete.html") - repo = Repo.retrieve(name = request.params['repo']) - if isinstance(repo.owner.object, Group) and not InnerPermission.is_accepted('delete_repository', repo.owner.object, request.user): + repo = Repo.retrieve(name=request.params['repo']) + if ( + repo.owner != request.user and + not InnerPermission.is_accepted( + 'delete_repository', + repo.owner.object, + request.user + ) + ): return Response.redirect(self.app.convert_url('/')) try: if request.method == 'POST': if not request.user.password_check(request.post()['password']): - raise AuthorizationError('wrong password') - shutil.rmtree(self.app.project_dir / ('repos/' + repo.name + '.git')) + raise Exception('wrong password') + shutil.rmtree( + self.app.project_dir / ('repos/' + repo.name + '.git') + ) repo.delete() return Response.redirect(self.app.convert_url('/')) except Exception as err: @@ -109,13 +125,18 @@ def delete(self, request): def retrieve(self, request): template = self.view.get_template("repo/retrieve.html") - repo = Repo.retrieve(name = request.params['repo']) - query = request.query + repo = Repo.retrieve(name=request.params['repo']) current_head = request.params.get('head', 'master') entity = git.Repo( self.app.project_dir / 'repos/{}.git'.format(repo.name), ) - head = getattr(entity.heads, current_head) if hasattr(entity.heads, current_head) else None + head = getattr( + entity.heads, + current_head + ) if hasattr( + entity.heads, + current_head + ) else None commit = None tree = None readme = None @@ -138,13 +159,19 @@ def retrieve(self, request): def blob(self, request): template = self.view.get_template("repo/blob.html") - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) query = request.query branch = query.get('branch', 'master') entity = git.Repo( self.app.project_dir / 'repos/{}.git'.format(repo.name), ) - head = getattr(entity.heads, branch) if hasattr(entity.heads, branch) else None + head = getattr( + entity.heads, + branch + ) if hasattr( + entity.heads, + branch + ) else None tree = head.commit.tree or None content = None for obj in tree: @@ -162,17 +189,17 @@ def blob(self, request): def commit(self, request): template = self.view.get_template("repo/commit.html") - repo = Repo.retrieve(name = request.params['repo']) - query = request.query + repo = Repo.retrieve(name=request.params['repo']) entity = repo.entity commit = entity.commit(request.params['commit']) - diff_str = entity.git.diff(str(commit) + '~1', commit, ignore_blank_lines=True, ignore_space_at_eol=True) if len(commit.parents) > 0 else None + diff_str = entity.git.diff( + str(commit) + '~1', commit, + ignore_blank_lines=True, + ignore_space_at_eol=True + ) if len(commit.parents) > 0 else None diff = None if diff_str: diff = PatchSet(diff_str) - for patch in diff: - for hunk in patch: - print(dir(hunk)) return Response.render(template, { 'repo': repo, 'entity': entity, @@ -182,10 +209,11 @@ def commit(self, request): def log(self, request): template = self.view.get_template("repo/log.html") - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) current_head = request.params.get('head', 'master') - query = request.query - commits = repo.entity.iter_commits(current_head) if hasattr(repo.entity.heads, current_head) else None + commits = repo.entity.iter_commits( + current_head + ) if hasattr(repo.entity.heads, current_head) else None return Response.render(template, { 'repo': repo, 'entity': repo.entity, @@ -193,10 +221,11 @@ def log(self, request): 'commits': commits }) + class HookController(Controller): def handle(self, request): template = self.view.get_template('hook/list.html') - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) hooks = list() for hook in glob.glob(str(self.app.project_dir / 'repos/{}.git/hooks'.format(repo.name)) + '/*'): hooks.append(os.path.basename(hook)) @@ -204,9 +233,10 @@ def handle(self, request): 'repo': repo, 'hooks': hooks }) + def retrieve(self, request): template = self.view.get_template('hook/retrieve.html') - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) with open(self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, request.params['hook'])) as f: code = f.read() return Response.render(template, { @@ -214,37 +244,88 @@ def retrieve(self, request): 'name': request.params['hook'], 'code': code }) + def create(self, request): template = self.view.get_template('hook/create.html') - repo = Repo.retrieve(name = request.params['repo']) - if isinstance(repo.owner.object, Group) and not InnerPermission.is_accepted('update_repository', repo.owner.object, request.user): - return Response.redirect(self.app.convert_url('/' + request.params['repo'] + '/hook')) + repo = Repo.retrieve(name=request.params['repo']) + if ( + repo.owner != request.user and + not InnerPermission.is_accepted( + 'update_repository', + repo.owner, + request.user + ) + ): + return Response.redirect( + self.app.convert_url('/' + request.params['repo'] + '/hook') + ) error = '' if request.method == 'POST': form = HookCreateForm(request.post()) - with open(self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, form['name']), 'w') as f: + with open( + self.app.project_dir / 'repos/{}.git/hooks/{}'.format( + repo.name, + form['name'] + ), + 'w' + ) as f: f.write(form['code']) - return Response.redirect(self.app.convert_url('/{}/hook/{}'.format(repo.name, form['name']))) + return Response.redirect( + self.app.convert_url( + '/{}/hook/{}'.format(repo.name, form['name']) + ) + ) return Response.render(template, { 'repo': repo, 'error': error }) + def update(self, request): template = self.view.get_template('hook/update.html') - repo = Repo.retrieve(name = request.params['repo']) - if isinstance(repo.owner.object, Group) and not InnerPermission.is_accepted('update_repository', repo.owner.object, request.user): - return Response.redirect(self.app.convert_url('/' + request.params['repo'] + '/hook')) + repo = Repo.retrieve(name=request.params['repo']) + if ( + repo.owner.object != request.user and + not InnerPermission.is_accepted( + 'update_repository', + repo.owner, + request.user + ) + ): + return Response.redirect( + self.app.convert_url('/' + request.params['repo'] + '/hook') + ) error = '' if request.method == 'POST': form = HookCreateForm(request.post()) shutil.move( - self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, request.params['hook']), - self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, form['name']) + self.app.project_dir / 'repos/{}.git/hooks/{}'.format( + repo.name, + request.params['hook'] + ), + self.app.project_dir / 'repos/{}.git/hooks/{}'.format( + repo.name, + form['name'] + ) ) - with open(self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, form['name']), 'w') as f: + with open( + self.app.project_dir / 'repos/{}.git/hooks/{}'.format( + repo.name, + form['name'] + ), + 'w' + ) as f: f.write(form['code']) - return Response.redirect(self.app.convert_url('/{}/hook/{}'.format(repo.name, form['name']))) - with open(self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, request.params['hook'])) as f: + return Response.redirect( + self.app.convert_url( + '/{}/hook/{}'.format(repo.name, form['name']) + ) + ) + with open( + self.app.project_dir / 'repos/{}.git/hooks/{}'.format( + repo.name, + request.params['hook'] + ) + ) as f: code = f.read() return Response.render(template, { 'repo': repo, @@ -252,25 +333,43 @@ def update(self, request): 'code': code, 'error': error }) + def delete(self, request): template = self.view.get_template('hook/delete.html') - repo = Repo.retrieve(name = request.params['repo']) - if isinstance(repo.owner.object, Group) and not InnerPermission.is_accepted('update_repository', repo.owner.object, request.user): - return Response.redirect(self.app.convert_url('/' + request.params['repo'] + '/hook')) + repo = Repo.retrieve(name=request.params['repo']) + if ( + isinstance(repo.owner.object, Group) and + not InnerPermission.is_accepted( + 'update_repository', + repo.owner.object, + request.user + ) + ): + return Response.redirect( + self.app.convert_url('/' + request.params['repo'] + '/hook') + ) error = '' if request.method == 'POST': - os.remove(self.app.project_dir / 'repos/{}.git/hooks/{}'.format(repo.name, request.params['hook'])) - return Response.redirect(self.app.convert_url('/{}/hook'.format(repo.name))) + os.remove( + self.app.project_dir / 'repos/{}.git/hooks/{}'.format( + repo.name, + request.params['hook'] + ) + ) + return Response.redirect( + self.app.convert_url('/{}/hook'.format(repo.name)) + ) return Response.render(template, { 'repo': repo, 'name': request.params['hook'], 'error': error }) + class MergeController(Controller): def handle(self, request): template = self.view.get_template('merge/list.html') - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) merges = Merge.query.filter(Merge.repo == repo).all() return Response.render(template, { "repo": repo, @@ -279,7 +378,7 @@ def handle(self, request): def create(self, request): template = self.view.get_template('merge/create.html') - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) error = "" if request.method == "POST": try: @@ -292,7 +391,11 @@ def create(self, request): merge.repo = repo merge.user = request.user merge.create() - return Response.redirect(self.app.convert_url('/' + repo.name + '/merge/' + merge._id)) + return Response.redirect( + self.app.convert_url( + '/' + repo.name + '/merge/' + merge._id + ) + ) except Exception as err: error = str(err) return Response.render(template, { @@ -303,7 +406,7 @@ def create(self, request): def retrieve(self, request): template = self.view.get_template('merge/retrieve.html') - repo = Repo.retrieve(name = request.params['repo']) + repo = Repo.retrieve(name=request.params['repo']) merge = Merge.retrieve(request.params['merge']) if request.method == 'POST': post = request.post() @@ -319,11 +422,16 @@ def retrieve(self, request): "merge": merge }) + class ProxyController(Controller): def handle(self, request): - repo = Repo.retrieve(name = request.params['repo'][:-4]) + repo = Repo.retrieve(name=request.params['repo'][:-4]) if repo.owner.object._id != request.user._id and (isinstance(repo.owner, Group) and not repo.owner.object.is_in(request.user)): - return Response(status=401, reason='Unauthorized', text='You are not the owner of the repository.') + return Response( + status=401, + reason='Unauthorized', + text='You are not the owner of the repository.' + ) environ = dict(request.environ) environ['REQUEST_METHOD'] = request.method environ['PATH_INFO'] = self.app.revert_url(environ['PATH_INFO']) @@ -332,16 +440,20 @@ def handle(self, request): reason, headers, body - ) = gitHttpBackend.wsgi_to_git_http_backend(environ, self.app.project_dir / 'repos') + ) = gitHttpBackend.wsgi_to_git_http_backend( + environ, + self.app.project_dir / 'repos' + ) content_type = headers['Content-Type'] return Response( - body = body, - status = status, - reason = reason, - headers = headers, - content_type = content_type + body=body, + status=status, + reason=reason, + headers=headers, + content_type=content_type ) + class SettingController(Controller): def handle(self, request): if not is_admin(request.user): @@ -350,9 +462,15 @@ def handle(self, request): if request.method == "POST": try: form = SettingsForm(request.post()) - for permission_screen_name, permission_roles in form['permission'].items(): - permission = InnerPermission.retrieve(screen_name = permission_screen_name) - permission.roles = [InnerRole.retrieve(screen_name = role) for role in permission_roles] + for screen_name, permission_roles in form['permission'].items(): + permission = InnerPermission.retrieve( + screen_name=screen_name + ) + permission.roles = [ + InnerRole.retrieve(screen_name=role) + for role + in permission_roles + ] permission.update() error = "変更を保存しました" except Exception as err: @@ -363,5 +481,3 @@ def handle(self, request): "permissions": InnerPermission.list(), "error": error }) - - diff --git a/izanami/model.py b/izanami/model.py index d467909..a8554a1 100644 --- a/izanami/model.py +++ b/izanami/model.py @@ -57,6 +57,14 @@ def merge(self): self.repo.merge(self.base, self.compare) self.on("merge")() + @property + def diff(self): + entity = self.repo.entity + diff_str = entity.git.diff(self.base, self.compare, ignore_blank_lines=True, ignore_space_at_eol=True) if len(commit.parents) > 0 else None + diff = PatchSet(diff_str) + return diff + + Merge.listen("merge") InnerPermission = inner_permission(db, [ diff --git a/izanami/templates/merge/retrieve.html b/izanami/templates/merge/retrieve.html index 06cb71c..e70faf1 100644 --- a/izanami/templates/merge/retrieve.html +++ b/izanami/templates/merge/retrieve.html @@ -14,17 +14,17 @@ フック -
{{ merge.base }} ← {{ merge.compare }}