-
Notifications
You must be signed in to change notification settings - Fork 147
/
git.js
123 lines (105 loc) · 2.78 KB
/
git.js
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
// @flow
const Git = require("nodegit");
import type {
Repository,
Signature,
Revparse,
Remote,
CredAcquireCb as CredCb
} from "nodegit";
export type {
Repository,
CredAcquireCb as CredCb,
Remote,
Signature
} from "nodegit";
const createSignature = (
time?: number = parseInt(Date.now() / 1000, 10),
offset?: number = new Date().getTimezoneOffset()
): Signature =>
Git.Signature.create("API", "api@amazee.io", time, offset);
const createCredentialsCb = (username: string, password: string): CredCb =>
() => Git.Cred.userpassPlaintextNew(username, password);
const expectDefaultState = (repository: Repository): Repository => {
if (!repository.isDefaultState()) {
throw new Error("The repository is not in the default state.");
}
return repository;
};
const getRepository = async (
url: string,
branch: string,
destination: string,
credCb: CredCb
): Promise<Repository> =>
{
try {
// Get the repository if it already exists.
return await Git.Repository.open(destination);
} catch (e) {
// Repository doesn't exist locally yet. Clone it.
return await Git.Clone.clone(url, destination, {
checkoutBranch: branch,
fetchOpts: {
callbacks: { certificateCheck: () => 1, credentials: credCb }
}
});
}
};
const getRemote = (repository: Repository, remote: string): Promise<Remote> =>
repository.getRemote(remote);
const remotePush = (
remote: Remote,
refs: Array<string>,
credCb: CredCb
): Promise<void> =>
remote.push(refs, {
// Attempt to push any pending commits.
callbacks: { certificateCheck: () => 1, credentials: credCb }
});
const revparseSingle = (
repository: Repository,
spec: string
): Promise<Revparse> =>
Git.Revparse.single(repository, spec);
const fetchAll = (
repository: Repository,
credentialsCb: CredCb
): Promise<void> =>
repository.fetchAll({
// Fetch any changes from the remote.
callbacks: { certificateCheck: () => 1, credentials: credentialsCb }
});
// Open the repository directory or fetch it from the remote.
const ensureRepository = async (
url: string,
branch: string,
destination: string,
credCb: CredCb
): Promise<Repository> =>
{
const repository = await getRepository(url, branch, destination, credCb);
// Ensure that we are on the right branch.
await repository.checkoutBranch(branch);
return expectDefaultState(repository);
};
const rebase = (
repository: Repository,
branch: string,
upstream: string,
onto: string,
sig: Signature
): Promise<void> =>
repository.rebaseBranches(branch, upstream, onto, sig);
module.exports = {
createSignature,
createCredentialsCb,
expectDefaultState,
getRepository,
getRemote,
remotePush,
revparseSingle,
fetchAll,
ensureRepository,
rebase
};