aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/poll-git-repository.scm
blob: 124c559549257f77e88d22235df39cf8d04b6245 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2023 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program 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
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-data-service poll-git-repository)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-19)
  #:use-module (srfi srfi-71)
  #:use-module (ice-9 threads)
  #:use-module (squee)
  #:use-module (git)
  #:use-module (guix git)
  #:use-module (guix channels)
  #:use-module ((guix build syscalls)
                #:select (set-thread-name))
  #:use-module (guix-data-service database)
  #:use-module (guix-data-service model git-repository)
  #:use-module (guix-data-service model git-branch)
  #:use-module (guix-data-service model git-commit)
  #:use-module (guix-data-service jobs load-new-guix-revision)
  #:export (start-thread-to-poll-git-repository))

(define (start-thread-to-poll-git-repository git-repository-id)
  (call-with-new-thread
   (lambda ()
     (catch 'system-error
       (lambda ()
         (set-thread-name
          (simple-format #f "poll git ~A"
                         git-repository-id)))
       (const #t))

     (libgit2-init!)
     (honor-system-x509-certificates!)

     (with-postgresql-connection
      (simple-format #f "poll-git-repository-~A"
                     git-repository-id)
      (lambda (conn)
        (let loop ()
          (with-exception-handler
              (lambda (exn)
                (simple-format #t "exception when polling git repository (~A): ~A\n"
                               git-repository-id exn))
            (lambda ()
              (with-throw-handler #t
                (lambda ()
                  (poll-git-repository conn git-repository-id))
                (lambda _
                  (backtrace))))
            #:unwind? #t)

          (and=>
           (fifth (select-git-repository conn git-repository-id))
           (lambda (poll-interval)
             (sleep poll-interval)
             (loop)))))))))

(define* (just-update-cached-checkout url
                                      #:key
                                      (ref '())
                                      recursive?
                                      (cache-directory
                                       (url-cache-directory
                                        url (%repository-cache-directory)
                                        #:recursive? recursive?)))
  (let* ((cache-exists? (openable-repository? cache-directory))
         (repository    (if cache-exists?
                            (repository-open cache-directory)
                            ((@@ (guix git) clone/swh-fallback)
                             url ref cache-directory))))
    ;; Only fetch remote if it has not been cloned just before.
    (when cache-exists?
      (remote-fetch (remote-lookup repository "origin")
                    #:fetch-options ((@@ (guix git) make-default-fetch-options))))
    (repository-close! repository)))

(define (poll-git-repository conn git-repository-id)
  (define git-repository-details
    (select-git-repository conn git-repository-id))

  ;; Obtain a session level lock here, to avoid conflicts with other jobs over
  ;; the Git repository.
  (with-advisory-session-lock/log-time
   conn
   'latest-channel-instances
   (lambda ()
     ;; This was using update-cached-checkout, but it wants to checkout
     ;; refs/remotes/origin/HEAD by default, and that can fail for some reason
     ;; on some repositories:
     ;;
     ;;   reference 'refs/remotes/origin/HEAD' not found
     ;;
     ;; I just want to update the cached checkout though, so trying to
     ;; checkout some revision is unnecessary, hence
     ;; just-update-cached-checkout
     (just-update-cached-checkout (second git-repository-details))

     (let* ((repository-directory
             (url-cache-directory
              (second git-repository-details)))

            (included-branches
             excluded-branches
             (select-includes-and-excluded-branches-for-git-repository
              conn
              git-repository-id))

            ;; remote-ls returns remote-head's where the oid's aren't like the
            ;; oid's found through branches, and I'm not sure how to handle
            ;; them. Work around this by just using remote-ls to check what
            ;; branches exist on the remote.
            (remote-branch-names
             (with-repository repository-directory repository
               (let ((remote (remote-lookup repository "origin")))
                 (remote-connect remote)

                 (filter-map
                  (lambda (rh)
                    (let ((name (remote-head-name rh)))
                      (if (string-prefix? "refs/heads/" name)
                          (string-drop name
                                       (string-length "refs/heads/"))
                          #f)))
                  (remote-ls remote)))))

            (repository-branches
             (with-repository repository-directory repository
               (filter-map
                (lambda (branch-reference)
                  (let* ((branch-name
                          (string-drop (reference-shorthand branch-reference)
                                       (string-length "origin/"))))
                    (and
                     ;; branch-list may list branches which don't exist on the
                     ;; remote, so use the information from remote-ls to
                     ;; filter them out
                     (member branch-name remote-branch-names)
                     (cons
                      branch-name
                      ;; TODO Not sure what the right way to do this is
                      (and=> (false-if-exception
                              (reference-target branch-reference))
                             oid->string)))))
                (branch-list repository BRANCH-REMOTE)))))

       (with-postgresql-transaction
        conn
        (lambda (conn)
          (exec-query conn "LOCK TABLE git_commits IN EXCLUSIVE MODE")

          (let* ((repository-branch-details
                  (all-branches-with-most-recent-commit conn
                                                        git-repository-id))
                 (branch-names
                  (filter
                   (lambda (branch-name)
                     (let ((excluded-branch?
                            (member branch-name excluded-branches string=?))
                           (included-branch?
                            (member branch-name included-branches string=?)))
                       (and (not excluded-branch?)
                            (or (null? included-branches)
                                included-branch?))))
                   (delete-duplicates!
                    (append!
                     (map car repository-branches)
                     (map car repository-branch-details))))))

            (for-each
             (lambda (branch-name)
               (define (git-branch-entry)
                 (or (git-branch-for-repository-and-name
                      conn
                      git-repository-id
                      branch-name)
                     (insert-git-branch-entry
                      conn
                      git-repository-id
                      branch-name)))

               (let ((repository-commit
                      (assoc-ref repository-branches branch-name))
                     (database-commit
                      (and=> (assoc-ref repository-branch-details
                                        branch-name)
                             first)))
                 (if repository-commit
                     (if (and database-commit
                              (string=? database-commit
                                        repository-commit))
                         #f ;; Nothing to do
                         (begin
                           (insert-git-commit-entry conn
                                                    (git-branch-entry)
                                                    repository-commit
                                                    (current-date 0))

                           (unless #f
                             (enqueue-load-new-guix-revision-job
                              conn
                              git-repository-id
                              repository-commit
                              "poll"))))
                     (if (or (not database-commit)
                             (string=? database-commit ""))
                         #f ;; Nothing to do
                         (insert-git-commit-entry conn
                                                  (git-branch-entry)
                                                  ""
                                                  (current-date 0))))))
             branch-names))))))))