-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit-sync.js
executable file
·38 lines (38 loc) · 1.1 KB
/
git-sync.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
#!/usr/bin/env node
require('shelljs/global');
var util = require('util');
// Update the specified local branch with latest upstream changes, attempt to rebase local changes
var curr_branch = exec('git br', {
silent: 1
}).output.trim();
var branch = process.argv[2] || curr_branch;
var remote = exec('git origin', {
silent: 1
}).output.trim();
// fetch all remote changes
exec('git fetch ' + remote);
exec('git remote prune ' + remote);
var tracking = exec('git tr ' + branch, {
silent: 1
}).output.trim();
if (!tracking) {
echo(util.format('No remote tracking branch found for %s', branch));
exit(1);
}
// checkout to the target branch
if (curr_branch !== branch) {
exec('git checkout ' + branch);
}
// try first to fast forward.
if (exec('git merge ' + tracking + ' --ff-only').code !== 0) {
echo('Unable to fast-forward local branch, trying to rebase...');
// resort to rebase if fast forward fails.
if (exec('git rebase ' + tracking).code !== 0) {
echo('Unable to rebase local changes, aborted.');
exit(1);
}
}
// restore the original branch
if (curr_branch !== branch) {
exec('git checkout -');
}