git.scm 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2016, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build git)
  19. #:use-module (guix build utils)
  20. #:use-module (srfi srfi-34)
  21. #:use-module (ice-9 format)
  22. #:export (git-fetch))
  23. ;;; Commentary:
  24. ;;;
  25. ;;; This is the build-side support code of (guix git-download). It allows a
  26. ;;; Git repository to be cloned and checked out at a specific commit.
  27. ;;;
  28. ;;; Code:
  29. (define* (git-fetch url commit directory
  30. #:key (git-command "git") recursive?)
  31. "Fetch COMMIT from URL into DIRECTORY. COMMIT must be a valid Git commit
  32. identifier. When RECURSIVE? is true, all the sub-modules of URL are fetched,
  33. recursively. Return #t on success, #f otherwise."
  34. ;; Disable TLS certificate verification. The hash of the checkout is known
  35. ;; in advance anyway.
  36. (setenv "GIT_SSL_NO_VERIFY" "true")
  37. (mkdir-p directory)
  38. (guard (c ((invoke-error? c)
  39. (format (current-error-port)
  40. "git-fetch: '~a~{ ~a~}' failed with exit code ~a~%"
  41. (invoke-error-program c)
  42. (invoke-error-arguments c)
  43. (or (invoke-error-exit-status c) ;XXX: not quite accurate
  44. (invoke-error-stop-signal c)
  45. (invoke-error-term-signal c)))
  46. (delete-file-recursively directory)
  47. #f))
  48. (with-directory-excursion directory
  49. (invoke git-command "init")
  50. (invoke git-command "remote" "add" "origin" url)
  51. (if (zero? (system* git-command "fetch" "--depth" "1" "origin" commit))
  52. (invoke git-command "checkout" "FETCH_HEAD")
  53. (begin
  54. (setvbuf (current-output-port) 'line)
  55. (format #t "Failed to do a shallow fetch; retrying a full fetch...~%")
  56. (invoke git-command "fetch" "origin")
  57. (invoke git-command "checkout" commit)))
  58. (when recursive?
  59. ;; Now is the time to fetch sub-modules.
  60. (invoke git-command "submodule" "update" "--init" "--recursive")
  61. ;; In sub-modules, '.git' is a flat file, not a directory,
  62. ;; so we can use 'find-files' here.
  63. (for-each delete-file-recursively
  64. (find-files directory "^\\.git$")))
  65. ;; The contents of '.git' vary as a function of the current
  66. ;; status of the Git repo. Since we want a fixed output, this
  67. ;; directory needs to be taken out.
  68. (delete-file-recursively ".git")
  69. #t)))
  70. ;;; git.scm ends here