{"thread":{"id":"4393","subject":"[PATCH] Support for configurable git command aliases (v2)","startedAt":"2006-06-04T21:19:31Z","lastAt":"2006-06-07T03:21:30Z","messageCount":12,"participants":["Petr Baudis","Martin Mares","Junio C Hamano","Linus Torvalds","Martin Langhoff"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"21225","messageId":"20060604211931.10117.82695.stgit@machine.or.cz","threadId":"4393","inReplyTo":null,"subject":"[PATCH] Support for configurable git command aliases (v2)","fromName":"Petr Baudis","fromEmail":"pasky@suse.cz","sentAt":"2006-06-04T21:19:31Z","receivedAt":"2006-06-04T21:19:31Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"This patch adds support for configurable aliases for git commands -\n\"alias.WHATEVER = which ever\" will kick in when you do \"git WHATEVER\"\nand substitute WHATEVER with \"which ever\" (splitted to arguments at\nwhitespaces).\n\nThe second version does all the work in handle_aliases() which was\ninspired by Johannes Schindelin's patch.\n\nSigned-off-by: Petr Baudis <pasky@suse.cz>\n---\n\n Documentation/config.txt |    5 ++++\n Documentation/git.txt    |    3 ++\n git.c                    |   60 ++++++++++++++++++++++++++++++++++++++++++++--\n 3 files changed, 65 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex c861c6c..aaaa33d 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -91,6 +91,11 @@ core.warnAmbiguousRefs::\n \tIf true, git will warn you if the ref name you passed it is ambiguous\n \tand might match multiple refs in the .git/refs/ tree. True by default.\n \n+alias.*::\n+\tCommand aliases for the gitlink:git[1] command wrapper - e.g.\n+\tafter defining \"alias.last = cat-file commit HEAD\", the invocation\n+\t\"git last\" is equivalent to \"git cat-file commit HEAD\".\n+\n apply.whitespace::\n \tTells `git-apply` how to handle whitespaces, in the same way\n \tas the '--whitespace' option. See gitlink:git-apply[1].\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex 24ca55d..e474bdf 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -21,6 +21,9 @@ link:everyday.html[Everyday Git] for a u\n \"man git-commandname\" for documentation of each command.  CVS users may\n also want to read link:cvs-migration.html[CVS migration].\n \n+The COMMAND is either a name of a Git command (see below) or an alias\n+as defined in the configuration file (see gitlink:git-repo-config[1]).\n+\n OPTIONS\n -------\n --version::\ndiff --git a/git.c b/git.c\nindex bc463c9..50850fb 100644\n--- a/git.c\n+++ b/git.c\n@@ -10,6 +10,7 @@ #include <limits.h>\n #include <stdarg.h>\n #include \"git-compat-util.h\"\n #include \"exec_cmd.h\"\n+#include \"cache.h\" /* setup_git_directory_gently() */\n \n #include \"builtin.h\"\n \n@@ -88,13 +89,65 @@ static void handle_internal_command(int \n \t}\n }\n \n+static const char *cmd;\n+static char *cmdalias;\n+\n+int git_alias_config(const char *var, const char *value)\n+{\n+\tif (strncmp(var, \"alias.\", 6))\n+\t\treturn 0;\n+\tvar += /* strlen(\"alias.\") */ 6;\n+\tif (!strcmp(var, cmd))\n+\t\tcmdalias = strdup(value);\n+\treturn 0;\n+}\n+\n+void handle_alias(int *argc, const char ***argv)\n+{\n+\t/* XXX: We do a redundant git directory detection. */\n+\tint nongit = 0;\n+\tconst char *subdir;\n+\n+\tif (!isatty(1))\n+\t\treturn;\n+\n+\tsubdir = setup_git_directory_gently(&nongit);\n+\tif (!nongit) {\n+\t\tgit_config(git_alias_config);\n+\t\tif (cmdalias) {\n+\t\t\t/* More than the worst case: */\n+\t\t\tconst char **argv2 = malloc((strlen(cmdalias) + *argc) * sizeof(char*));\n+\t\t\tint argc2 = 0, i = 1;\n+\n+\t\t\twhile (cmdalias && *cmdalias) {\n+\t\t\t\targv2[argc2++] = strsep(&cmdalias, \" \\t\");\n+\t\t\t\tif (cmdalias)\n+\t\t\t\t\twhile (*cmdalias == ' ' || *cmdalias == '\\t')\n+\t\t\t\t\t\tcmdalias++;\n+\t\t\t}\n+\t\t\twhile (i < *argc) {\n+\t\t\t\targv2[argc2++] = (*argv)[i++];\n+\t\t\t}\n+\t\t\targv2[argc2] = NULL;\n+\t\t\t*argv = argv2;\n+\t\t\t*argc = argc2;\n+\t\t}\n+\t}\n+\n+\t/* Go back so that the commands start with clean table */\n+\tif (subdir)\n+\t\tchdir(subdir);\n+}\n+\n+\n int main(int argc, const char **argv, char **envp)\n {\n-\tconst char *cmd = argv[0];\n-\tchar *slash = strrchr(cmd, '/');\n+\tchar *slash = strrchr(argv[0], '/');\n \tchar git_command[PATH_MAX + 1];\n \tconst char *exec_path = NULL;\n \n+\tcmd = argv[0];\n+\n \t/*\n \t * Take the basename of argv[0] as the command\n \t * name, and the dirname as the default exec_path\n@@ -121,6 +174,7 @@ int main(int argc, const char **argv, ch\n \tif (!strncmp(cmd, \"git-\", 4)) {\n \t\tcmd += 4;\n \t\targv[0] = cmd;\n+\t\thandle_alias(&argc, &argv);\n \t\thandle_internal_command(argc, argv, envp);\n \t\tdie(\"cannot handle %s internally\", cmd);\n \t}\n@@ -178,7 +232,7 @@ int main(int argc, const char **argv, ch\n \texec_path = git_exec_path();\n \tprepend_to_path(exec_path, strlen(exec_path));\n \n-\t/* See if it's an internal command */\n+\thandle_alias(&argc, &argv);\n \thandle_internal_command(argc, argv, envp);\n \n \t/* .. then try the external ones */\n"},{"id":"21226","messageId":"20060604212050.GV10488@pasky.or.cz","threadId":"4393","inReplyTo":"20060604211931.10117.82695.stgit@machine.or.cz","subject":"Re: [PATCH] Support for configurable git command aliases (v2)","fromName":"Petr Baudis","fromEmail":"pasky@ucw.cz","sentAt":"2006-06-04T21:20:51Z","receivedAt":"2006-06-04T21:20:51Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Sun, Jun 04, 2006 at 11:19:31PM CEST, I got a letter\nwhere Petr Baudis <pasky@suse.cz> said that...\n> This patch adds support for configurable aliases for git commands -\n> \"alias.WHATEVER = which ever\" will kick in when you do \"git WHATEVER\"\n> and substitute WHATEVER with \"which ever\" (splitted to arguments at\n> whitespaces).\n> \n> The second version does all the work in handle_aliases() which was\n> inspired by Johannes Schindelin's patch.\n> \n> Signed-off-by: Petr Baudis <pasky@suse.cz>\n\nAnd I forgot to mention that it also adds the interactivity test\nrequested by Janek - aliases are now interpreted only when stdout is a\ntty.\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nA person is just about as big as the things that make them angry.\n"},{"id":"21229","messageId":"mj+md-20060604.221036.15619.albireo@ucw.cz","threadId":"4393","inReplyTo":"20060604212050.GV10488@pasky.or.cz","subject":"Re: [PATCH] Support for configurable git command aliases (v2)","fromName":"Martin Mares","fromEmail":"mj@ucw.cz","sentAt":"2006-06-04T22:11:14Z","receivedAt":"2006-06-04T22:11:14Z","isPatch":true,"sender":{"key":"mj@ucw.cz","avatar":null},"body":"Hello, world!\n\n> And I forgot to mention that it also adds the interactivity test\n> requested by Janek - aliases are now interpreted only when stdout is a\n> tty.\n\nDoes this really make sense? Why should an alias stop working\nif I happen to redirect its output? Or am I missing something?\n\n\t\t\t\tHave a nice fortnight\n-- \nMartin `MJ' Mares   <mj@ucw.cz>   http://atrey.karlin.mff.cuni.cz/~mj/\nFaculty of Math and Physics, Charles University, Prague, Czech Rep., Earth\nGod is real, unless declared integer.\n"},{"id":"21230","messageId":"20060604221930.GW10488@pasky.or.cz","threadId":"4393","inReplyTo":"mj+md-20060604.221036.15619.albireo@ucw.cz","subject":"Re: [PATCH] Support for configurable git command aliases (v2)","fromName":"Petr Baudis","fromEmail":"pasky@ucw.cz","sentAt":"2006-06-04T22:19:30Z","receivedAt":"2006-06-04T22:19:30Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"  Hi,\n\nDear diary, on Mon, Jun 05, 2006 at 12:11:14AM CEST, I got a letter\nwhere Martin Mares <mj@ucw.cz> said that...\n> > And I forgot to mention that it also adds the interactivity test\n> > requested by Janek - aliases are now interpreted only when stdout is a\n> > tty.\n> \n> Does this really make sense? Why should an alias stop working\n> if I happen to redirect its output? Or am I missing something?\n\n  make\n\n\t[alias]\n\tlog = log --pretty=raw\n\nand then any script that works on git log output might get very\nconfused.\n\n  Then again, as pointed out on IRC you might get very confused as well\nif you do git log | less. Besides, this is not going to help you with\naliases like commit = commit -a.\n\n  So, some other possibilities are to:\n\n  (i) Test stdin. Even in scripts, stdin is frequently terminal, but you\nmight add </dev/null after each git invocation and get a serious case of\nRSI.\n\n  (ii) Add a --no-alias git argument. This way lies madness, too.\n\n  (iii) Check a $GIT_NO_ALIAS environment variable. This might work\nbest, after all. Opinions? Or some other clever idea?\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nA person is just about as big as the things that make them angry.\n"},{"id":"21232","messageId":"7v8xoc8s1t.fsf@assigned-by-dhcp.cox.net","threadId":"4393","inReplyTo":"20060604221930.GW10488@pasky.or.cz","subject":"Re: [PATCH] Support for configurable git command aliases (v2)","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-06-04T22:38:54Z","receivedAt":"2006-06-04T22:38:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Petr Baudis <pasky@ucw.cz> writes:\n\n>\n>   Then again, as pointed out on IRC you might get very confused as well\n> if you do git log | less. Besides, this is not going to help you with\n> aliases like commit = commit -a.\n>\n>   So, some other possibilities are to:\n>\n>   (i) Test stdin. Even in scripts, stdin is frequently terminal, but you\n> might add </dev/null after each git invocation and get a serious case of\n> RSI.\n>\n>   (ii) Add a --no-alias git argument. This way lies madness, too.\n>\n>   (iii) Check a $GIT_NO_ALIAS environment variable. This might work\n> best, after all. Opinions? Or some other clever idea?\n\nPerhaps the simplest:\n\n    (iv) Refuse/ignore an alias that hides existing command, and\n         train users to write portable scripts by not using\n         aliases.  E.g. \"alias.log = log --pretty=raw\" is\n         ignored, and you would do \"alias.l = log --pretty=raw\"\n         instead.\n"},{"id":"21236","messageId":"Pine.LNX.4.64.0606041621010.5498@g5.osdl.org","threadId":"4393","inReplyTo":"20060604212050.GV10488@pasky.or.cz","subject":"Re: [PATCH] Support for configurable git command aliases (v2)","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-06-04T23:31:54Z","receivedAt":"2006-06-04T23:31:54Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Sun, 4 Jun 2006, Petr Baudis wrote:\n> \n> And I forgot to mention that it also adds the interactivity test\n> requested by Janek - aliases are now interpreted only when stdout is a\n> tty.\n\nI don't think that's a good test.\n\nThe fact is, I do\n\n\tgit diff | less -S\n\nall the time, and if I start doing aliases, I'd expect them to work the \nsame regardless of whether I piped the output to \"less\" or not.\n\nAlso, a lot of scripts have stdout going to the regular stdout, so I don't \nthink it's even a sufficient test for scripting anyway.\n\nSo I would suggest some other way to suppress aliases if we need it, not \nbased on \"isatty()\" and frields.\n\nBut I suspect that the easiest solution is to just disallow aliases of \nreal built-ins. I realize it could be cool, but the fact is, it's also \nextremely confusing if \"git diff\" does something else than what it's \nsupposed to do, and it _will_ break scripts.\n\nThe alternatives is to\n - not do aliases for the \"git-xyzzy\" format\n - and add a special environment flag (\"GIT_NO_ALIASES\") to allow scripts \n   to easily disable aliases (and add a \"export GIT_NO_ALIASES\" line to \n   the git-sh-setup.sh script)\n\nHmm?\n\n\t\tLinus\n"},{"id":"21356","messageId":"7vodx5n8en.fsf_-_@assigned-by-dhcp.cox.net","threadId":"4393","inReplyTo":"Pine.LNX.4.64.0606041621010.5498@g5.osdl.org","subject":"[PATCH/RFC] \"git --less cmd\" to page anywhere","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-06-06T23:58:40Z","receivedAt":"2006-06-06T23:58:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This allows you to say:\n\n\tgit --less diff v2.6.16-rc5..\n\nto pipe the output of any git command to your pager.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n  Linus Torvalds <torvalds@osdl.org> writes:\n\n  > On Sun, 4 Jun 2006, Petr Baudis wrote:\n  >> \n  >> And I forgot to mention that it also adds the interactivity test\n  >> requested by Janek - aliases are now interpreted only when stdout is a\n  >> tty.\n  >\n  > I don't think that's a good test.\n  >\n  > The fact is, I do\n  >\n  > \tgit diff | less -S\n  >\n  > all the time,...\n\n  This is not a serious patch, since I suspect it would obviously\n  not make much sense to say \"git --less commit\" or somesuch,\n  but it was fun to do.\n\n git.c |    5 +++++\n 1 files changed, 5 insertions(+), 0 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex bc463c9..c52da8c 100644\n--- a/git.c\n+++ b/git.c\n@@ -10,6 +10,7 @@ #include <limits.h>\n #include <stdarg.h>\n #include \"git-compat-util.h\"\n #include \"exec_cmd.h\"\n+#include \"cache.h\"\n \n #include \"builtin.h\"\n \n@@ -162,6 +163,10 @@ int main(int argc, const char **argv, ch\n \t\t\tputs(git_exec_path());\n \t\t\texit(0);\n \t\t}\n+\t\tif (!strcmp(cmd, \"less\")) {\n+\t\t\tsetup_pager();\n+\t\t\tcontinue;\n+\t\t}\n \t\tcmd_usage(0, NULL, NULL);\n \t}\n \targv[0] = cmd;\n"},{"id":"21357","messageId":"Pine.LNX.4.64.0606061703380.5498@g5.osdl.org","threadId":"4393","inReplyTo":"7vodx5n8en.fsf_-_@assigned-by-dhcp.cox.net","subject":"Re: [PATCH/RFC] \"git --less cmd\" to page anywhere","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-06-07T00:05:59Z","receivedAt":"2006-06-07T00:05:59Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Tue, 6 Jun 2006, Junio C Hamano wrote:\n>\n> This allows you to say:\n> \n> \tgit --less diff v2.6.16-rc5..\n\nI've seriously considered something like that, although you chose a pretty \nstrange - and long - flag.\n\nI was thinking something like \"git -p log -p\" (the first \"-p\" is for \n\"paginate\" - think \"dir/p\" in old DOS times, but we could claim it is for \n\"pager\" so that people don't laugh at us)\n\n\t\tLinus\n"},{"id":"21358","messageId":"20060607000816.GY10488@pasky.or.cz","threadId":"4393","inReplyTo":"Pine.LNX.4.64.0606061703380.5498@g5.osdl.org","subject":"Re: [PATCH/RFC] \"git --less cmd\" to page anywhere","fromName":"Petr Baudis","fromEmail":"pasky@suse.cz","sentAt":"2006-06-07T00:08:16Z","receivedAt":"2006-06-07T00:08:16Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Wed, Jun 07, 2006 at 02:05:59AM CEST, I got a letter\nwhere Linus Torvalds <torvalds@osdl.org> said that...\n> On Tue, 6 Jun 2006, Junio C Hamano wrote:\n> >\n> > This allows you to say:\n> > \n> > \tgit --less diff v2.6.16-rc5..\n> \n> I've seriously considered something like that, although you chose a pretty \n> strange - and long - flag.\n> \n> I was thinking something like \"git -p log -p\" (the first \"-p\" is for \n> \"paginate\" - think \"dir/p\" in old DOS times, but we could claim it is for \n> \"pager\" so that people don't laugh at us)\n\nActually, you made a case in the misty past that cg-log should just\nstuff things through a pager by default and I ended up finding that\nextremely convenient - what is the reason git log does not do the same?\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nA person is just about as big as the things that make them angry.\n"},{"id":"21360","messageId":"Pine.LNX.4.64.0606061711000.5498@g5.osdl.org","threadId":"4393","inReplyTo":"20060607000816.GY10488@pasky.or.cz","subject":"Re: [PATCH/RFC] \"git --less cmd\" to page anywhere","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-06-07T00:12:50Z","receivedAt":"2006-06-07T00:12:50Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Wed, 7 Jun 2006, Petr Baudis wrote:\n> \n> Actually, you made a case in the misty past that cg-log should just\n> stuff things through a pager by default and I ended up finding that\n> extremely convenient - what is the reason git log does not do the same?\n\n\"git log\" does, \"git diff\" does not (and yeah, I just chose a bad example, \nI meant \"git -p diff -p\")\n\nFor \"git diff\", pagination by default is definitely not the right thing to \ndo, but it's something you often end up wanting.\n\n\t\tLinus\n"},{"id":"21366","messageId":"46a038f90606061929i68073677i686a3c1934ed64b1@mail.gmail.com","threadId":"4393","inReplyTo":"Pine.LNX.4.64.0606061711000.5498@g5.osdl.org","subject":"Re: [PATCH/RFC] \"git --less cmd\" to page anywhere","fromName":"Martin Langhoff","fromEmail":"martin.langhoff@gmail.com","sentAt":"2006-06-07T02:29:54Z","receivedAt":"2006-06-07T02:29:54Z","isPatch":true,"sender":{"key":"martin.langhoff@gmail.com","avatar":"https://gravatar.com/avatar/1e3f311b6c4c15836501901ca58f8c0b0667246488084ba524d8bc9867e22fd9?d=mp&s=160"},"body":"On 6/7/06, Linus Torvalds <torvalds@osdl.org> wrote:\n> For \"git diff\", pagination by default is definitely not the right thing to\n> do, but it's something you often end up wanting.\n\nWhy is it not a good default? Unless I expect it to be shorter than\nthe terminal, I always tack \"| less\" after it. Except when I tack \">\nfoo.patch\" and then the auto-pager knows what to do anyway.\n\ncheers,\n\n\n\nmartin\n"},{"id":"21367","messageId":"Pine.LNX.4.64.0606062011030.5498@g5.osdl.org","threadId":"4393","inReplyTo":"46a038f90606061929i68073677i686a3c1934ed64b1@mail.gmail.com","subject":"Re: [PATCH/RFC] \"git --less cmd\" to page anywhere","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-06-07T03:21:30Z","receivedAt":"2006-06-07T03:21:30Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Wed, 7 Jun 2006, Martin Langhoff wrote:\n> \n> Why is it not a good default? Unless I expect it to be shorter than\n> the terminal, I always tack \"| less\" after it. Except when I tack \">\n> foo.patch\" and then the auto-pager knows what to do anyway.\n\nI often tack \"| less\" after it in some situations, but in other situations \nI _never_ do.\n\nI often use \"git diff\" as a way to see that my tree is clean. Sure, I \ncould do \"git status\", but if it's not clean, it's usually something \nsmall, so I want to know what I changed last. At that point, a default \npager would be very annoying.\n\nThe \"more\" behaviour (which only paginates unto the end) might work for \nme (ie the \"git diff\" auto-pager would act like \"more\" or \"less -EX\")\n\nI dunno.\n\n\t\tLinus\n"}]}