diff --git a/docs/repo-hooks.md b/docs/repo-hooks.md index cbb1aac7c..18de13b1e 100644 --- a/docs/repo-hooks.md +++ b/docs/repo-hooks.md @@ -133,3 +133,30 @@ def main(project_list, worktree_list=None, **kwargs): kwargs: Leave this here for forward-compatibility. """ ``` + +### post-sync + +The `post-sync` hook allows you to execute a script automatically after a +successful `repo sync`. This is useful for post-processing tasks such as +setting up git hooks, bootstrapping configuration files, or running project +initialization logic. + +The hook is defined using the existing `` manifest block and is +optional. If the hook script fails or is missing, `repo sync` will still +complete successfully, and the error will be printed as a warning. + +Example: + +```xml + + + + +``` + +The `post-sync.py` file should be defined like: + +```py +def main(**kwargs): + print("Running post-sync tasks...") +``` diff --git a/subcmds/sync.py b/subcmds/sync.py index 3a4151dfe..97500b199 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -79,6 +79,7 @@ from repo_logging import RepoLogger from repo_trace import Trace import ssh from wrapper import Wrapper +from hooks import RepoHook _ONE_DAY_S = 24 * 60 * 60 @@ -565,6 +566,7 @@ later is required to fix a server side protocol bug. action="store_true", help=optparse.SUPPRESS_HELP, ) + RepoHook.AddOptionGroup(p, 'post-sync') def _GetBranch(self, manifest_project): """Returns the branch name for getting the approved smartsync manifest. @@ -1736,11 +1738,27 @@ later is required to fix a server side protocol bug. errors = [] try: self._ExecuteHelper(opt, args, errors) + self._RunPostSyncHook(opt) + except (RepoExitError, RepoChangedException): raise except (KeyboardInterrupt, Exception) as e: raise RepoUnhandledExceptionError(e, aggregate_errors=errors) + def _RunPostSyncHook(self, opt): + """Run post-sync hook if configured in manifest .""" + try: + post_sync_hook = RepoHook.FromSubcmd( + hook_type="post-sync", + manifest=self.manifest, + opt=opt, + abort_if_user_denies=False, + ) + post_sync_hook.Run() + + except Exception as e: + print(f"Warning: post-sync hook failed: {e}") + def _ExecuteHelper(self, opt, args, errors): manifest = self.outer_manifest if not opt.outer_manifest: