Commit 6af83b47 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

branches

parent 7853acd8
......@@ -16,3 +16,15 @@ class Command(BaseCommand):
for repo in project.repo_set.all():
logger.info(f' fetching {repo.forge} - {repo.namespace}')
repo.git().fetch()
logger.info(f' fetching devel & master for {project}')
git = project.git()
remote = git.remote(project.main_repo().git_remote())
if 'devel' not in git.heads:
try:
git.create_head('devel', remote.refs.devel).set_tracking_branch(remote.refs.devel).checkout()
except AttributeError:
pass
if 'master' not in git.heads:
git.create_head('master', remote.refs.master).set_tracking_branch(remote.refs.master).checkout()
logger.info(f' updating branches for {project}')
project.update_branches()
# Generated by Django 2.0.1 on 2018-01-30 14:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('rainboard', '0004_project_main_forge'),
]
operations = [
migrations.RemoveField(
model_name='branch',
name='commit',
),
migrations.RemoveField(
model_name='branch',
name='repo',
),
migrations.AddField(
model_name='branch',
name='ahead',
field=models.PositiveSmallIntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='branch',
name='behind',
field=models.PositiveSmallIntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='branch',
name='project',
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='rainboard.Project'),
preserve_default=False,
),
]
# Generated by Django 2.0.1 on 2018-01-30 14:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rainboard', '0005_branches'),
]
operations = [
migrations.RemoveField(
model_name='branch',
name='slug',
),
migrations.AlterField(
model_name='branch',
name='name',
field=models.CharField(max_length=200),
),
]
# Generated by Django 2.0.1 on 2018-01-30 14:57
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('rainboard', '0006_auto_20180130_1456'),
]
operations = [
migrations.AlterUniqueTogether(
name='branch',
unique_together={('project', 'name')},
),
]
......@@ -15,6 +15,8 @@ from .utils import SOURCES, TARGETS, slugify_with_dots
logger = logging.getLogger('rainboard.models')
MAIN_BRANCHES = ['master', 'devel']
class Article(NamedModel):
authors = models.ManyToManyField(settings.AUTH_USER_MODEL)
......@@ -165,13 +167,33 @@ class Project(Links, NamedModel, TimeStampedModel):
return git.Repo(str(path / '.git'))
def main_repo(self):
try:
return self.repo_set.get(forge=self.main_forge, namespace=self.main_namespace)
except ObjectDoesNotExist:
repo = Repo.objects.create(name=self.name, forge=self.main_forge, namespace=self.main_namespace,
project=self, default_branch='master', repo_id=0)
repo, created = Repo.objects.get_or_create(forge=self.main_forge, namespace=self.main_namespace, project=self,
defaults={'name': self.name, 'default_branch': 'master',
'repo_id': 0})
if created:
repo.api_update()
return repo
return repo
def update_branches(self, main=True):
branches = MAIN_BRANCHES if main else [b[2:] for b in self.git().git.branch('-a', '--no-color').split('\n')]
for branch in branches:
if branch in MAIN_BRANCHES:
if branch not in self.git().heads:
continue
instance, created = Branch.objects.get_or_create(name=branch, project=self)
if created:
instance.update_ab()
else:
name = '/'.join(branch.split('/')[1:])
forge, namespace = name.split('/')[:2]
repo, created = Repo.objects.get_or_create(forge__slug=forge, namespace__slug=namespace, project=self,
defaults={'name': self.name, 'default_branch': 'master',
'repo_id': 0})
if created:
repo.api_update()
instance, created = Branch.objects.get_or_create(name=name, project=self)
if created:
instance.update_ab()
class Repo(TimeStampedModel):
......@@ -267,12 +289,33 @@ class Commit(NamedModel, TimeStampedModel):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
class Branch(NamedModel, TimeStampedModel):
repo = models.ForeignKey(Repo, on_delete=models.CASCADE)
commit = models.ForeignKey(Commit, on_delete=models.CASCADE)
class Branch(TimeStampedModel):
name = models.CharField(max_length=200)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
ahead = models.PositiveSmallIntegerField(blank=True, null=True)
behind = models.PositiveSmallIntegerField(blank=True, null=True)
def __str__(self):
return f'{self.repo}/{self.name}'
return self.name
class Meta:
unique_together = ('project', 'name')
def get_ahead(self, branch='master'):
return len(self.project.git().git.rev_list(f'{self}..{branch}').split('\n'))
def get_behind(self, branch='master'):
return len(self.project.git().git.rev_list(f'{branch}..{self}').split('\n'))
def update_ab(self):
self.project.main_repo().git().fetch()
if self.name not in MAIN_BRANCHES:
forge, namespace = self.name.split('/')[:2]
Repo.objects.get(forge__slug=forge, namespace__slug=namespace, project=self.project).git().fetch()
main_branch = 'devel' if 'devel' in self.project.git().heads else 'master'
self.ahead = self.get_ahead(main_branch)
self.behind = self.get_behind(main_branch)
self.save()
class Test(TimeStampedModel):
......
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