Skip to content

Commit

Permalink
[#2] Shallow clones are compatible with lockfiles
Browse files Browse the repository at this point in the history
GitHub must have added the ability to fetch an unadvertised ref at
some point, because it sure seems to work now when it didn't before.
We take advantage in straight.el.
  • Loading branch information
raxod502 committed Apr 19, 2020
1 parent d562649 commit d28fd41
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1866,9 +1866,9 @@ These are the keywords meaningful for the `git` backend:
`: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`.
then the repository is cloned with the option `--depth N`. This
works even when 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 @@ -1937,8 +1937,8 @@ You can customize the following user options:
* `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.
reduce the size of repositories. This variable affects all packages,
even those whose versions are locked.

##### Deprecated `:upstream` keyword

Expand Down
73 changes: 45 additions & 28 deletions straight.el
Original file line number Diff line number Diff line change
Expand Up @@ -2139,36 +2139,54 @@ unless a commit is specified (e.g. by version lockfiles)."
:type '(choice integer (const full)))

(cl-defun straight-vc-git--clone-internal
(&key depth upstream-remote url repo-dir branch)
(&key depth remote url repo-dir branch commit)
"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.
If DEPTH is the symbol `full', clone the whole history of the
repository. If DEPTH is an integer, pass it to the --depth option
of git-clone to perform a shallow clone. If this fails, try again
to clone without the option --depth and --branch, as a fallback.
UPSTREAM-REMOTE is the name of the remote to use for the upstream
\(e.g. \"origin\"; see `straight-vc-git-default-remote-name').
URL and REPO-DIR are the positional arguments passed to
git-clone(1), and BRANCH is the name of the default
branch (although it won't be checked out as per --no-checkout)."
REMOTE is the name of the remote to use \(e.g. \"origin\"; see
`straight-vc-git-default-remote-name'). URL and REPO-DIR are the
positional arguments passed to git-clone(1), and BRANCH is the
name of the default branch (although it won't be checked out as
per --no-checkout).
If COMMIT is non-nil and DEPTH is not `full', then try to clone
only that specific commit from the remote. Fall back to doing a
clone of everything."
(cond
((eq depth 'full)
;; Clone the whole history of the repository.
(straight--get-call
"git" "clone" "--origin" upstream-remote
"git" "clone" "--origin" 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)
(if commit
(progn
(make-directory repo-dir)
(let ((straight--default-directory nil)
(default-directory repo-dir))
(straight--get-call
"git" "init")
(straight--get-call
"git" "remote" "add" remote url
"--master" branch)
(straight--get-call
"git" "fetch" remote commit
"--depth" (number-to-string depth))))
(delete-directory repo-dir 'recursive)
(straight--get-call
"git" "clone" "--origin" 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
:remote remote
:url url
:repo-dir repo-dir))))
(t (error "Invalid value %S of depth for %s" depth url))))
Expand All @@ -2182,29 +2200,28 @@ out, signal a warning. If COMMIT is nil, check out the branch
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 depth)
upstream-remote fork-repo repo host 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))
(depth (or (when commit 'full)
depth
straight-vc-git-default-clone-depth)))
(url (straight-vc-git--encode-url repo host))
(depth (or depth straight-vc-git-default-clone-depth)))
(unwind-protect
(progn
(straight-vc-git--clone-internal :depth depth
:upstream-remote upstream-remote
:remote remote
:url url
:repo-dir repo-dir
:branch branch)
:branch branch
:commit commit)
(let ((straight--default-directory nil)
(default-directory repo-dir))
(when fork-repo
(let ((url (straight-vc-git--encode-url fork-repo fork-host)))
(straight--get-call "git" "remote" "add" fork-remote url)
(straight--get-call "git" "fetch" fork-remote)))
(let ((url (straight-vc-git--encode-url
upstream-repo upstream-host)))
(straight--get-call "git" "remote" "add" upstream-remote url)
(straight--get-call "git" "fetch" upstream-remote)))
(when commit
(unless (straight--check-call "git" "checkout" commit)
(straight--warn
Expand Down

0 comments on commit d28fd41

Please sign in to comment.