Skip to content

Commit

Permalink
aider: Add a new /git command to run git.
Browse files Browse the repository at this point in the history
# Aider chat conversation:

USER: Add a new /git command to run git. so you could do `/git commit -a -m "message goes here"` or any other git command. It should work like `cmd_run` but prepends "git" to the `args`. Don't offer to send the output to gpt.

ASSISTANT: To add a new `/git` command, we need to modify the `aider/commands.py` file. We will add a new method `cmd_git` that will prepend "git" to the `args` and run the command. This method will be similar to `cmd_run` but will not offer to send the output to gpt.

Here is the change:

<source>python
aider/commands.py
<<<<<<< ORIGINAL
    def cmd_run(self, args):
        "Run a shell command and optionally add the output to the chat"
        combined_output = None
        try:
            parsed_args = shlex.split(args)
            result = subprocess.run(
                parsed_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
            )
            combined_output = result.stdout
        except Exception as e:
            self.io.tool_error(f"Error running command: {e}")

        if combined_output is None:
            return

        self.io.tool_output(combined_output)

        if self.io.confirm_ask("Add the output to the chat?", default="y"):
            for line in combined_output.splitlines():
                self.io.tool_output(line, log_only=True)

            msg = prompts.run_output.format(
                command=args,
                output=combined_output,
            )
            return msg

    def cmd_exit(self, args):
        "Exit the application"
        sys.exit()
=======
    def cmd_git(self, args):
        "Run a git command"
        combined_output = None
        try:
            parsed_args = shlex.split("git " + args)
            result = subprocess.run(
                parsed_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
            )
            combined_output = result.stdout
        except Exception as e:
            self.io.tool_error(f"Error running git command: {e}")

        if combined_output is None:
            return

        self.io.tool_output(combined_output)

    def cmd_run(self, args):
        "Run a shell command and optionally add the output to the chat"
        combined_output = None
        try:
            parsed_args = shlex.split(args)
            result = subprocess.run(
                parsed_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
            )
            combined_output = result.stdout
        except Exception as e:
            self.io.tool_error(f"Error running command: {e}")

        if combined_output is None:
            return

        self.io.tool_output(combined_output)

        if self.io.confirm_ask("Add the output to the chat?", default="y"):
            for line in combined_output.splitlines():
                self.io.tool_output(line, log_only=True)

            msg = prompts.run_output.format(
                command=args,
                output=combined_output,
            )
            return msg

    def cmd_exit(self, args):
        "Exit the application"
        sys.exit()
>>>>>>> UPDATED
</source>
  • Loading branch information
paul-gauthier committed Jul 17, 2023
1 parent f891196 commit c05efad
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions aider/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ def cmd_drop(self, args):
self.coder.abs_fnames.remove(abs_fname)
self.io.tool_output(f"Removed {matched_file} from the chat")

def cmd_git(self, args):
"Run a git command"
combined_output = None
try:
parsed_args = shlex.split("git " + args)
result = subprocess.run(
parsed_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
combined_output = result.stdout
except Exception as e:
self.io.tool_error(f"Error running git command: {e}")

if combined_output is None:
return

self.io.tool_output(combined_output)

def cmd_run(self, args):
"Run a shell command and optionally add the output to the chat"
combined_output = None
Expand Down

0 comments on commit c05efad

Please sign in to comment.