summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2024-03-13 17:51:41 +0100
committerLudovic Courtès <ludo@gnu.org>2024-05-01 17:26:19 +0200
commit8d1d98a3aa3448b9d983e4bd64243a938b96e8ab (patch)
treeb9f677f4b88872700c87c45e3c453bf46dce83dd
parent1a5041a502ee26c04d264983232b2aaa6a5e27d8 (diff)
downloadguix-patches-8d1d98a3aa3448b9d983e4bd64243a938b96e8ab.tar
guix-patches-8d1d98a3aa3448b9d983e4bd64243a938b96e8ab.tar.gz
git authenticate: Install pre-push and post-checkout hooks.
* guix/scripts/git/authenticate.scm (install-hooks): New procedure. (guix-git-authenticate): Use it. * doc/guix.texi (Invoking guix git authenticate): Document it. Change-Id: I4464a33193186e85b476a12740e54412bd58429c
-rw-r--r--doc/guix.texi5
-rw-r--r--guix/scripts/git/authenticate.scm49
2 files changed, 53 insertions, 1 deletions
diff --git a/doc/guix.texi b/doc/guix.texi
index 743ac09b67..749d689ac1 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -7362,6 +7362,11 @@ branches, you can extend @file{.git/config} along these lines:
keyring = keyring
@end smallexample
+The first run also attempts to install pre-push and post-merge hooks,
+such that @command{guix git authenticate} is invoked as soon as you run
+@command{git push}, @command{git pull}, and related commands; it does
+not overwrite preexisting hooks though.
+
The command-line options described below allow you to fine-tune the
process.
diff --git a/guix/scripts/git/authenticate.scm b/guix/scripts/git/authenticate.scm
index 0797cba0b6..e3ecb67c89 100644
--- a/guix/scripts/git/authenticate.scm
+++ b/guix/scripts/git/authenticate.scm
@@ -148,6 +148,52 @@ in repository configuration file~%")))
(warning (G_ "could not record introduction and keyring configuration\
(Guile-Git too old?)~%"))))
+(define (install-hooks repository)
+ "Attempt to install in REPOSITORY hooks that invoke 'guix git authenticate'.
+Bail out if one of these already exists."
+ ;; Guile-Git < 0.7.0 lacks 'repository-common-directory'.
+ (if (module-defined? (resolve-interface '(git))
+ 'repository-common-directory)
+ (let ()
+ (define directory
+ (repository-common-directory repository))
+
+ (define pre-push-hook
+ (in-vicinity directory "hooks/pre-push"))
+
+ (define post-merge-hook
+ (in-vicinity directory "hooks/post-merge"))
+
+ (if (or (file-exists? pre-push-hook)
+ (file-exists? post-merge-hook))
+ (begin
+ (warning (G_ "not overriding pre-existing hooks '~a' and '~a'~%")
+ pre-push-hook post-merge-hook)
+ (display-hint (G_ "Consider running @command{guix git authenticate}
+from your pre-push and post-merge hooks so your repository is automatically
+authenticated before you push and when you pull updates.")))
+ (begin
+ (call-with-output-file pre-push-hook
+ (lambda (port)
+ (format port "#!/bin/sh
+# Installed by 'guix git authenticate'.
+set -e
+while read local_ref local_oid remote_ref remote_oid
+do
+ guix git authenticate --end=\"$local_oid\"
+done\n")
+ (chmod port #o755)))
+ (call-with-output-file post-merge-hook
+ (lambda (port)
+ (format port "#!/bin/sh
+# Installed by 'guix git authenticate'.
+exec guix git authenticate\n")
+ (chmod port #o755)))
+ (info (G_ "installed hooks '~a' and '~a'~%")
+ pre-push-hook post-merge-hook))))
+ (warning (G_ "cannot determine where to install hooks\
+ (Guile-Git too old?)~%"))))
+
(define (show-stats stats)
"Display STATS, an alist containing commit signing stats as returned by
'authenticate-repository'."
@@ -271,7 +317,8 @@ expected COMMIT and SIGNER~%")))
(unless (configured? repository)
(record-configuration repository
#:commit commit #:signer signer
- #:keyring-reference keyring))
+ #:keyring-reference keyring)
+ (install-hooks repository))
(when (and show-stats? (not (null? stats)))
(show-stats stats))