{"thread":{"id":"14631","subject":"[FYI PATCH] Added a git search and replace command","startedAt":"2008-07-23T15:36:06Z","lastAt":"2008-07-23T15:36:06Z","messageCount":1,"participants":["Sverre Rabbelier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"84531","messageId":"1216827366-13587-1-git-send-email-srabbelier@gmail.com","threadId":"14631","inReplyTo":null,"subject":"[FYI PATCH] Added a git search and replace command","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2008-07-23T15:36:06Z","receivedAt":"2008-07-23T15:36:06Z","isPatch":true,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"A simple script that uses 'git ls-files' and xargs to\nsearch and replace all the specified files.\n---\n\nNot meant for inclusion, just a simple script I wrote up\nwhen I wanted to change a function name in my repository\nbut didn't want to figure out which magic argument to find\ndoes what I want. Another advantage of using git ls-files\nis that it will prevent that any unrevertable changes will\nbe made (since the script checks if the tree is dirty or\nnot). I'm sure there's plenty of room for improvement here,\nit's probably not portable at all either, so comments are\nwelcome :).\n\n .gitignore |    1 +\n Makefile   |    1 +\n git-sar.sh |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 63 insertions(+), 0 deletions(-)\n create mode 100755 git-sar.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex a213e8e..451cb93 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -108,6 +108,7 @@ git-rev-list\n git-rev-parse\n git-revert\n git-rm\n+git-sar\n git-send-email\n git-send-pack\n git-sh-setup\ndiff --git a/Makefile b/Makefile\nindex b01cf1c..979e9ea 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -252,6 +252,7 @@ SCRIPT_SH += git-sh-setup.sh\n SCRIPT_SH += git-stash.sh\n SCRIPT_SH += git-submodule.sh\n SCRIPT_SH += git-web--browse.sh\n+SCRIPT_SH += git-sar.sh\n \n SCRIPT_PERL += git-add--interactive.perl\n SCRIPT_PERL += git-archimport.perl\ndiff --git a/git-sar.sh b/git-sar.sh\nnew file mode 100755\nindex 0000000..db6317b\n--- /dev/null\n+++ b/git-sar.sh\n@@ -0,0 +1,61 @@\n+#!/bin/bash\n+\n+do_sed () {\n+  sed \"$2\" $3 > $3.replaced\n+  mv $3.replaced $3\n+}\n+\n+do_git_find() {\n+  # Sanity check\n+  if is_dirty_tree\n+  then\n+    echo \"Refusing to work on a dirty tree\"\n+  fi\n+\n+  files=`git ls-files \"$1\"`\n+  if test -z \"$files\"\n+  then\n+    echo \"Your pathspec did not match any files.\"\n+    exit 1\n+  fi\n+  echo \"$files\" | xargs -n 1 git-sar --replace $2\n+}\n+\n+is_dirty_tree () {\n+  `git diff --quiet`\n+  test $? -ne 0\n+}\n+\n+do_show_usage() {\n+  echo \"usage: git-sar pathspec sed\"\n+  exit 128\n+}\n+\n+do_main() {\n+  # Verify argument size\n+  if test \"$#\" -le 1 -o \"$#\" -ge 4\n+  then\n+    do_show_usage\n+  fi\n+\n+  # Two argument form\n+  if test \"$#\" -eq 2\n+  then\n+    do_git_find \"$@\"\n+  fi\n+\n+  # Three argument form, we're calling ourselves through sed\n+  if test \"$#\" -eq 3\n+  then\n+    # Double check if the user typoed or if we are indeed meta-calling\n+    if test \"$1\" != \"--replace\"\n+    then\n+      do_show_usage\n+    fi\n+\n+    do_sed \"$@\"\n+  fi\n+}\n+\n+do_main \"$@\"\n+\n-- \n1.5.6.4.570.g052e.dirty\n"}]}