-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_git_repos.dart
85 lines (83 loc) · 3.14 KB
/
check_git_repos.dart
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
import 'dart:io';
const String onBranch = 'On branch ';
const String upToDate = 'Your branch is up to date with \'';
const String behind = 'Your branch is behind \'';
const String behindMiddle = '\' by ';
const String behindEnd = ' commits, and can be fast-forwarded.';
const String nothingToCommit = 'nothing to commit, working tree clean';
const String unstagedChanges = 'Changes not staged for commit:';
const String noCommits = 'No commits yet';
void main() {
Directory dir = Directory('<directory>');
for (FileSystemEntity file in dir.listSync()) {
if (file is File) {
print('File: ${file.path}');
} else if (file is Directory) {
if (!file.listSync().any((e) => e.path.endsWith('.git'))) {
print('directory ${file.path} has no git repo');
continue;
}
List<String> gitStatus =
Process.runSync('git', ['status'], workingDirectory: file.path)
.stdout
.replaceAll('\r\n', '\n')
.split('\n');
String branchNameLine = gitStatus.first;
if (!branchNameLine.startsWith(onBranch)) {
print(
'Failed to parse branch name line for directory ${file.path}: $branchNameLine');
continue;
}
String branch = branchNameLine.substring(onBranch.length);
if (branch != 'main' && branch != 'master') {
print('directory ${file.path} is on branch $branch');
}
String mainStatusLine = gitStatus[1];
if (mainStatusLine.startsWith(behind)) {
int endingQuote = mainStatusLine.lastIndexOf('\'');
String remoteName =
mainStatusLine.substring(behind.length, endingQuote);
int? commitCount = int.tryParse(mainStatusLine.substring(
endingQuote + behindMiddle.length,
mainStatusLine.lastIndexOf(behindEnd)));
if (commitCount == null) {
print(
'Failed to parse main status line for directory ${file.path}: $mainStatusLine');
continue;
}
print(
'directory ${file.path} is behind remote \'$remoteName\' by $commitCount commits');
continue;
}
if (mainStatusLine == '') {
String noCommitsLine = gitStatus[2];
if (noCommitsLine != noCommits) {
print(
'Failed to parse no commits line for directory ${file.path}: $noCommitsLine');
continue;
}
print('directory ${file.path} has no commits');
continue;
}
if (mainStatusLine == nothingToCommit || mainStatusLine == unstagedChanges) {
print('directory ${file.path} has no remote');
continue;
}
if (!mainStatusLine.startsWith(upToDate)) {
print(
'Failed to parse main status line for directory ${file.path}: $mainStatusLine');
continue;
}
String workingTreeStatusLine = gitStatus[3];
if (workingTreeStatusLine == unstagedChanges) {
print('directory ${file.path} has unstaged changes');
continue;
}
if (workingTreeStatusLine != nothingToCommit) {
print(
'Failed to parse working tree status line: $workingTreeStatusLine');
continue;
}
}
}
}