diff --git a/build/ensure/git/git.ts b/build/ensure/git/git.ts index 5eae2b745c..f67648fd31 100644 --- a/build/ensure/git/git.ts +++ b/build/ensure/git/git.ts @@ -15,6 +15,15 @@ namespace $ { } protected override update(dir: string) { + if (this.submodules().has(dir)) { + this.$.$mol_log3_rise({ + place: '$mol_build_ensure_git.update()', + message: 'Submodule detected, no git pull', + dir, + }) + return false + } + const out = this.$.$mol_run.spawn({ command: 'git rev-parse --abbrev-ref --symbolic-full-name HEAD', dir, }) @@ -50,21 +59,52 @@ namespace $ { return git_dir.exists() && git_dir.type() === 'dir' } - @ $mol_mem - protected submodules() { - const dir = this.root().path() - if (! this.is_git( dir ) ) return new Set() + @ $mol_action + protected submodule_dirs(opts: { dir: string, recursive?: boolean }) { + const dir = this.$.$mol_file.absolute( opts.dir ) - const command = 'git submodule status --recursive' - const output = this.$.$mol_run.spawn({ command, dir }).stdout.toString().trim() + const output = this.$.$mol_run.spawn({ + command: [ 'git', 'submodule', 'status', ...( opts.recursive ? ['--recursive'] : [] ) ], + dir: dir.path(), + }).stdout.toString().trim() const dirs = output .split('\n') .map( str => str.match( /^\s*[^ ]+\s+([^ ]*).*/ )?.[1]?.trim() ) .filter($mol_guard_defined) - .map(str => `${dir}/${str}`) + .map(subdir => dir.resolve(subdir)) - return new Set(dirs) + return dirs + } + + @ $mol_mem + protected root_is_submodule() { + const root = this.root() + if (this.is_git(root.path())) return false + + const parent = root.parent() + + try { + const dirs = this.submodule_dirs({ dir: parent.path() }) + + return dirs.includes(root) + } catch (e) { + if ($mol_promise_like(e)) $mol_fail_hidden(e) + this.$.$mol_fail_log(e) + return false + } + } + + @ $mol_mem + protected submodules() { + const root = this.root() + if (! this.is_git( root.path() ) ) return new Set() + + const dirs = this.submodule_dirs({ dir: root.path(), recursive: true }) + + if (this.root_is_submodule()) dirs.push(root) + + return new Set(dirs.map(mod => mod.path())) } protected override inited(path: string) { @@ -89,9 +129,9 @@ namespace $ { const branch_norm = branch ?? this.branch_remote(dir) - this.$.$mol_run.spawn( { command: ['git', 'remote', 'add', '--track', branch_norm, 'origin' , url ], dir } ) + this.$.$mol_run.spawn( { command: [ 'git', 'remote', 'add', '--track', branch_norm, 'origin' , url ], dir } ) this.$.$mol_run.spawn( { command: [ 'git', 'pull', 'origin', branch_norm ], dir } ) - + return null }