threads / rfc / 15819

RFC patchAdd post-init hook

Subject: [PATCH/RFC] Add post-init hook

## tl;dr

7 messages between Oct 7, 2008 and Oct 9, 2008. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Jonathan del Strother· Oct 7, 2008, 23:10 UTC · lore
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@bestbefore.tv>
---

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(-)
Show changes to 3 files +45 −2

Documentation/config.txt, Documentation/git-init.txt, builtin-init-db.c

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@osdl.org>
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
Jeff King· Oct 8, 2008, 01:53 UTC · re: Jonathan del Strother · lore

Re: [PATCH/RFC] Add post-init hook

On Wed, Oct 08, 2008 at 12:10:33AM +0100, Jonathan del Strother wrote:
> 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.
Why is the --template parameter to clone and init not sufficient?
-Peff
Jonathan del Strother· Oct 8, 2008, 08:16 UTC · re: Jeff King · lore

Re: [PATCH/RFC] Add post-init hook

On Wed, Oct 8, 2008 at 2:53 AM, Jeff King <peff@peff.net> wrote:
Show 8 quoted lines
> On Wed, Oct 08, 2008 at 12:10:33AM +0100, Jonathan del Strother wrote:
>
>> 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.
>
> Why is the --template parameter to clone and init not sufficient?
>
 Partly that I never remember to include the --template parameter,
partly that I wanted to customize its behaviour (it copies the files,
I want symlinks so I only have to edit my hooks in one place whenever
I change them)
Jeff King· Oct 9, 2008, 01:41 UTC · re: Jonathan del Strother · lore

Re: [PATCH/RFC] Add post-init hook

On Wed, Oct 08, 2008 at 09:16:56AM +0100, Jonathan del Strother wrote:
> > Why is the --template parameter to clone and init not sufficient?
> >
>  Partly that I never remember to include the --template parameter,

In that case, I would think a core.template parameter might make more sense.

> partly that I wanted to customize its behaviour (it copies the files,
> I want symlinks so I only have to edit my hooks in one place whenever
> I change them)

This makes a lot of sense to me as a best-practice. I wonder if we wouldn't do better to add a core.symlink-templates option?

-Peff
Junio C Hamano· Oct 9, 2008, 11:24 UTC · re: Jeff King · lore

Re: [PATCH/RFC] Add post-init hook

Jeff King <peff@peff.net> writes:
Show 7 quoted lines
> On Wed, Oct 08, 2008 at 09:16:56AM +0100, Jonathan del Strother wrote:
> ...
>> I want symlinks so I only have to edit my hooks in one place whenever
>> I change them)
>
> This makes a lot of sense to me as a best-practice. I wonder if we
> wouldn't do better to add a core.symlink-templates option?

AFAIR, "git init" copies symlinks in templates as symlinks, so I do not see why you would even want to have such an option.

Wouldn't it be better if users and installations with such a special set of templates specified by core.template or --template prepare a template directory with files and symbolic links of their liking? If they want some hooks to point at the latest copy of installation specific standard hook script, templates/hooks/$that_hook can be a symlink to the real location, no?

Jeff King· Oct 9, 2008, 11:32 UTC · re: Junio C Hamano · lore

Re: [PATCH/RFC] Add post-init hook

On Thu, Oct 09, 2008 at 04:24:29AM -0700, Junio C Hamano wrote:
Show 5 quoted lines
> > This makes a lot of sense to me as a best-practice. I wonder if we
> > wouldn't do better to add a core.symlink-templates option?
> 
> AFAIR, "git init" copies symlinks in templates as symlinks, so I do not
> see why you would even want to have such an option.

Ah, I didn't realize that. In that case, yes, setting up a template directory of symlinks makes the most sense. I think a core.template option is still worthwhile, though.

-Peff
Alex Riesen· Oct 9, 2008, 11:34 UTC · re: Junio C Hamano · lore

Re: [PATCH/RFC] Add post-init hook

2008/10/9 Junio C Hamano <gitster@pobox.com>:
Show 18 quoted lines
> Jeff King <peff@peff.net> writes:
>> On Wed, Oct 08, 2008 at 09:16:56AM +0100, Jonathan del Strother wrote:
>> ...
>>> I want symlinks so I only have to edit my hooks in one place whenever
>>> I change them)
>>
>> This makes a lot of sense to me as a best-practice. I wonder if we
>> wouldn't do better to add a core.symlink-templates option?
>
> AFAIR, "git init" copies symlinks in templates as symlinks, so I do not
> see why you would even want to have such an option.
>
> Wouldn't it be better if users and installations with such a special
> set of templates specified by core.template or --template prepare a
> template directory with files and symbolic links of their liking?  If
> they want some hooks to point at the latest copy of installation specific
> standard hook script, templates/hooks/$that_hook can be a symlink to the
> real location, no?

It will be replaced with a file next time you update your git installation. Seems like we need ~/.gitconfig/*...

← back to recent threads