{"thread":{"id":"25215","subject":"[RFC PATCH 0/2] submodule aware grep","startedAt":"2010-09-23T21:17:05Z","lastAt":"2010-09-24T17:10:13Z","messageCount":6,"participants":["Chris Packham","Heiko Voigt","Jens Lehmann"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"151403","messageId":"1285276627-7907-1-git-send-email-judge.packham@gmail.com","threadId":"25215","inReplyTo":null,"subject":"[RFC PATCH 0/2] submodule aware grep","fromName":"Chris Packham","fromEmail":"judge.packham@gmail.com","sentAt":"2010-09-23T21:17:05Z","receivedAt":"2010-09-23T21:17:05Z","isPatch":true,"sender":{"key":"judge.packham@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155667?v=4"},"body":"This series contains 2 RFC patches that both implement a grep feature for\nsubmodules.  The first patch is a self-contained script for contrib that should\nwork with most current git versions. The 2nd is basically the same\nimplementation but done as a proper git submodule command with some of the\nhelper code moved to git-sh-setup.sh\n\nThere are a couple of questions for this. Technically I'm making submodule \ngrep-aware, should I be making grep submodule-aware instead? I haven't looked \nat the grep code yet but I imagine its harder.\n\nShould I be putting the submodule_root* routines in git-sh-setup or should I be\nlooking at adding it to 'git rev parse' instead. I'm guessing eventually more \ncommands might want to be able to get at the base superproject.\n\nChris Packham (2):\n      contrib: add git-submodule-grep.sh\n      submodule: add grep command\n"},{"id":"151405","messageId":"1285276627-7907-2-git-send-email-judge.packham@gmail.com","threadId":"25215","inReplyTo":"1285276627-7907-1-git-send-email-judge.packham@gmail.com","subject":"[RFC PATCH 1/2] contrib: add git-submodule-grep.sh","fromName":"Chris Packham","fromEmail":"judge.packham@gmail.com","sentAt":"2010-09-23T21:17:06Z","receivedAt":"2010-09-23T21:17:06Z","isPatch":true,"sender":{"key":"judge.packham@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155667?v=4"},"body":"A simple wrapper around 'git grep' that is submodule aware.\n\nThe advantage of this over \"git submodule foreach 'git grep <pattern>'\" is\nthat the results are presented with a path relative to the current\ndirectory. Also this script will work from any subdirectory of any submodule\nin the current superproject.\n\nSigned-off-by: Chris Packham <judge.packham@gmail.com>\n---\n contrib/git-submodule-grep.sh |   35 +++++++++++++++++++++++++++++++++++\n 1 files changed, 35 insertions(+), 0 deletions(-)\n create mode 100755 contrib/git-submodule-grep.sh\n\ndiff --git a/contrib/git-submodule-grep.sh b/contrib/git-submodule-grep.sh\nnew file mode 100755\nindex 0000000..407fdb0\n--- /dev/null\n+++ b/contrib/git-submodule-grep.sh\n@@ -0,0 +1,35 @@\n+#!/bin/sh\n+# Copyright (c) 2010, Chris Packham\n+# Wrapper for git grep that is submodule aware\n+\n+SUBDIRECTORY_OK=\"yes\"\n+. git-sh-setup\n+\n+submodule_root_relative()\n+{\n+\tceiling=\"$HOME\"\n+\twhile test ! -e \"$cdup.gitmodules\"; do\n+\t\tcdup=\"$cdup../\"\n+\t\tif test \"$(cd $cdup && pwd)\" == \"$ceiling\"; then\n+\t\t\techo >&2 \"fatal: failed to find superproject root,\"\n+\t\t\techo >&2 \"       stopped searching at $ceiling.\"\n+\t\t\texit 1\n+\t\tfi\n+\tdone\n+\techo \"$cdup\"\n+}\n+\n+submodule_root()\n+{\n+\t(cd \"$(submodule_root_relative)\" && pwd)\n+}\n+\n+# we don't do anything with the arg but we should check that we\n+# have something to pass to git grep\n+test $# -lt 1 && die \"fatal: no pattern given.\"\n+\n+prefix=\"$(submodule_root_relative)\" || exit 1\n+(cd \"./$prefix\"; \\\n+\tgit submodule --quiet foreach \\\n+\t\t\"git --no-pager grep $@ | sed s\\\"|.*|$prefix\\$path/&|\\\" || true\") \\\n+\t\t\t| git_pager\n-- \n1.7.3.dirty\n"},{"id":"151404","messageId":"1285276627-7907-3-git-send-email-judge.packham@gmail.com","threadId":"25215","inReplyTo":"1285276627-7907-1-git-send-email-judge.packham@gmail.com","subject":"[RFC PATCH 2/2] submodule: add grep command","fromName":"Chris Packham","fromEmail":"judge.packham@gmail.com","sentAt":"2010-09-23T21:17:07Z","receivedAt":"2010-09-23T21:17:07Z","isPatch":true,"sender":{"key":"judge.packham@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155667?v=4"},"body":"This adds a 'grep' command to the 'git submodule' commands. Internally it is\nbasically a wrapper for \"git submodule foreach 'git grep <pattern>'\" but\ndoes have the advantage of supplying results with a path relative to the\ncurrent directory.\n\nUnlike contrib/git-submodule-grep.sh this can only be run from the root of a\nsubmodule or the root of the superproject.\n\nSigned-off-by: Chris Packham <judge.packham@gmail.com>\n---\n git-sh-setup.sh  |   19 +++++++++++++++++++\n git-submodule.sh |   41 +++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 58 insertions(+), 2 deletions(-)\n\ndiff --git a/git-sh-setup.sh b/git-sh-setup.sh\nindex 6131670..2a1d585 100644\n--- a/git-sh-setup.sh\n+++ b/git-sh-setup.sh\n@@ -145,6 +145,25 @@ require_work_tree () {\n \tdie \"fatal: $0 cannot be used without a working tree.\"\n }\n \n+submodule_root_relative()\n+{\n+\tceiling=\"$HOME\"\n+\twhile test ! -e \"$cdup.gitmodules\"; do\n+\t\tcdup=\"$cdup../\"\n+\t\tif test \"$(cd $cdup && pwd)\" == \"$ceiling\"; then\n+\t\t\techo >&2 \"fatal: failed to find superproject root.\"\n+\t\t\techo >&2 \"       stopped searching at $ceiling\".\n+\t\t\texit 1\n+\t\tfi\n+\tdone\n+    echo \"$cdup\"\n+}\n+\n+submodule_root()\n+{\n+    (cd \"$(submodule_root_relative)\" && pwd)\n+}\n+\n get_author_ident_from_commit () {\n \tpick_author_script='\n \t/^author /{\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex 9ebbab7..3edaa49 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -11,7 +11,8 @@ USAGE=\"[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <r\n    or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]\n    or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]\n    or: $dashless [--quiet] foreach [--recursive] <command>\n-   or: $dashless [--quiet] sync [--] [<path>...]\"\n+   or: $dashless [--quiet] sync [--] [<path>...]\n+   or: $dashless [--quiet] grep [--] [-n] [<pattern>]\"\n OPTIONS_SPEC=\n . git-sh-setup\n . git-parse-remote\n@@ -849,6 +850,42 @@ cmd_sync()\n \t\tfi\n \tdone\n }\n+#\n+# Search for a pattern within all submodules\n+# This is a fairly simple wrapper using foreach and git grep,\n+# arguments could be passed to git grep but for now we only\n+# understand -n\n+#\n+cmd_grep()\n+{\n+\tgrep_args=\"\"\n+\twhile test $# -ne 0\n+\tdo\n+\t\tcase \"$1\" in\n+\t\t-n)\n+\t\t\tgrep_args=\"$1\"\n+\t\t\tshift\n+\t\t\t;;\n+\t\t--)\n+\t\t\tshift\n+\t\t\tbreak\n+\t\t\t;;\n+\t\t-*)\n+\t\t\tusage\n+\t\t\t;;\n+\t\t*)\n+\t\t\tbreak\n+\t\t\t;;\n+\t\tesac\n+\tdone\n+\ttest $# -lt 1 && die \"fatal: no pattern given\"\n+\n+\tprefix=\"$(submodule_root_relative)\" || exit 1\n+\t(cd \"./$prefix\"; \\\n+\t\tcmd_foreach \"git --no-pager grep $grep_args -- $@ | \\\n+\t\t\tsed s\\\"|.*|$prefix\\$path/&|\\\" || true\") \\\n+\t\t\t\t| git_pager\n+}\n \n # This loop parses the command line arguments to find the\n # subcommand name to dispatch.  Parsing of the subcommand specific\n@@ -859,7 +896,7 @@ cmd_sync()\n while test $# != 0 && test -z \"$command\"\n do\n \tcase \"$1\" in\n-\tadd | foreach | init | update | status | summary | sync)\n+\tadd | foreach | init | update | status | summary | sync | grep)\n \t\tcommand=$1\n \t\t;;\n \t-q|--quiet)\n-- \n1.7.3.dirty\n"},{"id":"151477","messageId":"20100924134748.GA576@book.hvoigt.net","threadId":"25215","inReplyTo":"1285276627-7907-1-git-send-email-judge.packham@gmail.com","subject":"Re: [RFC PATCH 0/2] submodule aware grep","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2010-09-24T13:47:49Z","receivedAt":"2010-09-24T13:47:49Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"Hi,\n\nOn Thu, Sep 23, 2010 at 02:17:05PM -0700, Chris Packham wrote:\n> This series contains 2 RFC patches that both implement a grep feature for\n> submodules.  The first patch is a self-contained script for contrib that should\n> work with most current git versions. The 2nd is basically the same\n> implementation but done as a proper git submodule command with some of the\n> helper code moved to git-sh-setup.sh\n> \n> There are a couple of questions for this. Technically I'm making submodule \n> grep-aware, should I be making grep submodule-aware instead? I haven't looked \n> at the grep code yet but I imagine its harder.\n\nNice work! IMO it would be even nicer to have it as part of git grep.\nHave a look at Jens branch about submodule checkout:\n\nhttp://github.com/jlehmann/git-submod-enhancements (enhance_git_for_submodules)\n\nparticularly how checkout_submodule() is implemented. It forks a git\ncheckout inside the submodule. In a similar way you could fork a grep\nthere. Then you just have to teach the forked grep to prepend the\nsubmodules path.\n\nCheers Heiko\n"},{"id":"151504","messageId":"4C9CCCCC.4000909@gmail.com","threadId":"25215","inReplyTo":"20100924134748.GA576@book.hvoigt.net","subject":"Re: [RFC PATCH 0/2] submodule aware grep","fromName":"Chris Packham","fromEmail":"judge.packham@gmail.com","sentAt":"2010-09-24T16:07:40Z","receivedAt":"2010-09-24T16:07:40Z","isPatch":true,"sender":{"key":"judge.packham@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155667?v=4"},"body":"On 24/09/10 06:47, Heiko Voigt wrote:\n> Hi,\n> \n> On Thu, Sep 23, 2010 at 02:17:05PM -0700, Chris Packham wrote:\n>> This series contains 2 RFC patches that both implement a grep feature for\n>> submodules.  The first patch is a self-contained script for contrib that should\n>> work with most current git versions. The 2nd is basically the same\n>> implementation but done as a proper git submodule command with some of the\n>> helper code moved to git-sh-setup.sh\n>>\n>> There are a couple of questions for this. Technically I'm making submodule \n>> grep-aware, should I be making grep submodule-aware instead? I haven't looked \n>> at the grep code yet but I imagine its harder.\n> \n> Nice work! IMO it would be even nicer to have it as part of git grep.\n> Have a look at Jens branch about submodule checkout:\n> \n> http://github.com/jlehmann/git-submod-enhancements (enhance_git_for_submodules)\n> \n> particularly how checkout_submodule() is implemented. It forks a git\n> checkout inside the submodule. In a similar way you could fork a grep\n> there. Then you just have to teach the forked grep to prepend the\n> submodules path.\n> \n\nI'll look into it, from following Jens code it doesn't look too hard\n(like you say just fork and pass a text prefix). I'm currently doing my\ngit hacking on my main development machine so I should probably setup\nand environment where I can hack without affecting $dayjob work.\n\nIn the meantime I've got an updated patch for contrib if anyone is\ninterested (it just adds some grep options to be passed through).\n\nThanks,\nChris\n"},{"id":"151515","messageId":"4C9CDB75.9070405@web.de","threadId":"25215","inReplyTo":"20100924134748.GA576@book.hvoigt.net","subject":"Re: [RFC PATCH 0/2] submodule aware grep","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2010-09-24T17:10:13Z","receivedAt":"2010-09-24T17:10:13Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 24.09.2010 15:47, schrieb Heiko Voigt:\n>> There are a couple of questions for this. Technically I'm making submodule \n>> grep-aware, should I be making grep submodule-aware instead? I haven't looked \n>> at the grep code yet but I imagine its harder.\n> \n> Nice work! IMO it would be even nicer to have it as part of git grep.\n\nI like it too! And yes, me too thinks adding a \"--recursive\" option\nto \"git grep\" would make more sense than adding a \"grep\" option to\nthe \"git submodule\" script.\n"}]}