-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaccmp.js
129 lines (116 loc) · 3.07 KB
/
accmp.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
123
124
125
126
127
128
129
'use strict';
// accmp : short for add commit checkout merge push,
// whole example from add file to stage to push to remote
const Git = require('nodegit');
const _ = require('underscore');
const common = require('./common');
const cnst = require('./constant');
let gitAddCommit = (files) => {
let index, repo, oid;
return Git.Repository.open(cnst.test_git_path)
.then((repoResult) => repo = repoResult)
.then(() => repo.refreshIndex())
.then((indexResult) => index = indexResult)
.then(() => {
let promises = [];
// TODO
_.each(files, (v) => {
promises.push(index.addByPath(v));
});
return Promise.all(promises);
})
// 将文件写入到tree
.then(() => index.write())
.then(() => index.writeTree())
.then((oidResult) => oid = oidResult)
.then(() => Git.Reference.nameToId(repo, 'HEAD'))
.then((head) => repo.getCommit(head))
.then((parent) => {
return repo.createCommit(
'HEAD',
common.get_commiter_sign(cnst),
common.get_commiter_sign(cnst),
'system commit file : ' + files.join(','),
oid,
[parent]
);
})
.then(() => repo.free());
};
let gitCheckoutBranch = (branchName, mainBranch) => {
let repo;
// 先切分支, 再copy文件
return Git.Repository.open(cnst.test_git_path)
.then((repoResult) => repo = repoResult)
.then(() => {
return new Promise((resolve) => {
repo.getBranch(branchName)
.then(() => resolve(true), () => resolve(false));
});
})
.then((result) => {
if(!result){
return repo.checkoutBranch(mainBranch)
.then(() => repo.getHeadCommit())
.then((commit) => {
return repo.createBranch(
branchName,
commit,
true,
common.get_commiter_sign(cnst),
'Created new branch : '+branchName
);
});
} else {
return Promise.resolve();
}
})
.then(() => repo.checkoutBranch(branchName))
.then(() => repo.free());
};
let gitMerge = ( branchName, mainBranch ) => {
let repo;
return gitCheckoutBranch(branchName)
.then(() => Git.Repository.open(cnst.test_git_path))
.then((repoResult) => repo = repoResult)
.then(() => {
return repo.mergeBranches(
mainBranch,
branchName,
common.get_commiter_sign(cnst),
1
);
})
.then(() => repo.free());
};
let gitPush = (branchName, mainBranch) => {
let repo;
return gitCheckoutBranch( mainBranch, mainBranch )
.then(() => Git.Repository.open(cnst.test_git_path))
.then((repoResult) => repo = repoResult)
.then(() => Git.Remote.lookup(repo, 'origin'))
.then((remote) => {
return remote.push(
['refs/heads/'+mainBranch+':refs/heads/'+mainBranch],
{
callbacks: {
credentials : () => Git.Cred.userpassPlaintextNew(cnst.account, cnst.password)
}
}
);
})
.then(() => repo.free());
};
let loop = 0;
// start push
setInterval(() => {
loop++;
console.log('loop', loop);
Promise.resolve()
.then(() => gitCheckoutBranch(`test${loop}`, 'master'))
.then(() => common.create_files(loop, 1, +new Date()))
.then((files) => gitAddCommit(files))
.then(() => gitMerge(`test${loop}`, 'master'))
.then(() => gitPush('master', 'master'))
.catch((e) => console.log(e));
}, 5000);