Commit 0b60d576 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

branch.repo

parent 33c15933
# Generated by Django 2.0.1 on 2018-02-05 13:52
from django.db import migrations, models
import django.db.models.deletion
def populate_branch_repo(apps, schema_editor):
Branch = apps.get_model('rainboard', 'Branch')
Repo = apps.get_model('rainboard', 'Repo')
for branch in Branch.objects.all():
if branch.name in ['master', 'devel']:
forge = branch.project.main_forge.slug
namespace = branch.project.main_namespace.slug
else:
forge, namespace = branch.name.split('/')[:2]
branch.repo = Repo.objects.get(forge__slug=forge, namespace__slug=namespace, project=branch.project)
branch.save()
class Migration(migrations.Migration):
dependencies = [
('rainboard', '0012_branch_updated'),
]
operations = [
migrations.AddField(
model_name='branch',
name='repo',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='rainboard.Repo'),
),
migrations.RunPython(populate_branch_repo),
migrations.AlterField(
model_name='branch',
name='repo',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rainboard.Repo'),
),
]
......@@ -344,6 +344,7 @@ class Branch(TimeStampedModel):
ahead = models.PositiveSmallIntegerField(blank=True, null=True)
behind = models.PositiveSmallIntegerField(blank=True, null=True)
updated = models.DateTimeField(blank=True, null=True)
repo = models.ForeignKey(Repo, on_delete=models.CASCADE)
def __str__(self):
return self.name
......@@ -359,15 +360,10 @@ class Branch(TimeStampedModel):
commits = self.project.git().git.rev_list(f'{self}..{branch}')
return len(commits.split('\n')) if commits else 0
def remote(self):
# TODO: ForeignKey to Repo
forge, namespace, branch = self.name.split('/', maxsplit=2)
return Repo.objects.get(forge__slug=forge, namespace__slug=namespace, project=self.project).git()
def git(self):
git_repo = self.project.git()
if self.name not in git_repo.branches:
remote = self.remote()
remote = self.repo.git()
_, _, branch = self.name.split('/', maxsplit=2)
git_repo.create_head(self.name, remote.refs[branch]).set_tracking_branch(remote.refs[branch])
return git_repo.branches[self.name]
......@@ -376,7 +372,7 @@ class Branch(TimeStampedModel):
if pull:
self.project.main_repo().git().fetch()
if self.name not in MAIN_BRANCHES:
self.remote().fetch()
self.repo.git().fetch()
main_branch = self.project.main_branch()
self.ahead = self.get_ahead(main_branch)
self.behind = self.get_behind(main_branch)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment