Esempio n. 1
0
    def test_has_diff(self):
        linear = mpygit.Repository(Repository.objects.get(name="linear").path)
        commit = next(gitutil.walk(linear, linear.HEAD, 1))

        ENDPOINT = f"/linear/info/{commit.oid}"

        response = self.client.get(ENDPOINT)
        self.assertEqual(response.status_code, 200)
        content = response.content.decode()

        # this took far longer than i'm willing to admit
        DIFF_REGEX = (r"""<table class="highlighttable">"""
                      r"""\s*<tr>"""
                      r"""\s*<td class="linenos">"""
                      r"""[\s\S]*"""
                      r"""</td>"""
                      r"""<td class="code">"""
                      r"""\s*<div class="highlight">"""
                      r"""\s*<pre>[\s\S]*"""
                      r"""\s*<span class="gd">\-\-\- a/file</span>"""
                      r"""\s*<span class="gi">\+\+\+ b/file</span>"""
                      r"""\s*<span class="gu">@@ -1,4 \+1,4 @@</span>"""
                      r"""\s*<span class="gd">-#4</span>"""
                      r"""\s*<span class="gi">\+#5</span>"""
                      """\n multi"""
                      """\n line"""
                      """\n file"""
                      """\n"""
                      r"""</pre>"""
                      r"""\s*</div>"""
                      r"""\s*</td>"""
                      r"""\s*</tr>"""
                      r"""\s*</table>""")
        self.assertTrue(re.search(DIFF_REGEX, content))
Esempio n. 2
0
def chain(request, permission, repo_name, oid):
    """Display chain of Git repository commits.

    Args:
        permission: permission rights of accessing user.
        repo_name: name of managed repository.
        oid: offset object ID to walk from when listing commits.
    """
    if permission == permission.NO_ACCESS:
        # TODO return Http404 properly
        return HttpResponseNotFound("no matching repository")

    db_repo_obj = get_object_or_404(Repository, name=repo_name)
    repo = mpygit.Repository(db_repo_obj.path)

    context = {
        "repo_name": repo_name,
        "oid": oid,
        "can_manage": permission == Permission.CAN_MANAGE,
    }

    try:
        obj = repo[oid]
        if obj is None:
            return HttpResponse("Invalid branch or commit ID")
    except KeyError:
        pass
    else:
        context["commits"] = gitutil.walk(repo, obj.oid, 100)

    return render(request, "chain.html", context=context)
Esempio n. 3
0
    def test_displays_textual_contents(self):
        ENDPOINT = "/files/view/master/multi_line_textual_file"
        content = self._get_content(ENDPOINT)

        files = mpygit.Repository(Repository.objects.get(name="files").path)
        commit = [c for c in gitutil.walk(files, files.HEAD, 2)][-1]
        fs_blob = resolve_path(files, commit.tree, "multi_line_textual_file")

        # validate contents
        HIGHLIGHT_REGEX = (r"""<div class="blob_box blob_code">"""
                           r"""\s*<table class="highlighttable">"""
                           r"""\s*<tr>"""
                           r"""\s*<td class="linenos">"""
                           r"""\s*<div class="linenodiv">"""
                           r"""\s*<pre>"""
                           r"""\s*<span class="normal">1</span>"""
                           r"""\s*<span class="normal">2</span>"""
                           r"""\s*<span class="normal">3</span>"""
                           r"""\s*</pre>"""
                           r"""\s*</div>"""
                           r"""\s*</td>"""
                           r"""\s*<td class="code">"""
                           r"""\s*<div class="highlight">"""
                           r"""\s*<pre>"""
                           r"""\s*<span></span>multi\n"""
                           r"""line\n"""
                           r"""file\n"""
                           r"""\s*</pre>"""
                           r"""\s*</div>"""
                           r"""\s*</td>"""
                           r"""\s*</tr>"""
                           r"""\s*</table>"""
                           r"""\s*</div>""")
        self.assertTrue(re.search(HIGHLIGHT_REGEX, content))
Esempio n. 4
0
    def test_correct_committer_info(self):
        linear = mpygit.Repository(Repository.objects.get(name="linear").path)
        commit = next(gitutil.walk(linear, linear.HEAD, 1))

        ENDPOINT = f"/linear/info/{commit.oid}"

        response = self.client.get(ENDPOINT)
        self.assertEqual(response.status_code, 200)

        content = response.content.decode()

        DIFF_REGEX = (
            r"""<tr>\s*"""
            r"""<td>M</td>\s*"""
            r"""<td>\+\+1</td>\s*"""
            r"""<td>--1</td>\s*"""
            f"""<td><a href="/linear/view/{commit.oid}/file">file</a></td>"""
            r"""\s*</tr>""")
        self.assertTrue(re.search(DIFF_REGEX, content))

        COMMITTER_REGEX = (r"""<table class="commit_info">"""
                           r"""\s*<tr>"""
                           r"""\s*<td>From:</td>"""
                           r"""\s*<td>(?P<commiter_info>.*)</td>"""
                           r"""\s*</tr>"""
                           r"""\s*<tr>"""
                           r"""\s*<td>Date:</td>"""
                           r"""\s*<td>"""
                           r"""\s*<time [\s\S]*>(?P<date>.*)</time>"""
                           r"""\s*</td>"""
                           r"""\s*</tr>"""
                           r"""\s*<tr>"""
                           r"""\s*<td class="commit_msg"[\s\S]*>"""
                           r"""\s*<pre>(?P<msg>.*)</pre>"""
                           r"""\s*</td>"""
                           r"""\s*</tr>"""
                           r"""\s*</table>""")

        match = re.search(COMMITTER_REGEX, content)
        self.assertTrue(match)

        committer_info, timestamp, msg = match.groups()
        name, email = committer_info.rsplit(" ", 1)
        email = email[4:-4]

        self.assertEqual(commit.committer.name, name)
        self.assertEqual(commit.committer.email, email)
        self.assertEqual(commit.message, msg)
        fs_timestamp = dt.utcfromtimestamp(
            commit.committer.timestamp).strftime("%Y-%m-%d %H:%M")
        self.assertEqual(fs_timestamp, timestamp)
Esempio n. 5
0
    def test_displays_commits(self):
        content = self._get_chain("linear")
        repo = mpygit.Repository(Repository.objects.get(name="linear").path)
        repo_walker = gitutil.walk(repo, repo.HEAD, 5)
        for fs_ent, match in zip(repo_walker,
                                 re.finditer(self.REGEX_CHAIN_ENTS, content)):
            oid, short_oid, msg, committer, timestamp = match.groups()
            self.assertEqual(fs_ent.oid, oid)
            self.assertEqual(fs_ent.short_oid, short_oid)
            self.assertEqual(fs_ent.message, msg)
            self.assertEqual(fs_ent.committer.name, committer)

            fs_timestamp = dt.utcfromtimestamp(
                fs_ent.committer.timestamp).strftime("%Y-%m-%d")
            self.assertEqual(fs_timestamp, timestamp)
Esempio n. 6
0
    def test_inspect_tree_at_commit(self):
        linear = mpygit.Repository(Repository.objects.get(name="linear").path)
        commit = next(gitutil.walk(linear, linear.HEAD, 1))

        ENDPOINT = f"/linear/info/{commit.oid}"

        response = self.client.get(ENDPOINT)
        self.assertEqual(response.status_code, 200)
        content = response.content.decode()

        ANCHOR_REGEX = (r"""<a class="button commit_inspect" [\s\S]*"""
                        f"""href="/linear/view/{commit.oid}/">"""
                        r"""Inspect Tree"""
                        r"""\s*</a>""")
        self.assertTrue(re.search(ANCHOR_REGEX, content))
Esempio n. 7
0
def chain(request, permission, repo_name, oid):
    if permission == permission.NO_ACCESS:
        raise Http404("no matching repository")

    db_repo_obj = get_object_or_404(Repository, name=repo_name)
    # Open a repo object to the requested repo
    repo = mpygit.Repository(db_repo_obj.path)

    obj = repo[oid]
    if obj is None:
        return HttpResponse("Invalid branch or commit ID")

    context = {
        "repo_name": repo_name,
        "oid": oid,
        "commits": gitutil.walk(repo, obj.oid, 100),
        "can_manage": permission == Permission.CAN_MANAGE,
    }
    return render(request, "chain.html", context=context)
Esempio n. 8
0
    def test_displays_binary_contents(self):
        ENDPOINT = "/files/view/master/small_binary_file"
        content = self._get_content(ENDPOINT)

        files = mpygit.Repository(Repository.objects.get(name="files").path)
        commit = [c for c in gitutil.walk(files, files.HEAD, 2)][-2]
        fs_blob = resolve_path(files, commit.tree, "multi_line_textual_file")

        HEXDUMP_REGEX = (r"""<div class="blob_box">"""
                         r"""\s*<table id="hex-dump">"""
                         r"""\s*<tr>"""
                         r"""\s*<td>00000000</td>"""
                         r"""\s*<td>89 50 4e 47 0d 0a 1a 0a</td>"""
                         r"""\s*<td>00 00 00 0d 49 48 44 52</td>"""
                         r"""\s*<td>.PNG........IHDR</td>"""
                         r"""\s*</tr>"""
                         r"""\s*</table>"""
                         r"""\s*</div>""")
        self.assertTrue(re.search(HEXDUMP_REGEX, content))
Esempio n. 9
0
    def test_displays_commit_info(self):
        ENDPOINT = "/files/view/master/multi_line_textual_file"
        content = self._get_content(ENDPOINT)

        files = mpygit.Repository(Repository.objects.get(name="files").path)
        commit = [c for c in gitutil.walk(files, files.HEAD, 2)][-1]

        # validate author info
        # if the message and OIDs match then the author info will match similarly
        # otherwise the commit info page would fail (same backend logic)
        COMMIT_ANCHOR_REGEX = (
            r"""<table class="blob_box">"""
            r"""\s*<tr>"""
            r"""\s*<td>"""
            f"""{commit.message}"""
            f"""\\s*\\[<a href="/files/info/{commit.oid}">{commit.short_oid}</a>\\]"""
            r"""\s*</td>"""
            r"""\s*</tr>"""
            r"""[\s\S]*</table>""")
        self.assertTrue(re.search(COMMIT_ANCHOR_REGEX, content))