undoFileChange function

Future<bool> undoFileChange(
  1. String filePath
)

Undo a file change by restoring from backup.

Implementation

Future<bool> undoFileChange(String filePath) async {
  // Find the most recent backup
  final dir = File(filePath).parent;
  final baseName = p.basename(filePath);
  final backups = <File>[];

  await for (final entity in dir.list()) {
    if (entity is File) {
      final name = p.basename(entity.path);
      if (name.startsWith('$baseName.bak.')) {
        backups.add(entity);
      }
    }
  }

  if (backups.isEmpty) return false;

  // Sort by timestamp (newest first)
  backups.sort((a, b) => b.path.compareTo(a.path));
  final latest = backups.first;

  // Restore
  await latest.copy(filePath);
  await latest.delete();
  return true;
}