-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
git.js
174 lines (160 loc) · 4.46 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
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
/**
* External dependencies
*/
const SimpleGit = require( 'simple-git/promise' );
/**
* Internal dependencies
*/
const { getRandomTemporaryPath } = require( './utils' );
/**
* Clones a Github repository.
*
* @param {string} repositoryUrl
*
* @return {Promise<string>} Repository local Path
*/
async function clone( repositoryUrl ) {
const gitWorkingDirectoryPath = getRandomTemporaryPath();
const simpleGit = SimpleGit();
await simpleGit.clone( repositoryUrl, gitWorkingDirectoryPath, [
'--depth=1',
'--no-single-branch',
] );
return gitWorkingDirectoryPath;
}
/**
* Commits changes to the repository.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} message Commit message.
* @param {string[]} filesToAdd Files to add.
*
* @return {Promise<string>} Commit Hash
*/
async function commit( gitWorkingDirectoryPath, message, filesToAdd = [] ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.add( filesToAdd );
const commitData = await simpleGit.commit( message );
const commitHash = commitData.commit;
return commitHash;
}
/**
* Creates a local branch.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} branchName Branch Name
*/
async function createLocalBranch( gitWorkingDirectoryPath, branchName ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.checkoutLocalBranch( branchName );
}
/**
* Checkout a local branch.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} branchName Branch Name
*/
async function checkoutRemoteBranch( gitWorkingDirectoryPath, branchName ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.fetch( 'origin', branchName );
await simpleGit.checkout( branchName );
}
/**
* Creates a local tag.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} tagName Tag Name
*/
async function createLocalTag( gitWorkingDirectoryPath, tagName ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.addTag( tagName );
}
/**
* Pushes a local branch to the origin.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} branchName Branch Name
*/
async function pushBranchToOrigin( gitWorkingDirectoryPath, branchName ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.push( 'origin', branchName );
}
/**
* Pushes tags to the origin.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
*/
async function pushTagsToOrigin( gitWorkingDirectoryPath ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.pushTags( 'origin' );
}
/**
* Discard local changes.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
*/
async function discardLocalChanges( gitWorkingDirectoryPath ) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.reset( 'hard' );
}
/**
* Reset local branch against the origin.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} branchName Branch Name
*/
async function resetLocalBranchAgainstOrigin(
gitWorkingDirectoryPath,
branchName
) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.fetch();
await simpleGit.checkout( branchName );
await simpleGit.pull( 'origin', branchName );
}
/**
* Cherry-picks a commit into master
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} commitHash Branch Name
*/
async function cherrypickCommitIntoBranch(
gitWorkingDirectoryPath,
commitHash
) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.checkout( 'master' );
await simpleGit.raw( [ 'cherry-pick', commitHash ] );
}
/**
* Replaces the local branch's content with the content from another branch.
*
* @param {string} gitWorkingDirectoryPath Local repository path.
* @param {string} sourceBranchName Branch Name
*/
async function replaceContentFromRemoteBranch(
gitWorkingDirectoryPath,
sourceBranchName
) {
const simpleGit = SimpleGit( gitWorkingDirectoryPath );
await simpleGit.raw( [ 'rm', '-r', '.' ] );
await simpleGit.raw( [
'checkout',
`origin/${ sourceBranchName }`,
'--',
'.',
] );
}
module.exports = {
clone,
commit,
checkoutRemoteBranch,
createLocalBranch,
createLocalTag,
pushBranchToOrigin,
pushTagsToOrigin,
discardLocalChanges,
resetLocalBranchAgainstOrigin,
cherrypickCommitIntoBranch,
replaceContentFromRemoteBranch,
};