aboutsummaryrefslogtreecommitdiff
path: root/guix-qa-frontpage/git-repository.scm
blob: 3820646408dde6aedd2f52429d7a902c693f26f3 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
(define-module (guix-qa-frontpage git-repository)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-19)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 threads)
  #:use-module (ice-9 exceptions)
  #:use-module (fibers scheduler)
  #:use-module (git)
  #:use-module (git rev-parse)
  #:use-module (git object)
  #:use-module (git oid)
  #:use-module (git structs)
  #:use-module (guix build utils)
  #:use-module ((guix build utils) #:select (with-directory-excursion))
  #:export (%git-repository-location
            %git-repository-patches-remote-use-ssh?

            ensure-repository-exists!
            update-repository!
            with-bare-git-repository
            with-git-worktree

            get-commit

            get-git-branch-head-committer-date
            get-git-merge-base
            get-git-remote-branches))

(define %git-repository-location
  (make-parameter #f))

(define %git-repository-patches-remote-use-ssh?
  (make-parameter #f))

(define* (patches-repository-remote-url
          #:key (use-ssh? (%git-repository-patches-remote-use-ssh?)))
  (if use-ssh?
      "git@git.qa.guix.gnu.org:guix-patches"
      "https://git.qa.guix.gnu.org/git/guix-patches"))

(define* (guix.git-excursion thunk #:optional dir)
  (when (current-scheduler)
    (error "guix.git-excursion can't be used from fibers"))

  (monitor
   (with-directory-excursion (or dir (%git-repository-location))
     (thunk))))

(define (ensure-repository-exists!)
  (unless (file-exists? (%git-repository-location))
    (when (current-scheduler)
      (error "guix.git-excursion can't be used from fibers"))
    (monitor
     (unless (file-exists? (%git-repository-location))
       (invoke "git" "init" "--bare"
               (%git-repository-location))

       (guix.git-excursion
        (lambda ()
          (invoke "git" "remote" "add" "origin"
                  "https://git.guix.gnu.org/guix.git")
          (invoke "git" "remote" "add" "patches"
                  (patches-repository-remote-url))

          (invoke "git" "config" "user.name" "Guix Patches Tester")
          (invoke "git" "config" "user.email" "")))))))

(define (update-repository!)
  (let* ((repo (repository-open (%git-repository-location)))
         (patches-remote
          (remote-lookup repo "patches"))
         (current-patches-url
          (remote-url patches-remote)))
    (unless (string=?
             (patches-repository-remote-url)
             current-patches-url)
      (remote-set-url! repo
                       "patches"
                       (patches-repository-remote-url))))

  (with-bare-git-repository
   (lambda ()
     (invoke "git" "prune")
     (invoke "git" "fetch" "--prune" "origin")
     (invoke "git" "fetch" "--prune" "patches")
     (invoke "git" "fetch" "--force" "--tags" "patches"))))

(define (with-bare-git-repository thunk)
  (ensure-repository-exists!)

  (guix.git-excursion thunk))

(define* (with-git-worktree name commit thunk
                            #:key remove-after?)
  (with-bare-git-repository
   (lambda ()
     (invoke "git" "worktree" "add"
             (string-append "../" name)
             "-b" name
             commit)))

  (guix.git-excursion thunk name)

  (when remove-after?
    (with-bare-git-repository
     (lambda ()
       (system* "git" "worktree" "remove" "--force" name)
       (system* "git" "branch" "-D" name)))))

(define (get-commit ref)
  (ensure-repository-exists!)

  (with-exception-handler
      (lambda (exn)
        (if (and (exception-with-irritants? exn)
                 (let ((irritant (first (exception-irritants exn))))
                   (and (git-error? irritant)
                        (= (git-error-code irritant) -3))))
            #f
            (raise-exception exn)))
    (lambda ()
      (let ((repo (repository-open (%git-repository-location))))
        (oid->string (object-id (revparse-single repo ref)))))
    #:unwind? #t))

(define (get-git-branch-head-committer-date branch)
  (with-bare-git-repository
   (lambda ()
     (let ((pipe (open-pipe* OPEN_READ
                             "git" "show" "-s" "--format=%ci" branch "--")))
       (let loop ((line (read-line pipe))
                  (lines '()))
         (if (eof-object? line)
             (let ((exit-val
                    (status:exit-val (close-pipe pipe))))
               (unless (= 0 exit-val)
                 (raise-exception
                  (make-exception-with-message
                   (simple-format #f "error doing git show on ~A (exit-val: ~A)"
                                  branch
                                  exit-val))))

               (if (null? lines)
                   #f
                   (string->date (first lines)
                                 "~Y-~m-~d ~H:~M:~S ~z")))
             (loop (read-line pipe)
                   (cons line lines))))))))

(define (get-git-merge-base a b)
  (with-bare-git-repository
   (lambda ()
     (let ((pipe (open-pipe* OPEN_READ
                             "git" "merge-base" a b)))
       (let loop ((line (read-line pipe))
                  (lines '()))
         (if (eof-object? line)
             (let ((exit-val
                    (status:exit-val (close-pipe pipe))))
               (unless (= 0 exit-val)
                 (raise-exception
                  (make-exception-with-message
                   (simple-format #f "error doing merge-base on ~A ~A (exit-val: ~A)"
                                  a
                                  b
                                  exit-val))))

               (if (null? lines)
                   #f
                   (first lines)))
             (loop (read-line pipe)
                   (cons line lines))))))))

(define (get-git-remote-branches remote)
  (with-bare-git-repository
   (lambda ()
     (let ((pipe (open-pipe* OPEN_READ
                             "git" "ls-remote" "--heads" remote)))
       (let loop ((line (read-line pipe))
                  (result '()))
         (if (eof-object? line)
             (let ((exit-val
                    (status:exit-val (close-pipe pipe))))
               (unless (= 0 exit-val)
                 (raise-exception
                  (make-exception-with-message
                   (simple-format #f "error doing ls-remote on ~A (exit-val: ~A)"
                                  remote
                                  exit-val))))
               result)
             (let ((commit (string-take line 40))
                   (branch (string-drop line 52)))
               (loop (read-line pipe)
                     (cons
                      `(("name" . ,branch)
                        ("commit" . ,commit))
                      result)))))))))