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

[FYI PATCH] Added a git search and replace command

From
Sverre Rabbelier <srabbelier@gmail.com>
Date
Jul 23, 2008, 15:36 UTC
Message-ID
<1216827366-13587-1-git-send-email-srabbelier@gmail.com>

A simple script that uses 'git ls-files' and xargs to search and replace all the specified files. ---

Not meant for inclusion, just a simple script I wrote up when I wanted to change a function name in my repository but didn't want to figure out which magic argument to find does what I want. Another advantage of using git ls-files is that it will prevent that any unrevertable changes will be made (since the script checks if the tree is dirty or not). I'm sure there's plenty of room for improvement here, it's probably not portable at all either, so comments are welcome :).

 .gitignore |    1 +
 Makefile   |    1 +
 git-sar.sh |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 0 deletions(-)
 create mode 100755 git-sar.sh
diff --git a/.gitignore b/.gitignore
index a213e8e..451cb93 100644
--- a/.gitignore
+++ b/.gitignore
@@ -108,6 +108,7 @@ git-rev-list
 git-rev-parse
 git-revert
 git-rm
+git-sar
 git-send-email
 git-send-pack
 git-sh-setup
diff --git a/Makefile b/Makefile
index b01cf1c..979e9ea 100644
--- a/Makefile
+++ b/Makefile
@@ -252,6 +252,7 @@ SCRIPT_SH += git-sh-setup.sh
 SCRIPT_SH += git-stash.sh
 SCRIPT_SH += git-submodule.sh
 SCRIPT_SH += git-web--browse.sh
+SCRIPT_SH += git-sar.sh
 
 SCRIPT_PERL += git-add--interactive.perl
 SCRIPT_PERL += git-archimport.perl
diff --git a/git-sar.sh b/git-sar.sh
new file mode 100755
index 0000000..db6317b
--- /dev/null
+++ b/git-sar.sh
@@ -0,0 +1,61 @@
+#!/bin/bash
+
+do_sed () {
+  sed "$2" $3 > $3.replaced
+  mv $3.replaced $3
+}
+
+do_git_find() {
+  # Sanity check
+  if is_dirty_tree
+  then
+    echo "Refusing to work on a dirty tree"
+  fi
+
+  files=`git ls-files "$1"`
+  if test -z "$files"
+  then
+    echo "Your pathspec did not match any files."
+    exit 1
+  fi
+  echo "$files" | xargs -n 1 git-sar --replace $2
+}
+
+is_dirty_tree () {
+  `git diff --quiet`
+  test $? -ne 0
+}
+
+do_show_usage() {
+  echo "usage: git-sar pathspec sed"
+  exit 128
+}
+
+do_main() {
+  # Verify argument size
+  if test "$#" -le 1 -o "$#" -ge 4
+  then
+    do_show_usage
+  fi
+
+  # Two argument form
+  if test "$#" -eq 2
+  then
+    do_git_find "$@"
+  fi
+
+  # Three argument form, we're calling ourselves through sed
+  if test "$#" -eq 3
+  then
+    # Double check if the user typoed or if we are indeed meta-calling
+    if test "$1" != "--replace"
+    then
+      do_show_usage
+    fi
+
+    do_sed "$@"
+  fi
+}
+
+do_main "$@"
+
-- 
1.5.6.4.570.g052e.dirty
Message 1 of 1 in “Added a git search and replace command”
  1. Added a git search and replace commandSverre Rabbelier, Jul 23, 2008

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.