Skip to content

Instantly share code, notes, and snippets.

@lucaslugao
Last active April 25, 2019 19:25
Show Gist options
  • Save lucaslugao/60f8ca8e9aabbee9cabfc6649ccd72ab to your computer and use it in GitHub Desktop.
Save lucaslugao/60f8ca8e9aabbee9cabfc6649ccd72ab to your computer and use it in GitHub Desktop.
Simulate a checkout of a detached commit in the gitlab upstream
#!/bin/bash
#
# Simulate a checkout of a detached commit in the gitlab upstream
#
# Usage
# $ gitlab-force-checkout COMMIT_HASH
set -e
if [ ! -z "$(git status --porcelain)" ]; then
echo "Please stash your changes before continue!"
exit 1
fi
DOWNLOAD_DIR="${HOME}/Downloads"
GIT_UPSTREAM="$(git remote -v | grep push | awk '{print $2}' | awk -F @ '{print $2}' | tr ':' '/' | sed "s=.git==g")"
REPO_BASE_NAME="${GIT_UPSTREAM##*/}"
ARCHIVE_URL="https://${GIT_UPSTREAM}/-/archive/${1}"
ARCHIVE_FILE="${REPO_BASE_NAME}-${1}.zip"
gio open "${ARCHIVE_URL}/${ARCHIVE_FILE}" &
while :; do
sleep 2
ARCHIVE_FILE_LOCAL="$(find "${DOWNLOAD_DIR}" -name "${ARCHIVE_FILE}")"
if [ ! -z "${ARCHIVE_FILE_LOCAL}" ]; then
break
fi
done
TEMP_DIR="$(mktemp -d)"
unzip "${ARCHIVE_FILE_LOCAL}" -d "${TEMP_DIR}"
GIT_BASE_DIR="$(git rev-parse --show-toplevel)"
shopt -s extglob
eval rm -r "${GIT_BASE_DIR}/!(.git)"
cp -TR "${TEMP_DIR}/${REPO_BASE_NAME}-${1}/" "${GIT_BASE_DIR}/"
rm -r -f "${TEMP_DIR}"
git status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment