125 lines
2.2 KiB
Bash
125 lines
2.2 KiB
Bash
#!/bin/bash
|
|
|
|
function is_git {
|
|
|
|
# check if current folder is git repository
|
|
git_check_command=$(git -C $1 rev-parse > /dev/null 2>&1)
|
|
if [[ $? != 0 ]]; then
|
|
return 0
|
|
fi
|
|
|
|
return 1
|
|
}
|
|
|
|
function get_current_branch {
|
|
echo $(git branch | grep "*" | awk '{print $NF}')
|
|
}
|
|
|
|
function git_auto_save {
|
|
|
|
if [[ $(is_git ".") -ne 0 ]]; then
|
|
echo "Current dir is not a git repository."
|
|
return 1
|
|
fi
|
|
|
|
current_branch_name=$(get_current_branch)
|
|
|
|
timestamp=`date +%Y_%m_%d_%H_%M_%S`
|
|
new_branch_name="git_auto_save/$HOST/$timestamp"
|
|
|
|
# check if there is unsaved work
|
|
changes=`git status --ignore-submodules=dirty | grep -E 'modified|deleted|new'`
|
|
if [[ ! $changes ]]; then
|
|
echo "No changes found."
|
|
return
|
|
fi
|
|
|
|
# stash changes
|
|
git stash
|
|
|
|
# create new branch
|
|
git checkout -b $new_branch_name
|
|
git push origin $new_branch_name
|
|
|
|
# apply stashed changes
|
|
git stash apply
|
|
|
|
# add changed files
|
|
git add -u
|
|
|
|
# commit changes
|
|
git commit -m "git autosave from $timestamp"
|
|
|
|
# push changes
|
|
git push --set-upstream origin $new_branch_name
|
|
|
|
# move back to initial branch
|
|
git checkout $current_branch_name
|
|
|
|
}
|
|
|
|
function remove_auto_save {
|
|
|
|
if [[ $(is_git ".") -ne 0 ]]; then
|
|
echo "Current dir is not a git repository."
|
|
return 1
|
|
fi
|
|
|
|
if [[ $1 ]]; then
|
|
auto_save_branch=$1
|
|
else
|
|
auto_save_branch=`git branch | grep "git_auto_save" | head -1 | awk '{print $FN}'`
|
|
fi
|
|
|
|
if [[ ! $auto_save_branch ]]; then
|
|
echo "No auto-save branch found."
|
|
return 1
|
|
fi
|
|
|
|
auto_save_branch=${auto_save_branch:2}
|
|
current_branch=$(get_current_branch)
|
|
|
|
# if on autosave branch, move to master
|
|
if [[ $current_branch == $auto_save_branch ]]; then
|
|
echo "Currently on autosave path, moving to Master"
|
|
git checkout master
|
|
fi
|
|
|
|
# remove local branch
|
|
git branch -d $auto_save_branch
|
|
|
|
# remove remote branch
|
|
git push origin --delete $auto_save_branch
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
function remove_all_auto_saves {
|
|
|
|
if [[ $(is_git ".") -ne 0 ]]; then
|
|
echo "Current dir is not a git repository."
|
|
return 1
|
|
fi
|
|
|
|
if [[ $1 != "y" ]]; then
|
|
echo "Please confirm with 'y'"
|
|
return
|
|
fi
|
|
|
|
# remove all auto save branches
|
|
while [[ 1 ]]; do
|
|
if ! remove_auto_save; then
|
|
break
|
|
fi
|
|
echo "Auto-Save removed."
|
|
done
|
|
|
|
echo "All auto-saves removed."
|
|
|
|
}
|
|
|
|
function load_last_auto_save {
|
|
echo ""
|
|
}
|