[PATCH] Adds configuration options for some commonly used command-line options (GSOC micro-project)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



	This is a patch for my work on one of the micro projects, as I intend
to apply for the Google Summer of Code 2015 under the Git community.
This patch gives the user the oppotunity to specify configuration
options for some commonly used command-line options for exampel:
	git config defopt.am 'am -3'
---
 Makefile |    1 +
 defopt.c |   24 ++++++++++++++++++++++++
 git.c    |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 defopt.c

diff --git a/Makefile b/Makefile
index 2320de5..e713e23 100644
--- a/Makefile
+++ b/Makefile
@@ -789,6 +789,7 @@ LIB_OBJS += csum-file.o
 LIB_OBJS += ctype.o
 LIB_OBJS += date.o
 LIB_OBJS += decorate.o
+LIB_OBJS += defopt.o
 LIB_OBJS += diffcore-break.o
 LIB_OBJS += diffcore-delta.o
 LIB_OBJS += diffcore-order.o
diff --git a/defopt.c b/defopt.c
new file mode 100644
index 0000000..b4fa9e2
--- /dev/null
+++ b/defopt.c
@@ -0,0 +1,24 @@
+#include "cache.h"
+
+static const char *defopt_key;
+static char *defopt_val;
+
+static int defopt_lookup_cb(const char *k, const char *v, void *cb)
+{
+	const char *name;
+	if (skip_prefix(k, "defopt.", &name) && !strcmp(name, defopt_key)) {
+		if (!v)
+			return config_error_nonbool(k);
+		defopt_val = xstrdup(v);
+		return 0;
+	}
+	return 0;
+}
+
+char *defopt_lookup(const char *defopt)
+{
+	defopt_key = defopt;
+	defopt_val = NULL;
+	git_config(defopt_lookup_cb, NULL);
+	return defopt_val;
+}
diff --git a/git.c b/git.c
index 9c49519..4b556e1 100644
--- a/git.c
+++ b/git.c
@@ -223,6 +223,59 @@ static int handle_options(const char ***argv, int
*argc, int *envchanged)
 	return (*argv) - orig_argv;
 }

+static int handle_defopt(int *argcp, const char ***argv)
+{	
+	int envchanged = 0, ret = 0, saved_errno = errno;
+	const char *subdir;
+	int count, option_count;
+	const char **new_argv;
+	const char *defopt_command;
+	char *defopt_string;
+	int unused_nongit;
+
+	subdir = setup_git_directory_gently(&unused_nongit);
+
+	defopt_command = (*argv)[0];
+	defopt_string = defopt_lookup(defopt_command);
+	if (defopt_string) {
+		
+		count = split_cmdline(defopt_string, &new_argv);
+		if (count < 0)
+			return;
+		option_count = handle_options(&new_argv, &count, &envchanged);
+		
+		memmove(new_argv - option_count, new_argv,
+				count * sizeof(char *));
+		new_argv -= option_count;
+
+		if (count < 1)
+			return;
+
+		if (strcmp(defopt_command, new_argv[0]))
+			return;
+
+		trace_argv_printf(new_argv,
+				  "trace: defopt: %s =>",
+				  defopt_command);
+
+		new_argv = xrealloc(new_argv, sizeof(char *) *
+				    (count + *argcp));
+		/* insert after command name */
+		memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
+
+		*argv = new_argv;
+		*argcp += count - 1;
+
+	}
+
+	if (subdir && chdir(subdir))
+		die_errno("Cannot change to '%s'", subdir);
+
+	errno = saved_errno;
+
+}
+
+
 static int handle_alias(int *argcp, const char ***argv)
 {
 	int envchanged = 0, ret = 0, saved_errno = errno;
@@ -517,6 +570,9 @@ static void handle_builtin(int argc, const char **argv)
 		argv[1] = argv[0];
 		argv[0] = cmd = "help";
 	}
+	
+	if(is_builtin(cmd) && argc == 1)
+		handle_defopt(&argc, &argv);

 	for (i = 0; i < ARRAY_SIZE(commands); i++) {
 		struct cmd_struct *p = commands+i;
-- 
	Signed-off-by: Yolande Amate <yolandeamate@xxxxxxxxx>
1.7.10.4
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]