Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion agentstack/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,21 @@ def init_project(
packaging.create_venv()
log.info("Installing dependencies...")
packaging.install_project()
repo.init() # initialize git repo

if repo.find_parent_repo(conf.PATH):
# if a repo already exists, we don't want to initialize a new one
log.info("Found existing git repository; disabling tracking.")
with conf.ConfigFile() as config:
config.use_git = False
else:
# create a new git repo in the project dir
repo.init()

# now we can interact with the project and add Agents, Tasks, and Tools
# we allow dependencies to be installed along with these, so the project must
# be fully initialized first.
with repo.Transaction() as commit:
commit.add_message("Initialized new project")
for task in template_data.tasks:
commit.add_message(f"Added task {task.name}")
generation.add_task(**task.model_dump())
Expand Down
27 changes: 25 additions & 2 deletions agentstack/repo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional
from pathlib import Path
import shutil
import git
from agentstack import conf, log
Expand Down Expand Up @@ -108,6 +110,18 @@ def _require_git():
raise EnvironmentError(message)


def find_parent_repo(path: Path) -> Optional[Path]:
"""
Traverse the directory tree upwards from `path` until a .git directory is found.
"""
current = path.absolute()
while current != current.parent:
if (current / '.git').exists():
return current
current = current.parent
return None


def _get_repo() -> git.Repo:
"""
Get the git repository for the current project.
Expand All @@ -118,22 +132,31 @@ def _get_repo() -> git.Repo:
"""
_require_git()
try:
# look for a repository in the project's directory
return git.Repo(conf.PATH.absolute())
except git.exc.InvalidGitRepositoryError:
message = "No git repository found in the current project."
log.warning(message) # log now since this won't bubble to the user
raise EnvironmentError(message)


def init() -> None:
def init(force_creation: bool = False) -> None:
"""
Create a git repository for the current project and commit a .gitignore file
to initialize the repo. Assumes that a repo does not already exist.
to initialize the repo.

`force_creation` will create a new repo even if one already exists in a parent
directory. default: False
"""
try:
_require_git()
except EnvironmentError as e:
return # git is not installed or tracking is disabled

if find_parent_repo(conf.PATH.absolute()):
log.warning("A git repository already exists in a parent directory.")
if not force_creation:
return

# creates a new repo at conf.PATH / '.git
repo = git.Repo.init(path=conf.PATH.absolute(), initial_branch=MAIN_BRANCH_NAME)
Expand Down
Loading