-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgit.ts
169 lines (132 loc) · 5.16 KB
/
git.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
namespace $ {
export class $mol_build_ensure_git extends $mol_build_ensure_vcs {
override vcs_type() { return 'git' }
override root_repo() { return 'https://github.com/hyoo-ru/mam.git' }
@ $mol_mem
protected version() {
$mol_wire_solid()
return this.$.$mol_run.spawn({ command: 'git version', dir: this.root().path() })
.stdout.toString().trim().match(/.*\s+([\d\.]+\d+)/)?.[1] ?? ''
}
protected deepen_supported() {
return $mol_compare_text()(this.version(), '2.42.0') >= 0
}
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,
})
const current_branch = out.stdout.toString().trim()
// когда не на ветке - не надо пулить, например сборка во время git bisect
if (! current_branch) return false
const command = ['git', 'pull']
if ( ! this.interactive() && this.deepen_supported()) {
/**
depth и deepen не годятся для локальной разработки, поэтому оставляем ограничение глубины пула только для CI
--depth=1 в сочетании с сабмодулями обрезает историю, кроме первого коммита
--deepen=1 если не сделать unset GIT_DIR
в git-конфиге сабмодуля выставляет bare=true, после этого все команды падают с сообщением
warning: core.bare and core.worktree do not make sense
fatal: unable to set up work tree using invalid config
*/
command.push( '--deepen=1' )
}
const timeout = this.pull_timeout()
this.$.$mol_run.spawn( { command, dir, timeout }).stdout.toString().trim()
return true
}
protected is_git(path: string) {
const mod = this.$.$mol_file.absolute( path )
const git_dir = mod.resolve( '.git' )
return git_dir.exists() && git_dir.type() === 'dir'
}
@ $mol_action
protected submodule_dirs(opts: { dir: string, recursive?: boolean }) {
const dir = this.$.$mol_file.absolute( opts.dir )
try {
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(subdir => dir.resolve(subdir))
return dirs
} catch (e) {
if ($mol_promise_like(e)) $mol_fail_hidden(e)
this.$.$mol_fail_log(e)
return []
}
}
@ $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<string>()
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) {
return this.is_git(path) || this.submodules().has(path)
}
protected repo_ensured(dir: string) {
const repo = this.repo(dir)
if (! repo) throw new Error(`"${dir}" not a repo`)
return repo
}
@ $mol_mem_key
protected branch_remote(dir: string) {
const repo = this.repo_ensured(dir)
const res = this.$.$mol_run.spawn( { command: ['git', 'remote', 'show', repo.url ], dir } )
return res.stdout.toString().match( /HEAD branch: (.*?)\n/ )?.[1] ?? 'master'
}
protected override init_existing(dir: string) {
// Например, если вручную склонить ревизию папки в глубине (например, hyoo/mol) перед запуском билда,
// то hyoo надо проинициалзировать в соответствии с meta.ree
const repo = this.repo_ensured(dir)
const { url, branch } = repo
this.$.$mol_run.spawn( { command: ['git', 'init'], dir } )
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', 'pull', 'origin', branch_norm ], dir } )
return null
}
protected override init(path: string) {
const mod = this.$.$mol_file.absolute( path )
const repo = this.repo_ensured(path)
const command = [
'git', 'clone' , '--depth', '1',
...( repo.branch ? [ '-b', repo.branch ] : [] ),
' --single-branch',
repo.url ,
mod.relate( this.root() )
]
const dir = this.root().path()
this.$.$mol_run.spawn( { command, dir } )
return null
}
}
}