Skip to content

Commit 1ee5544

Browse files
authored
Add support for detached mode (patched-codes#119)
* add support for detached mode * lint * version bump
1 parent b34d88c commit 1ee5544

File tree

9 files changed

+264
-247
lines changed

9 files changed

+264
-247
lines changed

patchwork/app.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ def list_option_callback(ctx: click.Context, param: click.Parameter, value: str
104104
)
105105
@click.argument("patchflow", nargs=1, required=True)
106106
@click.argument("opts", nargs=-1, type=click.UNPROCESSED, required=False)
107-
@click.option("--output", type=click.Path(exists=False, resolve_path=True, writable=True), help="Path to the output file which contains the state after the patchflow finishes.")
108-
@click.option("data_format", "--format", type=click.Choice(["yaml", "json"]), default="json", help="Format of the output file.")
107+
@click.option(
108+
"--output",
109+
type=click.Path(exists=False, resolve_path=True, writable=True),
110+
help="Path to the output file which contains the state after the patchflow finishes.",
111+
)
112+
@click.option(
113+
"data_format", "--format", type=click.Choice(["yaml", "json"]), default="json", help="Format of the output file."
114+
)
109115
def cli(log: str, patchflow: str, opts: list[str], config: str | None, output: str | None, data_format: str):
110116
if "::" in patchflow:
111117
module_path, _, patchflow_name = patchflow.partition("::")

patchwork/logger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import logging
22
import os
3+
34
import click
45
from typing_extensions import Callable
56

6-
from patchwork.managed_files import LOG_FILE, HOME_FOLDER
7+
from patchwork.managed_files import HOME_FOLDER, LOG_FILE
78

89
# default noop logger
910
logger = logging.getLogger("patched")
@@ -42,7 +43,7 @@ def init_cli_logger(log_level: str) -> logging.Logger:
4243
logger.removeHandler(_noop)
4344
logger.addHandler(ClickHandler(log_level.upper()))
4445
logger.setLevel(logging.DEBUG)
45-
if not os.path.exists(HOME_FOLDER): # Check if HOME_FOLDER exists at this point
46+
if not os.path.exists(HOME_FOLDER): # Check if HOME_FOLDER exists at this point
4647
os.makedirs(HOME_FOLDER)
4748

4849
try:

patchwork/patchflows/DependencyUpgrade/DependencyUpgrade.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import copy
2-
import json
32
from pathlib import Path
43

54
import yaml

patchwork/patchflows/ResolveIssue/ResolveIssue.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import json
2-
import tempfile
31
from pathlib import Path
42

53
import yaml

patchwork/steps/CallLLM/CallLLM.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def call(self, prompts):
5858
response_dict = response.json()
5959
except Exception as e:
6060
logger.error(e)
61-
continue
61+
response_dict = {}
6262

6363
candidate = response_dict.get("candidates", [{}])[0]
6464
text_response = candidate.get("content", {}).get("parts", [{}])[0].get("text", "")
@@ -96,9 +96,13 @@ def call(self, prompts) -> list[str]:
9696
contents = []
9797
for prompt in prompts:
9898
logger.debug(f"Message sent: \n{indent(pformat(prompt), ' ')}")
99-
completion = self.client.chat.completions.create(model=self.model, messages=prompt, **self.model_args)
99+
try:
100+
completion = self.client.chat.completions.create(model=self.model, messages=prompt, **self.model_args)
101+
except Exception as e:
102+
logger.error(e)
103+
completion = None
100104

101-
if len(completion.choices) < 1:
105+
if completion is None or len(completion.choices) < 1:
102106
logger.error(f"No response choice given")
103107
content = ""
104108
elif completion.choices[0].finish_reason == "length":

patchwork/steps/CallLLM/typed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ class CallLLMInputs(TypedDict):
1515

1616
class CallLLMOutputs(TypedDict):
1717
new_code: Iterable[dict]
18-
openai_response: list[str]
18+
openai_responses: list[str]

patchwork/steps/CommitChanges/CommitChanges.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ def get_slug_from_remote_url(remote_url: str) -> str:
2424
def transitioning_branches(
2525
repo: Repo, branch_prefix: str, branch_suffix: str = "", force: bool = True
2626
) -> Generator[tuple[Head, Head], None, None]:
27-
from_branch = repo.active_branch
28-
final_func = from_branch.checkout
27+
if repo.head.is_detached:
28+
from_branch = next((branch for branch in repo.branches if branch.commit == repo.head.commit), None)
29+
else:
30+
from_branch = repo.active_branch
31+
32+
if from_branch is None:
33+
raise ValueError(
34+
"Could not determine the current branch."
35+
"Make sure repository is not in a detached HEAD state with additional commits."
36+
)
2937

3038
next_branch_name = f"{branch_prefix}{from_branch.name}{branch_suffix}"
3139
if next_branch_name in repo.heads and not force:
@@ -38,7 +46,7 @@ def transitioning_branches(
3846
to_branch.checkout()
3947
yield from_branch, to_branch
4048
finally:
41-
final_func()
49+
from_branch.checkout()
4250

4351

4452
class _EphemeralGitConfig:

poetry.lock

Lines changed: 233 additions & 232 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "patchwork-cli"
3-
version = "0.0.9"
3+
version = "0.0.10"
44
description = ""
55
authors = ["patched.codes"]
66
license = "AGPL"

0 commit comments

Comments
 (0)