* [PATCH] Support for configurable git command aliases (v2)
@ 2006-06-04 21:19 Petr Baudis
2006-06-04 21:20 ` Petr Baudis
0 siblings, 1 reply; 12+ messages in thread
From: Petr Baudis @ 2006-06-04 21:19 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
This patch adds support for configurable aliases for git commands -
"alias.WHATEVER = which ever" will kick in when you do "git WHATEVER"
and substitute WHATEVER with "which ever" (splitted to arguments at
whitespaces).
The second version does all the work in handle_aliases() which was
inspired by Johannes Schindelin's patch.
Signed-off-by: Petr Baudis <pasky@suse.cz>
---
Documentation/config.txt | 5 ++++
Documentation/git.txt | 3 ++
git.c | 60 ++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index c861c6c..aaaa33d 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -91,6 +91,11 @@ core.warnAmbiguousRefs::
If true, git will warn you if the ref name you passed it is ambiguous
and might match multiple refs in the .git/refs/ tree. True by default.
+alias.*::
+ Command aliases for the gitlink:git[1] command wrapper - e.g.
+ after defining "alias.last = cat-file commit HEAD", the invocation
+ "git last" is equivalent to "git cat-file commit HEAD".
+
apply.whitespace::
Tells `git-apply` how to handle whitespaces, in the same way
as the '--whitespace' option. See gitlink:git-apply[1].
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 24ca55d..e474bdf 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -21,6 +21,9 @@ link:everyday.html[Everyday Git] for a u
"man git-commandname" for documentation of each command. CVS users may
also want to read link:cvs-migration.html[CVS migration].
+The COMMAND is either a name of a Git command (see below) or an alias
+as defined in the configuration file (see gitlink:git-repo-config[1]).
+
OPTIONS
-------
--version::
diff --git a/git.c b/git.c
index bc463c9..50850fb 100644
--- a/git.c
+++ b/git.c
@@ -10,6 +10,7 @@ #include <limits.h>
#include <stdarg.h>
#include "git-compat-util.h"
#include "exec_cmd.h"
+#include "cache.h" /* setup_git_directory_gently() */
#include "builtin.h"
@@ -88,13 +89,65 @@ static void handle_internal_command(int
}
}
+static const char *cmd;
+static char *cmdalias;
+
+int git_alias_config(const char *var, const char *value)
+{
+ if (strncmp(var, "alias.", 6))
+ return 0;
+ var += /* strlen("alias.") */ 6;
+ if (!strcmp(var, cmd))
+ cmdalias = strdup(value);
+ return 0;
+}
+
+void handle_alias(int *argc, const char ***argv)
+{
+ /* XXX: We do a redundant git directory detection. */
+ int nongit = 0;
+ const char *subdir;
+
+ if (!isatty(1))
+ return;
+
+ subdir = setup_git_directory_gently(&nongit);
+ if (!nongit) {
+ git_config(git_alias_config);
+ if (cmdalias) {
+ /* More than the worst case: */
+ const char **argv2 = malloc((strlen(cmdalias) + *argc) * sizeof(char*));
+ int argc2 = 0, i = 1;
+
+ while (cmdalias && *cmdalias) {
+ argv2[argc2++] = strsep(&cmdalias, " \t");
+ if (cmdalias)
+ while (*cmdalias == ' ' || *cmdalias == '\t')
+ cmdalias++;
+ }
+ while (i < *argc) {
+ argv2[argc2++] = (*argv)[i++];
+ }
+ argv2[argc2] = NULL;
+ *argv = argv2;
+ *argc = argc2;
+ }
+ }
+
+ /* Go back so that the commands start with clean table */
+ if (subdir)
+ chdir(subdir);
+}
+
+
int main(int argc, const char **argv, char **envp)
{
- const char *cmd = argv[0];
- char *slash = strrchr(cmd, '/');
+ char *slash = strrchr(argv[0], '/');
char git_command[PATH_MAX + 1];
const char *exec_path = NULL;
+ cmd = argv[0];
+
/*
* Take the basename of argv[0] as the command
* name, and the dirname as the default exec_path
@@ -121,6 +174,7 @@ int main(int argc, const char **argv, ch
if (!strncmp(cmd, "git-", 4)) {
cmd += 4;
argv[0] = cmd;
+ handle_alias(&argc, &argv);
handle_internal_command(argc, argv, envp);
die("cannot handle %s internally", cmd);
}
@@ -178,7 +232,7 @@ int main(int argc, const char **argv, ch
exec_path = git_exec_path();
prepend_to_path(exec_path, strlen(exec_path));
- /* See if it's an internal command */
+ handle_alias(&argc, &argv);
handle_internal_command(argc, argv, envp);
/* .. then try the external ones */
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] Support for configurable git command aliases (v2)
2006-06-04 21:19 [PATCH] Support for configurable git command aliases (v2) Petr Baudis
@ 2006-06-04 21:20 ` Petr Baudis
2006-06-04 22:11 ` Martin Mares
2006-06-04 23:31 ` Linus Torvalds
0 siblings, 2 replies; 12+ messages in thread
From: Petr Baudis @ 2006-06-04 21:20 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
Dear diary, on Sun, Jun 04, 2006 at 11:19:31PM CEST, I got a letter
where Petr Baudis <pasky@suse.cz> said that...
> This patch adds support for configurable aliases for git commands -
> "alias.WHATEVER = which ever" will kick in when you do "git WHATEVER"
> and substitute WHATEVER with "which ever" (splitted to arguments at
> whitespaces).
>
> The second version does all the work in handle_aliases() which was
> inspired by Johannes Schindelin's patch.
>
> Signed-off-by: Petr Baudis <pasky@suse.cz>
And I forgot to mention that it also adds the interactivity test
requested by Janek - aliases are now interpreted only when stdout is a
tty.
--
Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] Support for configurable git command aliases (v2)
2006-06-04 21:20 ` Petr Baudis
@ 2006-06-04 22:11 ` Martin Mares
2006-06-04 22:19 ` Petr Baudis
2006-06-04 23:31 ` Linus Torvalds
1 sibling, 1 reply; 12+ messages in thread
From: Martin Mares @ 2006-06-04 22:11 UTC (permalink / raw)
To: Petr Baudis; +Cc: Junio C Hamano, git
Hello, world!
> And I forgot to mention that it also adds the interactivity test
> requested by Janek - aliases are now interpreted only when stdout is a
> tty.
Does this really make sense? Why should an alias stop working
if I happen to redirect its output? Or am I missing something?
Have a nice fortnight
--
Martin `MJ' Mares <mj@ucw.cz> http://atrey.karlin.mff.cuni.cz/~mj/
Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth
God is real, unless declared integer.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] Support for configurable git command aliases (v2)
2006-06-04 22:11 ` Martin Mares
@ 2006-06-04 22:19 ` Petr Baudis
2006-06-04 22:38 ` Junio C Hamano
0 siblings, 1 reply; 12+ messages in thread
From: Petr Baudis @ 2006-06-04 22:19 UTC (permalink / raw)
To: Martin Mares; +Cc: Junio C Hamano, git
Hi,
Dear diary, on Mon, Jun 05, 2006 at 12:11:14AM CEST, I got a letter
where Martin Mares <mj@ucw.cz> said that...
> > And I forgot to mention that it also adds the interactivity test
> > requested by Janek - aliases are now interpreted only when stdout is a
> > tty.
>
> Does this really make sense? Why should an alias stop working
> if I happen to redirect its output? Or am I missing something?
make
[alias]
log = log --pretty=raw
and then any script that works on git log output might get very
confused.
Then again, as pointed out on IRC you might get very confused as well
if you do git log | less. Besides, this is not going to help you with
aliases like commit = commit -a.
So, some other possibilities are to:
(i) Test stdin. Even in scripts, stdin is frequently terminal, but you
might add </dev/null after each git invocation and get a serious case of
RSI.
(ii) Add a --no-alias git argument. This way lies madness, too.
(iii) Check a $GIT_NO_ALIAS environment variable. This might work
best, after all. Opinions? Or some other clever idea?
--
Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] Support for configurable git command aliases (v2)
2006-06-04 22:19 ` Petr Baudis
@ 2006-06-04 22:38 ` Junio C Hamano
0 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2006-06-04 22:38 UTC (permalink / raw)
To: Petr Baudis; +Cc: git
Petr Baudis <pasky@ucw.cz> writes:
>
> Then again, as pointed out on IRC you might get very confused as well
> if you do git log | less. Besides, this is not going to help you with
> aliases like commit = commit -a.
>
> So, some other possibilities are to:
>
> (i) Test stdin. Even in scripts, stdin is frequently terminal, but you
> might add </dev/null after each git invocation and get a serious case of
> RSI.
>
> (ii) Add a --no-alias git argument. This way lies madness, too.
>
> (iii) Check a $GIT_NO_ALIAS environment variable. This might work
> best, after all. Opinions? Or some other clever idea?
Perhaps the simplest:
(iv) Refuse/ignore an alias that hides existing command, and
train users to write portable scripts by not using
aliases. E.g. "alias.log = log --pretty=raw" is
ignored, and you would do "alias.l = log --pretty=raw"
instead.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] Support for configurable git command aliases (v2)
2006-06-04 21:20 ` Petr Baudis
2006-06-04 22:11 ` Martin Mares
@ 2006-06-04 23:31 ` Linus Torvalds
2006-06-06 23:58 ` [PATCH/RFC] "git --less cmd" to page anywhere Junio C Hamano
1 sibling, 1 reply; 12+ messages in thread
From: Linus Torvalds @ 2006-06-04 23:31 UTC (permalink / raw)
To: Petr Baudis; +Cc: Junio C Hamano, git
On Sun, 4 Jun 2006, Petr Baudis wrote:
>
> And I forgot to mention that it also adds the interactivity test
> requested by Janek - aliases are now interpreted only when stdout is a
> tty.
I don't think that's a good test.
The fact is, I do
git diff | less -S
all the time, and if I start doing aliases, I'd expect them to work the
same regardless of whether I piped the output to "less" or not.
Also, a lot of scripts have stdout going to the regular stdout, so I don't
think it's even a sufficient test for scripting anyway.
So I would suggest some other way to suppress aliases if we need it, not
based on "isatty()" and frields.
But I suspect that the easiest solution is to just disallow aliases of
real built-ins. I realize it could be cool, but the fact is, it's also
extremely confusing if "git diff" does something else than what it's
supposed to do, and it _will_ break scripts.
The alternatives is to
- not do aliases for the "git-xyzzy" format
- and add a special environment flag ("GIT_NO_ALIASES") to allow scripts
to easily disable aliases (and add a "export GIT_NO_ALIASES" line to
the git-sh-setup.sh script)
Hmm?
Linus
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH/RFC] "git --less cmd" to page anywhere
2006-06-04 23:31 ` Linus Torvalds
@ 2006-06-06 23:58 ` Junio C Hamano
2006-06-07 0:05 ` Linus Torvalds
0 siblings, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2006-06-06 23:58 UTC (permalink / raw)
To: Linus Torvalds; +Cc: git
This allows you to say:
git --less diff v2.6.16-rc5..
to pipe the output of any git command to your pager.
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
Linus Torvalds <torvalds@osdl.org> writes:
> On Sun, 4 Jun 2006, Petr Baudis wrote:
>>
>> And I forgot to mention that it also adds the interactivity test
>> requested by Janek - aliases are now interpreted only when stdout is a
>> tty.
>
> I don't think that's a good test.
>
> The fact is, I do
>
> git diff | less -S
>
> all the time,...
This is not a serious patch, since I suspect it would obviously
not make much sense to say "git --less commit" or somesuch,
but it was fun to do.
git.c | 5 +++++
1 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/git.c b/git.c
index bc463c9..c52da8c 100644
--- a/git.c
+++ b/git.c
@@ -10,6 +10,7 @@ #include <limits.h>
#include <stdarg.h>
#include "git-compat-util.h"
#include "exec_cmd.h"
+#include "cache.h"
#include "builtin.h"
@@ -162,6 +163,10 @@ int main(int argc, const char **argv, ch
puts(git_exec_path());
exit(0);
}
+ if (!strcmp(cmd, "less")) {
+ setup_pager();
+ continue;
+ }
cmd_usage(0, NULL, NULL);
}
argv[0] = cmd;
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH/RFC] "git --less cmd" to page anywhere
2006-06-06 23:58 ` [PATCH/RFC] "git --less cmd" to page anywhere Junio C Hamano
@ 2006-06-07 0:05 ` Linus Torvalds
2006-06-07 0:08 ` Petr Baudis
0 siblings, 1 reply; 12+ messages in thread
From: Linus Torvalds @ 2006-06-07 0:05 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
On Tue, 6 Jun 2006, Junio C Hamano wrote:
>
> This allows you to say:
>
> git --less diff v2.6.16-rc5..
I've seriously considered something like that, although you chose a pretty
strange - and long - flag.
I was thinking something like "git -p log -p" (the first "-p" is for
"paginate" - think "dir/p" in old DOS times, but we could claim it is for
"pager" so that people don't laugh at us)
Linus
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH/RFC] "git --less cmd" to page anywhere
2006-06-07 0:05 ` Linus Torvalds
@ 2006-06-07 0:08 ` Petr Baudis
2006-06-07 0:12 ` Linus Torvalds
0 siblings, 1 reply; 12+ messages in thread
From: Petr Baudis @ 2006-06-07 0:08 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Junio C Hamano, git
Dear diary, on Wed, Jun 07, 2006 at 02:05:59AM CEST, I got a letter
where Linus Torvalds <torvalds@osdl.org> said that...
> On Tue, 6 Jun 2006, Junio C Hamano wrote:
> >
> > This allows you to say:
> >
> > git --less diff v2.6.16-rc5..
>
> I've seriously considered something like that, although you chose a pretty
> strange - and long - flag.
>
> I was thinking something like "git -p log -p" (the first "-p" is for
> "paginate" - think "dir/p" in old DOS times, but we could claim it is for
> "pager" so that people don't laugh at us)
Actually, you made a case in the misty past that cg-log should just
stuff things through a pager by default and I ended up finding that
extremely convenient - what is the reason git log does not do the same?
--
Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH/RFC] "git --less cmd" to page anywhere
2006-06-07 0:08 ` Petr Baudis
@ 2006-06-07 0:12 ` Linus Torvalds
2006-06-07 2:29 ` Martin Langhoff
0 siblings, 1 reply; 12+ messages in thread
From: Linus Torvalds @ 2006-06-07 0:12 UTC (permalink / raw)
To: Petr Baudis; +Cc: Junio C Hamano, git
On Wed, 7 Jun 2006, Petr Baudis wrote:
>
> Actually, you made a case in the misty past that cg-log should just
> stuff things through a pager by default and I ended up finding that
> extremely convenient - what is the reason git log does not do the same?
"git log" does, "git diff" does not (and yeah, I just chose a bad example,
I meant "git -p diff -p")
For "git diff", pagination by default is definitely not the right thing to
do, but it's something you often end up wanting.
Linus
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH/RFC] "git --less cmd" to page anywhere
2006-06-07 0:12 ` Linus Torvalds
@ 2006-06-07 2:29 ` Martin Langhoff
2006-06-07 3:21 ` Linus Torvalds
0 siblings, 1 reply; 12+ messages in thread
From: Martin Langhoff @ 2006-06-07 2:29 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Petr Baudis, Junio C Hamano, git
On 6/7/06, Linus Torvalds <torvalds@osdl.org> wrote:
> For "git diff", pagination by default is definitely not the right thing to
> do, but it's something you often end up wanting.
Why is it not a good default? Unless I expect it to be shorter than
the terminal, I always tack "| less" after it. Except when I tack ">
foo.patch" and then the auto-pager knows what to do anyway.
cheers,
martin
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH/RFC] "git --less cmd" to page anywhere
2006-06-07 2:29 ` Martin Langhoff
@ 2006-06-07 3:21 ` Linus Torvalds
0 siblings, 0 replies; 12+ messages in thread
From: Linus Torvalds @ 2006-06-07 3:21 UTC (permalink / raw)
To: Martin Langhoff; +Cc: Petr Baudis, Junio C Hamano, git
On Wed, 7 Jun 2006, Martin Langhoff wrote:
>
> Why is it not a good default? Unless I expect it to be shorter than
> the terminal, I always tack "| less" after it. Except when I tack ">
> foo.patch" and then the auto-pager knows what to do anyway.
I often tack "| less" after it in some situations, but in other situations
I _never_ do.
I often use "git diff" as a way to see that my tree is clean. Sure, I
could do "git status", but if it's not clean, it's usually something
small, so I want to know what I changed last. At that point, a default
pager would be very annoying.
The "more" behaviour (which only paginates unto the end) might work for
me (ie the "git diff" auto-pager would act like "more" or "less -EX")
I dunno.
Linus
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2006-06-07 3:21 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-04 21:19 [PATCH] Support for configurable git command aliases (v2) Petr Baudis
2006-06-04 21:20 ` Petr Baudis
2006-06-04 22:11 ` Martin Mares
2006-06-04 22:19 ` Petr Baudis
2006-06-04 22:38 ` Junio C Hamano
2006-06-04 23:31 ` Linus Torvalds
2006-06-06 23:58 ` [PATCH/RFC] "git --less cmd" to page anywhere Junio C Hamano
2006-06-07 0:05 ` Linus Torvalds
2006-06-07 0:08 ` Petr Baudis
2006-06-07 0:12 ` Linus Torvalds
2006-06-07 2:29 ` Martin Langhoff
2006-06-07 3:21 ` Linus Torvalds
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).