2019-09-14 11:54:06 -04:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2021-09-17 04:04:44 -04:00
|
|
|
;;; Copyright © 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
|
2021-09-17 04:04:36 -04:00
|
|
|
;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
|
2019-09-14 11:54:06 -04:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix tests git)
|
|
|
|
#:use-module (git)
|
2019-09-14 17:16:54 -04:00
|
|
|
#:use-module ((guix git) #:select (with-repository))
|
2019-09-14 11:54:06 -04:00
|
|
|
#:use-module (guix utils)
|
|
|
|
#:use-module (guix build utils)
|
|
|
|
#:use-module (ice-9 match)
|
2021-12-23 18:46:36 -05:00
|
|
|
#:use-module ((ice-9 control) #:select (let/ec))
|
2019-09-14 11:54:06 -04:00
|
|
|
#:export (git-command
|
|
|
|
with-temporary-git-repository
|
2021-12-22 05:28:55 -05:00
|
|
|
with-git-repository
|
2019-09-14 11:54:06 -04:00
|
|
|
find-commit))
|
|
|
|
|
|
|
|
(define git-command
|
|
|
|
(make-parameter "git"))
|
|
|
|
|
|
|
|
(define (populate-git-repository directory directives)
|
|
|
|
"Initialize a new Git checkout and repository in DIRECTORY and apply
|
|
|
|
DIRECTIVES. Each element of DIRECTIVES is an sexp like:
|
|
|
|
|
|
|
|
(add \"foo.txt\" \"hi!\")
|
|
|
|
|
|
|
|
Return DIRECTORY on success."
|
|
|
|
|
|
|
|
;; Note: As of version 0.2.0, Guile-Git lacks the necessary bindings to do
|
|
|
|
;; all this, so resort to the "git" command.
|
|
|
|
(define (git command . args)
|
2020-04-19 07:16:52 -04:00
|
|
|
;; Make sure Git doesn't rely on the user's config.
|
|
|
|
(call-with-temporary-directory
|
|
|
|
(lambda (home)
|
|
|
|
(call-with-output-file (string-append home "/.gitconfig")
|
|
|
|
(lambda (port)
|
|
|
|
(display "[user]
|
|
|
|
email = charlie@example.org\n name = Charlie Guix\n"
|
|
|
|
port)))
|
|
|
|
|
|
|
|
(with-environment-variables
|
|
|
|
`(("GIT_CONFIG_NOSYSTEM" "1")
|
|
|
|
("GIT_ATTR_NOSYSTEM" "1")
|
2021-09-17 04:04:36 -04:00
|
|
|
("GIT_CONFIG_GLOBAL" ,(string-append home "/.gitconfig"))
|
2020-04-19 07:16:52 -04:00
|
|
|
("HOME" ,home))
|
|
|
|
(apply invoke (git-command) "-C" directory
|
|
|
|
command args)))))
|
2019-09-14 11:54:06 -04:00
|
|
|
|
2021-12-22 05:28:55 -05:00
|
|
|
(unless (directory-exists? (string-append directory "/.git"))
|
|
|
|
(mkdir-p directory)
|
|
|
|
(git "init"))
|
2019-09-14 11:54:06 -04:00
|
|
|
|
|
|
|
(let loop ((directives directives))
|
|
|
|
(match directives
|
|
|
|
(()
|
|
|
|
directory)
|
|
|
|
((('add file contents) rest ...)
|
|
|
|
(let ((file (string-append directory "/" file)))
|
|
|
|
(mkdir-p (dirname file))
|
|
|
|
(call-with-output-file file
|
|
|
|
(lambda (port)
|
2022-08-09 09:58:14 -04:00
|
|
|
(set-port-encoding! port "UTF-8")
|
2019-09-14 17:16:54 -04:00
|
|
|
(display (if (string? contents)
|
|
|
|
contents
|
|
|
|
(with-repository directory repository
|
|
|
|
(contents repository)))
|
|
|
|
port)))
|
2019-09-14 11:54:06 -04:00
|
|
|
(git "add" file)
|
|
|
|
(loop rest)))
|
2021-12-22 05:28:55 -05:00
|
|
|
((('add file-name-and-content) rest ...)
|
|
|
|
(loop (cons `(add ,file-name-and-content ,file-name-and-content)
|
|
|
|
rest)))
|
2020-06-07 17:06:41 -04:00
|
|
|
((('remove file) rest ...)
|
|
|
|
(git "rm" "-f" file)
|
|
|
|
(loop rest))
|
2019-09-14 11:54:06 -04:00
|
|
|
((('commit text) rest ...)
|
|
|
|
(git "commit" "-m" text)
|
|
|
|
(loop rest))
|
2020-06-01 17:20:06 -04:00
|
|
|
((('commit text ('signer fingerprint)) rest ...)
|
|
|
|
(git "commit" "-m" text (string-append "--gpg-sign=" fingerprint))
|
|
|
|
(loop rest))
|
2019-09-21 15:29:30 -04:00
|
|
|
((('tag name) rest ...)
|
|
|
|
(git "tag" name)
|
|
|
|
(loop rest))
|
2021-09-17 04:04:44 -04:00
|
|
|
((('tag name text) rest ...)
|
|
|
|
(git "tag" "-m" text name)
|
|
|
|
(loop rest))
|
2019-09-14 11:54:06 -04:00
|
|
|
((('branch name) rest ...)
|
|
|
|
(git "branch" name)
|
|
|
|
(loop rest))
|
|
|
|
((('checkout branch) rest ...)
|
|
|
|
(git "checkout" branch)
|
|
|
|
(loop rest))
|
2021-12-22 05:28:55 -05:00
|
|
|
((('checkout branch 'orphan) rest ...)
|
|
|
|
(git "checkout" "--orphan" branch)
|
|
|
|
(loop rest))
|
2019-09-14 11:54:06 -04:00
|
|
|
((('merge branch message) rest ...)
|
|
|
|
(git "merge" branch "-m" message)
|
2020-06-01 17:20:06 -04:00
|
|
|
(loop rest))
|
|
|
|
((('merge branch message ('signer fingerprint)) rest ...)
|
|
|
|
(git "merge" branch "-m" message
|
|
|
|
(string-append "--gpg-sign=" fingerprint))
|
2021-12-22 05:28:55 -05:00
|
|
|
(loop rest))
|
|
|
|
((('reset to) rest ...)
|
|
|
|
(git "reset" "--hard" to)
|
2019-09-14 11:54:06 -04:00
|
|
|
(loop rest)))))
|
|
|
|
|
|
|
|
(define (call-with-temporary-git-repository directives proc)
|
|
|
|
(call-with-temporary-directory
|
|
|
|
(lambda (directory)
|
|
|
|
(populate-git-repository directory directives)
|
|
|
|
(proc directory))))
|
|
|
|
|
|
|
|
(define-syntax-rule (with-temporary-git-repository directory
|
|
|
|
directives exp ...)
|
|
|
|
"Evaluate EXP in a context where DIRECTORY contains a checkout populated as
|
|
|
|
per DIRECTIVES."
|
|
|
|
(call-with-temporary-git-repository directives
|
|
|
|
(lambda (directory)
|
|
|
|
exp ...)))
|
|
|
|
|
2021-12-22 05:28:55 -05:00
|
|
|
(define-syntax-rule (with-git-repository directory
|
|
|
|
directives exp ...)
|
|
|
|
"Evaluate EXP in a context where DIRECTORY is (further) populated as
|
|
|
|
per DIRECTIVES."
|
|
|
|
(begin
|
|
|
|
(populate-git-repository directory directives)
|
|
|
|
exp ...))
|
|
|
|
|
2019-09-14 11:54:06 -04:00
|
|
|
(define (find-commit repository message)
|
|
|
|
"Return the commit in REPOSITORY whose message includes MESSAGE, a string."
|
|
|
|
(let/ec return
|
|
|
|
(fold-commits (lambda (commit _)
|
|
|
|
(and (string-contains (commit-message commit)
|
|
|
|
message)
|
|
|
|
(return commit)))
|
|
|
|
#f
|
|
|
|
repository)
|
|
|
|
(error "commit not found" message)))
|