summaryrefslogtreecommitdiff
path: root/server/continuedev/plugins/context_providers/diff.py
blob: 05da3547f300f9cc80da6ff47a5857babfb0f13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import subprocess
from typing import List

from pydantic import Field

from ...core.context import ContextProvider
from ...core.main import (
    ContextItem,
    ContextItemDescription,
    ContextItemId,
    ContinueCustomException,
)


class DiffContextProvider(ContextProvider):
    """
    Type '@diff' to reference all of the changes you've made to your current branch. This is useful if you want to summarize what you've done or ask for a general review of your work before committing.
    """

    title = "diff"
    display_title = "Diff"
    description = "Output of 'git diff' in current repo"
    dynamic = True

    _DIFF_CONTEXT_ITEM_ID = "diff"

    workspace_dir: str = Field(
        None, description="The workspace directory in which to run `git diff`"
    )

    @property
    def BASE_CONTEXT_ITEM(self):
        return ContextItem(
            content="",
            description=ContextItemDescription(
                name="Diff",
                description="Reference the output of 'git diff' for the current workspace",
                id=ContextItemId(
                    provider_title=self.title, item_id=self._DIFF_CONTEXT_ITEM_ID
                ),
            ),
        )

    async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
        self.workspace_dir = workspace_dir
        return [self.BASE_CONTEXT_ITEM]

    async def get_item(self, id: ContextItemId, query: str) -> ContextItem:
        if not id.provider_title == self.title:
            raise Exception("Invalid provider title for item")

        result = subprocess.run(
            ["git", "diff"], cwd=self.workspace_dir, capture_output=True, text=True
        )
        diff = result.stdout
        error = result.stderr
        if error.strip() != "":
            if error.startswith("warning: Not a git repository"):
                raise ContinueCustomException(
                    title="Not a git repository",
                    message="The @diff context provider only works in git repositories.",
                )
            raise ContinueCustomException(
                title="Error running git diff",
                message=f"Error running git diff:\n\n{error}",
            )

        if diff.strip() == "":
            diff = "No changes"

        ctx_item = self.BASE_CONTEXT_ITEM.copy()
        ctx_item.content = diff
        return ctx_item