Skip to content

Instantly share code, notes, and snippets.

@BR0kEN-
Last active April 29, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BR0kEN-/ecbf03261aaefc84563d5e2c955a9ab1 to your computer and use it in GitHub Desktop.
Save BR0kEN-/ecbf03261aaefc84563d5e2c955a9ab1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Move contents from this subdirectory into root directory of a repository.
# - Any relative directory could be as a value.
# - Pass "--default" to keep this value and affect on the next.
DIRECTORY="docroot"
# Commit moved content in this branch.
# - Any name can be specified. Branch will be created.
# - Pass "--default" to keep this value and affect on the next.
BRANCH="sources"
# Push commit to a remote.
# - Any name or fully qualified URL of remote can be specified.
# - Pass "--default" to keep this value and affect on the next.
REMOTE="origin"
GITIGNORE=".*
!.git*"
type "realpath" > /dev/null 2>&1
if [ $? -gt 0 ]; then
realpath()
{
# Try to read a symlink.
local LINK=$(\readlink $1)
if [ -n "${LINK}" ]; then
\echo ${LINK}
else
local PATH=$(\cd -P -- $(\dirname -- $1) && \pwd -P)
while [ -h "${PATH}" ]; do
# 1. Go to the directory with a symlink.
# 2. Go to the directory to which the symlink points.
# 3. Get the path.
PATH=$(\cd $(\dirname -- ${PATH}) && \cd $(\dirname -- $(\readlink ${PATH})) && \pwd)
done
\echo ${PATH}/${1##*/}
fi
}
fi
arg()
{
[[ -z "$1" || "$1" == "--default" ]] && \echo $2 || \echo $1
}
################################################################################
# ARGUMENT $1 - path to directory with source code (relative or absolute).
################################################################################
DIRECTORY=$(realpath $(arg $1 ${DIRECTORY}))
if [ ! -d "${DIRECTORY}" ]; then
\echo "The \"${DIRECTORY}\" path does not exists!"
\exit 1
fi
################################################################################
# ARGUMENT $2 - the name of one of branches.
################################################################################
BRANCH=$(arg $2 ${BRANCH})
################################################################################
# ARGUMENT $3 - the name of one of remotes or fully qualified URL.
################################################################################
# Try to recognize remote Git server by name.
REMOTE=$(\git remote -v | \grep $(arg $3 ${REMOTE}) | \awk '{print $2}' | \head -n1)
if [ -z "${REMOTE}" ]; then
# Try to recognize remote Git server by URL.
\git ls-remote --exit-code ${REMOTE} > /dev/null 2>&1
# Update variable if remote repository exists.
if [ $? -eq 0 ]; then
REMOTE=$3
fi
fi
# Finally check existence of remote repository.
if [ -z "${REMOTE}" ]; then
\echo "Unable to recognize \"$3\" Git remote."
\exit 3
fi
################################################################################
# Process.
################################################################################
TEMPORARY_REPO=/tmp/emptyrepo
BRANCH_EXISTS_LOCAL=false
BRANCH_EXISTS_REMOTE=false
\echo "Clear temporary storage: ${TEMPORARY_REPO}"
\rm -rf ${TEMPORARY_REPO}
\echo "Remote repository: ${REMOTE}"
\echo "Branch: ${BRANCH}"
# Check for local branch.
\git rev-parse --verify ${BRANCH} > /dev/null 2>&1
if [ $? -eq 0 ]; then
BRANCH_EXISTS_LOCAL=true
fi
if [ -n "$(\git ls-remote --heads ${REMOTE} ${BRANCH})" ]; then
BRANCH_EXISTS_REMOTE=true
fi
if ${BRANCH_EXISTS_REMOTE}; then
\echo "Remote branch exists."
\git clone ${REMOTE} --branch=${BRANCH} ${TEMPORARY_REPO}
\echo "Copying data from \"${DIRECTORY}\" to \"${TEMPORARY_REPO}\""
\rsync -ra ${DIRECTORY}/* ${TEMPORARY_REPO}/
\cd ${TEMPORARY_REPO}
\git checkout ${BRANCH} > /dev/null 2>&1
else
# Remove content after latest slash.
# Example "/var/www/site" => "/var/www".
COPY_FROM=${DIRECTORY%/*}
# Remove content before latest slash.
# Example "/var/www/site" => "site".
CONTENT_DIR=${DIRECTORY##*/}
\echo "Not remote nor local branches aren't exists."
\echo "Copying data from \"${COPY_FROM}\" to \"${TEMPORARY_REPO}\""
\rsync -ra ${COPY_FROM}/ ${TEMPORARY_REPO}/
\cd ${TEMPORARY_REPO}
\echo "Remove all files and directories, except \"${CONTENT_DIR}\" and \".git\", from ${TEMPORARY_REPO}"
\ls -A | \grep -v "^${CONTENT_DIR}$" | \grep -v "^.git$" | \xargs \rm -rf
\echo "Move content from \"${CONTENT_DIR}\" to repository root."
\mv ${CONTENT_DIR}/* ./
\echo "Generate an empty .gitignore"
\echo ${GITIGNORE} > .gitignore
\git checkout -b ${BRANCH}
fi
\git add .
\git commit -m "Automatic commit generated via script."
\git push ${REMOTE} ${BRANCH}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment