def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER') ]: repopaths.append(os.path.join(folder, project.path)) hook_files = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files') hook_file = os.path.join(hook_files, 'pagure_hook.py') for repopath in repopaths: # Init the git repo in case pygit2.Repository(repopath) # Install the hook itself hook_path = os.path.join(repopath, 'hooks', 'post-receive.pagure') hook_file = os.path.join(hook_files, 'pagure_hook.py') if not os.path.exists(hook_path): os.symlink(hook_file, hook_path)
def delete_branch(repo, branchname, username=None): """ Delete the branch of a project. """ repo_obj = pagure.lib.get_project(SESSION, repo, user=username) if not repo_obj: flask.abort(404, 'Project not found') if not is_repo_admin(repo_obj): flask.abort(403, 'You are not allowed to delete branch for this project') if branchname == 'master': flask.abort(403, 'You are not allowed to delete the master branch') reponame = pagure.get_repo_path(repo_obj) repo_git = pygit2.Repository(reponame) if branchname not in repo_git.listall_branches(): flask.abort(404, 'Branch no found') try: branch = repo_git.lookup_branch(branchname) branch.delete() flask.flash('Branch `%s` deleted' % branchname) except pygit2.GitError, err: APP.logger.exception(err) flask.flash('Could not delete `%s`' % branchname, 'error')
def sync_pull_ref(self, name, namespace, user, requestid): """ Synchronize a pull/ reference from the content in the forked repo, allowing local checkout of the pull-request. """ session = pagure.lib.create_session() project = pagure.lib._get_project( session, namespace=namespace, name=name, user=user, case=APP.config.get('CASE_SENSITIVE', False)) with project.lock('WORKER'): request = pagure.lib.search_pull_requests( session, project_id=project.id, requestid=requestid) _log.debug( 'Update pull refs of: %s#%s', request.project.fullname, request.id) if request.remote: # Get the fork repopath = pagure.get_remote_repo_path( request.remote_git, request.branch_from) else: # Get the fork repopath = pagure.get_repo_path(request.project_from) _log.debug(' working on the repo in: %s', repopath) repo_obj = pygit2.Repository(repopath) pagure.lib.git.update_pull_ref(request, repo_obj) session.remove() gc_clean()
def delete_branch(repo, branchname, username=None): """ Delete the branch of a project. """ repo_obj = pagure.lib.get_project(SESSION, repo, user=username) if not repo_obj: flask.abort(404, 'Project not found') if not is_repo_admin(repo_obj): flask.abort( 403, 'You are not allowed to delete branch for this project') if branchname == 'master': flask.abort(403, 'You are not allowed to delete the master branch') reponame = pagure.get_repo_path(repo_obj) repo_git = pygit2.Repository(reponame) if branchname not in repo_git.listall_branches(): flask.abort(404, 'Branch no found') try: branch = repo_git.lookup_branch(branchname) branch.delete() flask.flash('Branch `%s` deleted' % branchname) except pygit2.GitError, err: APP.logger.exception(err) flask.flash('Could not delete `%s`' % branchname, 'error')
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. The list is sorted using the time of the commit associated to the tag """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = [ repo_obj[repo_obj.lookup_reference(tag).target] for tag in repo_obj.listall_references() if 'refs/tags/' in tag and repo_obj.lookup_reference(tag) ] sorted_tags = [] tags_sort = {} for tag in tags: # If the object is a tag, get his associated commit time if isinstance(tag, pygit2.Tag): tags_sort[tag.get_object().commit_time] = tag elif isinstance(tag, pygit2.Commit): tags_sort[tag.commit_time] = tag # If object is neither a tag or commit return an unsorted list else: return tags for tag in sorted(tags_sort, reverse=True): sorted_tags.append(tags_sort[tag]) return sorted_tags
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) hook_files = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'files') repo_obj = pygit2.Repository(repopath) # Configure the hook repo_obj.config.set_multivar( 'multimailhook.mailingList', '', dbobj.mail_to ) repo_obj.config.set_multivar( 'multimailhook.environment', '', 'gitolite') # Install the hook itself hook_file = os.path.join(repopath, 'hooks', 'post-receive.mail') if not os.path.exists(hook_file): os.symlink( os.path.join(hook_files, 'git_multimail.py'), hook_file )
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) if not os.path.exists(repopath): flask.abort(404, 'No git repo found') hook_files = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'files') # Make sure the hooks folder exists hookfolder = os.path.join(repopath, 'hooks') if not os.path.exists(hookfolder): os.makedirs(hookfolder) # Install the hook itself hook_file = os.path.join(repopath, 'hooks', 'post-receive.fedmsg') if not os.path.exists(hook_file): os.symlink( os.path.join(hook_files, 'fedmsg_hook.py'), hook_file )
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER')]: repopaths.append( os.path.join(folder, project.path) ) hook_files = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'files') hook_file = os.path.join(hook_files, 'pagure_hook.py') for repopath in repopaths: # Init the git repo in case pygit2.Repository(repopath) # Install the hook itself hook_path = os.path.join( repopath, 'hooks', 'post-receive.pagure') hook_file = os.path.join(hook_files, 'pagure_hook.py') if not os.path.exists(hook_path): os.symlink(hook_file, hook_path)
def delete_branch(repo, branchname, username=None): """ Delete the branch of a project. """ repo_obj = pagure.lib.get_project(SESSION, repo, user=username) if not repo_obj: flask.abort(404, "Project not found") if not is_repo_admin(repo_obj): flask.abort(403, "You are not allowed to delete branch for this project") if branchname == "master": flask.abort(403, "You are not allowed to delete the master branch") reponame = pagure.get_repo_path(repo_obj) repo_git = pygit2.Repository(reponame) if branchname not in repo_git.listall_branches(): flask.abort(404, "Branch no found") try: branch = repo_git.lookup_branch(branchname) branch.delete() flask.flash("Branch `%s` deleted" % branchname) except pygit2.GitError as err: APP.logger.exception(err) flask.flash("Could not delete `%s`" % branchname, "error") return flask.redirect(flask.url_for("view_repo", repo=repo, username=username))
def set_up(cls, project): ''' Install the generic post-receive hook that allow us to call multiple post-receive hooks as set per plugin. ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER')]: repopaths.append( os.path.join(folder, project.path) ) hook_files = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'files') for repopath in repopaths: # Make sure the hooks folder exists hookfolder = os.path.join(repopath, 'hooks') if not os.path.exists(hookfolder): os.makedirs(hookfolder) # Install the main post-receive file postreceive = os.path.join(hookfolder, cls.hook_type) if not os.path.exists(postreceive): shutil.copyfile( os.path.join(hook_files, cls.hook_type), postreceive) os.chmod(postreceive, 0755)
def change_ref_head(repo, username=None): """ Change HEAD reference """ if admin_session_timedout(): flask.flash('Action canceled, try it again', 'error') url = flask.url_for('view_settings', username=username, repo=repo) return flask.redirect(flask.url_for('auth_login', next=url)) repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to change the settings for this project') repopath = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(repopath) branches = repo_obj.listall_branches() form = pagure.forms.DefaultBranchForm(branches=branches) if form.validate_on_submit(): branchname = form.branches.data try: reference = repo_obj.lookup_reference('refs/heads/%s' % branchname).resolve() repo_obj.set_head(reference.name) flask.flash('Default branch updated to %s' % branchname) except Exception as err: # pragma: no cover APP.logger.exception(err) return flask.redirect( flask.url_for('view_settings', username=username, repo=repo.name))
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. The list is sorted using the time of the commit associated to the tag """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = {} for tag in repo_obj.listall_references(): if 'refs/tags/' in tag and repo_obj.lookup_reference(tag): commit_time = "" theobject = repo_obj[repo_obj.lookup_reference(tag).target] objecttype = "" if isinstance(theobject, pygit2.Tag): commit_time = theobject.get_object().commit_time objecttype = "tag" elif isinstance(theobject, pygit2.Commit): commit_time = theobject.commit_time objecttype = "commit" tags[commit_time] = { "object": repo_obj[repo_obj.lookup_reference(tag).target], "tagname": tag.replace("refs/tags/", ""), "date": commit_time, "objecttype": objecttype } sorted_tags = [] for tag in sorted(tags, reverse=True): sorted_tags.append(tags[tag]) return sorted_tags
def set_up(cls, project): ''' Install the generic post-receive hook that allow us to call multiple post-receive hooks as set per plugin. ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER') ]: repopaths.append(os.path.join(folder, project.path)) hook_files = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files') for repopath in repopaths: # Make sure the hooks folder exists hookfolder = os.path.join(repopath, 'hooks') if not os.path.exists(hookfolder): os.makedirs(hookfolder) # Install the main post-receive file postreceive = os.path.join(hookfolder, cls.hook_type) if not os.path.exists(postreceive): os.symlink(os.path.join(hook_files, cls.hook_type), postreceive)
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. The list is sorted using the time of the commit associated to the tag """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = {} for tag in repo_obj.listall_references(): if 'refs/tags/' in tag and repo_obj.lookup_reference(tag): commit_time = "" theobject = repo_obj[repo_obj.lookup_reference(tag).target] objecttype = "" if isinstance(theobject, pygit2.Tag): commit_time = theobject.get_object().commit_time objecttype = "tag" elif isinstance(theobject, pygit2.Commit): commit_time = theobject.commit_time objecttype = "commit" tags[commit_time] = { "object":repo_obj[repo_obj.lookup_reference(tag).target], "tagname":tag.replace("refs/tags/",""), "date":commit_time, "objecttype": objecttype } sorted_tags = [] for tag in sorted(tags, reverse=True): sorted_tags.append(tags[tag]) return sorted_tags
def view_commit(repo, commitid, username=None): """ Render a commit in a repo """ repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, "Project not found") reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) try: commit = repo_obj.get(commitid) except ValueError: flask.abort(404, "Commit not found") if commit is None: flask.abort(404, "Commit not found") if commit.parents: diff = commit.tree.diff_to_tree() parent = repo_obj.revparse_single("%s^" % commitid) diff = repo_obj.diff(parent, commit) else: # First commit in the repo diff = commit.tree.diff_to_tree(swap=True) return flask.render_template( "commit.html", select="logs", repo=repo, username=username, commitid=commitid, commit=commit, diff=diff )
def change_ref_head(repo, username=None): """ Change HEAD reference """ if admin_session_timedout(): flask.flash('Action canceled, try it again', 'error') url = flask.url_for( 'view_settings', username=username, repo=repo) return flask.redirect( flask.url_for('auth_login', next=url)) repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') if not is_repo_admin(repo): flask.abort( 403, 'You are not allowed to change the settings for this project') repopath = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(repopath) branches = repo_obj.listall_branches() form = pagure.forms.DefaultBranchForm(branches=branches) if form.validate_on_submit(): branchname = form.branches.data try: reference = repo_obj.lookup_reference('refs/heads/%s'%branchname).resolve() repo_obj.set_head(reference.name) flask.flash('Default branch updated to %s'%branchname) except Exception as err: # pragma: no cover APP.logger.exception(err) return flask.redirect(flask.url_for( 'view_settings', username=username, repo=repo.name))
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project)
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)]
def request_pull_patch(repo, requestid, username=None): """ Returns the commits from the specified pull-request as patches. """ repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') if not repo.settings.get('pull_requests', True): flask.abort(404, 'No pull-requests found for this project') request = pagure.lib.search_pull_requests( SESSION, project_id=repo.id, requestid=requestid) if not request: flask.abort(404, 'Pull-request not found') repo_from = request.project_from repopath = pagure.get_repo_path(repo_from) repo_obj = pygit2.Repository(repopath) parentpath = _get_parent_repo_path(repo_from) orig_repo = pygit2.Repository(parentpath) branch = repo_obj.lookup_branch(request.branch_from) commitid = None if branch: commitid = branch.get_object().hex diff_commits = [] if request.status != 'Open': commitid = request.commit_stop for commit in repo_obj.walk(commitid, pygit2.GIT_SORT_TIME): diff_commits.append(commit) if commit.oid.hex == request.commit_start: break else: try: diff_commits, diff = pagure.lib.git.diff_pull_request( SESSION, request, repo_obj, orig_repo, requestfolder=APP.config['REQUESTS_FOLDER'], with_diff=False) except pagure.exceptions.PagureException as err: flask.flash(err.message, 'error') return flask.redirect(flask.url_for( 'view_repo', username=username, repo=repo.name)) except SQLAlchemyError as err: # pragma: no cover SESSION.rollback() APP.logger.exception(err) flask.flash( 'Could not update this pull-request in the database', 'error') diff_commits.reverse() patch = pagure.lib.git.commit_to_patch(repo_obj, diff_commits) return flask.Response(patch, content_type="text/plain;charset=UTF-8")
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] cls.base_install(repopaths, dbobj, 'fedmsg', 'fedmsg_hook.py')
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] cls.base_remove(repopaths, 'pagureforcecommit')
def view_settings(repo, username=None): """ Presents the settings of the project. """ if admin_session_timedout(): if flask.request.method == 'POST': flask.flash('Action canceled, try it again', 'error') return flask.redirect( flask.url_for('auth_login', next=flask.request.url)) repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') repo_admin = is_repo_admin(repo) if not repo_admin: flask.abort( 403, 'You are not allowed to change the settings for this project') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) plugins = pagure.ui.plugins.get_plugin_names( APP.config.get('DISABLED_PLUGINS')) tags = pagure.lib.get_tags_of_project(SESSION, repo) form = pagure.forms.ConfirmationForm() tag_form = pagure.forms.AddIssueTagForm() branches = repo_obj.listall_branches() branches_form = pagure.forms.DefaultBranchForm(branches=branches) if form.validate_on_submit(): settings = {} for key in flask.request.form: if key == 'csrf_token': continue settings[key] = flask.request.form[key] try: message = pagure.lib.update_project_settings( SESSION, repo=repo, settings=settings, user=flask.g.fas_user.username, ) SESSION.commit() flask.flash(message) return flask.redirect(flask.url_for( 'view_repo', username=username, repo=repo.name)) except pagure.exceptions.PagureException as msg: SESSION.rollback() flask.flash(msg, 'error') except SQLAlchemyError, err: # pragma: no cover SESSION.rollback() flask.flash(str(err), 'error')
def _commit_exists(user, namespace, repo, githash): """ Utility method checking if a given commit exists. """ repo_obj = pagure.get_authorized_project( pagure.SESSION, project_name=repo, user=user, namespace=namespace) if not repo_obj: return False reponame = pagure.get_repo_path(repo_obj) git_repo = pygit2.Repository(reponame) return githash in git_repo
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] repo_obj = pygit2.Repository(repopaths[0])
def get_git_tags(project): """ Returns the list of tags created in the git repositorie of the specified project. """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = [tag.split("refs/tags/")[1] for tag in repo_obj.listall_references() if "refs/tags/" in tag] return tags
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) hook_path = os.path.join(repopath, 'hooks', 'post-receive.rtd') if os.path.exists(hook_path): os.unlink(hook_path)
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] cls.base_install(repopaths, dbobj, 'pagureunsignedcommit', 'pagure_block_unsigned.py')
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = [ repo_obj.lookup_reference(tag) for tag in repo_obj.listall_references() if 'refs/tags/' in tag ] return tags
def get_git_tags(project): """ Returns the list of tags created in the git repositorie of the specified project. """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = [ tag.split('refs/tags/')[1] for tag in repo_obj.listall_references() if 'refs/tags/' in tag ] return tags
def request_pulls(repo, username=None): """ Request pulling the changes from the fork into the project. """ status = flask.request.args.get("status", "Open") assignee = flask.request.args.get("assignee", None) author = flask.request.args.get("author", None) repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, "Project not found") if not repo.settings.get("pull_requests", True): flask.abort(404, "No pull-requests found for this project") if str(status).lower() in ["false", "0"]: status = False if str(status).lower() in ["true", "1", "open"]: requests = pagure.lib.search_pull_requests( SESSION, project_id=repo.id, status=True, assignee=assignee, author=author ) oth_requests = pagure.lib.search_pull_requests( SESSION, project_id=repo.id, status=False, assignee=assignee, author=author, count=True ) else: requests = pagure.lib.search_pull_requests( SESSION, project_id=repo.id, assignee=assignee, author=author, status=status ) oth_requests = pagure.lib.search_pull_requests( SESSION, project_id=repo.id, status=True, assignee=assignee, author=author, count=True ) reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) if not repo_obj.is_empty and not repo_obj.head_is_unborn: head = repo_obj.head.shorthand else: head = "master" return flask.render_template( "requests.html", select="requests", repo=repo, username=username, requests=requests, oth_requests=oth_requests, status=status, assignee=assignee, author=author, repo_admin=is_repo_admin(repo), form=pagure.forms.ConfirmationForm(), head=head, )
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. """ repopath = pagure.get_repo_path(project) repo_obj = pygit2.Repository(repopath) tags = [ repo_obj.lookup_reference(tag) for tag in repo_obj.listall_references() if 'refs/tags/' in tag ] return tags
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) hook_files = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files') repo_obj = pygit2.Repository(repopath)
def view_tree(repo, identifier=None, username=None): """ Render the tree of the repo """ repo = pagure.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) branchname = None content = None output_type = None commit = None if not repo_obj.is_empty: if identifier in repo_obj.listall_branches(): branchname = identifier branch = repo_obj.lookup_branch(identifier) commit = branch.get_object() else: try: commit = repo_obj.get(identifier) branchname = identifier except (ValueError, TypeError): # If it's not a commit id then it's part of the filename if 'master' in repo_obj.listall_branches(): commit = repo_obj[repo_obj.head.target] branchname = 'master' # If we're arriving here from the release page, we may have a Tag # where we expected a commit, in this case, get the actual commit if isinstance(commit, pygit2.Tag): commit = commit.get_object() if commit: content = sorted(commit.tree, key=lambda x: x.filemode) output_type = 'tree' return flask.render_template( 'file.html', select='tree', repo_obj=repo_obj, origin='view_tree', repo=repo, username=username, branchname=branchname, branches=sorted(repo_obj.listall_branches()), filename='', content=content, output_type=output_type, repo_admin=is_repo_admin(repo), )
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) hook_files = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'files') repo_obj = pygit2.Repository(repopath)
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) hook_path = os.path.join(repopath, 'hooks', 'post-receive.fedmsg') if os.path.exists(hook_path): os.unlink(hook_path)
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = [ repo_obj[repo_obj.lookup_reference(tag).target] for tag in repo_obj.listall_references() if "refs/tags/" in tag and repo_obj.lookup_reference(tag) ] return tags
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' # Init the git repo in case repopaths = [get_repo_path(project)] pygit2.Repository(repopaths[0]) cls.base_install(repopaths, dbobj, 'pagureforcecommit', 'pagure_force_commit_hook.py')
def view_tree(repo, identifier=None, username=None): """ Render the tree of the repo """ repo = pagure.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) branchname = None content = None output_type = None commit = None if not repo_obj.is_empty: if identifier in repo_obj.listall_branches(): branchname = identifier branch = repo_obj.lookup_branch(identifier) commit = branch.get_object() else: try: commit = repo_obj.get(identifier) branchname = identifier except (ValueError, TypeError): # If it's not a commit id then it's part of the filename if 'master' in repo_obj.listall_branches(): commit = repo_obj[repo_obj.head.target] branchname = 'master' # If we're arriving here from the release page, we may have a Tag # where we expected a commit, in this case, get the actual commit if isinstance(commit, pygit2.Tag): commit = commit.get_object() if commit: content = sorted(commit.tree, key=lambda x: x.filemode) output_type = 'tree' return flask.render_template( 'file.html', select='tree', repo_obj=repo_obj, repo=repo, username=username, branchname=branchname, filename='', content=content, output_type=output_type, repo_admin=is_repo_admin(repo), )
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER') ]: repopaths.append(os.path.join(folder, project.path)) cls.base_install(repopaths, dbobj, 'pagure', 'pagure_hook.py')
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER') ]: repopaths.append(os.path.join(folder, project.path)) cls.base_remove(repopaths, 'pagure')
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER')]: repopaths.append( os.path.join(folder, project.path) ) cls.base_remove(repopaths, 'pagure')
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER')]: repopaths.append( os.path.join(folder, project.path) ) cls.base_install(repopaths, dbobj, 'pagure', 'pagure_hook.py')
def view_tree(repo, identifier=None, username=None): """ Render the tree of the repo """ repo = pagure.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) branchname = None content = None output_type = None commit = None if not repo_obj.is_empty: if identifier in repo_obj.listall_branches(): branchname = identifier branch = repo_obj.lookup_branch(identifier) commit = branch.get_object() else: try: commit = repo_obj.get(identifier) branchname = identifier except (ValueError, TypeError): # If it's not a commit id then it's part of the filename if 'master' in repo_obj.listall_branches(): commit = repo_obj[repo_obj.head.target] branchname = 'master' if commit: content = sorted(commit.tree, key=lambda x: x.filemode) output_type = 'tree' return flask.render_template( 'file.html', select='tree', repo_obj=repo_obj, repo=repo, username=username, branchname=branchname, filename='', content=content, output_type=output_type, )
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] repo_obj = pygit2.Repository(repopaths[0]) # Configure the hook repo_obj.config.set_multivar('multimailhook.mailingList', '', dbobj.mail_to) repo_obj.config.set_multivar('multimailhook.environment', '', 'gitolite') # Install the hook itself cls.base_install(repopaths, dbobj, 'mail', 'git_multimail.py')
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER') ]: repopaths.append(os.path.join(folder, project.path)) for repopath in repopaths: hook_path = os.path.join(repopath, 'hooks', 'post-receive.pagure') if os.path.exists(hook_path): os.unlink(hook_path)
def remove(cls, project): ''' Method called to remove the hook of a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopaths = [get_repo_path(project)] for folder in [ APP.config.get('DOCS_FOLDER'), APP.config.get('REQUESTS_FOLDER')]: repopaths.append( os.path.join(folder, project.path) ) for repopath in repopaths: hook_path = os.path.join( repopath, 'hooks', 'post-receive.pagure') if os.path.exists(hook_path): os.unlink(hook_path)
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. The list is sorted using the time of the commit associated to the tag """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = {} for tag in repo_obj.listall_references(): if 'refs/tags/' in tag and repo_obj.lookup_reference(tag): commit_time = "" theobject = repo_obj[repo_obj.lookup_reference(tag).target] objecttype = "" if isinstance(theobject, pygit2.Tag): commit_time = theobject.get_object().commit_time objecttype = "tag" elif isinstance(theobject, pygit2.Commit): commit_time = theobject.commit_time objecttype = "commit" tags[commit_time] = { "object": repo_obj[repo_obj.lookup_reference(tag).target], "tagname": tag.replace("refs/tags/",""), "date": commit_time, "objecttype": objecttype, "head_msg": None, "body_msg": None, } if objecttype == 'tag': head_msg, _, body_msg = tags[commit_time][ "object"].message.partition('\n') if body_msg.strip().endswith('\n-----END PGP SIGNATURE-----'): body_msg = body_msg.rsplit( '-----BEGIN PGP SIGNATURE-----', 1)[0].strip() tags[commit_time]["head_msg"] = head_msg tags[commit_time]["body_msg"] = body_msg sorted_tags = [] for tag in sorted(tags, reverse=True): sorted_tags.append(tags[tag]) return sorted_tags
def get_git_tags_objects(project): """ Returns the list of references of the tags created in the git repositorie the specified project. The list is sorted using the time of the commit associated to the tag """ repopath = pagure.get_repo_path(project) repo_obj = PagureRepo(repopath) tags = {} for tag in repo_obj.listall_references(): if 'refs/tags/' in tag and repo_obj.lookup_reference(tag): commit_time = "" theobject = repo_obj[repo_obj.lookup_reference(tag).target] objecttype = "" if isinstance(theobject, pygit2.Tag): commit_time = theobject.get_object().commit_time objecttype = "tag" elif isinstance(theobject, pygit2.Commit): commit_time = theobject.commit_time objecttype = "commit" tags[commit_time] = { "object": repo_obj[repo_obj.lookup_reference(tag).target], "tagname": tag.replace("refs/tags/", ""), "date": commit_time, "objecttype": objecttype, "head_msg": None, "body_msg": None, } if objecttype == 'tag': head_msg, _, body_msg = tags[commit_time][ "object"].message.partition('\n') if body_msg.strip().endswith('\n-----END PGP SIGNATURE-----'): body_msg = body_msg.rsplit('-----BEGIN PGP SIGNATURE-----', 1)[0].strip() tags[commit_time]["head_msg"] = head_msg tags[commit_time]["body_msg"] = body_msg sorted_tags = [] for tag in sorted(tags, reverse=True): sorted_tags.append(tags[tag]) return sorted_tags
def install(cls, project, dbobj): ''' Method called to install the hook for a project. :arg project: a ``pagure.model.Project`` object to which the hook should be installed ''' repopath = get_repo_path(project) hook_files = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'files') hook_file = os.path.join(hook_files, 'pagure_block_unsigned.py') # Init the git repo in case pygit2.Repository(repopath) # Install the hook itself hook_path = os.path.join(repopath, 'hooks', 'pre-receive.pagureunsignedcommit') if not os.path.exists(hook_path): os.symlink(hook_file, hook_path)
def delete_branch(self, name, namespace, user, branchname): """ Delete a branch from a git repo. """ session = pagure.lib.create_session() project = pagure.lib._get_project( session, namespace=namespace, name=name, user=user, case=APP.config.get('CASE_SENSITIVE', False)) with project.lock('WORKER'): repo_obj = pygit2.Repository(pagure.get_repo_path(project)) try: branch = repo_obj.lookup_branch(branchname) branch.delete() except pygit2.GitError as err: _log.exception(err) session.remove() return ret('view_repo', repo=name, namespace=namespace, username=user)
def view_commit(repo, commitid, username=None): """ Render a commit in a repo """ repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) try: commit = repo_obj.get(commitid) except ValueError: flask.abort(404, 'Commit not found') if commit is None: flask.abort(404, 'Commit not found') if commit.parents: diff = commit.tree.diff_to_tree() parent = repo_obj.revparse_single('%s^' % commitid) diff = repo_obj.diff(parent, commit) else: # First commit in the repo diff = commit.tree.diff_to_tree(swap=True) return flask.render_template( 'commit.html', select='logs', repo=repo, username=username, commitid=commitid, commit=commit, diff=diff, )
def view_commit_patch(repo, commitid, username=None): """ Render a commit in a repo as patch """ repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, 'Project not found') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) try: commit = repo_obj.get(commitid) except ValueError: flask.abort(404, 'Commit not found') if commit is None: flask.abort(404, 'Commit not found') patch = pagure.lib.git.commit_to_patch(repo_obj, commit) return flask.Response(patch, content_type="text/plain;charset=UTF-8")
def view_repo(repo, username=None): """ Front page of a specific repo. """ repo = pagure.lib.get_project(SESSION, repo, user=username) if repo is None: flask.abort(404, 'Project not found') reponame = pagure.get_repo_path(repo) repo_obj = pygit2.Repository(reponame) cnt = 0 last_commits = [] tree = [] if not repo_obj.is_empty: try: for commit in repo_obj.walk(repo_obj.head.target, pygit2.GIT_SORT_TIME): last_commits.append(commit) cnt += 1 if cnt == 3: break tree = sorted(last_commits[0].tree, key=lambda x: x.filemode) except pygit2.GitError: pass readme = None safe = False for i in tree: name, ext = os.path.splitext(i.name) if name == 'README': content = repo_obj[i.oid].data readme, safe = pagure.doc_utils.convert_readme( content, ext, view_file_url=flask.url_for('view_raw_file', username=username, repo=repo.name, identifier='master', filename='')) diff_commits = [] if repo.is_fork: parentname = os.path.join(APP.config['GIT_FOLDER'], repo.parent.path) if repo.parent.is_fork: parentname = os.path.join(APP.config['FORK_FOLDER'], repo.parent.path) else: parentname = os.path.join(APP.config['GIT_FOLDER'], repo.path) orig_repo = pygit2.Repository(parentname) if not repo_obj.is_empty and not orig_repo.is_empty: orig_branch = orig_repo.lookup_branch('master') branch = repo_obj.lookup_branch('master') if orig_branch and branch: master_commits = [ commit.oid.hex for commit in orig_repo.walk( orig_branch.get_object().hex, pygit2.GIT_SORT_TIME) ] repo_commit = repo_obj[branch.get_object().hex] for commit in repo_obj.walk(repo_commit.oid.hex, pygit2.GIT_SORT_TIME): if commit.oid.hex in master_commits: break diff_commits.append(commit.oid.hex) return flask.render_template( 'repo_info.html', select='overview', repo=repo, repo_obj=repo_obj, username=username, readme=readme, safe=safe, branches=sorted(repo_obj.listall_branches()), branchname='master', last_commits=last_commits, tree=tree, diff_commits=diff_commits, repo_admin=is_repo_admin(repo), form=pagure.forms.ConfirmationForm(), )