sync: support post-sync hook in <repo-hooks>
Add support for a new hook type "post-sync" declared in the manifest using <repo-hooks>. This allows executing a script automatically after a successful `repo sync`. This is useful for initializing developer environments, installing project-wide Git hooks, generating configs, and other post-sync automation tasks. Example manifest usage: <project name="myorg/repo-hooks" path="hooks" revision="main" /> <repo-hooks in-project="myorg/repo-hooks" enabled-list="post-sync"> <hook name="post-sync" /> </repo-hooks> The hook script must be named `post-sync.py` and located at the root of the hook project. The post-sync hook does not block `repo sync`; if the script fails, the sync still completes successfully with a warning. Test: Added `post-sync.py` in hook project and verified it runs after `repo sync` Change-Id: I69f3158f0fc319d73a85028d6e90fea02c1dc8c8 Signed-off-by: Kenny Cheng <chao.shun.cheng.tw@gmail.com>
This commit is contained in:
parent
08815ad3eb
commit
e54e77132b
2 changed files with 50 additions and 0 deletions
|
@ -133,3 +133,30 @@ def main(project_list, worktree_list=None, **kwargs):
|
|||
kwargs: Leave this here for forward-compatibility.
|
||||
"""
|
||||
```
|
||||
|
||||
### Hook: 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 `<repo-hooks>` 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
|
||||
<project name="myorg/dev-tools" path="tools" revision="main" />
|
||||
<repo-hooks in-project="myorg/dev-tools" enabled-list="post-sync">
|
||||
<hook name="post-sync" />
|
||||
</repo-hooks>
|
||||
```
|
||||
|
||||
The `post-sync.py` file should be defined like:
|
||||
|
||||
```py
|
||||
def main(**kwargs):
|
||||
print("Running post-sync tasks...")
|
||||
```
|
||||
|
|
|
@ -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
|
||||
|
@ -1736,11 +1737,33 @@ later is required to fix a server side protocol bug.
|
|||
errors = []
|
||||
try:
|
||||
self._ExecuteHelper(opt, args, errors)
|
||||
self._RunPostSyncHook()
|
||||
|
||||
except (RepoExitError, RepoChangedException):
|
||||
raise
|
||||
except (KeyboardInterrupt, Exception) as e:
|
||||
raise RepoUnhandledExceptionError(e, aggregate_errors=errors)
|
||||
|
||||
def _RunPostSyncHook(self):
|
||||
"""Run post-sync hook if configured in manifest <repo-hooks>."""
|
||||
|
||||
class DummyOpt:
|
||||
bypass_hooks=False
|
||||
allow_all_hooks=True
|
||||
ignore_hooks=True # Don't block sync even if hook fails
|
||||
|
||||
try:
|
||||
post_sync_hook = RepoHook.FromSubcmd(
|
||||
hook_type="post-sync",
|
||||
manifest=self.manifest,
|
||||
opt=DummyOpt(),
|
||||
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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue