Skip to content

Commit

Permalink
Make fetchneedles work with modern Git repos like the example distri
Browse files Browse the repository at this point in the history
* Avoid running into an error when a Git repository like our example
  distribution that does not use the `master` branch anymore is present
* Avoid hard-coding the default branch name for test and needle
  repositories
    * Use `git rev-parse --abbrev-ref refs/remotes/origin/HEAD` instead
      which works with all test and needle repositories we have in our two
      production instances (after I created `.git/refs/remotes/origin/HEAD`
      in the SLE needles checkout; see added comment for details)
    * Keep the possibility to specify `branch` and `needles_branch` in case
      one wants to fetch a special branch
* Tested locally with repos using master and main including the code branch
  for repairing the needle repos
* See https://progress.opensuse.org/issues/166658
  • Loading branch information
Martchus committed Sep 23, 2024
1 parent ecb52cf commit 313ee7a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions script/fetchneedles
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
: "${dist_name:=${dist:-"openSUSE"}}" # the display name, for the help message
: "${dist:="opensuse"}"
: "${giturl:="https://github.com/os-autoinst/os-autoinst-distri-opensuse.git"}"
: "${branch:="master"}"
: "${branch:=""}"
: "${email:="openqa@$HOST"}"
: "${username:="openQA web UI"}"
: "${product:="$dist"}"

: "${git_lfs:="0"}"
: "${needles_separate:="1"}"
: "${needles_giturl:="https://github.com/os-autoinst/os-autoinst-needles-opensuse.git"}"
: "${needles_branch:="master"}"
: "${needles_branch:=""}"

: "${updateall:="0"}"
: "${force:="0"}"
Expand Down Expand Up @@ -72,24 +72,40 @@ deal_with_stale_lockfile() {
fi
}

# Determine the "main branch" for the remote origin unless a branch has been specified explicitly
# note: This relies on a file called `refs/remotes/origin/HEAD` with contents like `ref: refs/remotes/origin/master` being
# present under the `.git` directory. This should be the case for all recently cloned Git repositories. Otherwise
# one can just create the file making it point to the desired branch.
get_git_base() {
branch=$1
if [ "$branch" ]; then
echo "origin/$branch"
else
git rev-parse --abbrev-ref refs/remotes/origin/HEAD
fi
}

git_update() {
branch="${1:-"master"}"
deal_with_stale_lockfile
git gc --auto --quiet
git fetch -q origin

# Clear any uncommitted changes that would prevent a rebase
[ "$force" = 1 ] && git reset -q --hard HEAD
git rebase -q origin/"$branch" || fail 'Use force=1 to discard uncommitted changes before rebasing'
base=$(get_git_base "$1")
git rebase -q "$base" || fail 'Use force=1 to discard uncommitted changes before rebasing'
}

# For needles repos because of needle saving we might end up in conflict, i.e.
# detached HEAD so we need to repair this
git_update_needles() {
git_update "$needles_branch"
if [ "$(git rev-parse --abbrev-ref --symbolic-full-name HEAD)" = "HEAD" ]; then
base=$(get_git_base "$needles_branch")
needles_branch=${base#origin/}
git branch -D "$needles_branch"
git checkout -b "$needles_branch"
git branch "--set-upstream-to=origin/$needles_branch" "$needles_branch"
git branch "--set-upstream-to=$base" "$needles_branch"
git push origin "HEAD:$needles_branch"
fi
}
Expand Down

0 comments on commit 313ee7a

Please sign in to comment.