createBackupStash method
Implementation
Future<String?> createBackupStash() async {
// ensure there are staged files
final staged =
await Process.run('git', ['diff', '--cached', '--name-only']);
final hasFiles = switch (staged.stdout) {
final String files => files.trim().isNotEmpty,
_ => false,
};
if (!hasFiles) {
return null;
}
final result = await Process.run('git', [
'stash',
'create',
backupStashMessage,
]);
final hash = switch (result.stdout) {
final String hash => hash.trim(),
_ => null,
};
if (hash == null) {
logger
..err('Failed to create stash')
..detail('Error: ${result.stderr}');
throw Exception('Failed to create stash');
}
// dart is running too fast for git to cache the stash,
// resulting in a error throwing race condition
await Future<void>.delayed(const Duration(milliseconds: 100));
final storeResult = await Process.run(
'git',
[
'stash',
'store',
'--message',
'"$_stashMessage"',
hash,
],
);
if (storeResult.exitCode != 0) {
logger
..err('Failed to store stash')
..detail('Error: ${storeResult.stderr}');
throw Exception('Failed to store stash');
}
return hash;
}