-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
206 lines (179 loc) · 5.09 KB
/
index.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
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
var childProcess = require('child_process');
module.exports = pushDir;
function pushDir(opts) {
getLastCommitInfo().then(function(info) {
var hash = info.hash;
var detachedHead = info.branch === '';
var originalBranch = detachedHead ? hash : info.branch;
var directory = opts.dir;
var local = opts.branch + '-' + hash;
var remote = opts.remote || 'origin';
var remoteBranch = opts.branch;
var message = typeof opts.message === 'function' ?
opts.message(hash) : opts.message || hash;
var allowUnclean = opts['allow-unclean'] || opts.force;
var overwriteLocal = opts['overwrite-local'] || opts.force;
var cleanup = opts.cleanup === undefined ? false : opts.cleanup;
var verbose = opts.verbose;
Promise.resolve()
.then(checkIfClean)
.catch(function onUnclean(reason) {
return allowUnclean ?
console.log('ignoring unclean git...') : Promise.reject(reason);
})
.then(noLocalBranchConflict.bind(null, local))
.catch(function onBranchConflict(reason) {
return overwriteLocal ?
overwriteLocalBranch(local, verbose) : Promise.reject(reason);
})
.then(checkoutOrphanBranch.bind(null, directory, local, verbose))
.then(addDir.bind(null, directory, verbose))
.then(commitDir.bind(null, directory, message, verbose))
.then(pushDirToRemote.bind(null, remote, remoteBranch, verbose))
.then(resetBranch.bind(null, originalBranch, detachedHead, verbose))
.then(cleanup && deleteLocalBranch.bind(null, local, verbose))
.catch(handleError);
}, handleError);
}
/**
* Tasks
*/
function overwriteLocalBranch(local, verbose) {
console.log('will overwite local branch...');
return deleteLocalBranch(local, verbose);
}
function checkIfClean() {
return expectOutputEmpty(
'git status --porcelain',
'git not clean'
);
}
/**
* Returns name of current branch or empty string if detached HEAD
* @return {string} - name of current branch
*/
function getCurrentBranch(verbose) {
return Promise.resolve()
.then(execCmd.bind(null,
'git',
['symbolic-ref', 'HEAD', '-q'],
'problem getting current branch',
verbose
))
.catch(function() {
return '';
})
.then(function(result) {
return result.replace(new RegExp('^refs\/heads\/'), '');
});
}
function resetBranch(branch, detach, verbose) {
var detached = detach ? '--detach' : false;
return execCmd(
'git',
['checkout', '-f', detached, branch].filter(Boolean),
'problem resetting branch',
verbose
);
}
function addDir(directory, verbose) {
return execCmd(
'git',
['--work-tree', directory, 'add', '--all'],
'problem adding directory to local branch',
verbose
);
}
function commitDir(directory, message, verbose) {
return execCmd(
'git',
['--work-tree', directory, 'commit', '-m', message],
'problem committing directory to local branch',
verbose
);
}
function pushDirToRemote(remote, remoteBranch, verbose) {
return execCmd(
'git',
['push', remote, 'HEAD:' + remoteBranch, '--force'],
'problem pushing local branch to remote',
verbose
);
}
function checkoutOrphanBranch(directory, branch, verbose) {
return execCmd(
'git',
['--work-tree', directory, 'checkout', '--orphan', branch],
'problem creating local orphan branch',
verbose
);
}
function deleteLocalBranch(branch, verbose) {
return execCmd(
'git',
['branch', '-D', branch],
'problem deleting local branch',
verbose
);
}
function noLocalBranchConflict(branch) {
return expectOutputEmpty(
'git branch --list ' + branch,
'local branch with name already exists'
);
}
function getLastCommitInfo() {
return Promise
.all([
getLastCommitHash(),
getCurrentBranch()
])
.then(function(info) {
info = info.map(function(s) { return s.trim(); });
return {
hash: info[0],
branch: info[1]
};
});
}
function getLastCommitHash(verbose) {
return execCmd(
'git',
['rev-parse', '--short', 'HEAD'],
'problem getting last commit hash',
verbose
);
}
/**
* Helpers
*/
function handleError(err) {
console.error('aborted:', err);
process.exit(1);
}
function execCmd(cmd, args, errMessage, verbose) {
return new Promise(function(resolve, reject) {
verbose && console.log(cmd, args.join(' '));
const proc = childProcess.spawn(cmd, args, { stdio: 'pipe' });
const stdoutChunks = [];
proc.stdout.on('data', function(data) {
stdoutChunks.push(data);
verbose && process.stdout.write(data);
});
proc.stderr.on('data', function(data) {
verbose && process.stderr.write(data);
});
proc.on('close', function(code) {
verbose && console.log('\n');
if (code !== 0) reject(errMessage);
else resolve(Buffer.concat(stdoutChunks).toString());
});
});
}
function expectOutputEmpty(cmd, errMessage) {
return new Promise(function(resolve, reject) {
childProcess.exec(cmd, function(error, stdout, stderr) {
(error || stdout.length || stderr.length) ? reject(errMessage) : resolve();
});
});
}