/
GitCommandGitProject.ts
412 lines (374 loc) · 15.2 KB
/
GitCommandGitProject.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright © 2018 Atomist, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as _ from "lodash";
import promiseRetry = require("promise-retry");
import { ProjectOperationCredentials } from "../../operations/common/ProjectOperationCredentials";
import {
isRemoteRepoRef,
RemoteRepoRef,
RepoRef,
} from "../../operations/common/RepoId";
import {
CloneDirectoryInfo,
CloneOptions,
DefaultCloneOptions,
DirectoryManager,
} from "../../spi/clone/DirectoryManager";
import { TmpDirectoryManager } from "../../spi/clone/tmpDirectoryManager";
import {
execPromise,
ExecPromiseResult,
} from "../../util/child_process";
import { logger } from "../../util/logger";
import {
isLocalProject,
ReleaseFunction,
} from "../local/LocalProject";
import { NodeFsLocalProject } from "../local/NodeFsLocalProject";
import { Project } from "../Project";
import {
GitProject,
GitPushOptions,
} from "./GitProject";
import {
GitStatus,
runStatusIn,
} from "./gitStatus";
export const DefaultDirectoryManager: DirectoryManager = TmpDirectoryManager;
/**
* Implements GitProject interface using the Git binary from the command line.
* Works only if git is installed.
*/
export class GitCommandGitProject extends NodeFsLocalProject implements GitProject {
public static fromProject(p: Project, credentials: ProjectOperationCredentials): GitProject {
if (isLocalProject(p)) {
return GitCommandGitProject.fromBaseDir(p.id, p.baseDir, credentials, () => Promise.resolve());
}
throw new Error(`Project ${p.name} doesn't have a local directory`);
}
/**
* Create a project from an existing git directory
* @param {RepoRef} id
* @param {string} baseDir
* @param {ProjectOperationCredentials} credentials
* @param release call this when you're done with the project. make its filesystem resources available to others.
* @param provenance optional; for debugging, describe how this was constructed
* @return {GitCommandGitProject}
*/
public static fromBaseDir(id: RepoRef, baseDir: string,
credentials: ProjectOperationCredentials,
release: ReleaseFunction,
provenance?: string): GitCommandGitProject {
return new GitCommandGitProject(id, baseDir, credentials, release, provenance);
}
/**
* Create a new GitCommandGitProject by cloning the given remote project
* @param {ProjectOperationCredentials} credentials
* @param {RemoteRepoRef} id
* @param {CloneOptions} opts
* @param {DirectoryManager} directoryManager
* @return {Promise<GitCommandGitProject>}
*/
public static async cloned(credentials: ProjectOperationCredentials,
id: RemoteRepoRef,
opts: CloneOptions = DefaultCloneOptions,
directoryManager: DirectoryManager = DefaultDirectoryManager): Promise<GitProject> {
const p = await clone(credentials, id, opts, directoryManager);
if (!!id.path) {
// It's possible to request a clone but only work with part of it.
const pathInsideRepo = id.path.startsWith("/") ? id.path : "/" + id.path;
const gp = GitCommandGitProject.fromBaseDir(id, p.baseDir + pathInsideRepo, credentials,
() => p.release(),
p.provenance + "\ncopied into one with extra path " + id.path);
return gp;
} else {
return p;
}
}
public branch: string;
public remote: string;
public newRepo: boolean = false;
private constructor(id: RepoRef,
public baseDir: string,
private readonly credentials: ProjectOperationCredentials,
release: ReleaseFunction,
public provenance?: string) {
super(id, baseDir, release);
this.branch = (id.branch || id.sha) || "master";
logger.debug(`Created GitProject`);
}
public async init(): Promise<this> {
this.newRepo = true;
this.branch = "master";
return this.gitInProjectBaseDir(["init"])
.then(() => this);
}
public isClean(): Promise<boolean> {
return this.gitInProjectBaseDir(["status", "--porcelain"])
.then(result => result.stdout === "");
}
public gitStatus(): Promise<GitStatus> {
return runStatusIn(this.baseDir);
}
/**
* Remote is of form https://github.com/USERNAME/REPOSITORY.git
* @param remote
*/
public setRemote(remote: string): Promise<this> {
this.remote = remote;
return this.gitInProjectBaseDir(["remote", "add", "origin", remote])
.then(() => this);
}
public setUserConfig(user: string, email: string): Promise<this> {
return this.gitInProjectBaseDir(["config", "user.name", user])
.then(() => this.gitInProjectBaseDir(["config", "user.email", email]))
.then(() => this);
}
public createAndSetRemote(gid: RemoteRepoRef,
description: string = gid.repo,
visibility: "private" | "public"): Promise<this> {
this.id = gid;
return gid.createRemote(this.credentials, description, visibility)
.then(res => {
if (res.success) {
logger.debug(`Repo created ok`);
return this.setRemote(gid.cloneUrl(this.credentials));
} else {
return Promise.reject(res.error);
}
});
}
public configureFromRemote(): Promise<this> {
if (isRemoteRepoRef(this.id)) {
return this.id.setUserConfig(this.credentials, this)
.then(() => this);
}
return Promise.resolve(this);
}
/**
* Raise a PR after a push to this branch
* @param title
* @param body
*/
public raisePullRequest(title: string, body: string = this.name, targetBranch: string = "master"): Promise<this> {
if (!this.branch) {
throw new Error("Cannot create a PR: no branch has been created");
}
if (!isRemoteRepoRef(this.id)) {
throw new Error("No remote in " + JSON.stringify(this.id));
}
return this.id.raisePullRequest(this.credentials, title, body, this.branch, targetBranch)
.then(() => this);
}
/**
* `git add .` and `git commit -m MESSAGE`
* @param {string} message Commit message
* @returns {Promise<this>}
*/
public commit(message: string): Promise<this> {
return this.gitInProjectBaseDir(["add", "."])
.then(() => this.gitInProjectBaseDir(["commit", "-m", message]))
.then(() => this);
}
/**
* Check out a particular commit. We'll end in detached head state
* @param ref branch or SHA
* @return {any}
*/
public async checkout(ref: string): Promise<this> {
await this.gitInProjectBaseDir(["checkout", ref, "--"]);
if (!isValidSHA1(ref)) {
this.branch = ref;
}
return this;
}
/**
* Revert all changes since last commit
* @return {any}
*/
public async revert(): Promise<this> {
return clean(this.baseDir)
.then(() => this);
}
public async push(options?: GitPushOptions): Promise<this> {
const gitPushArgs = ["push"];
_.forOwn(options, (v, k) => {
const opt = k.replace(/_/g, "-");
if (typeof v === "boolean") {
if (v === false) {
gitPushArgs.push(`--no-${opt}`);
} else {
gitPushArgs.push(`--${opt}`);
}
} else if (typeof v === "string") {
gitPushArgs.push(`--${opt}=${v}`);
} else {
return Promise.reject(new Error(`Unknown option key type for ${k}: ${typeof v}`));
}
});
if (!!this.branch && !!this.remote) {
// We need to set the remote
gitPushArgs.push(this.remote, this.branch);
} else {
gitPushArgs.push("--set-upstream", "origin", this.branch);
}
return this.gitInProjectBaseDir(gitPushArgs)
.then(() => this)
.catch(err => {
err.message = `Unable to push 'git "${gitPushArgs.join('" "')}"': ${err.message}`;
logger.error(err.message);
return Promise.reject(err);
});
}
/**
* Create branch from current HEAD.
* @param name Name of branch to create.
* @return project object
*/
public async createBranch(name: string): Promise<this> {
return this.gitInProjectBaseDir(["branch", name])
.then(() => this.gitInProjectBaseDir(["checkout", name, "--"]))
.then(() => {
this.branch = name;
return this;
});
}
public async hasBranch(name: string): Promise<boolean> {
return this.gitInProjectBaseDir(["branch", "--list", name])
.then(result => result.stdout.includes(name));
}
private async gitInProjectBaseDir(args: string[]): Promise<ExecPromiseResult> {
return execPromise("git", args, { cwd: this.baseDir });
}
}
/**
* Clone the given repo from GitHub
* @param credentials git provider credentials
* @param id remote repo ref
* @param opts options for clone
* @param directoryManager strategy for cloning
*/
async function clone(
credentials: ProjectOperationCredentials,
id: RemoteRepoRef,
opts: CloneOptions,
directoryManager: DirectoryManager,
secondTry: boolean = false,
): Promise<GitProject> {
const cloneDirectoryInfo = await directoryManager.directoryFor(id.owner, id.repo, id.branch, opts);
logger.info("Directory info: %j", cloneDirectoryInfo);
switch (cloneDirectoryInfo.type) {
case "empty-directory":
return cloneInto(credentials, cloneDirectoryInfo, opts, id);
case "existing-directory":
const repoDir = cloneDirectoryInfo.path;
try {
await resetOrigin(repoDir, credentials, id); // sometimes the credentials are in the origin URL
// Why do we not fetch?
await checkout(repoDir, id.branch || id.sha);
await clean(repoDir);
return GitCommandGitProject.fromBaseDir(id,
repoDir, credentials, cloneDirectoryInfo.release,
cloneDirectoryInfo.provenance + "\nRe-using existing clone");
} catch (error) {
await cloneDirectoryInfo.invalidate();
if (secondTry) {
throw error;
} else {
return clone(credentials, id, opts, directoryManager, true);
}
}
default:
throw new Error("What is this type: " + cloneDirectoryInfo.type);
}
}
async function cloneInto(
credentials: ProjectOperationCredentials,
targetDirectoryInfo: CloneDirectoryInfo,
opts: CloneOptions,
id: RemoteRepoRef,
): Promise<GitCommandGitProject> {
logger.debug(
`Cloning repo with owner '${id.owner}', name '${id.repo}', branch '${id.branch}', sha '${id.sha}' and options '${JSON.stringify(opts)}'`);
const sha = id.sha || "HEAD";
const repoDir = targetDirectoryInfo.path;
const url = id.cloneUrl(credentials);
const cloneBranch = id.branch;
const cloneArgs = ["clone", url, repoDir];
// If we wanted a deep clone, just clone it
if (!opts.alwaysDeep) {
// If we didn't ask for a deep clone, then default to cloning only the tip of the default branch.
// the cloneOptions let us ask for more commits than that
cloneArgs.push("--depth", ((opts.depth && opts.depth > 0) ? opts.depth : 1).toString(10));
if (cloneBranch) {
// if not cloning deeply, be sure we clone the right branch
cloneArgs.push("--branch", cloneBranch);
}
}
// Note: branch takes preference for checkout because we might be about to commit to it.
// If you want to be sure to land on your SHA, set opts.detachHead to true.
// Or don't, but then call gitStatus() on the returned project to check whether the branch is still at the SHA you wanted.
const checkoutRef = opts.detachHead ? sha : id.branch || sha;
// TODO CD user:password should be replaced too
const cleanUrl = url.replace(/\/\/.*:x-oauth-basic/, "//TOKEN:x-oauth-basic");
logger.debug(`Cloning repo '${cleanUrl}' in '${repoDir}'`);
const retryOptions = {
retries: 4,
factor: 2,
minTimeout: 100,
maxTimeout: 500,
randomize: false,
};
await promiseRetry(retryOptions, (retry, count) => {
return execPromise("git", cloneArgs)
.catch(err => {
logger.warn(`Clone of ${id.owner}/${id.repo} attempt ${count} failed: ` + err.message);
retry(err);
});
});
try {
await execPromise("git", ["checkout", checkoutRef, "--"], { cwd: repoDir });
} catch (err) {
// When the head moved on and we only cloned with depth; we might have to do a full clone to get to the commit we want
logger.warn(`Ref ${checkoutRef} not in cloned history. Attempting full clone`);
await execPromise("git", ["fetch", "--unshallow"], { cwd: repoDir })
.then(() => execPromise("git", ["checkout", checkoutRef, "--"], { cwd: repoDir }));
}
logger.debug(`Clone succeeded with URL '${cleanUrl}'`);
return GitCommandGitProject.fromBaseDir(id, repoDir, credentials,
targetDirectoryInfo.release,
targetDirectoryInfo.provenance + "\nfreshly cloned");
}
async function resetOrigin(
repoDir: string,
credentials: ProjectOperationCredentials,
id: RemoteRepoRef,
): Promise<ExecPromiseResult> {
return execPromise("git", ["remote", "set", "origin", id.cloneUrl(credentials)], { cwd: repoDir });
}
async function checkout(repoDir: string, branch: string = "HEAD"): Promise<ExecPromiseResult> {
return execPromise("git", ["fetch", "origin", branch], { cwd: repoDir })
.then(() => execPromise("git", ["checkout", branch, "--"], { cwd: repoDir }))
.then(() => execPromise("git", ["reset", "--hard", `origin/${branch}`], { cwd: repoDir }));
}
async function clean(repoDir: string): Promise<ExecPromiseResult> {
return execPromise("git", ["clean", "-dfx"], { cwd: repoDir }) // also removes ignored files
.then(() => execPromise("git", ["checkout", "--", "."], { cwd: repoDir }));
}
export function isValidSHA1(s: string): boolean {
return /^[a-fA-F0-9]{40}$/.test(s);
}