Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created March 14, 2024 08:33
Show Gist options
  • Save seungjin/b347f88006e3ce1398af5dba43b1389f to your computer and use it in GitHub Desktop.
Save seungjin/b347f88006e3ce1398af5dba43b1389f to your computer and use it in GitHub Desktop.
git autosave
autosave = "!f () { \
origin=$(git remote) ; \
project_name=$(git remote get-url $origin | xargs basename -s .git) ; \
current_branch=$(git rev-parse --abbrev-ref HEAD) ; \
gsm=$(git status) ; \
ntc=$(echo $gsm | grep \"nothing to commit, working tree clean\" | wc -l) ; \
ybi=$(echo $gsm | grep \"Your branch is ahead of '$origin/$current_branch' by\" | wc -l) ; \
ybi2=$(echo $gsm | grep \"Your branch is ahead of '$origin/main' by\" | wc -l) ; \
if [ $ntc -ne 1 ] || ([ $ybi -ne 1 ] && [ $ybi2 -ne 0 ]); then\
echo \"💾 Autosaving...\" ; \
new_branch_name=\"$USER/autosave/`hostname`@`date +'%Y-%m-%dT%H.%M.%S%z'`\" ; \
commit_msg=$(git diff | OPENAI_TEMPERATURE=0.4 OPENAI_PROMPT_TEXT=\"Generate a Git commit message based on the following diff\" OPENAI_MODEL_NAME=gpt-3.5-turbo ask_openai); \
echo -e \"\\n\\e[1;32mMachine generated commit message:\"; \
echo $commit_msg; \
echo -e \"\\033[m\"; \
git add -A; git commit -m \"(Autocommit) `whoami`@`hostname` \n$commit_msg \" ; \
git push \
-o merge_request.create \
-o merge_request.remove_source_branch \
-o merge_request.title=\"$project_name 🚀 Please merge for $new_branch_name 🙏\" \
-o merge_request.description=\"Automated MR by [git.aliases](https://codeberg.org/seungjin/_home.d/src/branch/main/.gitconfig)\" \
-o merge_request.labe=\"autosave\" \
$origin $current_branch:$new_branch_name; \
if [ $? -eq 0 ]; then\
echo \"⛵ Autosaving done.\"; \
else \
echo \"🔥 Autosaving failed. \"; \
fi \
else \
echo \"🥣 Nothing to autosave.\" ; \
fi;\
git fetch --all; \
}; \
f; \
unset -f f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment