-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathfluxRepository.js
More file actions
78 lines (59 loc) · 1.9 KB
/
fluxRepository.js
File metadata and controls
78 lines (59 loc) · 1.9 KB
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
const path = require('node:path');
const os = require('node:os');
const sg = require('simple-git');
class FluxRepository {
// this may not exist
defaultRepoDir = path.join(os.homedir(), 'zelflux');
constructor(options = {}) {
this.repoPath = options.repoDir || this.defaultRepoDir;
const gitOptions = {
baseDir: this.repoPath,
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: true,
};
this.git = sg.simpleGit(gitOptions);
}
async remotes() {
return this.git.getRemotes(true).catch(() => []);
}
async addRemote(name, url) {
await this.git.addRemote(name, url).catch(() => { });
}
async currentCommitId() {
const id = await this.git.revparse('HEAD').catch(() => null);
return id;
}
async currentBranch() {
const branches = await this.git.branch().catch(() => null);
if (!branches) return null;
const { current, detached } = branches;
return detached ? null : current;
}
async resetToCommitId(id) {
await this.git.reset(sg.ResetMode.HARD, [id]).catch(() => { });
}
async switchBranch(branch, options = {}) {
const forceClean = options.forceClean || false;
const reset = options.reset || false;
const remote = options.remote || 'origin';
const remoteBranch = `${remote}/${branch}`;
// fetch first incase there are errors.
await this.git.fetch(remote, branch);
if (forceClean) {
await this.git.clean(sg.CleanOptions.FORCE + sg.CleanOptions.RECURSIVE);
}
if (reset) {
await this.git.reset(sg.ResetMode.HARD, [remoteBranch]);
}
const exists = await this.git.revparse(['--verify', branch]).catch(() => false);
if (exists) {
await this.git.checkout(branch);
// don't think we need to reset here
await this.git.merge(['--ff-only']);
return;
}
await this.git.checkout(['--track', remoteBranch]);
}
}
module.exports = { FluxRepository };