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

[TOY PATCH] git-resurrect: find traces of a branch name and resurrect it

From
Thomas Rast <trast@student.ethz.ch>
Date
Jan 18, 2009, 01:38 UTC
Message-ID
<1232242703-19086-1-git-send-email-trast@student.ethz.ch>
In-Reply-To
<alpine.DEB.1.00.0901161213370.3586@pacific.mpi-cbg.de>

Add a tool 'git resurrect <branch>...' that tries to find traces of each <branch> in the HEAD reflog and, optionally, all merge commits in the repository. It can then resurrect the branch, pointing it at the most recent of all candidate commits found.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---

So here's a slightly more polished version so gmane can keep it forever. Thanks for the sed trick! I was too lazy to add more options, but at least there's a "fast" and a "complete" mode.

 Makefile         |    1 +
 git-resurrect.sh |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 0 deletions(-)
 create mode 100755 git-resurrect.sh
diff --git a/Makefile b/Makefile
index 2b873fa..87cb539 100644
--- a/Makefile
+++ b/Makefile
@@ -260,6 +260,7 @@ SCRIPT_SH += git-merge-resolve.sh
 SCRIPT_SH += git-mergetool.sh
 SCRIPT_SH += git-parse-remote.sh
 SCRIPT_SH += git-pull.sh
+SCRIPT_SH += git-resurrect.sh
 SCRIPT_SH += git-quiltimport.sh
 SCRIPT_SH += git-rebase--interactive.sh
 SCRIPT_SH += git-rebase.sh
diff --git a/git-resurrect.sh b/git-resurrect.sh
new file mode 100755
index 0000000..6d5a0c7
--- /dev/null
+++ b/git-resurrect.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+USAGE="git resurrect [-m | --merges] [-n | --dry-run] <name>..."
+LONG_USAGE="git-resurrect attempts to find traces of a branch tip called <name>,
+and tries to resurrect it.  Currently, the reflog is searched for
+checkout and merge messages.  With --merges, the history of all refs
+is scanned for merge commit subjects, which is rather slow but allows
+you to resurrect other people's topic branches."
+
+. git-sh-setup
+cd_to_toplevel
+
+OPTIONS_SPEC="\
+git resurrect [-m | --merges] [-n | --dry-run] <name>...
+--
+m,merges             also scan merges (slow)
+n,dry-run            don't recreate the branch"
+
+test "$#" = 0 && usage
+
+eval "$(echo "$OPTIONS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+
+search_reflog () {
+        sed -n 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \
+                < .git/logs/HEAD
+}
+
+search_reflog_merges () {
+        sed -n 's~^[^ ]* \([^ ]*\) .*\tmerge '"$1"':~\1~p' \
+                < .git/logs/HEAD
+}
+
+search_merges () {
+	git rev-list --pretty=tformat:"%h %p:%s" --all |
+	grep "Merge branch.*'$branch'.*into" |
+	while read sha rest; do
+		parents="$(echo "$rest" | cut -d: -f1)"
+		case "$parents" in
+		    *' '*' '*)
+			warn "$branch took part in octopus merge $sha"
+			warn "check manually!"
+			;;
+		    *' '*)
+			echo "$parents" | cut -d' ' -f2
+			;;
+		esac
+	done
+}
+
+search_merge_targets () {
+	git rev-list --pretty=tformat:"%h %s" --all |
+	grep "Merge branch '[^']*' into $branch$" |
+	cut -d' ' -f1
+}
+
+dry_run=
+scan_merges=
+
+while test "$#" != 0; do
+	case "$1" in
+	    -n|--dry-run)
+		dry_run=t
+		;;
+	    -m|--merges)
+		scan_merges=t
+		;;
+	    --)
+		shift
+		break
+		;;
+	    *)
+		usage
+		;;
+	esac
+	shift
+done
+
+for branch in "$@"; do
+	candidates="$(search_reflog $1; search_reflog_merges $1)"
+	if test ! -z "$scan_merges"; then
+		candidates="$candidates $(search_merges $1; search_merge_targets $1)"
+	fi
+
+	candidates="$(git rev-parse $candidates | sort -u)"
+
+	if test -z "$candidates"; then
+		echo "** No candidates for $branch found **"
+		test -z "$scan_merges" && echo "(maybe try again with -m)"
+	else
+		echo "** Candidates for $branch **"
+		for cmt in $candidates; do
+			git --no-pager log --pretty=oneline --abbrev-commit -1 $cmt
+		done
+
+		newest="$(git rev-list -1 $candidates)"
+		if test ! -z "$dry_run"; then
+			printf "Most recent: "
+			git --no-pager log -1 --pretty=tformat:"%h %s" $newest
+		elif ! git rev-parse --verify --quiet $branch >/dev/null; then
+			printf "** Restoring $branch to "
+			git --no-pager log -1 --pretty=tformat:"%h %s" $newest
+			git branch $branch $newest
+		else
+			printf "Most recent: "
+			git --no-pager log -1 --pretty=tformat:"%h %s" $newest
+			echo "** $branch already exists, doing nothing"
+		fi
+	fi
+done
-- 
1.6.1.320.gd5dca.dirty
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 32 of 46 in “checkout: implement "-" shortcut name for last branch”
  1. checkout: implement "-" shortcut name for last branchThomas Rast, Jan 15, 2009
  2. checkout: implement "-" shortcut name for last branchThomas Rast, Jan 15, 2009
  3. Johannes SixtJan 15, 2009
  4. Johannes SchindelinJan 15, 2009
  5. Thomas RastJan 15, 2009
  6. Johannes SchindelinJan 15, 2009
  7. Johannes SchindelinJan 15, 2009
  8. Junio C HamanoJan 15, 2009
  9. Johannes SchindelinJan 15, 2009
  10. revision walker: include a detached HEAD in --allJohannes Schindelin, Jan 16, 2009
  11. Santi BéjarJan 16, 2009
  12. Johannes SchindelinJan 16, 2009
  13. David KastrupJan 16, 2009
  14. Santi BéjarJan 16, 2009
  15. Santi BéjarJan 16, 2009
  16. Junio C HamanoJan 18, 2009
  17. Junio C HamanoJan 18, 2009
  18. Johannes SchindelinJan 18, 2009
  19. Johannes SchindelinJan 18, 2009
  20. Johan HerlandJan 15, 2009
  21. Johannes SchindelinJan 15, 2009
  22. Junio C HamanoJan 15, 2009
  23. Junio C HamanoJan 15, 2009
  24. Johannes SchindelinJan 16, 2009
  25. Johannes SchindelinJan 15, 2009
  26. Thomas RastJan 15, 2009
  27. Johannes SchindelinJan 15, 2009
  28. Thomas RastJan 15, 2009
  29. Johannes SchindelinJan 15, 2009
  30. Thomas RastJan 16, 2009
  31. Johannes SchindelinJan 16, 2009
  32. git-resurrect: find traces of a branch name and resurrect itThomas Rast, Jan 18, 2009
  33. Johannes SchindelinJan 18, 2009
  34. Thomas RastJan 20, 2009
  35. Boyd Stephen Smith Jr.Jan 20, 2009
  36. Boyd Stephen Smith Jr.Jan 20, 2009
  37. contrib git-resurrect: find traces of a branch name and resurrect itThomas Rast, Jan 23, 2009
  38. Boyd Stephen Smith Jr.Jan 23, 2009
  39. Thomas RastJan 26, 2009
  40. contrib git-resurrect: find traces of a branch name and resurrect itThomas Rast, Jan 26, 2009
  41. Junio C HamanoJan 27, 2009
  42. Thomas RastJan 30, 2009
  43. contrib git-resurrect: find traces of a branch name and resurrect itThomas Rast, Feb 1, 2009
  44. Junio C HamanoFeb 2, 2009
  45. contrib git-resurrect: find traces of a branch name and resurrect itThomas Rast, Feb 4, 2009
  46. Junio C HamanoFeb 5, 2009

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.