forked from dimitri/el-get
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathel-get-git.el
221 lines (203 loc) · 9.82 KB
/
el-get-git.el
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
;;; el-get --- Manage the external elisp bits and pieces you depend upon
;;
;; Copyright (C) 2010-2011 Dimitri Fontaine
;;
;; Author: Dimitri Fontaine <[email protected]>
;; URL: http://www.emacswiki.org/emacs/el-get
;; GIT: https://github.com/dimitri/el-get
;; Licence: WTFPL, grab your copy here: http://sam.zoy.org/wtfpl/
;;
;; This file is NOT part of GNU Emacs.
;;
;; Install
;; Please see the README.md file from the same distribution
(require 'el-get-core)
(require 'el-get-recipes)
(defcustom el-get-git-clone-hook nil
"Hook run after git clone."
:group 'el-get
:type 'hook)
(defcustom el-get-git-shallow-clone nil
"If t, then run git-clone with `--depth 1'."
:group 'el-get
:type 'boolean)
(defcustom el-get-git-known-smart-domains '("www.github.com" "www.bitbucket.org" "repo.or.cz" "code.orgmode.org")
"List of domains which are known to support shallow clone, el-get will not make
explicit checks for these"
:group 'el-get
:type 'list)
(defun el-get-git-executable ()
"Return git executable to use, or signal an error when not
found."
(let ((git-executable (if (and (boundp 'magit-git-executable)
(file-executable-p magit-git-executable))
magit-git-executable
(executable-find "git"))))
(unless (and git-executable (file-executable-p git-executable))
(error
(concat "el-get-git-clone requires `magit-git-executable' to be set, "
"or the binary `git' to be found in your PATH")))
git-executable))
(defun el-get-git-url-from-known-smart-domains-p (url)
"Check if URL belongs to known smart domains, it basically looks up the url's
domain in `el-get-git-known-smart-domains'
This is needed because some domains like bitbucket support shallow clone even
though they do not indicate this in their response headers see
`el-get-git-is-host-smart-http-p'"
(let* ((host (el-get-url-host url))
;; Prepend www to domain, if it consists only of two components
(prefix (when (= (length (split-string host "\\.")) 2)
"www.")))
(member (concat prefix host) el-get-git-known-smart-domains)))
(defun el-get-git-is-host-smart-http-p (giturl)
"Detect if the host supports shallow clones using http(s). GITURL is url to
the git repository, this function is intended to be used only with http(s)
urls. The function uses the approach described here [http://stackoverflow.com/questions/9270488/]
Basically it makes a HEAD request and checks the Content-Type for 'smart' MIME
type. This approach does not work for some domains like `bitbucket', which do
not return 'smart' headers despite supporting shallow clones"
(let ((url-request-method "HEAD")
(req-url (format "%s%s/info/refs\?service\=git-upload-pack"
giturl
;; The url may not end with ".git" in which case we
;; need to add append ".git" to the url
(if (string-match "\\.git\\'" giturl)
""
".git")))
(smart-content-type "Content-Type: application/x-git-upload-pack-advertisement"))
(with-current-buffer (url-retrieve-synchronously req-url)
(goto-char (point-min))
(numberp (ignore-errors (search-forward-regexp smart-content-type))))))
(defun el-get-git-shallow-clone-supported-p (url)
"Check if shallow clone is supported for given URL"
;; All other protocols git, ssh and file support shallow clones
(or (not (string-prefix-p "http" url))
;; Check if url belongs to one of known smart domains
(el-get-git-url-from-known-smart-domains-p url)
;; If all else fails make an explicit call to check if shallow clone is
;; supported
(el-get-git-is-host-smart-http-p url)))
(defun el-get-git-clone (package url post-install-fun)
"Clone the given package following the URL."
(let* ((git-executable (el-get-executable-find "git"))
(pdir (el-get-package-directory package))
(pname (el-get-as-string package))
(name (format "*git clone %s*" package))
(source (el-get-package-def package))
(branch (plist-get source :branch))
(submodule-prop (plist-get source :submodule))
;; nosubmodule is true only if :submodules is explicitly set to nil.
(explicit-nosubmodule (and (plist-member source :submodule)
(not submodule-prop)))
(checkout (or (plist-get source :checkout)
(plist-get source :checksum)))
(shallow (when (el-get-git-shallow-clone-supported-p url)
(el-get-plist-get-with-default source :shallow
el-get-git-shallow-clone)))
(clone-args (append '("--no-pager" "clone")
(when shallow '("--depth" "1"))
(cond
;; With :checkout, the "git checkout"
;; command is a separate step, so don't
;; do it during cloning.
(checkout '("--no-checkout"))
;; With a specified branch, check that branch out
(branch (list "-b" branch))
;; Otherwise, just checkout the default branch
(t nil))
(list url pname)))
(ok (format "Package %s installed." package))
(ko (format "Could not install package %s." package)))
(el-get-insecure-check package url)
(el-get-start-process-list
package
(list
`(:command-name ,name
:buffer-name ,name
:default-directory ,el-get-dir
:program ,git-executable
:args ,clone-args
:message ,ok
:error ,ko)
(when checkout
(list :command-name (format "*git checkout %s*" checkout)
:buffer-name name
:default-directory pdir
:program git-executable
:args (list "--no-pager" "checkout" checkout)
:message (format "git checkout %s ok" checkout)
:error (format "Could not checkout %s for package %s" checkout package)))
(unless explicit-nosubmodule
(list :command-name "*git submodule update*"
:buffer-name name
:default-directory pdir
:program git-executable
:args (list "--no-pager" "submodule" "update" "--init" "--recursive")
:message "git submodule update ok"
:error "Could not update git submodules")))
post-install-fun)))
(defun el-get-git-pull (package url post-update-fun)
"git pull the package."
(let* ((git-executable (el-get-executable-find "git"))
(pdir (el-get-package-directory package))
(name (format "*git pull %s*" package))
(source (el-get-package-def package))
(submodule-prop (plist-get source :submodule))
;; nosubmodule is true only if :submodules is explicitly set to nil.
(explicit-nosubmodule (and (plist-member source :submodule)
(not submodule-prop)))
(checkout (or (plist-get source :checkout)
(plist-get source :checksum)))
;; When dealing with a specific checkout, we cannot use
;; "pull", but must instead use "fetch" and then "checkout".
(pull-args (list "--no-pager" (if checkout "fetch" "pull")))
(ok (format "Pulled package %s." package))
(ko (format "Could not update package %s." package)))
(el-get-insecure-check package url)
(el-get-start-process-list
package
`((:command-name ,name
:buffer-name ,name
:default-directory ,pdir
:program ,git-executable
:args ,pull-args
:message ,ok
:error ,ko)
,(when checkout
(list :command-name (format "*git checkout %s*" checkout)
:buffer-name name
:default-directory pdir
:program git-executable
:args (list "--no-pager" "checkout" checkout)
:message (format "git checkout %s ok" checkout)
:error (format "Could not checkout %s for package %s" checkout package)))
,(unless explicit-nosubmodule
`(:command-name "*git submodule update*"
:buffer-name ,name
:default-directory ,pdir
:program ,git-executable
:args ("--no-pager" "submodule" "update" "--init" "--recursive")
:message "git submodule update ok"
:error "Could not update git submodules")))
post-update-fun)))
(defun el-get-git-compute-checksum (package)
"Return the hash of the checked-out revision of PACKAGE."
(let ((default-directory (el-get-package-directory package)))
;; We cannot simply check the recipe for `:type git' because it
;; could also be github, emacsmirror, or any other unknown git-ish
;; type. Instead, we check for the existence of a ".git" directory
;; in the package directory. A better approach might be to call
;; "git status" and check that it returns success.
(assert (file-directory-p ".git") nil
"Package %s is not a git package" package)
(with-temp-buffer
(call-process (el-get-executable-find "git") nil '(t t) nil
"log" "--pretty=format:%H" "-n1")
(buffer-string))))
(el-get-register-method :git
:install #'el-get-git-clone
:update #'el-get-git-pull
:remove #'el-get-rmdir
:install-hook 'el-get-git-clone-hook
:compute-checksum #'el-get-git-compute-checksum)
(provide 'el-get-git)