Skip to content

Commit

Permalink
[radian-software#2] Add option for shallow clones (--depth N)
Browse files Browse the repository at this point in the history
This option is applied only for packages without version lock.

Merges radian-software#372.
  • Loading branch information
shwaka authored and hartzell committed Jun 14, 2019
1 parent d618ccb commit fcac138
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,11 @@ These are the keywords meaningful for the `git` backend:
you can use the `fetch-from-upstream` method to operate on the
upstream instead. The allowed keywords are `:repo`, `:host`,
`:branch`, and `:remote`.
* `:depth`: either the symbol `full` or an integer. If `full`, then
the repository is cloned with its whole history. If an integer `N`,
then the repository is cloned with the option `--depth N`, unless a
commit is specified (e.g. by version lockfiles). The default value
is `full`.

This section tells you how the `git` backend, specifically, implements
the version-control backend API:
Expand Down Expand Up @@ -1861,6 +1866,11 @@ You can customize the following user options:
will quietly do fast-forward, to suppress asking for instructions on
each package with updates, unless they're not trivial. Set to nil if
you'd prefer to inspect all changes.
* `straight-vc-git-default-clone-depth`: the default value for the
`:depth` keyword. It can be either the symbol `full` or an integer,
and defaults to `full`. Setting this variable to a small integer will
reduce the size of repositories. Note that this variable does *not*
affect packages whose versions are locked.

##### Deprecated `:upstream` keyword

Expand Down
2 changes: 1 addition & 1 deletion install.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

(let (;; This needs to have a default value, just in case the user
;; doesn't have any lockfiles.
(version :uranus)
(version :neptune)
(straight-profiles (if (boundp 'straight-profiles)
straight-profiles
'((nil . "default")))))
Expand Down
58 changes: 51 additions & 7 deletions straight.el
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,45 @@ with the remotes."
(straight-vc-git--ensure-head local-repo branch))
(straight-register-repo-modification local-repo)))))

(defcustom straight-vc-git-default-clone-depth 'full
"The default value for `:depth' when `:type' is the symbol `git'.
The value should be the symbol `full' or an integer. If the value
is `full', clone the whole history of repositories. If the value is
an integer N, remote repositories are cloned with the option --depth N,
unless a commit is specified (e.g. by version lockfiles)."
:group 'straight
:type '(choice integer (const full)))

(cl-defun straight-vc-git--clone-internal
(&key depth upstream-remote url repo-dir branch)
"Clone a remote repository from URL.
If DEPTH is the symbol `full', clone the whole history of the repository.
If DEPTH is an integer, clone with the option --depth DEPTH --branch BRANCH.
If this fails, try again to clone without the option --depth and --branch,
as a fallback."
(cond
((eq depth 'full)
;; Clone the whole history of the repository.
(straight--get-call
"git" "clone" "--origin" upstream-remote
"--no-checkout" url repo-dir))
((integerp depth)
;; Do a shallow clone.
(condition-case nil
(straight--get-call
"git" "clone" "--origin" upstream-remote
"--no-checkout" url repo-dir
"--depth" (number-to-string depth)
"--branch" branch)
;; Fallback for dumb http protocol.
(error (straight-vc-git--clone-internal :depth 'full
:upstream-remote upstream-remote
:url url
:repo-dir repo-dir))))
(t (error "Invalid value %S of depth for %s" depth url))))

;;;;;; API

(defun straight-vc-git-clone (recipe commit)
Expand All @@ -1948,17 +1987,22 @@ specified in RECIPE instead. If that fails, signal a warning."
(straight-vc-git--destructure recipe
(package local-repo branch remote upstream-repo upstream-host
upstream-remote fork-repo fork-host
fork-remote nonrecursive)
fork-remote nonrecursive depth)
(unless upstream-repo
(error "No `:repo' specified for package `%s'" package))
(let ((success nil)
(repo-dir (straight--repos-dir local-repo))
(url (straight-vc-git--encode-url upstream-repo upstream-host)))
(url (straight-vc-git--encode-url upstream-repo upstream-host))
(depth (or (when commit 'full)
depth
straight-vc-git-default-clone-depth)))
(unwind-protect
(progn
(straight--get-call
"git" "clone" "--origin" upstream-remote
"--no-checkout" url repo-dir)
(straight-vc-git--clone-internal :depth depth
:upstream-remote upstream-remote
:url url
:repo-dir repo-dir
:branch branch)
(let ((straight--default-directory nil)
(default-directory repo-dir))
(when fork-repo
Expand Down Expand Up @@ -2095,7 +2139,7 @@ then returned."

(defun straight-vc-git-keywords ()
"Return a list of keywords used by the VC backend for Git."
'(:repo :host :branch :remote :nonrecursive :upstream :fork))
'(:repo :host :branch :remote :nonrecursive :upstream :fork :depth))

;;;; Fetching repositories

Expand Down Expand Up @@ -4966,7 +5010,7 @@ according to the value of `straight-profiles'."
;;
;; The version keyword comes after the versions alist so
;; that you can ignore it if you don't need it.
"(%s)\n:uranus\n"
"(%s)\n:neptune\n"
(mapconcat
(apply-partially #'format "%S")
versions-alist
Expand Down

0 comments on commit fcac138

Please sign in to comment.