commitFile function

Future<void> commitFile(
  1. Directory testDir,
  2. String fileName, {
  3. String message = 'Commit Message',
  4. bool stage = true,
  5. bool ammend = false,
})

Commit the file with a name in the test directory

Implementation

Future<void> commitFile(
  Directory testDir,
  String fileName, {
  String message = 'Commit Message',
  bool stage = true,
  bool ammend = false,
}) async {
  // Nothing changed, nothing to commit. Amending would rewrite the last
  // commit all the same, so it asks first. A plain commit does not: git
  // refuses it when nothing has changed, and only then the question is
  // asked — one git process less for every commit that goes through.
  Future<bool> nothingHasChanged() async =>
      (await modifiedFiles(testDir)).isEmpty;

  if (ammend && await nothingHasChanged()) {
    return;
  }

  if (stage) {
    final staged = await ggRunProcess('git', [
      'add',
      fileName,
    ], workingDirectory: testDir.path);

    if (staged.exitCode != 0) {
      if (!ammend && await nothingHasChanged()) {
        return;
      }
      _throw('Could not stage $fileName', staged);
    }
  }

  final result2 = await ggRunProcess('git', [
    // git looks after every commit whether the repository needs tidying
    // up, in a process of its own that may outlive the test. A test
    // repository never needs it.
    '-c',
    'maintenance.auto=false',
    'commit',
    '-m',
    message,
    if (ammend) '--amend',
  ], workingDirectory: testDir.path);

  if (result2.exitCode != 0) {
    if (!ammend && await nothingHasChanged()) {
      return;
    }
    _throw('Could not commit $fileName', result2);
  }
}