Skip to contentSkip to conversation

cogito: Add cg-undo command

PATCH In review Matt Porter 2 participants 4 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cogito: Add cg-undo command

Adds a cg-undo command which takes a commit ID and resets HEAD to the parent of the commit ID...refreshing the tree. This undoes a single commit or a series of commits.

Makefile (mode:100644 sha1:6c282aeebe86ecee9e634481b3d51fd53a582791) renamed from Makefile (mode:100644 sha1:6ae0afa0208a8f755d383281a6d049a4ef90fe63) +1 -1
@@ -47,7 +47,7 @@
 	cg-add cg-admin-lsobj cg-cancel cg-clone cg-commit cg-diff \
 	cg-export cg-help cg-init cg-log cg-ls cg-merge cg-mkpatch \
 	cg-patch cg-pull cg-branch-add cg-branch-ls cg-rm cg-seek cg-status \
-	cg-tag cg-tag-ls cg-update cg-Xlib
+	cg-tag cg-tag-ls cg-undo cg-update cg-Xlib
 
 COMMON=	read-cache.o
 
cg-help (mode:100755 sha1:c7dc8f3e03895374cd0dae544570a37a459c2466) renamed from cg-help (mode:100755 sha1:1f5d2d79b67490d44ce0f575ff9a4b80134ea47f) +1 -0
Index: cg-help
===================================================================
@@ -43,6 +43,7 @@
 	cg-status
 	cg-tag		TNAME [COMMIT_ID]
 	cg-tag-ls
+	cg-undo		[COMMIT_ID]
 	cg-update	[BNAME]
 	cg-version
 
cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) renamed from dev/null (tree:a1aff2a6748c0c0d08058c7d74503e724abc5d03) +20 -0
Index: cg-undo
===================================================================
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+#
+# Undo a commit or a series of commits
+# Copyright (C) Matt Porter, 2005
+#
+# Takes a commit ID which is the earliest commit to be
+# removed from the repository.
+
+. cg-Xlib
+
+PARENT=`git-cat-file commit $1 | grep parent | cut -f 2 -d " "`
+echo "Undo from $1 to current HEAD"
+echo "Reset HEAD to $PARENT"
+echo "$PARENT" > .git/HEAD
+git-read-tree -m "$PARENT" || {
+	echo >&2 "$PARENT: bad commit"
+	exit 1
+}
+git-checkout-cache -f -a
+git-update-cache --refresh
#1 lore raw

Dear diary, on Tue, May 03, 2005 at 07:06:25PM CEST, I got a letter where Matt Porter <[email protected]> told me that...

cg-help (mode:100755 sha1:c7dc8f3e03895374cd0dae544570a37a459c2466) line 46

Quoted 9 lines

Index: cg-help
===================================================================

cg-help (mode:100755 sha1:c7dc8f3e03895374cd0dae544570a37a459c2466) renamed from cg-help (mode:100755 sha1:1f5d2d79b67490d44ce0f575ff9a4b80134ea47f) +1 -0
@@ -43,6 +43,7 @@
 	cg-status
 	cg-tag		TNAME [COMMIT_ID]
 	cg-tag-ls
+	cg-undo		[COMMIT_ID]

It doesn't seem very optional now.

Quoted 9 lines
cg-help (mode:100755 sha1:c7dc8f3e03895374cd0dae544570a37a459c2466) renamed from cg-help (mode:100755 sha1:1f5d2d79b67490d44ce0f575ff9a4b80134ea47f) +0 -0
 	cg-update	[BNAME]
 	cg-version
 

Index: cg-undo
===================================================================

cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) renamed from dev/null (tree:a1aff2a6748c0c0d08058c7d74503e724abc5d03) +1 -0
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash

cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) line 11

Quoted 10 lines
cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) renamed from dev/null (tree:a1aff2a6748c0c0d08058c7d74503e724abc5d03) +10 -0
+#
+# Undo a commit or a series of commits
+# Copyright (C) Matt Porter, 2005
+#
+# Takes a commit ID which is the earliest commit to be
+# removed from the repository.
+
+. cg-Xlib
+
+PARENT=`git-cat-file commit $1 | grep parent | cut -f 2 -d " "`

What's wrong with parent-id?

cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) line 13

Quoted 2 lines
cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) renamed from dev/null (tree:a1aff2a6748c0c0d08058c7d74503e724abc5d03) +2 -0
+echo "Undo from $1 to current HEAD"
+echo "Reset HEAD to $PARENT"

You talk way too much, I think. I'd just do

	echo "Rewinding $HEAD -> $PARENT" >&2

cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) line 20

Quoted 7 lines
cg-undo (mode:100755 sha1:7fd6d89158fb5aeee42aa05a93f2c81884d9bd34) renamed from dev/null (tree:a1aff2a6748c0c0d08058c7d74503e724abc5d03) +7 -0
+echo "$PARENT" > .git/HEAD
+git-read-tree -m "$PARENT" || {
+	echo >&2 "$PARENT: bad commit"
+	exit 1
+}
+git-checkout-cache -f -a
+git-update-cache --refresh

You really don't want to do this if the tree has any local modifications.

Please make sure the commit you are rewinding to is an ancestor of your current HEAD (there should be something like separate cg-branch-rm for killing enemy branches).

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
#2 lore raw
Quoted 5 lines · Petr Baudis

Dear diary, on Tue, May 03, 2005 at 07:06:25PM CEST, I got a letter where Matt Porter <[email protected]> told me that...

Review

Quoted 1 line
(quoted diff) +1 -0
+	cg-undo		[COMMIT_ID]

It doesn't seem very optional now.

Yep, changed.

Quoted 3 lines
Quoted 1 line

+PARENT=git-cat-file commit $1 | grep parent | cut -f 2 -d " "

What's wrong with parent-id?

Missed it...changed to use parent-id.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+echo "Undo from $1 to current HEAD"
+echo "Reset HEAD to $PARENT"

You talk way too much, I think. I'd just do

	echo "Rewinding $HEAD -> $PARENT" >&2

Done.

Quoted 4 lines
Quoted 1 line

+git-update-cache --refresh

You really don't want to do this if the tree has any local modifications.

Ugh, yes. I added a check for this.

Quoted 3 lines

Please make sure the commit you are rewinding to is an ancestor of your current HEAD (there should be something like separate cg-branch-rm for killing enemy branches).

Added a check for this as well as verifying we have an argument.

Makefile (mode:100644 sha1:6c282aeebe86ecee9e634481b3d51fd53a582791) renamed from Makefile (mode:100644 sha1:6ae0afa0208a8f755d383281a6d049a4ef90fe63) +1 -1
@@ -47,7 +47,7 @@
 	cg-add cg-admin-lsobj cg-cancel cg-clone cg-commit cg-diff \
 	cg-export cg-help cg-init cg-log cg-ls cg-merge cg-mkpatch \
 	cg-patch cg-pull cg-branch-add cg-branch-ls cg-rm cg-seek cg-status \
-	cg-tag cg-tag-ls cg-update cg-Xlib
+	cg-tag cg-tag-ls cg-undo cg-update cg-Xlib
 
 COMMON=	read-cache.o
 
cg-help (mode:100755 sha1:1b75114ee90d2b3b9786fc4f14bf179feef54f87) renamed from cg-help (mode:100755 sha1:1f5d2d79b67490d44ce0f575ff9a4b80134ea47f) +1 -0
Index: cg-help
===================================================================
@@ -43,6 +43,7 @@
 	cg-status
 	cg-tag		TNAME [COMMIT_ID]
 	cg-tag-ls
+	cg-undo		COMMIT_ID
 	cg-update	[BNAME]
 	cg-version
 
cg-undo (mode:100644 sha1:7b1cb04c79731b137d88ebd05ee41553af7246d2) renamed from dev/null (tree:aa6233be6d1b8bf42797c409a7c23b50593afc99) +32 -0
Index: cg-undo
===================================================================
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+#
+# Undo a commit or a series of commits
+# Copyright (C) Matt Porter, 2005
+#
+# Takes a commit ID which is the earliest commit to be
+# removed from the repository.
+
+. cg-Xlib
+
+[ "$1" ] || die "usage: cg-undo COMMIT_ID"
+
+HEAD=$(commit-id) || exit 1;
+PARENT=$(parent-id "$1") || exit 1;
+
+git-rev-list $HEAD | grep -q $1 || {
+	echo >&2 "$1: not an ancestor of HEAD"
+	exit 1
+}
+[ "$(git-diff-files -s)" ] && git-update-cache --refresh
+if [ "$(git-diff-files -s)" ] || [ "$(git-diff-cache $(tree-id))" ]; then
+        die "Undo blocked: local changes"
+fi
+
+echo "Rewinding HEAD -> $PARENT" >&2
+echo "$PARENT" > .git/HEAD
+git-read-tree -m "$PARENT" || {
+	echo >&2 "$PARENT: bad commit"
+	exit 1
+}
+git-checkout-cache -f -a
+git-update-cache --refresh
#3 lore raw

Dear diary, on Wed, May 04, 2005 at 07:40:07AM CEST, I got a letter where Matt Porter <[email protected]> told me that...

Quoted 1 line

Thanks. I took it, hacked upon it for a while and then committed something similar but largely rewritten as cg-admin-uncommit. ;-)

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor