import type { GitRuntimeOptions } from '../git-runtime-options' import { gitOptionsForWorktree } from '../git-runtime-options' import { gitExecFileAsync, gitOptionalLocksDisabledEnv } from '../runner' export async function readGitlinkOidFromTree( worktreePath: string, ref: string, submodulePath: string, options: GitRuntimeOptions ): Promise { try { const { stdout } = await gitExecFileAsync(['ls-tree', ref, '--', submodulePath], { ...gitOptionsForWorktree(worktreePath, options), env: gitOptionalLocksDisabledEnv() }) return stdout.match(/^160000 commit ([0-9a-f]+)\t/m)?.[1] ?? '' } catch { return '' } } export async function readGitlinkOidFromIndex( worktreePath: string, submodulePath: string, options: GitRuntimeOptions ): Promise { try { const { stdout } = await gitExecFileAsync(['ls-files', '-s', '--', submodulePath], { ...gitOptionsForWorktree(worktreePath, options), env: gitOptionalLocksDisabledEnv() }) return stdout.match(/^160000 ([0-9a-f]+) /m)?.[1] ?? '' } catch { return '' } } export async function readWorkingSubmoduleHead( submoduleWorktreePath: string, options: GitRuntimeOptions ): Promise { try { const { stdout } = await gitExecFileAsync(['rev-parse', 'HEAD'], { ...gitOptionsForWorktree(submoduleWorktreePath, options), env: gitOptionalLocksDisabledEnv() }) return stdout.trim() } catch { return '' } }