push function
Push
Implementation
Future push() async {
try {
File configFile = File('.git/config');
if (configFile.existsSync()) {
String configContent = configFile.readAsStringSync();
RegExp remotePattern = RegExp(r'\[remote "(.*?)"\]');
Iterable<Match> matches = remotePattern.allMatches(configContent);
List<String> remotes = matches.map((match) => match.group(1)!).toList();
if (remotes.isNotEmpty) {
for (String remote in remotes) {
// Push
printMessage('推送至 $remote\n');
final processGitPush = await Process.start(
'git',
['push', remote],
workingDirectory: projectPath,
runInShell: true,
);
processGitPush.stdout.transform(utf8.decoder).listen((data) => printMessage(data));
processGitPush.stderr.transform(utf8.decoder).listen((data) => printErrorMessage(data));
await processGitPush.exitCode;
printSuccessMessage('推送至 $remote 成功\n');
// Push Tag
printMessage('创建 Git Tag\n');
final processGitTag = await Process.start(
'git',
['tag', 'v${VersionManage().version}'],
workingDirectory: projectPath,
runInShell: true,
);
processGitTag.stdout.transform(utf8.decoder).listen((data) => printMessage(data));
processGitTag.stderr.transform(utf8.decoder).listen((data) => printErrorMessage(data));
await processGitTag.exitCode;
printMessage('推送 Tag 至 $remote\n');
final processGitPushTag = await Process.start(
'git',
['push', remote, 'v${VersionManage().version}'],
workingDirectory: projectPath,
runInShell: true,
);
processGitPushTag.stdout.transform(utf8.decoder).listen((data) => printMessage(data));
processGitPushTag.stderr.transform(utf8.decoder).listen((data) => printErrorMessage(data));
await processGitPushTag.exitCode;
printSuccessMessage('推送 Tag 至 $remote 成功\n');
}
} else {
printErrorMessage('未找到任何 Remote');
}
} else {
printErrorMessage('.git/config 文件不存在。');
}
} catch (e) {
printErrorMessage('推送 Tag 至 Git 异常:$e');
}
}