forked from cmlsharp/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsend
executable file
·59 lines (54 loc) · 2.5 KB
/
gsend
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
# Written by: crosroads1112
# Purpose: Easily adding, updating and removing files from a git repo
# NOTE: The -d flag assumes your git repos are subdirectories under your home directory (for example, mine are ~/bin and ~/.dotfiles). If this is not your desired behavior remove the ~/ from line 26. Then you may enter the absolute path of the git repo with the -d flag. If that doesn't suit your fancy, you may also just 'cd' into your git repo and forgo the -d flag entirely. # # #
#########################################
USAGE="${0##*/} [OPTIONS] FILE COMMENT\n\nOPTIONS:\n [-h --help ]\t\tPrint this help message\n [-r --remove]\t\tRemove file from repo\n [-d --dir ]\t\tSpecify local repo location (current directory by default)\n [-l --location]\tSpecify remote repo location (origin by default)\n [-b --branch]\t\tSpecify branch (master by default)"
remove=0
branch=master
location=origin
errmsg(){
echo "$@" >/dev/stderr #Print message supplied to function
exit 1
}
while [[ $(head -c 1 <<<"$1") == "-" ]]; do
case $1 in
-h|--help) echo -e "$USAGE"
exit 0
;;
-r|--remove) remove=1
shift #Get rid of $1 and move $2 to $1
;;
-d|--dir) cd ~/$2 > /dev/null 2>&1 || errmsg "No such directory" #If dir does not exist, run errmsg function
[[ -d ./.git || -d ../.git ]] || errmsg "No git repository exists in this directory or its parent directory" #If there is no .git folder, run errmsg function
shift 2 #Get rid of both the -d option and the folder` name and move $3 to $1
;;
-b|--branch) branch=$2
shift 2
;;
-l|--location) shift
location=$1
shift
;;
-*) echo "Unrecognized argument: $1"
echo -e $USAGE
exit 1
;;
esac
done
if (( $# -lt 2 )); then #If less than two arguments supplied excluding arguments, exit script.
echo "${0##*/}: This script requres at least two arguments (excluding parameters)"
echo "Execute ${0##*/} -h or ${0##*/} --help for usage instructions"
exit 1
fi
{
if [[ $remove == 0 ]]; then
git add $1
else
git rm $1
fi
} 2> /dev/null || errmsg "No such file in specified git repo" #Send stderr from the block of code to /dev/null. If either command that is run fails, run the errmsg function.
shift #Get rid of file name
description=$@ #Set $description equal to all remaining arguments
git commit -m "$description"
git push $location $branch