Skip to content

Commit

Permalink
Merge pull request #221 from mithro/git-submodule-update
Browse files Browse the repository at this point in the history
Improvements to local git submodule script.
  • Loading branch information
mithro committed Nov 6, 2019
2 parents cd3a627 + b0117df commit d57b5e9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 103 deletions.
114 changes: 114 additions & 0 deletions .travis/add-local-submodule-inner.sh
@@ -0,0 +1,114 @@
#! /bin/bash

if [ "$(whoami)" == "root" ]
then
echo "Please don't run this script as root!"
exit 1
fi

SCRIPT_SRC=$(realpath ${BASH_SOURCE[0]})

set -e

#cd $SCRIPT_DIR/..

echo
USER_SLUG="$1"
SUBMODULE="$2"
SHA1="$3"
REV=$(git rev-parse HEAD)

echo "Submodule $SUBMODULE @ '$SHA1'"

# Get the pull request info
REQUEST_USER="$(echo $USER_SLUG | perl -pe 's|^([^/]*)/.*|\1|')"
REQUEST_REPO="$(echo $USER_SLUG | perl -pe 's|.*?/([^/]*)$|\1|')"

echo "Request user is '$REQUEST_USER'".
echo "Request repo is '$REQUEST_REPO'".

# Get current origin from git
ORIGIN_URL="$(git config -f .gitmodules submodule.$SUBMODULE.url)"
#ORIGIN_URL="$(git remote get-url origin)"
if echo $ORIGIN_URL | grep -q "github.com"; then
echo "Found github"

ORIGIN_SLUG=$(echo $ORIGIN_URL | perl -pe 's|.*github.com/(.*?)(.git)?$|\1|')
echo "Origin slug is '$ORIGIN_SLUG'"

ORIGIN_USER="$(echo $ORIGIN_SLUG | perl -pe 's|^([^/]*)/.*|\1|')"
ORIGIN_REPO="$(echo $ORIGIN_SLUG | perl -pe 's|.*?/([^/]*)$|\1|')"

echo "Origin user is '$ORIGIN_USER'"
echo "Origin repo is '$ORIGIN_REPO'"

USER_URL="https://github.com/$REQUEST_USER/$ORIGIN_REPO.git"

# Check if the user's repo exists
echo -n "User's repo would be '$USER_URL' "
if git ls-remote --exit-code --heads "$USER_URL" > /dev/null 2>&1; then
echo "which exists!"
else
echo "which does *not* exist!"
USER_URL="$ORIGIN_URL"
fi
else
echo "Did not find github"
USER_URL="$ORIGIN_URL"
fi

# If submodule doesn't exist, clone directly from the users repo
if [ ! -e $SUBMODULE/.git ]; then
echo "Cloning '$ORIGIN_REPO' from repo '$ORIGIN_URL'"
git clone $ORIGIN_URL $SUBMODULE --origin origin
else
(
cd $SUBMODULE
git remote rm origin >/dev/null 2>&1 || true
git remote add origin $ORIGIN_URL
)
fi

# Fetch origin and make sure the submodule isn't shallow.
(
cd $SUBMODULE
if [ $(git rev-parse --is-shallow-repository) != "false" ]; then
git fetch origin --no-recurse-submodules --unshallow
fi
git fetch origin --no-recurse-submodules
)

# Add the user remote and fetch it.
if [ "$USER_URL" != "$ORIGIN_URL" ]; then
(
cd $SUBMODULE
git remote rm user >/dev/null 2>&1 || true
git remote add user $USER_URL
git fetch user --no-recurse-submodules
)
fi

# Checkout to the correct SHA1 value - which may come from origin or user.
(
cd $SUBMODULE
git reset --hard "$SHA1" --recurse-submodules=no
)

# Init the submodule
git submodule update --init $SUBMODULE

# Call ourselves recursively.
(
cd $SUBMODULE
# Checkout the submodule at the right revision
git submodule sync
git submodule status
echo
git submodule status | sed -e's/^.//' | while read SHA1 MODULE_PATH DESC
do
"$SCRIPT_SRC" "$USER_SLUG" "$MODULE_PATH" "$SHA1"
done
exit 0
) || exit 1

exit 0
94 changes: 0 additions & 94 deletions .travis/add-local-submodule.sh

This file was deleted.

29 changes: 29 additions & 0 deletions .travis/add-local-submodules.sh
@@ -0,0 +1,29 @@
#!/bin/bash

set -e

# Disable prompting for passwords - works with git version 2.3 or above
export GIT_TERMINAL_PROMPT=0

if [ z$1 == z ]; then
echo "Missing USER_SLUG as first argument!"
exit 1
fi
export USER_SLUG=$1

echo ""
echo ""
echo ""
echo "- Using local version of submodules (if they exist)"
echo "---------------------------------------------"
git submodule status
echo
git submodule status | sed -e's/^.//' | while read SHA1 MODULE_PATH DESC
do
"$PWD/.travis/add-local-submodule-inner.sh" "$USER_SLUG" "$MODULE_PATH" "$SHA1"
done
echo
git submodule status --recursive
echo
git submodule foreach --recursive 'git remote -v; echo'
exit 0
14 changes: 5 additions & 9 deletions .travis/fixup-git.sh
Expand Up @@ -51,7 +51,9 @@ echo ""
echo ""
echo "- Fetching non shallow to get git version"
echo "---------------------------------------------"
git fetch origin --unshallow || true
if git rev-parse --is-shallow-repository; then
git fetch origin --unshallow
fi
git fetch origin --tags

TRAVIS_COMMIT_ACTUAL=$(git log --pretty=format:'%H' -n 1)
Expand Down Expand Up @@ -99,10 +101,7 @@ if [ z"$TRAVIS_PULL_REQUEST_SLUG" != z ]; then
echo ""
echo "- Using pull request version of submodules (if they exist)"
echo "---------------------------------------------"
git submodule status | while read SHA1 MODULE_PATH
do
"$PWD/.travis/add-local-submodule.sh" "$TRAVIS_PULL_REQUEST_SLUG" "$MODULE_PATH"
done
$PWD/.travis/add-local-submodules.sh $TRAVIS_PULL_REQUEST_SLUG
echo "---------------------------------------------"
git submodule foreach --recursive 'git remote -v; echo'
echo "---------------------------------------------"
Expand All @@ -114,10 +113,7 @@ if [ z"$TRAVIS_REPO_SLUG" != z ]; then
echo ""
echo "- Using local version of submodules (if they exist)"
echo "---------------------------------------------"
git submodule status | while read SHA1 MODULE_PATH DESC
do
"$PWD/.travis/add-local-submodule.sh" "$TRAVIS_REPO_SLUG" "$MODULE_PATH"
done
$PWD/.travis/add-local-submodules.sh $TRAVIS_REPO_SLUG
echo "---------------------------------------------"
git submodule foreach --recursive 'git remote -v; echo'
echo "---------------------------------------------"
Expand Down

0 comments on commit d57b5e9

Please sign in to comment.