Merge pull request #129 from dart-lang/to-main

Rename master to main
diff --git a/bin/update_homebrew.dart b/bin/update_homebrew.dart
index 4816bf6..e052160 100644
--- a/bin/update_homebrew.dart
+++ b/bin/update_homebrew.dart
@@ -22,34 +22,25 @@
   final parser = ArgParser()
     ..addFlag('dry-run', abbr: 'n')
     ..addOption('revision', abbr: 'r')
-    ..addOption('channel', abbr: 'c', allowed: supportedChannels)
-    ..addOption('key', abbr: 'k');
+    ..addOption('channel', abbr: 'c', allowed: supportedChannels);
   final options = parser.parse(args);
   final dryRun = options['dry-run'] as bool;
-  final revision = options['revision'] as String;
-  final channel = options['channel'] as String;
-  if ([revision, channel].contains(null)) {
-    print(
-        "Usage: update_homebrew.dart -r version -c channel [-k ssh_key] [-n]\n"
-        "  ssh_key should allow pushes to $githubRepo on github");
+  final revision = options['revision'] as String?;
+  final channel = options['channel'] as String?;
+  if (revision == null || channel == null) {
+    print("Usage: update_homebrew.dart -r version -c channel [-n]");
     exitCode = 64;
     return;
   }
 
   final repository = Directory.current.path;
-  final key = options['key'] as String?;
-  final gitEnvironment = <String, String>{};
-  if (key != null) {
-    final sshWrapper =
-        Directory.current.uri.resolve('ssh_with_key').toFilePath();
-    gitEnvironment['GIT_SSH'] = sshWrapper;
-    gitEnvironment['SSH_KEY_PATH'] = key;
+  if (await writeHomebrewInfo(channel, revision, repository, dryRun)) {
+    await runGit(
+        ['commit', '-a', '-m', 'Updated $channel channel to version $revision'],
+        repository,
+        null,
+        dryRun);
+  } else {
+    print("Channel $channel is up to date at version $revision");
   }
-  await writeHomebrewInfo(channel, revision, repository, dryRun);
-  await runGit(
-      ['commit', '-a', '-m', 'Updated $channel branch to revision $revision'],
-      repository,
-      gitEnvironment,
-      dryRun);
-  await runGit(['push'], repository, gitEnvironment, dryRun);
 }
diff --git a/lib/src/formula.dart b/lib/src/formula.dart
index 679abec..f692f80 100644
--- a/lib/src/formula.dart
+++ b/lib/src/formula.dart
@@ -17,16 +17,11 @@
   //  sha256 "<hash>"
   var filesAndHashes = RegExp(
       'channels/$channel/release'
-      r'/(\d[\w\d\-\.]*)/sdk/([\w\d\-\.]+)\"\n(\s+)sha256 \"[\da-f]+\"',
+      r'/\d[\w\d\-\.]*/sdk/([\w\d\-\.]+)\"\n(\s+)sha256 \"[\da-f]+\"',
       multiLine: true);
   return contents.replaceAllMapped(filesAndHashes, (m) {
-    var currentVersion = m.group(1);
-    if (currentVersion == version) {
-      throw ArgumentError(
-          'Channel $channel is already at version $version in homebrew.');
-    }
-    var artifact = m.group(2);
-    var indent = m.group(3);
+    var artifact = m.group(1);
+    var indent = m.group(2);
     return 'channels/$channel/release/$version/sdk/$artifact"\n'
         '${indent}sha256 "${hashes[artifact]}"';
   });
diff --git a/lib/update_homebrew.dart b/lib/update_homebrew.dart
index 2352f07..b721aa0 100644
--- a/lib/update_homebrew.dart
+++ b/lib/update_homebrew.dart
@@ -25,29 +25,41 @@
 
 Iterable<String> get supportedChannels => formulaByChannel.keys;
 
-Future<void> writeHomebrewInfo(
+Future<bool> writeHomebrewInfo(
     String channel, String version, String repository, bool dryRun) async {
   var formula = File(p.join(repository, formulaByChannel[channel]));
   var contents = await formula.readAsString();
   var hashes = await _getHashes(channel, version);
   var updated = updateFormula(channel, contents, version, hashes);
-  if (dryRun) {
-    print(updated);
-  } else {
-    await formula.writeAsString(updated, flush: true);
+  bool changed = contents != updated;
+  if (changed) {
+    if (dryRun) {
+      print(updated);
+    } else {
+      await formula.writeAsString(updated, flush: true);
+    }
   }
+  return changed;
 }
 
 Future<void> runGit(List<String> args, String repository,
-    Map<String, String> gitEnvironment, bool dryRun) async {
+    Map<String, String>? gitEnvironment, bool dryRun) async {
   if (dryRun) {
     args = [args[0], '--dry-run', ...args.skip(1)];
   }
   print("git ${args.join(' ')}");
 
-  var result = await Process.run('git', args,
+  final result = await Process.run('git', args,
       workingDirectory: repository, environment: gitEnvironment);
 
-  print(result.stdout);
-  print(result.stderr);
+  if (result.stdout != "") {
+    print(result.stdout.trimRight());
+  }
+  if (result.stderr != "") {
+    print(result.stderr.trimRight());
+  }
+
+  if (result.exitCode != 0 && !dryRun /* the test doesn't write a file */) {
+    throw Exception("Command exited ${result.exitCode}: git ${args.join(' ')}");
+  }
 }