kieranpotts / dotfiles

A baseline, extensible UNIX user configuration.
MIT License
1 stars 0 forks source link

Git redo alias, undoes the last undo operation #219

Open kieranpotts opened 1 year ago

kieranpotts commented 1 year ago

A git redo operation could restore the contents of the "--SAVEPOINT--" commit produced by the git undo operation to the working directory. This would probably need to be constrained to working only if undo was the last operation, as we'd expect the "--SAVEPOINT--" commit to be in a certain position, probably HEAD@{1}.

The following solution is a WIP and does not yet work as expected. We need a reliable way of picking up the most recent commit with the message "--SAVEPOINT--" and then checking its location in the history

git reflog --grep="--SAVEPOINT--" seems to be our best bet, but it returns some other commits, and we need to capture the first line that includes "commit: --SAVEPOINT--".

redo =  "! f() { \
  MSG=$(git show -s --format=%B HEAD@{1}); \
  if [ "${MSG}" != "--SAVEPOINT--" ]; then \
    echo \"Cannot redo last undo operation, use 'git reflog' to recover your work\" >&2; \
    exit 1; \
  fi; \
  git cherry-pick HEAD@{1}; \
  git reset HEAD@{1} --mixed; \
}; f"