Created
July 20, 2014 11:55
-
-
Save vicb/3c0e404e276f62189b11 to your computer and use it in GitHub Desktop.
Archive merged branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:async'; | |
// Name for the master repo | |
var UPSTREAM = 'origin'; | |
// Name of your fork | |
var REMOTE = 'vicb'; | |
void main() { | |
getBranches() | |
.then((branches) { | |
branches.forEach((branch) { | |
if (branch == 'master') return; | |
isMerged(branch: branch, into: '$UPSTREAM/master').then((merged) { | |
if (merged) { | |
bool archived = archiveBranch(branch); | |
print(archived ? | |
'branch "$branch" merged and archived' : | |
'branch "$branch" merged, archive error'); | |
} else { | |
print('branch "$branch" not merged'); | |
} | |
}); | |
}); | |
}, | |
onError: (e) => print("Error $e")); | |
} | |
Future<List<String>> getBranches() { | |
var completer = new Completer(); | |
try { | |
Process.run('git', ['branch', '--list', '--no-color']).then((ProcessResult r) { | |
if (r.exitCode == 0) { | |
var branches = []; | |
r.stdout.split('\n').forEach((String line) { | |
if (line.isEmpty) return; | |
branches.add(line.replaceAll('*', '').trim()); | |
}); | |
completer.complete(branches); | |
} else { | |
completer.completeError("git branch exit code: ${r.exitCode}"); | |
} | |
}); | |
} catch (e) { | |
completer.completeError(e); | |
} | |
return completer.future; | |
} | |
Future<bool> isMerged({String branch, String into}) { | |
var completer = new Completer(); | |
try { | |
Process.run('git', ['cherry', into, branch]).then((ProcessResult r) { | |
if (r.exitCode == 0) { | |
var merged = r.stdout.split('\n').every((String line) { | |
// The output is "- sha1" when the commit has been merged | |
return line.isEmpty || line[0] == '-'; | |
}); | |
completer.complete(merged); | |
} else { | |
completer.completeError("git cherry exit code: ${r.exitCode}"); | |
} | |
}); | |
} catch (e) { | |
completer.completeError(e); | |
} | |
return completer.future; | |
} | |
bool archiveBranch(String branch) { | |
ProcessResult res; | |
bool status = true; | |
// Create a tag (archive the branch) | |
res = Process.runSync('git', ['tag', 'archive/$branch', branch]); | |
status = status && res.exitCode == 0; | |
// Delete the local branch | |
res = Process.runSync('git', ['branch', '-D', branch]); | |
status = status && res.exitCode == 0; | |
if (REMOTE != null && REMOTE.isNotEmpty) { | |
// Delete the remote branch | |
res = Process.runSync('git', ['push', REMOTE, ':$branch']); | |
status = status && res.exitCode == 0; | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment