File: clone.scm

package info (click to toggle)
guile-git 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: lisp: 6,231; makefile: 132; sh: 8
file content (145 lines) | stat: -rw-r--r-- 6,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; Guile-Git --- GNU Guile bindings of libgit2
;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2020-2021, 2024 Ludovic Courtès <ludo@gnu.org>
;;;
;;; This file is part of Guile-Git.
;;;
;;; Guile-Git 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.
;;;
;;; Guile-Git 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 Guile-Git.  If not, see <http://www.gnu.org/licenses/>.

(define-module (tests clone)
  #:use-module (git)
  #:use-module ((git configuration)
                #:select (%have-fetch-options-depth?
                          %have-GIT_OPT_SET_SERVER_CONNECT_TIMEOUT?))
  #:use-module (tests helpers)
  #:use-module (tests ssh)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-64)
  #:use-module (ice-9 match))

(test-begin "clone")

(libgit2-init!)

(define (make-ssh-url dir port)
  (format #f "ssh://localhost:~a/~a" port dir))

(define ssh-server-port 8899)

(define (ssh-fetch-options auth-method)
  (let* ((options (make-fetch-options auth-method))
         (callbacks (fetch-options-remote-callbacks options)))
    ;; Proceed even if the server's certificate is not in ~/.ssh/known_hosts.
    (set-remote-callbacks-certificate-check! callbacks (const #t))
    options))

(define (clone-test directory auth-method)
  (let* ((repo-dir (in-vicinity (getcwd) directory))
         (clone-dir (in-vicinity repo-dir "out")))
    (clone (make-ssh-url repo-dir ssh-server-port)
           clone-dir
           (make-clone-options #:fetch-options (ssh-fetch-options auth-method)))
    (let* ((repository (repository-open clone-dir))
           (oid (reference-target (repository-head repository))))
      (oid->string (commit-id (commit-lookup repository oid))))))

;; The following tests are skipped when sshd is unavailable.
(with-sshd-server ssh-server-port

  (with-repository "simple-bare" directory
    (test-equal "clone-auth-ssh-credentials"
      "3f848a1a52416ac99a5c5bf2e6bd55eb7b99d55b"
      (clone-test directory (make-client-ssh-auth))))

  (with-repository "simple-bare" directory
    (test-equal "clone-auth-ssh-agent"
      "3f848a1a52416ac99a5c5bf2e6bd55eb7b99d55b"
      (with-ssh-agent
       (clone-test directory (%make-auth-ssh-agent)))))

  (with-repository "simple-bare" directory
    (test-assert "clone-and-fetch-auth-ssh-credentials"
      (let* ((auth (make-client-ssh-auth))
             (do-clone (clone-test directory auth))
             (clone-dir (in-vicinity directory "out"))
             (repository (repository-open clone-dir))
             (remote (remote-lookup repository "origin"))
             (options (make-fetch-options auth))
             (callbacks (fetch-options-remote-callbacks options)))
        ;; Proceed even if the server's certificate is not in
        ;; ~/.ssh/known_hosts.
        (set-remote-callbacks-certificate-check! callbacks (const #t))
        (remote-fetch remote #:fetch-options options)
        #t)))

  (test-assert "clone + transfer-progress"
    (with-repository "simple-bare" repository-directory
      (let ((stats '()))                          ;list of <indexer-progress>
        (let* ((checkout-directory (in-vicinity repository-directory
                                                "checkout"))
               (transfer-progress (lambda (progress)
                                    (set! stats (cons progress stats))
                                    #t))
               (fetch-options (make-fetch-options (make-client-ssh-auth)
                                                  #:transfer-progress
                                                  transfer-progress))
               (callbacks (fetch-options-remote-callbacks fetch-options)))

          ;; Proceed even if the server's certificate is not in
          ;; ~/.ssh/known_hosts.
          (set-remote-callbacks-certificate-check! callbacks (const #t))
          (clone (make-ssh-url (canonicalize-path repository-directory)
                               ssh-server-port)
                 checkout-directory
                 (make-clone-options #:fetch-options fetch-options)))

        ;; Make sure the <indexer-progress> records we got exhibit
        ;; monotonic growth.
        (match (reverse stats)
          ((first rest ...)
           (let ((max (indexer-progress-total-objects first)))
             (equal? (map indexer-progress-received-objects
                          (take (cons first rest) (+ max 1)))
                     (iota (+ max 1))))))))))

(unless %have-GIT_OPT_SET_SERVER_CONNECT_TIMEOUT? (test-skip 1))
(test-equal "clone beyond timeout"
  (list GIT_ERROR GITERR_NET)

  ;; Listen on a socket but never accept(2) on it; try to clone from it and
  ;; make sure the configured connection timeout kicks in.
  (let ((sock (socket AF_INET SOCK_STREAM 0)))
    (bind sock AF_INET INADDR_LOOPBACK 0)
    (listen sock 1)
    (let ((tcp-port (sockaddr:port (getsockname sock))))
      (set-server-connection-timeout! 1000)       ;at most one second
      (catch 'git-error
        (lambda ()
          (let ((directory (string-append (or (getenv "TMPDIR") "/tmp")
                                          "/guile-git-checkout-test") ))
            (with-directory directory
              (clone (string-append "http://127.0.0.1:"
                                    (number->string tcp-port))
                     directory))))
        (lambda (key error . _)
          (list (git-error-code error)
                (git-error-class error)))))))

(libgit2-shutdown!)

(test-end)

;; Local Variables:
;; eval: (put 'with-sshd-server 'scheme-indent-function 1)
;; End: