1010INITIAL_COMMIT_MESSAGE = "Initial commit."
1111USER_CHANGES_COMMIT_MESSAGE = "Adding user changes before modifying project."
1212
13- _USE_GIT = None # global state to disable it tracking for one run
13+ _USE_GIT = None # global state to disable git for this run
1414
1515
1616def should_track_changes () -> bool :
1717 """
18- If git has been disabled for this run, return False.
19- Otherwise, return the value defined in agentstack.json.
18+ If git has been disabled for this run, return False. Next, look for the value
19+ defined in agentstack.json. Finally, default to True .
2020 """
2121 global _USE_GIT
22-
22+
2323 if _USE_GIT is not None :
2424 return _USE_GIT
25-
25+
2626 try :
27- return bool ( conf .ConfigFile ().use_git )
27+ return conf .ConfigFile ().use_git is not False
2828 except FileNotFoundError :
2929 return True
3030
@@ -41,8 +41,9 @@ def dont_track_changes() -> None:
4141class TrackingDisabledError (EnvironmentError ):
4242 """
4343 Raised when git is disabled for this run.
44+ Subclasses `EnvironmentError` so we can early exit using the same logic.
4445 """
45- # subclasses `EnvironmentError` so we can early exit using the same logic
46+
4647 pass
4748
4849
@@ -56,7 +57,6 @@ class Transaction:
5657 Path('foo').touch()
5758 transaction.add_message("Created foo")
5859 ```
59-
6060 Changes will be committed automatically on exit.
6161 """
6262
@@ -93,52 +93,56 @@ def _require_git():
9393 """
9494 if not should_track_changes ():
9595 raise TrackingDisabledError ("Git tracking is disabled by the user." )
96-
96+
9797 try :
9898 assert shutil .which ('git' )
99- except ( AssertionError , ImportError ) :
100- message = "git is not installed.\n In order to track changes to files in your project, install git. \n "
99+ except AssertionError :
100+ message = "git is not installed.\n Install it to track changes to files in your project. "
101101 if shutil .which ('apt' ):
102- message += "Hint : run `sudo apt install git`"
102+ message += "\n Hint : run `sudo apt install git`"
103103 elif shutil .which ('brew' ):
104- message += "Hint : run `brew install git`"
104+ message += "\n Hint : run `brew install git`"
105105 elif shutil .which ('port' ):
106- message += "Hint : run `sudo port install git`"
107- log .warning (message )
106+ message += "\n Hint : run `sudo port install git`"
107+ log .warning (message ) # log now since this won't bubble to the user
108108 raise EnvironmentError (message )
109109
110110
111111def _get_repo () -> git .Repo :
112112 """
113113 Get the git repository for the current project.
114+ Raises:
115+ - `TrackingDisabledError` if git tracking is disabled.
116+ - `EnvironmentError` if git is not installed.
117+ - `EnvironmentError` if the repo is not found.
114118 """
115119 _require_git ()
116120 try :
117121 return git .Repo (conf .PATH .absolute ())
118122 except git .exc .InvalidGitRepositoryError :
119123 message = "No git repository found in the current project."
120- log .warning (message )
124+ log .warning (message ) # log now since this won't bubble to the user
121125 raise EnvironmentError (message )
122126
123127
124128def init () -> None :
125129 """
126- Initialize a git repository for the current project if one does not exist
127- and commit all changes .
130+ Create a git repository for the current project and commit a .gitignore file
131+ to initialize the repo. Assumes that a repo does not already exist .
128132 """
129133 try :
130134 _require_git ()
131135 except EnvironmentError as e :
132- return
136+ return # git is not installed or tracking is disabled
133137
134138 # creates a new repo at conf.PATH / '.git
135139 repo = git .Repo .init (path = conf .PATH .absolute (), initial_branch = MAIN_BRANCH_NAME )
136-
137- # commit gitignore first, so we don't add untracked files
140+
141+ # commit gitignore first so we don't add untracked files
138142 gitignore = conf .PATH .absolute () / '.gitignore'
139143 gitignore .touch ()
140-
141- commit (INITIAL_COMMIT_MESSAGE , [str (gitignore ), ], automated = True )
144+
145+ commit (INITIAL_COMMIT_MESSAGE , [str (gitignore )], automated = True )
142146
143147
144148def commit (message : str , files : list [str ], automated : bool = True ) -> None :
@@ -149,7 +153,7 @@ def commit(message: str, files: list[str], automated: bool = True) -> None:
149153 try :
150154 repo = _get_repo ()
151155 except EnvironmentError as e :
152- return
156+ return # git is not installed or tracking is disabled
153157
154158 log .debug (f"Committing { len (files )} changed files" )
155159 repo .index .add (files )
@@ -168,19 +172,20 @@ def commit_all_changes(message: str, automated: bool = True) -> None:
168172
169173def commit_user_changes (automated : bool = True ) -> None :
170174 """
171- Commit any changes to the current repo and assume they're user changes.
175+ Commit any changes to the current repo as user changes.
176+ Include AUTOMATION_NOTE in the commit message if `automated` is `True`.
172177 """
173178 commit_all_changes (USER_CHANGES_COMMIT_MESSAGE , automated = automated )
174179
175180
176181def get_uncommitted_files () -> list [str ]:
177182 """
178- Get a list of all files that have been modified but not committed .
183+ Get a list of all files that have been modified since the last commit .
179184 """
180185 try :
181186 repo = _get_repo ()
182187 except EnvironmentError as e :
183- return []
188+ return [] # git is not installed or tracking is disabled
184189
185190 untracked = repo .untracked_files
186191 modified = [item .a_path for item in repo .index .diff (None ) if item .a_path ]
0 commit comments