git-pull

#!/usr/bin/env bash
## @arg $1 the branch to pull
## @arg $2 the branch dir
## @arg $3 the git dir


echo "Start git pull!"

branch="$1"
branch_dir="$2"
git_dir="$3"

cd "$branch_dir"

# git_prefix="git --git-dir="$git_dir" --work-tree="$branch_dir"";

git_status=$(git --git-dir="$git_dir" --work-tree="$branch_dir" status --porcelain)

# exit;
# echo -n "\nGit Status: $git_status\n"

if [[ -n "$git_status" ]];then
    echo "Clean Repo\n"
    echo "\n--\n"
    git --git-dir="$git_dir" --work-tree="$branch_dir" clean -fd
    git --git-dir="$git_dir" --work-tree="$branch_dir" reset --hard
fi

echo -n "\n--\n"

git --git-dir="$git_dir" --work-tree="$branch_dir" checkout "$branch"

git_status=$(git --git-dir="$git_dir" --work-tree="$branch_dir" status --porcelain)

echo -n "\nGit Status: $git_status\n"

if [[ -n "$git_status" ]];then
    echo -n "Clean Repo\n"
    echo -n "\n--\n"
    git --git-dir="$git_dir" --work-tree="$branch_dir" clean -fd
    git --git-dir="$git_dir" --work-tree="$branch_dir" reset --hard
fi

echo -n "\n--\n"


git --git-dir="$git_dir" --work-tree="$branch_dir" fetch --update-head-ok origin "$branch"
# git --git-dir="$git_dir" --work-tree="$branch_dir" pull origin "$branch"

git_status=$(git --git-dir="$git_dir" --work-tree="$branch_dir" status --porcelain)


echo -n "\nGit Status: $git_status\n"

if [[ -n "$git_status" ]];then
    echo -n "Clean Repo\n"
    echo -n "\n--\n"
    git --git-dir="$git_dir" --work-tree="$branch_dir" clean -fd
    git --git-dir="$git_dir" --work-tree="$branch_dir" reset --hard
fi

echo -n "\n--\n"

commit_hash=$(git --git-dir="$git_dir" --work-tree="$branch_dir" log --pretty=format:"%H" -n 1 "$branch")
git --git-dir="$git_dir" --work-tree="$branch_dir" checkout "$commit_hash"