git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Allow an alias to start with "-p"

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jul 24, 2006, 12:10 UTC
Message-ID
<Pine.LNX.4.63.0607241408170.29667@wbgn013.biozentrum.uni-wuerzburg.de>
In-Reply-To
<7v8xmjh5sv.fsf@assigned-by-dhcp.cox.net>
    
Now, something like
	[alias]
		pd = -p diff
works as expected.
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
On Sun, 23 Jul 2006, Junio C Hamano wrote:
Show 16 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > Now, something like
> >
> > 	[alias]
> > 		pd = -p diff
> >
> > works as expected.
> 
> I like what it wants to do but I am afraid this leads to an
> unmaintainable code (a micronit that already shows what I mean
> is that you can say "git --paginate diff", but you cannot say
> "pd = --paginate diff" in the configuration file).
> 
> Is there a cleaner way to do it without duplicating the argument
> loop of git.c::main()?

This patch uses a better approach: instead of duplicating the option parsing of the git wrapper, it refactors the code into the new function handle_options().

In related news, this function would be the perfect candidate to set GIT_DIR without environment variables...

 git.c |   37 +++++++++++++++++++++++++++++++------
 1 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/git.c b/git.c
index ee5a0e8..8d7c644 100644
--- a/git.c
+++ b/git.c
@@ -35,6 +35,27 @@ static void prepend_to_path(const char *
 	setenv("PATH", path, 1);
 }
 
+static int handle_options(const char*** argv, int* argc)
+{
+	int handled = 0;
+
+	while (*argc > 0) {
+		const char *cmd = (*argv)[0];
+		if (cmd[0] != '-')
+			break;
+
+		if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
+			setup_pager();
+		} else
+			die ("Unknown option: %s", cmd);
+
+		(*argv)++;
+		(*argc)--;
+		handled++;
+	}
+	return handled;
+}
+
 static const char *alias_command;
 static char *alias_string = NULL;
 
@@ -106,7 +127,7 @@ static int handle_alias(int *argcp, cons
 
 	subdir = setup_git_directory_gently(&nongit);
 	if (!nongit) {
-		int count;
+		int count, option_count;
 		const char** new_argv;
 
 		alias_command = (*argv)[0];
@@ -114,6 +135,10 @@ static int handle_alias(int *argcp, cons
 		if (alias_string) {
 
 			count = split_cmdline(alias_string, &new_argv);
+			option_count = handle_options(&new_argv, &count);
+			memmove(new_argv - option_count, new_argv,
+					count * sizeof(char *));
+			new_argv -= option_count;
 
 			if (count < 1)
 				die("empty alias for %s", alias_command);
@@ -264,6 +289,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);
 	}
@@ -273,13 +299,12 @@ int main(int argc, const char **argv, ch
 
 	/* Look for flags.. */
 	while (argc > 1) {
-		cmd = *++argv;
+		argv++;
 		argc--;
 
-		if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
-			setup_pager();
-			continue;
-		}
+		handle_options(&argv, &argc);
+			
+		cmd = *argv;
 
 		if (strncmp(cmd, "--", 2))
 			break;
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 8 in “Allow an alias to start with "-p"”
  1. Allow an alias to start with "-p"Johannes Schindelin, Jul 18, 2006
  2. Junio C HamanoJul 24, 2006
  3. Allow an alias to start with "-p"Johannes Schindelin, Jul 24, 2006
  4. Junio C HamanoJul 25, 2006
  5. Johannes SchindelinJul 25, 2006
  6. Jeff KingJul 25, 2006
  7. Johannes SchindelinJul 25, 2006
  8. Junio C HamanoJul 25, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.