Skip to contentSkip to conversation

Support for configurable git command aliases (v2)

PATCH In review Petr Baudis 6 participants 12 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Support for configurable git command aliases (v2)

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.

---

 Documentation/config.txt |    5 ++++
 Documentation/git.txt    |    3 ++
 git.c                    |   60 ++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 65 insertions(+), 3 deletions(-)
Documentation/config.txt +5 -0
index c861c6c..aaaa33d 100644
@@ -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].
Documentation/git.txt +3 -0
index 24ca55d..e474bdf 100644
@@ -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::
git.c +57 -3

89 lines not on this page · raw message

#1 lore raw

Dear diary, on Sun, Jun 04, 2006 at 11:19:31PM CEST, I got a letter where Petr Baudis <[email protected]> said that...

Quoted 9 lines

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.

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.

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
#2 lore raw
Quoted 4 lines · Petr Baudis

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

#3 lore raw

[PATCH/RFC] "git --less cmd" to page anywhere

This allows you to say:

	git --less diff v2.6.16-rc5..

to pipe the output of any git command to your pager.

---

Quoted 6 lines · Linus Torvalds
Quoted 4 lines · Petr Baudis

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.

Quoted 7 lines

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(-)
git.c +5 -0
index bc463c9..c52da8c 100644
@@ -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;
#4 lore raw

Re: [PATCH/RFC] "git --less cmd" to page anywhere

Quoted 4 lines · Junio C Hamano

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

#5 lore raw

Re: [PATCH/RFC] "git --less cmd" to page anywhere

Dear diary, on Wed, Jun 07, 2006 at 02:05:59AM CEST, I got a letter where Linus Torvalds <[email protected]> said that...

Quoted 12 lines · Junio C Hamano
Quoted 4 lines

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?

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
#6 lore raw

Re: [PATCH/RFC] "git --less cmd" to page anywhere

Quoted 4 lines · Petr Baudis

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

#7 lore raw

Re: [PATCH/RFC] "git --less cmd" to page anywhere

Quoted 2 lines · Linus Torvalds

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

#8 lore raw

Re: [PATCH/RFC] "git --less cmd" to page anywhere

Quoted 4 lines · Martin Langhoff

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

#9 lore raw
In reply to Petr Baudis

Hello, world!

Quoted 3 lines

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

Signature · 3 lines
Martin `MJ' Mares   <[email protected]>   http://atrey.karlin.mff.cuni.cz/~mj/
Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth
God is real, unless declared integer.
#10 lore raw

Hi,

Dear diary, on Mon, Jun 05, 2006 at 12:11:14AM CEST, I got a letter where Martin Mares <[email protected]> said that...

Quoted 6 lines
Quoted 3 lines

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?

Signature · 3 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A person is just about as big as the things that make them angry.
#11 lore raw
Quoted 7 lines · Petr Baudis

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:

Quoted 8 lines

(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.