Esempio n. 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)