bindHookInvocation function

({Hook boundHook, HookContext context, String? messageFile, String? remoteName, String? remoteUrl}) bindHookInvocation({
  1. required String name,
  2. required Hook hook,
  3. required List<String> args,
  4. String stdinContent = '',
})

Binds Git hook args into remote / message-file fields and a HookContext.

Implementation

({
  String? remoteName,
  String? remoteUrl,
  String? messageFile,
  Hook boundHook,
  HookContext context,
})
bindHookInvocation({
  required String name,
  required Hook hook,
  required List<String> args,
  String stdinContent = '',
}) {
  final normalized = normalizeHookName(name);

  String? remoteName;
  String? remoteUrl;
  String? messageFile;

  // pre-push: Git passes <remote-name> <remote-url>
  if (normalized == 'pre-push' && args.length >= 2) {
    remoteName = args[0];
    remoteUrl = args[1];
  } else if (args case [
    final String remote,
    final String url,
  ] when hook is PrePushHook) {
    remoteName = remote;
    remoteUrl = url;
  }

  // commit-msg / prepare-commit-msg: $1 is the message file path
  if (args.isNotEmpty &&
      (hook is CommitMsgHook ||
          normalized == 'commit-msg' ||
          normalized == 'prepare-commit-msg')) {
    messageFile = args.first;
  }

  final boundHook = switch (hook) {
    final CommitMsgHook commitMsg => commitMsg.bindMessageFile(messageFile),
    _ => hook,
  };

  final context = HookContext(
    args: List.unmodifiable(args),
    stdin: stdinContent,
    messageFile:
        messageFile ??
        (boundHook is CommitMsgHook ? boundHook.messageFile : null),
  );

  return (
    remoteName: remoteName,
    remoteUrl: remoteUrl,
    messageFile: messageFile,
    boundHook: boundHook,
    context: context,
  );
}