Parent: [f6d8a7] (diff)

Child: [c1be33] (diff)

Download this file

git_main.py    248 lines (213 with data), 9.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#-*- python -*-
import logging
import re
import os
import sys
import shutil
from subprocess import Popen
from itertools import islice
from datetime import datetime
from urllib import quote, unquote
# Non-stdlib imports
import pkg_resources
from tg import expose, validate, redirect, response, url, flash
from tg.decorators import with_trailing_slash, without_trailing_slash
import pylons
from pylons import g, c, request
from formencode import validators
from pymongo import bson
from webob import exc
from ming.orm.ormsession import ThreadLocalORMSession
from ming.orm.base import mapper
from pymongo.bson import ObjectId
# Pyforge-specific imports
from allura.app import Application, ConfigOption, SitemapEntry, DefaultAdminController
from allura.lib import helpers as h
from allura.lib.search import search
from allura.lib.decorators import audit, react
from allura.lib.security import require, has_artifact_access, has_project_access, require_authenticated
from allura.model import Project, ProjectRole, User, ArtifactReference, Feed
from allura.controllers import BaseController
# Local imports
from forgegit import model
from forgegit import version
from .widgets import GitRevisionWidget
from .reactors import reactors
from .controllers import BranchBrowser, CommitBrowser
log = logging.getLogger(__name__)
class W(object):
revision_widget = GitRevisionWidget()
class ForgeGitApp(Application):
'''This is the Git app for PyForge'''
__version__ = version.__version__
permissions = [ 'read', 'write', 'create', 'admin', 'configure' ]
config_options = Application.config_options + [
ConfigOption('cloned_from_project_id', ObjectId, None),
ConfigOption('cloned_from_repo_id', ObjectId, None)
]
tool_label='Git'
default_mount_label='Git'
default_mount_point='git'
ordinal=2
def __init__(self, project, config):
Application.__init__(self, project, config)
self.root = RootController()
self.admin = GitAdminController(self)
@property
@h.exceptionless([], log)
def sitemap(self):
menu_id = self.config.options.mount_label.title()
with h.push_config(c, app=self):
return [
SitemapEntry(menu_id, '.')[self.sidebar_menu()] ]
def admin_menu(self):
admin_url = c.project.url()+'admin/'+self.config.options.mount_point+'/'
links = [SitemapEntry('Viewable Files', admin_url + 'extensions', className='nav_child')]
# if self.permissions and has_artifact_access('configure', app=self)():
# links.append(SitemapEntry('Permissions', admin_url + 'permissions', className='nav_child'))
return links
@h.exceptionless([], log)
def sidebar_menu(self):
links = [ SitemapEntry('Browse',c.app.url + url(quote('ref/master:/')), ui_icon='folder-collapsed'),
SitemapEntry('History', c.app.url + url(quote('ref/master:/')) + 'log', ui_icon='document-b', small=c.app.repo.count())]
if has_artifact_access('admin', app=c.app)():
links.append(SitemapEntry('Admin', c.project.url()+'admin/'+self.config.options.mount_point, ui_icon='tool-admin'))
repo = c.app.repo
if repo:
branches= [ b.name for b in repo.branches ]
tags = [ t.name for t in repo.repo_tags() ]
if branches:
links.append(SitemapEntry('Branches'))
for b in branches:
links.append(SitemapEntry(
b, url(c.app.url, dict(branch=b)),
className='nav_child',
small=c.app.repo.count(branch=b)))
if tags:
links.append(SitemapEntry('Tags'))
for b in tags:
links.append(SitemapEntry(
b, url(c.app.url, dict(branch=b)),
className='nav_child'))
return links
@property
def repo(self):
return model.GitRepository.query.get(app_config_id=self.config._id)
@property
def templates(self):
return pkg_resources.resource_filename('forgegit', 'templates')
def install(self, project):
'Set up any default permissions and roles here'
self.config.options['project_name'] = project.name
super(ForgeGitApp, self).install(project)
# Setup permissions
role_developer = ProjectRole.query.get(name='Developer')._id
role_auth = ProjectRole.query.get(name='*authenticated')._id
self.config.acl.update(
configure=c.project.acl['tool'],
read=c.project.acl['read'],
create=[role_developer],
write=[role_developer],
admin=c.project.acl['tool'])
repo = model.GitRepository(
name=self.config.options.mount_point + '.git',
tool='git',
status='initing')
ThreadLocalORMSession.flush_all()
cloned_from_project_id = self.config.options.get('cloned_from_project_id')
cloned_from_repo_id = self.config.options.get('cloned_from_repo_id')
if cloned_from_project_id is not None:
with h.push_config(c, project=Project.query.get(_id=cloned_from_project_id)):
cloned_from = model.GitRepository.query.get(_id=cloned_from_repo_id)
g.publish('audit', 'scm.git.clone',
dict(repo_name=repo.name, repo_path=repo.fs_path, cloned_from=cloned_from.full_fs_path))
else:
g.publish('audit', 'scm.git.init',
dict(repo_name=repo.name, repo_path=repo.fs_path))
def uninstall(self, project):
g.publish('audit', 'scm.git.uninstall', dict(project_id=project._id))
@audit('scm.git.uninstall')
def _uninstall(self, routing_key, data):
"Remove all the tool's artifacts and the physical repository"
repo = self.repo
if repo is not None:
shutil.rmtree(repo.full_fs_path, ignore_errors=True)
model.GitRepository.query.remove(dict(app_config_id=self.config._id))
super(ForgeGitApp, self).uninstall(project_id=data['project_id'])
class GitAdminController(DefaultAdminController):
def __init__(self, app):
self.app = app
self.repo = app.repo
@with_trailing_slash
def index(self, **kw):
redirect('permissions')
@without_trailing_slash
@expose('jinja:git/admin_extensions.html')
def extensions(self, **kw):
return dict(app=self.app,
allow_config=has_artifact_access('configure', app=self.app)(),
additional_viewable_extensions=getattr(self.repo, 'additional_viewable_extensions', ''))
@without_trailing_slash
@expose()
def set_extensions(self, **post_data):
self.repo.additional_viewable_extensions = post_data['additional_viewable_extensions']
class RootController(BaseController):
def _check_security(self):
require(has_artifact_access('read'))
def __init__(self):
self.ref = Refs()
self.ci = Commits()
@expose('jinja:git/index.html')
def index(self, offset=0, branch='master', **kw):
# Add the colon so we know where the branch part ends
redirect(url(quote('ref/%s:/' % branch)))
@with_trailing_slash
@expose('jinja:git/fork.html')
def fork(self, to_name=None):
require_authenticated()
from_repo = c.app.repo
to_project_name = 'u/' + c.user.username
ThreadLocalORMSession.flush_all()
ThreadLocalORMSession.close_all()
from_project = c.project
to_project = Project.query.get(shortname=to_project_name)
with h.push_config(c, project=to_project):
if request.method!='POST' or to_name is None:
prefix_len = len(to_project_name+'/')
in_use = [sp.shortname[prefix_len:] for sp in to_project.direct_subprojects]
in_use += [ac.options['mount_point'] for ac in to_project.app_configs]
return dict(from_repo=from_repo,
to_project_name=to_project_name,
in_use=in_use,
to_name=to_name or '')
else:
if not to_project.database_configured:
to_project.configure_project_database(is_user_project=True)
require(has_project_access('tool', to_project))
try:
to_project.install_app(
'Git', to_name,
cloned_from_project_id=from_project._id,
cloned_from_repo_id=from_repo._id)
redirect('/'+to_project_name+'/'+to_name+'/')
except exc.HTTPRedirection:
raise
except Exception, ex:
flash(str(ex), 'error')
redirect(request.referer)
class Refs(object):
@expose()
def _lookup(self, *parts):
parts = map(unquote, parts)
ref = []
while parts:
part = parts.pop(0)
ref.append(part)
if part.endswith(':'): break
ref = '/'.join(ref)[:-1]
return BranchBrowser(ref), parts
class Commits(object):
@expose()
def _lookup(self, ci, *remainder):
return CommitBrowser(ci), remainder
h.mixin_reactors(ForgeGitApp, reactors)