Skip to contentSkip to conversation

support/download: make the git backend even more robust

PATCH Superseded Yann E. MORIN 1 participant 8 messages Apr 22, 2018

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover support/download: make the git backend even more robust

Hello All!

This series makes our git backend even more robust than wht we currently have. Especially, it will try to recover from a repository that was so utterly butchered that even a git-init can't salvage it.

The most significant changes are:

- ensure we can checkout from an unclean state;

- ensure we can checkout across changes in submodules setup;

- as a last-ditch recovery, trah the loccal cache and clone again from scratch.

Additionally, we also remove support for shallow clones, because they were in fact fundamentally broken, and did only work by chance.

Finally, we add a warning file, that the user should not use our git cache for development, neither directly in it or as a remote or the origin for worktrees.

Thanks a lot to Ricardo, Arnout and Thomas for their inputs during the discussions that led to this series. :-)

Regards,
Yann E. MORIN.

The following changes since commit d4158df6c19c76ea3405975b87f13b1c092a40e0

bluez5_utils: add patch to fix readline issue (2018-04-21 14:53:01 +0200)

are available in the git repository at:

git://git.buildroot.org/~ymorin/git/buildroot.git

for you to fetch changes up to cb54e8b49b631790559e87749f106a1ab9352128

download/git: add warning not to use or git cache (2018-04-22 10:59:30 +0200)

----------------------------------------------------------------
Yann E. MORIN (6):
download/git: run all git commands in the current directory download/git: try to recover from utterly-broken repositories download/git: ensure we checkout to a clean state download/git: ensure we can checkout repos with submodule conversions download/git: always do full-clone
download/git: add warning not to use or git cache

 support/download/git | 108 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 82 insertions(+), 26 deletions(-)
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#1 lore raw

PATCH v2 1/6 download/git: run all git commands in the current directory

That way, we can pushd earlier, which will help with last-ditch recovery in a followup commit.

Cc Maxime Hadjinlian <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
support/download/git +4 -4
index bf05c595a5..19a3e8ebee 100755
@@ -34,8 +34,10 @@ done
 
 shift $((OPTIND-1)) # Get rid of our options
 
-# We want to check if a cache of the git clone of this repo already exists.
+# Create and cd into the directory that will contain the local git cache
 git_cache="${dl_dir}/git"
+mkdir -p "${git_cache}"
+pushd "${git_cache}" >/dev/null
 
 # Caller needs to single-quote its arguments to prevent them from
 # being expanded a second time (in case there are spaces in them)
@@ -52,9 +54,7 @@ _git() {
 # We can still go through the wrapper, because 'init' does not use the
 # path pointed to by GIT_DIR, but really uses the directory passed as
 # argument.
-_git init "'${git_cache}'"
-
-pushd "${git_cache}" >/dev/null
+_git init .
 
 # Ensure the repo has an origin (in case a previous run was killed).
 if ! _git remote |grep -q -E '^origin$'; then
Signature · 1 line
2.14.1
#2 lore raw

PATCH v2 2/6 download/git: try to recover from utterly-broken repositories

In reply to Yann E. MORIN

In some cases, the repository may be in a state we can't automatically recover from, especially since we must still support oldish git versions that do not provide the necessary commands or options thereof.

As a last-ditch recovery, delete the repository and recreate the cache from scratch.

Cc Maxime Hadjinlian <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
support/download/git +27 -0
index 19a3e8ebee..ebd04bc7b3 100755
@@ -16,6 +16,30 @@ set -e
 # Environment:
 #   GIT      : the git command to call
 
+# Save out path and options in case we need to call ourselves again
+myname="${0}"
+declare -a OPTS=("${@}")
+
+# This function is called when an error occurs. Its job is to attempt a
+# clone from scratch (only once!) in case the git tree is borked, or in
+# case an unexpected and unsupported situation arises with submodules
+# or uncomitted stuff (e.g. if the user manually mucked around in the
+# git cache).
+_on_error() {
+    local ret=${?}
+
+    if ${BR_GIT_BACKEND_FIRST_FAULT:-false}; then
+        printf "Double-fault in git-cache"
+        exit ${ret}
+    fi
+    export BR_GIT_BACKEND_FIRST_FAULT=true
+
+    popd >/dev/null
+    rm -rf "${git_cache}"
+
+    exec "${myname}" "${OPTS[@]}" || exit ${ret}
+}
+
 verbose=
 recurse=0
 while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
@@ -39,6 +63,9 @@ git_cache="${dl_dir}/git"
 mkdir -p "${git_cache}"
 pushd "${git_cache}" >/dev/null
 
+# Any error now should try to recover
+trap _on_error ERR
+
 # Caller needs to single-quote its arguments to prevent them from
 # being expanded a second time (in case there are spaces in them)
 _git() {
Signature · 1 line
2.14.1
#3 lore raw

PATCH v2 2/6 download/git: try to recover from utterly-broken repositories

All,

On 2018-04-22 11:07 +0200, Yann E. MORIN spake thusly:

Quoted 6 lines

In some cases, the repository may be in a state we can't automatically recover from, especially since we must still support oldish git versions that do not provide the necessary commands or options thereof.

As a last-ditch recovery, delete the repository and recreate the cache from scratch.

As Thomas noticed on IRC, this commit would remove the local git cache in case the user enters an invalid changeset (typo in sha1 or tag name, or whatnot).

A missing commit should not be considered as a broken repository, especially since re-cloning from scratch would not help solve the issue.

I already have a local change that will avoid trashing a git cache in that case (using git rev-parse). I'll wait for some more reviews before submitting again...

Regards,
Yann E. MORIN.

Quoted 61 lines
Cc Maxime Hadjinlian <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
support/download/git +27 -0
index 19a3e8ebee..ebd04bc7b3 100755
@@ -16,6 +16,30 @@ set -e
 # Environment:
 #   GIT      : the git command to call
 
+# Save out path and options in case we need to call ourselves again
+myname="${0}"
+declare -a OPTS=("${@}")
+
+# This function is called when an error occurs. Its job is to attempt a
+# clone from scratch (only once!) in case the git tree is borked, or in
+# case an unexpected and unsupported situation arises with submodules
+# or uncomitted stuff (e.g. if the user manually mucked around in the
+# git cache).
+_on_error() {
+    local ret=${?}
+
+    if ${BR_GIT_BACKEND_FIRST_FAULT:-false}; then
+        printf "Double-fault in git-cache"
+        exit ${ret}
+    fi
+    export BR_GIT_BACKEND_FIRST_FAULT=true
+
+    popd >/dev/null
+    rm -rf "${git_cache}"
+
+    exec "${myname}" "${OPTS[@]}" || exit ${ret}
+}
+
 verbose=
 recurse=0
 while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
@@ -39,6 +63,9 @@ git_cache="${dl_dir}/git"
 mkdir -p "${git_cache}"
 pushd "${git_cache}" >/dev/null
 
+# Any error now should try to recover
+trap _on_error ERR
+
 # Caller needs to single-quote its arguments to prevent them from
 # being expanded a second time (in case there are spaces in them)
 _git() {
Signature · 6 lines
2.14.1

_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#4 lore raw

PATCH v2 3/6 download/git: ensure we checkout to a clean state

In reply to Yann E. MORIN

Force the checkout to ignore any local changes. This allows recovering from a previous partial checkout (e.g. killed by the user, or by a CI job...)

git checkout -f has been supported since the inception of git, so we can use it without any second thought.

Cc Maxime Hadjinlian <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
support/download/git +1 -1
index ebd04bc7b3..d97d07c51f 100755
@@ -125,7 +125,7 @@ fi
 
 # Checkout the required changeset, so that we can update the required
 # submodules.
-_git checkout -q "'${cset}'"
+_git checkout -f -q "'${cset}'"
 
 # Get date of commit to generate a reproducible archive.
 # %cD is RFC2822, so it's fully qualified, with TZ and all.
Signature · 1 line
2.14.1
#5 lore raw

PATCH v2 4/6 download/git: ensure we can checkout repos with submodule conversions

In reply to Yann E. MORIN

When a git tree has had sub-dir <-> sub-module conversions, or has had submodules added or removed over the course of time, checking out a changeset across those conversions/additions/removals may leave untracked files, or may fail because of a conflict of type.

So, before we checkout the new changeset, we forcibly remove the submodules. The new set of submodules, if any, will be restored later.

Ideally, we would use a native git command: git submodule deinit --all. However, that was only introduced in git 1.8.3 which, while not being recent by modern standards, is still too old for some enterprise-grade distributions (RHEL6 only has git-1.7.1).

So, instead, we just use git submodule foreach, to rm -rf the submodules directory.

Again, we would ideally use 'cd $toplevel && rm -rf $path', but $toplevel was only introduced in git 1.7.2. $path has always been there.

So, instead, we just cd back one level, and remove the basename of the directory.

Eventually, we need to get rid of now-empty and untracked directories, that were parents of a removed submodule. For example. ./foo/sub/ was a submodule, so ./foo/bar/ was removed, which left ./foo/ around.

Yet again, recent-ish git versions would have removed it during the forced checkout, but old-ish versions (e.g. 1.7.1) do not remove it with the forced checkout.

Instead we use a forced-forced clean of directories, untracked, and ignored content, to really get rid of extra stuff we are not interested in.

Cc Maxime Hadjinlian <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
support/download/git +29 -0
index d97d07c51f..344e253b1d 100755
@@ -123,10 +123,39 @@ if ! _git fetch origin "'${cset}:${cset}'" >/dev/null 2>&1; then
     printf "Could not fetch special ref '%s'; assuming it is not special.\n" "${cset}"
 fi
 
+# The new cset we want to checkout might have different submodules, or
+# have sub-dirs converted to.from a submodule. So we would need to
+# deregister _current_ submodules before we checkout.
+#
+# Using "git submodule deinit --all" would remove all the files for
+# all submodules, including the corresponding .git files or directories.
+# However, it  was only introduced with git-1.8.3, which is too recent
+# for some enterprise-grade distros.
+#
+# So, we fall-back to just removing all submodules directories. We do
+# not need to be recursive, as removing a submodule will de-facto remove
+# its own submodules.
+#
+# For recent git versions, the repository for submodules is stored
+# inside the repository of the super repository, so the following will
+# only remove the working copies of submodule, effectively caching the
+# submodules.
+#
+# For older versions, the repository is stored in the .git/ of the
+# submodule directory, so the following will effectively remove the
+# the working copy as well as the repository, which means submodules
+# will not be cached for older versions.
+#
+_git submodule --quiet foreach 'cd .. && rm -rf "${path##*/}"'
+
 # Checkout the required changeset, so that we can update the required
 # submodules.
 _git checkout -f -q "'${cset}'"
 
+# Get rid of now-untracked directories (that were parrents of submodules
+# removed above).
+_git clean -ffdx
+
 # Get date of commit to generate a reproducible archive.
 # %cD is RFC2822, so it's fully qualified, with TZ and all.
 date="$( _git log -1 --pretty=format:%cD )"
Signature · 1 line
2.14.1
#6 lore raw

PATCH v2 5/6 download/git: always do full-clone

In reply to Yann E. MORIN

We currently attempt a shallow clone, as tentative to save bandwidth and download time.

However, now that we keep the git tree as a cache, it may happen that we need to checkout an earlier commit, and that would not be present with a shallow clone.

Furthermore, the shallow fetch is already really broken, and just happens to work by chance. Consider the following actions, which are basically what happens today:

    mkdir git
    git init git
    cd git
    git remote add origin https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
    git fetch origin --depth 1 v4.17-rc1
    if ! git fetch origin v4.17-rc1:v4.17-rc1 ; then
        echo "warning"
    fi
    git checkout v4.17-rc1

The checkout succeeds just because of the git-fetch in the if-condition, which is initially there to fetch the special refs from github PRs, or gerrit reviews. That fails, but we just print a warning. If we were to ever remove support for special refs, then the checkout would fail.

The whole purpose of the git cache is to actually save bandwidth and download time, but in the long run. For one-offs, people would preferably use a wget download (e.g. with the github macro) instead of a git clone.

We switch to always doing a full clone. It is more correct, and pays off in the long run...

Cc Maxime Hadjinlian <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)
support/download/git +3 -21
index 344e253b1d..84cc6be94e 100755
@@ -90,27 +90,9 @@ fi
 
 _git remote set-url origin "'${uri}'"
 
-# Try to fetch with limited depth, since it is faster than a full clone - but
-# that only works if the version is a ref (tag or branch). Before trying to do
-# a shallow clone we check if ${cset} is in the list provided by git ls-remote.
-# If not we fallback to a full fetch.
-#
-# Messages for the type of clone used are provided to ease debugging in
-# case of problems
-git_done=0
-if [ -n "$(_git ls-remote origin "'${cset}'" 2>&1)" ]; then
-    printf "Doing a shallow fetch\n"
-    if _git fetch "${@}" --depth 1 origin "'${cset}'"; then
-        git_done=1
-    else
-        printf "Shallow fetch failed, falling back to fetching all refs\n"
-    fi
-fi
-if [ ${git_done} -eq 0 ]; then
-    printf "Fetching all references\n"
-    _git fetch origin
-    _git fetch origin -t
-fi
+printf "Fetching all references\n"
+_git fetch origin
+_git fetch origin -t
 
 # Try to get the special refs exposed by some forges (pull-requests for
 # github, changes for gerrit...). There is no easy way to know whether
Signature · 1 line
2.14.1
#7 lore raw

PATCH v2 6/6 download/git: add warning not to use or git cache

In reply to Yann E. MORIN

We really want the user not to use our git cache manually, or their changes (committed or not) may eventually get lost.

So, add a warning file, not unlike the one we put in the target/ directory, to warn the user not to use the git tree.

Ideally, we would have carried this file in support/misc/, but the git backend does not have acces to it: the working directory is somewhere unknown, and TOPDIR is not exported in the environment.

So, we have to carry it in-line in the backend instead.

Cc Maxime Hadjinlian <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Ricardo Martincoski <[email protected]>
Cc Arnout Vandecappelle <[email protected]>
---
 support/download/git | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
support/download/git +18 -0
index 84cc6be94e..8764474153 100755
@@ -72,6 +72,24 @@ _git() {
     eval GIT_DIR="${git_cache}/.git" ${GIT} "${@}"
 }
 
+# Create a warning file, that the user should not use the git cache.
+# It's ours. Our precious.
+cat <<-_EOF_ >"${dl_dir}/git.readme"
+	IMPORTANT NOTE!
+
+	The git tree located in this directory is for the exclusive use
+	by Buildroot, which uses it as a local cache to reduce bandwidth
+	usage.
+
+	Buildroot *will* trash any changes in that tree whenever it needs
+	to use it. Buildroot may even remove it in case it detects the
+	repository may have been damaged or corrupted.
+
+	Do *not* work in that directory; your changes will eventually get
+	lost. Do *not* even use it as a remote, or as the source for new
+	worktrees.
+_EOF_
+
 # Initialise a repository in the git cache. If the repository already
 # existed, this is a noop, unless the repository was broken, in which
 # case this magically restores it to working conditions. In the latter
Signature · 1 line
2.14.1