[PATCH/RFC] Add post-init hook

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

 



This adds a configuration variable 'init.post-init-file', to supply a path to a script that will be run following git-init (and thus also git-clone).

Signed-off-by: Jonathan del Strother <jon.delStrother@xxxxxxxxxxxxx>
---

I have a number of hooks that I have to install every time I create/clone a repository.  This patch adds a post-init hook that's perfect for setting up that sort of stuff.
It's my first git patch that does much C work (even if it was mostly a cut & paste job) - comments would be welcome.

 Documentation/config.txt   |    4 ++++
 Documentation/git-init.txt |    7 +++++++
 builtin-init-db.c          |   36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index bbe38cc..746d663 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -881,6 +881,10 @@ i18n.logOutputEncoding::
 	Character encoding the commit messages are converted to when
 	running 'git-log' and friends.
 
+init.post-init-file::
+	A path to a script to run on the initialization of any git
+	repository.
+
 instaweb.browser::
 	Specify the program that will be used to browse your working
 	repository in gitweb. See linkgit:git-instaweb[1].
diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
index 71749c0..c6ab6c9 100644
--- a/Documentation/git-init.txt
+++ b/Documentation/git-init.txt
@@ -113,6 +113,13 @@ $ git add .     <2>
 <2> add all existing file to the index
 
 
+CONFIGURATION
+-------------
+
+The global configuration variable init.post-init-file can be used to specify
+a script to be run whenever a repository is created.
+
+
 Author
 ------
 Written by Linus Torvalds <torvalds@xxxxxxxx>
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 8140c12..7a3d5c7 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -6,6 +6,7 @@
 #include "cache.h"
 #include "builtin.h"
 #include "exec_cmd.h"
+#include "run-command.h"
 
 #ifndef DEFAULT_GIT_TEMPLATE_DIR
 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -17,6 +18,37 @@
 #define TEST_FILEMODE 1
 #endif
 
+static const char* post_init_path;
+
+static int init_config(const char *var, const char *value, void *cb)
+{
+	if (!strcasecmp(var, "init.post-init-file")) {
+		return git_config_string(&post_init_path, var, value);
+	}
+	return git_default_config(var, value, cb);
+}
+
+
+static int post_init_hook()
+{
+	if (!post_init_path)
+		return 0;
+
+	struct child_process proc;
+	const char *argv[2];
+
+	if (access(post_init_path, X_OK) < 0)
+		return 0;
+
+	memset(&proc, 0, sizeof(proc));
+	argv[0] = post_init_path;
+	argv[1] = NULL;
+	proc.argv = argv;
+	proc.no_stdin = 1;
+	proc.stdout_to_stderr = 1;
+	return run_command(&proc);
+}
+
 static void safe_create_dir(const char *dir, int share)
 {
 	if (mkdir(dir, 0777) < 0) {
@@ -190,7 +222,7 @@ static int create_default_files(const char *template_path)
 	 */
 	copy_templates(template_path);
 
-	git_config(git_default_config, NULL);
+	git_config(init_config, NULL);
 
 	/*
 	 * We would have created the above under user's umask -- under
@@ -321,7 +353,7 @@ int init_db(const char *template_dir, unsigned int flags)
 		       shared_repository ? " shared" : "",
 		       get_git_dir());
 
-	return 0;
+	return post_init_hook();
 }
 
 static int guess_repository_type(const char *git_dir)
-- 
1.6.0.2.454.g27632.dirty

--
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]

  Powered by Linux