stashToCleanState function

Future<bool> stashToCleanState({
  1. String? cwd,
  2. String? message,
})

Stash all changes (including untracked) to return git to a clean state.

Implementation

Future<bool> stashToCleanState({String? cwd, String? message}) async {
  try {
    final stashMessage =
        message ?? 'Neomage auto-stash - ${DateTime.now().toIso8601String()}';

    final fileStatus = await getFileStatus(cwd: cwd);
    if (fileStatus.untracked.isNotEmpty) {
      final addResult = await _runGit([
        'add',
        ...fileStatus.untracked,
      ], cwd: cwd);
      if (addResult.code != 0) return false;
    }

    final result = await _runGit([
      'stash',
      'push',
      '--message',
      stashMessage,
    ], cwd: cwd);
    return result.code == 0;
  } catch (_) {
    return false;
  }
}