Example #1
0
    def create_branch(self, id, resource_id, branch_id=None):
        vars = get_vars(self, id, resource_id)
        repo = git.Repo(os.path.join(REPO_DIR, resource_id))
        if request.method == 'POST':
            title = request.POST.get('title')
            notes = request.POST.get('notes')
            modifications = request.POST.get('modifications')
            user = model.User.by_name(c.user)
            resource_file_name = c.resource['url'].rsplit('/', 1)[-1]
            resource_path = os.path.join(REPO_DIR, resource_id,
                                         resource_file_name)
            if branch_id is None:
                new_branch = repo.create_head(
                    '%s-%s' % (user.name, uuid.uuid4())
                )

                repo.head.reference = new_branch

                f = open(os.path.join(REPO_DIR, resource_id,
                                      resource_file_name), 'w')
                f.write(modifications.encode('utf-8'))
                f.close()

                repo.index.add([resource_path])
                repo.index.commit(notes)
                GitBranch.create(user_id=user.id, resource_id=resource_id,
                                 title=title, description=notes,
                                 branch=new_branch.name, status='pending')
                model.repo.commit()
            else:
                branch = GitBranch.get(id=branch_id)
                repo.git.checkout(branch.branch)
                f = open(os.path.join(REPO_DIR, resource_id,
                                      resource_file_name), 'w')
                f.write(modifications.encode('utf-8'))
                f.close()
                repo.index.add([resource_path])
                repo.index.commit(notes)
                branch.title = title
                branch.description = notes
                branch.save()
                model.repo.commit()
            return redirect('/dataset/%s/resource/%s/git/branches' %
                            (id, resource_id))

        if request.method == 'GET':
            resource_file_name = c.resource['url'].rsplit('/', 1)[-1]
            if branch_id is not None:
                branch = GitBranch.get(id=branch_id)
                repo.git.checkout(branch.branch)
                c.branch = branch
            else:
                repo.git.checkout('master')
            f = open(os.path.join(REPO_DIR, resource_id, resource_file_name),
                     'r')
            c.resource_content = f.read().decode('utf-8')
            f.close()
        return render('git/create_branch.html', extra_vars=vars)
Example #2
0
    def discard_branch(self, id, resource_id, branch_id):
        branch = GitBranch.get(id=branch_id)
        branch.status = 'discarded'
        branch.save()
        model.repo.commit()

        return redirect('/dataset/%s/resource/%s/git/list' %
                        (id, resource_id))
Example #3
0
    def check_branch(self, id, resource_id, branch_id):
        vars = get_vars(self, id, resource_id)
        branch = GitBranch.get(id=branch_id)
        repo = git.Repo(os.path.join(REPO_DIR, resource_id))
        repo.git.checkout(branch.branch)
        patch = repo.git.format_patch('master', '--stdout')
        c.patch_code = highlight(patch, DiffLexer(), HtmlFormatter(full=True))
        c.branch_id = branch_id

        return render('git/check_branch.html', extra_vars=vars)
Example #4
0
    def accept_branch(self, id, resource_id, branch_id):
        get_vars(self, id, resource_id)
        branch = GitBranch.get(id=branch_id)
        branch.status = 'accepted'
        branch.save()
        model.repo.commit()
        repo = git.Repo(os.path.join(REPO_DIR, resource_id))
        repo.git.checkout('master')
        repo.git.merge(branch.branch)

        resource_file_name = c.resource['url'].rsplit('/', 1)[-1]
        resource_dir = resource_id[:3]
        resource_subdir = resource_id[3:6]
        resource_dir_filename = resource_id[6:]
        shutil.copyfile(os.path.join(REPO_DIR,
                                     resource_id, resource_file_name),
                        os.path.join(STORAGE_DIR, 'resources',
                                     resource_dir, resource_subdir,
                                     resource_dir_filename))

        return redirect('/dataset/%s/resource/%s/git/list' %
                        (id, resource_id))