git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] undo and redo

From
CBCarl Baldwin <cnb@fc.hp.com>
Date
Aug 24, 2005, 18:10 UTC
Message-ID
<20050824181004.GA18790@hpsvcnb.fc.hp.com>
In-Reply-To
<20050824172339.GA7083@hpsvcnb.fc.hp.com>

Oops. I forgot to actually exit from the script if git-diff-files is non-empty.

Also, looking at it now, I don't think keeping undo information in a stack is the right thing. But keeping more than just one would be good. Oh well, my first shot is never perfect. ;-)

Carl
On Wed, Aug 24, 2005 at 11:23:39AM -0600, Carl Baldwin wrote:
Show 34 quoted lines
> Hello,
> 
> So, one thing that I liked about GNU Arch when I tried it out was the
> ability to undo and redo changes in the local working copy.  I decided
> to try to do this with git.  What I have is preliminary.  I'm sure it
> could use some work.
> 
> So, I started with the assumption that all changes in the working copy
> have been updated to the cache.  My scripts check this (with
> git-diff-files) and abort if this is not the case.
> 
> Undo calls git-write-tree to write the changes to the object store.  It
> stores that tree's hash and the current HEAD's tree's hash in a file.
> Then it reverts the working copy to HEAD.
> 
> Redo grabs these two trees from the file, does git-write-tree to produce
> a third tree and merges the three using the old HEAD's tree as the base
> of the merge.  This way, new commits can happen and the local copy can
> be modified since the undo and it should still work assuming no
> conflicts emerge.
> 
> Attached are the two scripts.  Comments and criticism are welcome.
> 
> Cheers,
> Carl
> 
> -- 
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>  Carl Baldwin                        Systems VLSI Laboratory
>  Hewlett Packard Company
>  MS 88                               work: 970 898-1523
>  3404 E. Harmony Rd.                 work: Carl.N.Baldwin@hp.com
>  Fort Collins, CO 80525              home: Carl@ecBaldwin.net
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Show 28 quoted lines
> #!/bin/sh
> 
> . git-sh-setup-script || die "Not a git archive"
> 
> if [ -n "$(git-diff-files)" ]; then
>     echo The following files should be updated!
>     echo
>     git-diff-files | awk '{print $6}'
> fi
> 
> undostack=$GIT_DIR/undostack
> 
> if [ ! -s $undostack ]; then
>     echo "No undo information in $undostack"
> else
>     # Read the top of the stack
>     basetree=$(cat $undostack | tail -n 2 | head -n 1)
>     redotree=$(cat $undostack | tail -n 1)
> 
>     # Pop the stack
>     cat $undostack | head -n -2 > $undostack.tmp
>     mv $undostack{.tmp,}
> 
>     currenttree=$(git-write-tree)
> 
>     git-read-tree -u -m $basetree $currenttree $redotree
>     git-merge-cache git-merge-one-file-script -a
> fi
Show 28 quoted lines
> #!/bin/sh
> 
> . git-sh-setup-script || die "Not a git archive"
> 
> if [ -n "$(git-diff-files)" ]; then
>     echo The following files should be updated!
>     echo
>     git-diff-files | awk '{print $6}'
> fi
> 
> undostack=$GIT_DIR/undostack
> 
> headtree=$(git-cat-file commit $(cat $GIT_DIR/HEAD) | head -n 1 | sed -e 's/tree //')
> undotree=$(git-write-tree)
> 
> if [ $headtree == $undotree ]; then
>     echo There are no changes to undo.
> else
>     {
>        echo $headtree
>        echo $undotree
>     } >> $undostack
> 
>     echo Saved current state as tree $undotree.
>     echo Reverting to HEAD, $headtree...
> 
>     git-checkout-script -f
> fi
-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Carl Baldwin                        Systems VLSI Laboratory
 Hewlett Packard Company
 MS 88                               work: 970 898-1523
 3404 E. Harmony Rd.                 work: Carl.N.Baldwin@hp.com
 Fort Collins, CO 80525              home: Carl@ecBaldwin.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Previous: Carl BaldwinNext: Linus Torvalds
Message 2 of 21 in “[RFC] undo and redo”
  1. Carl BaldwinAug 24, 2005
  2. Carl BaldwinAug 24, 2005
  3. Linus TorvaldsAug 24, 2005
  4. Carl BaldwinAug 24, 2005
  5. Daniel BarkalowAug 24, 2005
  6. Carl BaldwinAug 24, 2005
  7. Daniel BarkalowAug 24, 2005
  8. Junio C HamanoAug 24, 2005
  9. Carl BaldwinAug 25, 2005
  10. Junio C HamanoAug 25, 2005
  11. Carl BaldwinAug 25, 2005
  12. Kalle ValoAug 25, 2005
  13. Kirby C. BohlingAug 25, 2005
  14. Junio C HamanoAug 25, 2005
  15. Kirby C. BohlingAug 25, 2005
  16. Carl BaldwinAug 25, 2005
  17. Carl BaldwinAug 25, 2005
  18. Kirby C. BohlingAug 25, 2005
  19. Carl BaldwinAug 25, 2005
  20. Junio C HamanoAug 24, 2005
  21. Carl BaldwinAug 24, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.