Skip to content

Commit 3630692

Browse files
committed
Undo command #287
1 parent f3ba4fe commit 3630692

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

agentstack/cli/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from .cli import configure_default_model, welcome_message, get_validated_input, parse_insertion_point
1+
from .cli import (
2+
configure_default_model,
3+
welcome_message,
4+
get_validated_input,
5+
parse_insertion_point,
6+
undo,
7+
)
28
from .init import init_project
39
from .wizard import run_wizard
410
from .run import run_project

agentstack/cli/cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from agentstack.exceptions import ValidationError
88
from agentstack.utils import validator_not_empty, is_snake_case
99
from agentstack.generation import InsertionPoint
10+
from agentstack import repo
1011

1112

1213
PREFERRED_MODELS = [
@@ -34,6 +35,25 @@ def welcome_message():
3435
log.info(border)
3536

3637

38+
def undo() -> None:
39+
"""Undo the last committed changes."""
40+
conf.assert_project()
41+
42+
changed_files = repo.get_uncommitted_files()
43+
if changed_files:
44+
log.warning("There are uncommitted changes that may be overwritten.")
45+
for changed in changed_files:
46+
log.info(f" - {changed}")
47+
should_continue = inquirer.confirm(
48+
message="Do you want to continue?",
49+
default=False,
50+
)
51+
if not should_continue:
52+
return
53+
54+
repo.revert_last_commit(hard=True)
55+
56+
3757
def configure_default_model():
3858
"""Set the default model"""
3959
agentstack_config = ConfigFile()

agentstack/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
add_task,
1414
run_project,
1515
export_template,
16+
undo,
1617
)
1718
from agentstack.telemetry import track_cli_command, update_telemetry
1819
from agentstack.utils import get_version, term_color
@@ -154,7 +155,8 @@ def _main():
154155
)
155156
export_parser.add_argument('filename', help='The name of the file to export to')
156157

157-
update = subparsers.add_parser('update', aliases=['u'], help='Check for updates', parents=[global_parser])
158+
undo_parser = subparsers.add_parser('undo', help='Undo the last change to your project', parents=[global_parser])
159+
update_parser = subparsers.add_parser('update', aliases=['u'], help='Check for updates', parents=[global_parser])
158160

159161
# Parse known args and store unknown args in extras; some commands use them later on
160162
args, extra_args = parser.parse_known_args()
@@ -228,6 +230,8 @@ def _main():
228230
generate_parser.print_help()
229231
elif args.command in ['export', 'e']:
230232
export_template(args.filename)
233+
elif args.command in ['undo']:
234+
undo()
231235
else:
232236
parser.print_help()
233237

agentstack/repo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,24 @@ def get_uncommitted_files() -> list[str]:
190190
untracked = repo.untracked_files
191191
modified = [item.a_path for item in repo.index.diff(None) if item.a_path]
192192
return untracked + modified
193+
194+
195+
def revert_last_commit(hard: bool = False) -> None:
196+
"""
197+
Revert the last commit in the current project.
198+
"""
199+
try:
200+
repo = _get_repo()
201+
except EnvironmentError as e:
202+
return # git is not installed or tracking is disabled
203+
204+
if len(repo.head.commit.parents) == 0:
205+
log.error("No commits to revert.")
206+
return
207+
208+
def _format_commit_message(commit):
209+
return commit.message.split('\n')[0]
210+
211+
log.info(f"Reverting: {_format_commit_message(repo.head.commit)}")
212+
repo.git.reset('HEAD~1', hard=hard)
213+
log.info(f"Head is now at: {_format_commit_message(repo.head.commit)}")

0 commit comments

Comments
 (0)