<commit_before>from django import newforms as forms
from django.conf import settings
from django.core import validators
from django.core.validators import ValidationError
from reviewboard.diffviewer.models import DiffSet, FileDiff
from reviewboard.scmtools.models import Repository
import reviewboard.diffviewer.parser as diffparser
import reviewboard.scmtools as scmtools
from scmtools import PRE_CREATION

class EmptyDiffError(ValueError):
    pass


class UploadDiffForm(forms.Form):
    repositoryid = forms.IntegerField(required=True, widget=forms.HiddenInput)
    # XXX: it'd be really nice to have "required" dependent on scmtool
    basedir = forms.CharField(required=False)
    path = forms.CharField(widget=forms.FileInput())

    def create(self, file, diffset_history=None):
        # XXX Compatibility with Django 0.96 and 1.0.
        formdata = getattr(self, "cleaned_data",
                           getattr(self, "clean_data", None))

        # Parse the diff
        repository = Repository.objects.get(pk=formdata['repositoryid'])

        files = diffparser.parse(file["content"])

        if len(files) == 0:
            raise EmptyDiffError

        # Check that we can actually get all these files.
        tool = repository.get_scmtool()

        if tool.get_diffs_use_absolute_paths():
            basedir = ''
        else:
            basedir = str(formdata['basedir']) + '/'

        for f in files:
            f2, revision = tool.parse_diff_revision(f.origFile, f.origInfo)
            filename = basedir + f2

            if revision != PRE_CREATION and \
               not tool.file_exists(filename, revision):
                raise scmtools.FileNotFoundException(filename, revision)

            f.origFile = filename
            f.origInfo = revision

        diffset = DiffSet(name=file["filename"], revision=0,
                          history=diffset_history)
        diffset.repository = repository
        diffset.save()

        for f in files:
            filediff = FileDiff(diffset=diffset,
                                source_file=f.origFile,
                                dest_file=basedir + f.newFile,
                                source_revision=str(f.origInfo),
                                dest_detail=f.newInfo,
                                diff=f.data,
                                binary=f.binary)
            filediff.save()

        return diffset<commit_msg>Use os.path.join instead of + to join the base directory to the filename. This prevents double-separators.  Bug 79.<commit_after>import os

from django import newforms as forms
from django.conf import settings
from django.core import validators
from django.core.validators import ValidationError

from reviewboard.diffviewer.models import DiffSet, FileDiff
from reviewboard.scmtools.models import Repository
import reviewboard.diffviewer.parser as diffparser
import reviewboard.scmtools as scmtools
from scmtools import PRE_CREATION

class EmptyDiffError(ValueError):
    pass


class UploadDiffForm(forms.Form):
    repositoryid = forms.IntegerField(required=True, widget=forms.HiddenInput)
    # XXX: it'd be really nice to have "required" dependent on scmtool
    basedir = forms.CharField(required=False)
    path = forms.CharField(widget=forms.FileInput())

    def create(self, file, diffset_history=None):
        # XXX Compatibility with Django 0.96 and 1.0.
        formdata = getattr(self, "cleaned_data",
                           getattr(self, "clean_data", None))

        # Parse the diff
        repository = Repository.objects.get(pk=formdata['repositoryid'])

        files = diffparser.parse(file["content"])

        if len(files) == 0:
            raise EmptyDiffError

        # Check that we can actually get all these files.
        tool = repository.get_scmtool()

        if tool.get_diffs_use_absolute_paths():
            basedir = ''
        else:
            basedir = str(formdata['basedir'])

        for f in files:
            f2, revision = tool.parse_diff_revision(f.origFile, f.origInfo)
            filename = os.path.join(basedir, f2)

            if revision != PRE_CREATION and \
               not tool.file_exists(filename, revision):
                raise scmtools.FileNotFoundException(filename, revision)

            f.origFile = filename
            f.origInfo = revision

        diffset = DiffSet(name=file["filename"], revision=0,
                          history=diffset_history)
        diffset.repository = repository
        diffset.save()

        for f in files:
            filediff = FileDiff(diffset=diffset,
                                source_file=f.origFile,
                                dest_file=os.path.join(basedir, f.newFile),
                                source_revision=str(f.origInfo),
                                dest_detail=f.newInfo,
                                diff=f.data,
                                binary=f.binary)
            filediff.save()

        return diffset
{'fraction_non_alphanumeric': 0.0794, 'fraction_numerical': 0.004, 'mean_word_length': 2.110074626865672, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '724186', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1304, 'n_words': 329}