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

[PATCH] mergetool: factor out common code

From
Theodore Ts'o <tytso@mit.edu>
Date
Mar 29, 2007, 15:44 UTC
Message-ID
<11751830654112-git-send-email-tytso@mit.edu>
In-Reply-To
<11751830651361-git-send-email-tytso@mit.edu>

Create common function check_unchanged(), save_backup() and remove_backup().

Also fix some minor whitespace issues while we're at it.
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
---
 git-mergetool.sh |   80 +++++++++++++++++++++++++-----------------------------
 1 files changed, 37 insertions(+), 43 deletions(-)
diff --git a/git-mergetool.sh b/git-mergetool.sh
index 8a87f5e..aeb32ef 100755
--- a/git-mergetool.sh
+++ b/git-mergetool.sh
@@ -46,7 +46,7 @@ describe_file () {
 
     printf "    "
     if test -z "$mode"; then
-	printf  "'$path' was deleted"
+	printf "'$path' was deleted"
     elif is_symlink "$mode" ; then
 	printf "'$path' is a symlink containing '"
 	cat "$file"
@@ -108,12 +108,40 @@ resolve_deleted_merge () {
 	done
 }
 
+check_unchanged () {
+    if test "$path" -nt "$BACKUP" ; then
+	status=0;
+    else
+	while true; do
+	    echo "$path seems unchanged."
+	    printf "Was the merge successful? [y/n] "
+	    read answer < /dev/tty
+	    case "$answer" in
+		y*|Y*) status=0; break ;;
+		n*|N*) status=1; break ;;
+	    esac
+	done
+    fi
+}
+
+save_backup () {
+    if test "$status" -eq 0; then
+	mv -- "$BACKUP" "$path.orig"
+    fi
+}
+
+remove_backup () {
+    if test "$status" -eq 0; then
+	rm "$BACKUP"
+    fi
+}
+
 merge_file () {
     path="$1"
 
     f=`git-ls-files -u -- "$path"`
     if test -z "$f" ; then
-        if test ! -f "$path" ; then
+	if test ! -f "$path" ; then
 	    echo "$path: file not found"
 	else
 	    echo "$path: file does not need merging"
@@ -169,9 +197,7 @@ merge_file () {
 		    -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
 	    fi
 	    status=$?
-	    if test "$status" -eq 0; then
-		rm "$BACKUP"
-	    fi
+	    remove_backup
 	    ;;
 	tkdiff)
 	    if base_present ; then
@@ -180,29 +206,13 @@ merge_file () {
 		tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
 	    fi
 	    status=$?
-	    if test "$status" -eq 0; then
-		mv -- "$BACKUP" "$path.orig"
-	    fi
+	    save_backup
 	    ;;
 	meld|vimdiff)
 	    touch "$BACKUP"
 	    $merge_tool -- "$LOCAL" "$path" "$REMOTE"
-	    if test "$path" -nt "$BACKUP" ; then
-		status=0;
-	    else
-		while true; do
-		    echo "$path seems unchanged."
-		    printf "Was the merge successful? [y/n] "
-		    read answer < /dev/tty
-		    case "$answer" in
-			y*|Y*) status=0; break ;;
-			n*|N*) status=1; break ;;
-		    esac
-		done
-	    fi
-	    if test "$status" -eq 0; then
-		mv -- "$BACKUP" "$path.orig"
-	    fi
+	    check_unchanged
+	    save_backup
 	    ;;
 	xxdiff)
 	    touch "$BACKUP"
@@ -219,22 +229,8 @@ merge_file () {
 		    -R 'Accel.SearchForward: "Ctrl-G"' \
 		    --merged-file "$path" -- "$LOCAL" "$REMOTE"
 	    fi
-	    if test "$path" -nt "$BACKUP" ; then
-		status=0;
-	    else
-		while true; do
-		    echo "$path seems unchanged."
-		    printf "Was the merge successful? [y/n] "
-		    read answer < /dev/tty
-		    case "$answer" in
-			y*|Y*) status=0; break ;;
-			n*|N*) status=1; break ;;
-		    esac
-		done
-	    fi
-	    if test "$status" -eq 0; then
-		mv -- "$BACKUP" "$path.orig"
-	    fi
+	    check_unchanged
+	    save_backup
 	    ;;
 	emerge)
 	    if base_present ; then
@@ -243,9 +239,7 @@ merge_file () {
 		emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
 	    fi
 	    status=$?
-	    if test "$status" -eq 0; then
-		mv -- "$BACKUP" "$path.orig"
-	    fi
+	    save_backup
 	    ;;
     esac
     if test "$status" -ne 0; then
-- 
1.5.1.rc2.1.g8afe-dirty
Previous: Theodore Ts'oNext: Theodore Ts'o
Message 7 of 19 in “[GIT PULL] git mergetool fixes”
  1. Theodore Ts'oMar 29, 2007
  2. Fix minor formatting issue in man page for git-mergetoolTheodore Ts'o, Mar 29, 2007
  3. mergetool: Replace use of "echo -n" with printf(3) to be more portableTheodore Ts'o, Mar 29, 2007
  4. mergetool: Don't error out in the merge case where the local file is deletedTheodore Ts'o, Mar 29, 2007
  5. mergetool: portability fix: don't assume true is in /binTheodore Ts'o, Mar 29, 2007
  6. mergetool: portability fix: don't use reserved word functionTheodore Ts'o, Mar 29, 2007
  7. mergetool: factor out common codeTheodore Ts'o, Mar 29, 2007
  8. mergetool: Remove spurious error message if merge.tool config option not setTheodore Ts'o, Mar 29, 2007
  9. mergetool: Fix abort command when resolving symlinks and deleted filesTheodore Ts'o, Mar 29, 2007
  10. mergetool: Add support for Apple Mac OS X's opendiff commandTheodore Ts'o, Mar 29, 2007
  11. mergetool: Make git-rm quiet when resolving a deleted file conflictTheodore Ts'o, Mar 29, 2007
  12. mergetool: Clean up description of files and prompts for merge resolutionsTheodore Ts'o, Mar 29, 2007
  13. Matthias LederhoferMar 29, 2007
  14. Theodore TsoMar 30, 2007
  15. Randal L. SchwartzMar 30, 2007
  16. Matthias LederhoferMar 29, 2007
  17. Theodore TsoMar 29, 2007
  18. Junio C HamanoMar 29, 2007
  19. Theodore TsoMar 29, 2007

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.