restoreBackup method
Restores the index and working tree to the sha snapshot.
Untracked files are left alone -- stash create does not capture them,
so reverting them would destroy work the snapshot cannot restore.
Implementation
Future<bool> restoreBackup(String sha) async {
// A stash commit records the working tree as its own tree and the index as
// its second parent, so restoring both takes two steps.
// 1. Index and working tree both become the snapshot's working tree.
final workingTree = await _git(['read-tree', '--reset', '-u', sha]);
if (workingTree.exitCode != 0) {
logger.detail(
'Failed to restore the working tree from $sha. '
'Error: ${workingTree.stderr}',
);
return false;
}
// 2. The index alone becomes the snapshot's index, leaving the working
// tree from step 1 in place.
final index = await _git(['read-tree', '--reset', '$sha^2']);
if (index.exitCode != 0) {
logger.detail(
'Failed to restore the index from $sha. Error: ${index.stderr}',
);
return false;
}
return true;
}