From ab9df836457744014ad44f0dc0151e9090fcc4c0 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Wed, 16 Jun 2021 15:39:02 -0700 Subject: [PATCH] Retry dev/snippets git status, deflake snippets generator (#84728) --- dev/snippets/lib/main.dart | 45 ++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/dev/snippets/lib/main.dart b/dev/snippets/lib/main.dart index e8a49cb115..a9d2dd6ade 100644 --- a/dev/snippets/lib/main.dart +++ b/dev/snippets/lib/main.dart @@ -21,19 +21,46 @@ const String _kTemplateOption = 'template'; const String _kTypeOption = 'type'; const String _kShowDartPad = 'dartpad'; +class GitStatusFailed implements Exception { + GitStatusFailed(this.gitResult); + + final ProcessResult gitResult; + + @override + String toString() => 'git status exited with a non-zero exit code: ${gitResult.exitCode}:\n${gitResult.stderr}\n${gitResult.stdout}'; +} + String getChannelName() { final RegExp gitBranchRegexp = RegExp(r'^## (?.*)'); - final ProcessResult gitResult = Process.runSync('git', ['status', '-b', '--porcelain'], environment: { - 'GIT_TRACE': '2', - 'GIT_TRACE_SETUP': '2', - }, includeParentEnvironment: true); - if (gitResult.exitCode != 0) - throw 'git status exit with non-zero exit code: ${gitResult.exitCode}: ${gitResult.stderr}'; - final RegExpMatch? gitBranchMatch = gitBranchRegexp.firstMatch( - (gitResult.stdout as String).trim().split('\n').first); + final ProcessResult gitResult = Process.runSync('git', ['status', '-b', '--porcelain'], + environment: { + 'GIT_TRACE': '2', + 'GIT_TRACE_SETUP': '2' + }, + includeParentEnvironment: true + ); + if (gitResult.exitCode != 0) { + throw GitStatusFailed(gitResult); + } + final RegExpMatch? gitBranchMatch = gitBranchRegexp.firstMatch((gitResult.stdout as String).trim().split('\n').first); return gitBranchMatch == null ? '' : gitBranchMatch.namedGroup('branch')!.split('...').first; } +// This is a hack to workaround the fact that git status inexplicably fails +// (random non-zero error code) about 2% of the time. +String getChannelNameWithRetries() { + int retryCount = 0; + while(retryCount < 2) { + try { + return getChannelName(); + } on GitStatusFailed catch (e) { + retryCount += 1; + stderr.write('git status failed, retrying ($retryCount)\nError report:\n$e'); + } + } + return getChannelName(); +} + /// Generates snippet dartdoc output for a given input, and creates any sample /// applications needed by the snippet. void main(List argList) { @@ -185,7 +212,7 @@ void main(List argList) { ? int.tryParse(environment['SOURCE_LINE']!) : null, 'id': id.join('.'), - 'channel': getChannelName(), + 'channel': getChannelNameWithRetries(), 'serial': serial, 'package': packageName, 'library': libraryName,