
| Current Path : /bin/X11/X11/X11/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //bin/X11/X11/X11/git-sync |
#!/usr/bin/env bash
# reset environment variables that could interfere with normal usage
unset GREP_OPTIONS
# put all utility functions here
# make a temporary file
git_extra_mktemp() {
mktemp -t "$(basename "$0")".XXXXXXX
}
#
# check whether current directory is inside a git repository
#
is_git_repo() {
git rev-parse --show-toplevel > /dev/null 2>&1
result=$?
if test $result != 0; then
>&2 echo 'Not a git repo!'
exit $result
fi
}
is_git_repo
function _usage()
{
local command="git sync"
cat << EOS
Usage:
${command} [<remote> <branch>]
${command} -h | --help
${command} -s | --soft
Sync local branch with <remote>/<branch>.
When <remote> and <branch> are not specified on the command line, upstream of local branch will be used by default.
All changes and untracked files and directories will be removed unless you add -s(--soft).
Examples:
Sync with upstream of local branch:
${command}
Sync with origin/master:
${command} origin master
Sync without cleaning untracked files:
${command} -s
EOS
}
function main()
{
while [ "$1" != "" ]; do
case $1 in
-h | --help)
_usage
exit
;;
-s | --soft)
local soft="true"
;;
* )
if [ "${remote}" = "" ]; then
local remote="$1"
elif [ "${branch}" = "" ]; then
local branch="$1"
else
echo -e "Error: too many arguments.\n"
_usage
exit 1
fi
;;
esac
shift
done
local remote_branch
if [ "${remote}" = "" ]; then
if ! remote_branch="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)"; then
echo "There is no upstream information of local branch."
exit 1
fi
local branch="$(git rev-parse --abbrev-ref --symbolic-full-name @)"
local remote=$(git config "branch.${branch}.remote")
elif [ "${branch}" = "" ]; then
echo -e "Error: too few arguments.\n"
_usage
exit 1
else
remote_branch="${remote}/${branch}"
fi
echo -n "Are you sure you want to clean all changes & sync with '${remote_branch}'? [y/N]: "
local res
read res
case "${res}" in
"Y" | "y" | "yes" | "Yes" | "YES" )
if [ "${soft}" = "true" ]; then
git fetch "${remote}" "${branch}" && git reset --hard "${remote_branch}"
else
git fetch "${remote}" "${branch}" && git reset --hard "${remote_branch}" && git clean -d -f -x
fi
;;
* )
echo "Canceled."
;;
esac
}
main "$@"