Skip to content

Instantly share code, notes, and snippets.

@00sapo
Last active June 15, 2022 12:17
Show Gist options
  • Save 00sapo/893916ef26ee61310fe00186b26c9893 to your computer and use it in GitHub Desktop.
Save 00sapo/893916ef26ee61310fe00186b26c9893 to your computer and use it in GitHub Desktop.
#!/bin/env fish
#! GistId: 893916ef26ee61310fe00186b26c9893
# Settings
set working_branch 'working'
set auto_commit '[autopush]'
set remote 'server'
# if test (count $argv) -ne 2
# return
# end
function switch_to_branch_without_changing_files
# creating a temporary branch without changing files
git checkout -b temp
# moving the target branch to this commit without changing files
git reset --soft $argv[1]
# moving to the target branch without changing files
git checkout $argv[1]
# deleting the temporary branch
git branch -d temp
end
function safe_push
timeout 2s git ls-remote server > /dev/null
and git push -fq $argv[1] $argv[2] > /dev/null
end
# checking if there are modifications
set modifications (git status --porcelain=v1 | awk '/^ M /')
if test -z (string length $modifications[1])
echo "Autopush: nothing to do"
return
end
echo "Autopush: committing and pushing"
# getting current branch name
set this_branch (git status --porcelain=v2 --branch | awk '/branch.head/{print $3}')
switch_to_branch_without_changing_files $working_branch
# getting info about the last commit
set last_commit (git log -1 --pretty=%B)
# committing stuffs
switch "$last_commit"
case "$auto_commit*"
echo "amending last commit $remote/$working_branch"
git commit -a --amend --no-edit > /dev/null
safe_push $remote $working_branch
case "*"
echo "new commit: '$auto_commit' on $remote/$working_branch"
git commit -a -m "$auto_commit" --no-edit > /dev/null
safe_push $remote $working_branch
end
# going back to the initial branch
switch_to_branch_without_changing_files $this_branch
git reset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment