aboutsummaryrefslogtreecommitdiffstats
path: root/elpa/magit-20220503.1245/magit-bundle.el
blob: 0310ed84d2f1c8eff684beef00d467f783e4f8c0 (plain) (blame)
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
;;; magit-bundle.el --- Bundle support for Magit  -*- lexical-binding:t -*-

;; Copyright (C) 2008-2022 The Magit Project Contributors

;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>

;; SPDX-License-Identifier: GPL-3.0-or-later

;; Magit 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.
;;
;; Magit 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 Magit.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'magit)

;;; Commands

;;;###autoload (autoload 'magit-bundle "magit-bundle" nil t)
(transient-define-prefix magit-bundle ()
  "Create or verify Git bundles."
  :man-page "git-bundle"
  ["Actions"
   ("c" "create"     magit-bundle-create)
   ("v" "verify"     magit-bundle-verify)
   ("l" "list-heads" magit-bundle-list-heads)])

;;;###autoload (autoload 'magit-bundle-import "magit-bundle" nil t)
(transient-define-prefix magit-bundle-create (&optional file refs args)
  "Create a bundle."
  :man-page "git-bundle"
  ["Arguments"
   ("-a" "Include all refs" "--all")
   ("-b" "Include branches" "--branches=" :allow-empty t)
   ("-t" "Include tags"     "--tags="     :allow-empty t)
   ("-r" "Include remotes"  "--remotes="  :allow-empty t)
   ("-g" "Include refs"     "--glob=")
   ("-e" "Exclude refs"     "--exclude=")
   (magit-log:-n)
   (magit-log:--since)
   (magit-log:--until)]
  ["Actions"
   ("c" "create regular bundle" magit-bundle-create)
   ("t" "create tracked bundle" magit-bundle-create-tracked)
   ("u" "update tracked bundle" magit-bundle-update-tracked)]
  (interactive
   (and (eq transient-current-command 'magit-bundle-create)
        (list (read-file-name "Create bundle: " nil nil nil
                              (concat (file-name-nondirectory
                                       (directory-file-name (magit-toplevel)))
                                      ".bundle"))
              (magit-completing-read-multiple* "Refnames (zero or more): "
                                               (magit-list-refnames))
              (transient-args 'magit-bundle-create))))
  (if file
      (magit-git-bundle "create" file refs args)
    (transient-setup 'magit-bundle-create)))

;;;###autoload
(defun magit-bundle-create-tracked (file tag branch refs args)
  "Create and track a new bundle."
  (interactive
   (let ((tag    (magit-read-tag "Track bundle using tag"))
         (branch (magit-read-branch "Bundle branch"))
         (refs   (magit-completing-read-multiple*
                  "Additional refnames (zero or more): "
                  (magit-list-refnames))))
     (list (read-file-name "File: " nil nil nil (concat tag ".bundle"))
           tag branch
           (if (equal branch (magit-get-current-branch))
               (cons "HEAD" refs)
             refs)
           (transient-args 'magit-bundle-create))))
  (magit-git-bundle "create" file (cons branch refs) args)
  (magit-git "tag" "--force" tag branch
             "-m" (concat ";; git-bundle tracking\n"
                          (pp-to-string `((file   . ,file)
                                          (branch . ,branch)
                                          (refs   . ,refs)
                                          (args   . ,args))))))

;;;###autoload
(defun magit-bundle-update-tracked (tag)
  "Update a bundle that is being tracked using TAG."
  (interactive (list (magit-read-tag "Update bundle tracked by tag" t)))
  (let (msg)
    (let-alist (magit--with-temp-process-buffer
                 (save-excursion
                   (magit-git-insert "for-each-ref" "--format=%(contents)"
                                     (concat "refs/tags/" tag)))
                 (setq msg (buffer-string))
                 (ignore-errors (read (current-buffer))))
      (unless (and .file .branch)
        (error "Tag %s does not appear to track a bundle" tag))
      (magit-git-bundle "create" .file
                        (cons (concat tag ".." .branch) .refs)
                        .args)
      (magit-git "tag" "--force" tag .branch "-m" msg))))

;;;###autoload
(defun magit-bundle-verify (file)
  "Check whether FILE is valid and applies to the current repository."
  (interactive (list (magit-bundle--read-file-name "Verify bundle: ")))
  (magit-process-buffer)
  (magit-git-bundle "verify" file))

;;;###autoload
(defun magit-bundle-list-heads (file)
  "List the refs in FILE."
  (interactive (list (magit-bundle--read-file-name "List heads of bundle: ")))
  (magit-process-buffer)
  (magit-git-bundle "list-heads" file))

(defun magit-bundle--read-file-name (prompt)
  (read-file-name prompt nil nil t (magit-file-at-point) #'file-regular-p))

(defun magit-git-bundle (command file &optional refs args)
  (magit-git "bundle" command (magit-convert-filename-for-git file) refs args))

;;; _
(provide 'magit-bundle)
;;; magit-bundle.el ends here