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

[PATCH v3] Introduce core.initHook

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Mar 24, 2008, 23:21 UTC
Message-ID
<alpine.LSU.1.00.0803250020490.4353@racer.site>
In-Reply-To
<7vhcevu4ou.fsf@gitster.siamese.dyndns.org>

This variable, if set, specifies the path of a hook which is executed after every git-init. It can be used to override settings in the templates per-user.

In case of a re-initialization, the hook gets called with the argument "reinit".

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
On Mon, 24 Mar 2008, Junio C Hamano wrote:
Show 13 quoted lines
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > 	> > +	if (run_init_hook())
> > 	> > +		return 1;
> > 	> > +
> > 	> 
> > 	> Hmm.  Exit without a message even under !quiet?
> >
> > 	Even under quiet, run_init_hook() will complain if the hook is 
> > 	invalid.
> 
> Sorry, I may have been unclear but I was wondering more about the case the
> hook script errored out silently.
Ooops.  You're right.  This is the interdiff:
diff --git a/builtin-init-db.c b/builtin-init-db.c
index e1a54b5..cdeb1d7 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -332,7 +332,8 @@ static int run_init_hook(int reinit)
        argv[0] = init_hook;
        if (reinit)
                argv[1] = "reinit";
-       return run_command_v_opt(argv, 0);
+       return run_command_v_opt(argv, 0) ?
+               error("hook '%s' failed", init_hook) : 0;
 }


 Documentation/config.txt   |    5 +++++
 Documentation/git-init.txt |    6 ++++++
 builtin-init-db.c          |   29 +++++++++++++++++++++++++++++
 t/t0001-init.sh            |   21 +++++++++++++++++++++
 4 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index efde54d..355f049 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -358,6 +358,11 @@ core.whitespace::
   does not trigger if the character before such a carriage-return
   is not a whitespace (not enabled by default).
 
+core.inithook::
+	The path to a program which is run after each call to
+	linkgit:git-init[1].  The hook is called with the argument
+	"reinit" if an existing repository is re-initialized.
+
 alias.*::
 	Command aliases for the linkgit:git[1] command wrapper - e.g.
 	after defining "alias.last = cat-file commit HEAD", the invocation
diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
index 03a4f0e..26deaaf 100644
--- a/Documentation/git-init.txt
+++ b/Documentation/git-init.txt
@@ -60,6 +60,12 @@ If you are initializing a non-bare repository, the config variable
 `receive.guardCurrentBranch` is set to true.  This avoids problems with
 pushing into the current branch, which does not touch the working tree.
 
+If you want to run a specific script everytime git-init was issued, you
+can set the variable `core.initHook` in /etc/gitconfig or $HOME/.gitconfig,
+or in the existing repository when re-initializing.  In the case that you
+reinitialize a repository, the hook is called with the single argument
+"reinit".
+
 --
 
 
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 36c12a2..cdeb1d7 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"
@@ -310,6 +311,31 @@ static void guess_repository_type(const char *git_dir)
 	return;
 }
 
+static const char *init_hook;
+static int config_init_hook(const char *key, const char *value)
+{
+	if (!strcmp(key, "core.inithook"))
+		return git_config_string(&init_hook, key, value);
+	return 0;
+}
+
+static int run_init_hook(int reinit)
+{
+	const char *argv[3] = { NULL, NULL, NULL };
+
+	git_config(config_init_hook);
+	if (!init_hook)
+		return 0;
+	if (access(init_hook, X_OK) < 0)
+		return error("init hook '%s' not found", init_hook);
+
+	argv[0] = init_hook;
+	if (reinit)
+		argv[1] = "reinit";
+	return run_command_v_opt(argv, 0) ?
+		error("hook '%s' failed", init_hook) : 0;
+}
+
 static const char init_db_usage[] =
 "git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
 
@@ -407,6 +433,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 		git_config_set("receive.denyNonFastforwards", "true");
 	}
 
+	if (run_init_hook(reinit))
+		return 1;
+
 	if (!quiet)
 		printf("%s%s Git repository in %s/\n",
 		       reinit ? "Reinitialized existing" : "Initialized empty",
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index c015405..7c18d24 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -113,4 +113,25 @@ test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
 	fi
 '
 
+cat > init-hook.sh << EOF
+#!$SHELL_PATH
+
+git config test.message success
+EOF
+chmod a+x init-hook.sh
+
+# using reinit because of lacking system/global config in the tests
+
+test_expect_success 'core.initHook' '
+
+	mkdir hook &&
+	(cd hook &&
+	 git init &&
+	 test -z "$(git config test.message)" &&
+	 git config core.initHook "$(pwd)"/../init-hook.sh &&
+	 git init &&
+	 test success = "$(git config test.message)")
+
+'
+
 test_done
-- 
1.5.5.rc1.178.gd799d
Previous: Junio C Hamano
Message 25 of 25 in “Store autocrlf during init-db (was [msysGit] autocrlf problems with Git-preview20080301.exe)”
  1. Steffen ProhaskaMar 23, 2008
  2. init-db: Store current value of autocrlfSteffen Prohaska, Mar 23, 2008
  3. Dmitry PotapovMar 24, 2008
  4. Johannes SchindelinMar 23, 2008
  5. Steffen ProhaskaMar 23, 2008
  6. Johannes SchindelinMar 23, 2008
  7. Steffen ProhaskaMar 23, 2008
  8. Johannes SchindelinMar 23, 2008
  9. Steffen ProhaskaMar 23, 2008
  10. Johannes SchindelinMar 23, 2008
  11. Steffen ProhaskaMar 23, 2008
  12. Johannes SchindelinMar 23, 2008
  13. Dmitry PotapovMar 23, 2008
  14. Johannes SchindelinMar 24, 2008
  15. Dmitry PotapovMar 24, 2008
  16. Johannes SchindelinMar 24, 2008
  17. Dmitry PotapovMar 24, 2008
  18. Introduce core.initHookJohannes Schindelin, Mar 24, 2008
  19. init: show "Reinit" message even in an (existing) empty repositoryJohannes Schindelin, Mar 24, 2008
  20. Junio C HamanoMar 25, 2008
  21. Johannes SchindelinMar 25, 2008
  22. Junio C HamanoMar 24, 2008
  23. Introduce core.initHookJohannes Schindelin, Mar 24, 2008
  24. Junio C HamanoMar 24, 2008
  25. Introduce core.initHookJohannes Schindelin, Mar 24, 2008

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.