File: describe.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 (188 lines) | stat: -rw-r--r-- 8,187 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
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
;;; Guile-Git --- GNU Guile bindings of libgit2
;;; Copyright © 2019, 2021 Marius Bakke <marius@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 (git describe)
  #:use-module (system foreign)
  #:use-module (git errors)
  #:use-module (git bindings)
  #:use-module (git types)
  #:use-module (git structs)
  #:use-module (git repository)
  #:use-module (git reference)
  #:use-module (git commit)
  #:use-module (git oid)
  #:export (DESCRIBE-MAX-CANDIDATES
            DESCRIBE-STRATEGY-DEFAULT
            DESCRIBE-STRATEGY-TAGS
            DESCRIBE-STRATEGY-ALL
            DESCRIBE-STRATEGY
            DESCRIBE-PATTERN
            DESCRIBE-ONLY-FOLLOW-FIRST-PARENT?
            DESCRIBE-FALLBACK-TO-OID?
            make-describe-options
            describe-commit
            describe-workdir

            DESCRIBE-FORMAT-ABBREVIATED-SIZE
            DESCRIBE-FORMAT-ALWAYS-USE-LONG-FORMAT?
            DESCRIBE-FORMAT-DIRTY-SUFFIX
            make-describe-format-options
            describe-format

            describe-checkout))

;;; https://libgit2.org/libgit2/#HEAD/group/describe

(define DESCRIBE-OPTIONS-VERSION 1)

(define DESCRIBE-MAX-CANDIDATES 10)
(define DESCRIBE-STRATEGY-DEFAULT 0)
(define DESCRIBE-STRATEGY-TAGS 1)
(define DESCRIBE-STRATEGY-ALL 2)
(define DESCRIBE-STRATEGY 'default)
(define DESCRIBE-PATTERN "")
(define DESCRIBE-ONLY-FOLLOW-FIRST-PARENT? #f)
(define DESCRIBE-FALLBACK-TO-OID? #f)

(define (symbol->describe-strategy symbol)
  (case symbol
    ((default) DESCRIBE-STRATEGY-DEFAULT)
    ((tags) DESCRIBE-STRATEGY-TAGS)
    ((all) DESCRIBE-STRATEGY-ALL)
    (else (raise-git-error GIT_EINVALID))))

(define make-describe-options
  ;; Return a <describe-options> structure for use with DESCRIBE-COMMIT
  ;; or DESCRIBE-WORKDIR.  MAX-CANDIDATES specifies how many tags will
  ;; be considered when finding the descriptive name; use 0 to not
  ;; consider any tags at all.  STRATEGY can be either 'default to only
  ;; consider annotated tags, 'tags to consider all tags, or 'all to
  ;; also consider branch names.  If PATTERN is a non-empty string, only
  ;; refs matching the given glob(7) pattern are considered.
  ;; ONLY-FOLLOW-FIRST-PARENT? will ignore tags that are not direct
  ;; ancestors of the commit.  When FALLBACK-TO-OID? is true, the commit
  ;; ID will be returned if no matching tags were found.
  (let ((proc (libgit2->procedure* "git_describe_init_options"
                                   `(* ,unsigned-int))))
    (lambda* (#:key
              (max-candidates DESCRIBE-MAX-CANDIDATES)
              (strategy DESCRIBE-STRATEGY)
              (pattern DESCRIBE-PATTERN)
              (only-follow-first-parent? DESCRIBE-ONLY-FOLLOW-FIRST-PARENT?)
              (fallback-to-oid? DESCRIBE-FALLBACK-TO-OID?))
      (let ((describe-options (make-describe-options-bytestructure))
            (strategy (symbol->describe-strategy strategy)))
        (proc (describe-options->pointer describe-options) DESCRIBE-OPTIONS-VERSION)
        (set-describe-options-max-candidates-tag! describe-options max-candidates)
        (when (> strategy 0)
          (set-describe-options-strategy! describe-options strategy))
        (when (> (string-length pattern) 0)
          (set-describe-options-pattern! describe-options (string->pointer pattern)))
        (when only-follow-first-parent?
          (set-describe-options-only-follow-first-parent! describe-options 1))
        (when fallback-to-oid?
          (set-describe-options-show-commit-oid-as-fallback! describe-options 1))

        describe-options))))

(define %describe-result-free (libgit2->pointer "git_describe_result_free"))

(define (pointer->describe-result! pointer)
  (set-pointer-finalizer! pointer %describe-result-free)
  (pointer->describe-result pointer))

(define* describe-commit
  (let ((proc (libgit2->procedure* "git_describe_commit" '(* * *))))
    (lambda* (commit #:optional (options (make-describe-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (commit->pointer commit)
              (describe-options->pointer options))
        (pointer->describe-result! (dereference-pointer out))))))

(define* describe-workdir
  (let ((proc (libgit2->procedure* "git_describe_workdir" '(* * *))))
    (lambda* (repository #:optional (options (make-describe-options)))
      (let ((out (make-double-pointer)))
        (proc out
              (repository->pointer repository)
              (describe-options->pointer options))
        (pointer->describe-result! (dereference-pointer out))))))



(define DESCRIBE-FORMAT-OPTIONS-VERSION 1)

(define DESCRIBE-FORMAT-ABBREVIATED-SIZE 7)
(define DESCRIBE-FORMAT-ALWAYS-USE-LONG-FORMAT? #f)
(define DESCRIBE-FORMAT-DIRTY-SUFFIX "")

;; Return a <describe-format-options> structure for formatting the
;; output of DESCRIBE-FORMAT.  ABBREVIATED-SIZE specifies how many
;; characters of the commit ID to use.  When ALWAYS-USE-LONG-FORMAT? is
;; true, the returned string will be formatted with tag, number of
;; commits and abbreviated commit ID even when it exactly matches a tag.
;; DIRTY-SUFFIX is an optional string that will be appended to the output
;; when the worktree is considered 'dirty', i.e. modified.
(define make-describe-format-options
  (let ((proc (libgit2->procedure* "git_describe_init_format_options"
                                   `(* ,unsigned-int))))
    (lambda* (#:key
              (abbreviated-size DESCRIBE-FORMAT-ABBREVIATED-SIZE)
              (always-use-long-format? DESCRIBE-FORMAT-ALWAYS-USE-LONG-FORMAT?)
              (dirty-suffix DESCRIBE-FORMAT-DIRTY-SUFFIX))
      (let ((describe-format-options
             (make-describe-format-options-bytestructure)))
        (proc (describe-format-options->pointer describe-format-options)
              DESCRIBE-FORMAT-OPTIONS-VERSION)
        (set-describe-format-options-abbreviated-size! describe-format-options
                                                       abbreviated-size)
        (when always-use-long-format?
          (set-describe-format-options-always-use-long-format!
           describe-format-options 1))
        (when (> (string-length dirty-suffix) 0)
          (set-describe-format-options-dirty-suffix!
           describe-format-options (string->pointer dirty-suffix)))

        describe-format-options))))

(define describe-format
  (let ((proc (libgit2->procedure* "git_describe_format" '(* * *))))
    (lambda* (result #:optional (options (make-describe-format-options)))
      (let ((out (make-buffer)))
        (proc out (describe-result->pointer result)
              (describe-format-options->pointer options))
        (let ((out* (buffer-content/string out)))
          (free-buffer out)
          out*)))))


;; Describe CHECKOUT, optionally with OPTIONS, and return two values:
;; the current HEAD and its \"pretty name\".
(define describe-checkout
  (lambda* (checkout #:optional (options (make-describe-options
                                          ;; Consider unannotated tags.
                                          #:strategy 'tags
                                          ;; ...but not their ancestors.
                                          #:only-follow-first-parent? #t)))
    (let* ((repo (repository-open checkout))
           (head (reference-target (repository-head repo)))
           (commit (commit-lookup repo head))
           (description (describe-commit commit options)))
      (repository-close! repo)
      (values (oid->string head) (describe-format description)))))