-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-send.sh
36 lines (33 loc) · 1.23 KB
/
git-send.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# Adds, commits and pushes ALL your local changes
# Parameter: commit message (required)
function gsend() {
# Verify you are in a repo and only a commit message was supplied
if [[ ! -d .git ]]; then
echo "❌ '$(pwd)' is not part of a git repo" &&
return
fi
if [ "$#" -eq 0 ]; then
echo '❌ Must supply a commit message. Usage: gsend "commit message"' &&
return
fi
if [ "$#" -ne 1 ]; then
echo '❌ Unknown parameter(s): only one string allowed for commit message. Usage: gsend "commit message"' &&
return
fi
# Check if a pull is needed or the repo is diverged (see https://stackoverflow.com/a/3278427/12303271)
git fetch &&
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @ @{u})
if [[ $LOCAL = $REMOTE || $REMOTE = $BASE ]]; then
# Up to date ($LOCAL = $REMOTE, no push/pull needed) or push needed ($REMOTE = $BASE): we can commit and push
git add -A &&
git commit -m "$1" &&
git push
elif [[ $LOCAL = $BASE ]]; then
echo "❌ Cannot commit, pull remote changes first"
else
echo "❌ Cannot commit, your branch has diverged"
fi
}