gitsave.sh 971 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. # Basic options
  3. # Branch to save in
  4. branch=work
  5. # Remote to upload to
  6. remote=origin
  7. error() {
  8. echo $1
  9. exit 1
  10. }
  11. # Name of current branch
  12. cb=$(git rev-parse --abbrev-ref HEAD)
  13. # Check if correct branch
  14. [[ $branch == $cb ]] || error "Working on incorrect branch \"$cb\"!"
  15. # Check if there's anything to commit
  16. if [ -z "$(git status --porcelain)" ]; then
  17. error "Nothing to save!"
  18. fi
  19. # Name of last commit
  20. last=$(git log -1 --pretty=%s)
  21. # Check if last commit name is valid
  22. # Structure must be "commit###" where # is number
  23. [[ ${#last} -eq 9 ]] || error "Incorrect last commit name length \"$last\"!"
  24. [[ ${last:0:6} == "commit" ]] || error "Incorrect last commit name \"$last\"!"
  25. # Last commit number
  26. lcn=${last:6:9}
  27. # Check if integer
  28. [[ $lcn =~ ^-?[0-9]+$ ]] || error "Incorrect last commit number \"$last\"!"
  29. # New commit number
  30. printf -v ncn "%03d" $(($lcn + 1))
  31. git add .
  32. git commit -m "commit$ncn"
  33. git push $remote $branch
  34. echo "Work saved!"