aboutsummaryrefslogtreecommitdiff
path: root/commit.sh
diff options
context:
space:
mode:
Diffstat (limited to 'commit.sh')
-rwxr-xr-xcommit.sh164
1 files changed, 114 insertions, 50 deletions
diff --git a/commit.sh b/commit.sh
index 878714a..b3e4fc7 100755
--- a/commit.sh
+++ b/commit.sh
@@ -18,67 +18,131 @@
# Get the current branch name
branch=$(git rev-parse --abbrev-ref HEAD)
-# Get arguments passed to this script
-files=$1
-message=$2
-type=$3
-branch=$4
-
-# Check if the branch name is specified
-if [ "$branch" != "" ]; then
- # Check if the branch name is valid
- if [ $(git branch | grep -c "$branch") -eq 0 ]; then
- echo "Branch $branch does not exist"
- exit 1
- fi
-fi
-# Check if the change type is specified
-if [ "$type" != "" ]; then
- # Check if the change type is valid
- if [ $(echo $type | grep -c "feat\|fix\|docs\|style\|refactor\|perf\|test\|chore\|revert") -eq 0 ]; then
- echo "Invalid change type $type"
- exit 1
+# Help function
+
+helpFuntion() {
+ echo "Usage: $0 files -m message -t type -b branch"
+ echo files "\t files to be committed (space separated) or '.' if all files"
+ echo -m "\t message: commit message"
+ echo -t "\t type: change type (optional) - valid values are 'feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore' or 'revert'. If not specified, 'feat' is assumed."
+ echo -b "\t branch: branch name (optional) - if not specified, the current branch is assumed."
+ echo -h, --help "\t display this help message"
+ exit $1
+}
+
+
+# User Defined Commands
+commands() {
+ # Add commands here
+ # Example:
+ # echo "Hello World"
+
+ # Generate Requirements.txt, delete the old one if it exists
+ if [ -f requirements.txt ]
+ then
+ rm requirements.txt
fi
-fi
-
-# Check if the commit message is specified, if not, prompt the user
-if [ "$message" = "" ]; then
- echo "Enter commit message (required):"
- read message
-fi
+ pip3 freeze > requirements.txt
+}
+# Default values for optional arguments
+type="feat"
+branch=$(git rev-parse --abbrev-ref HEAD)
-# Check if files are specified, if not, prompt the user
-if [ "$files" = "" ]; then
- echo "Enter files to be committed (space separated) or '.' for all files (required):"
- read files
+# Commit function
+commit() {
+ # Run User Defined Commands
+ commands
+ # Check if the branch is the current branch
+ if [ "$branch" == "$(git rev-parse --abbrev-ref HEAD)" ]
+ then
+ # If the branch is the current branch, commit the files
+ git commit $1 -m "$2"
+ else
+ # If the branch is not the current branch, checkout the branch and commit the files
+ git checkout $3
+ git commit $1 -m "$2"
+ fi
+}
+
+# Check if the number of arguments is less than 2
+if [ $# -le 1 ]
+then
+ # Check if the first argument is -h or --help
+ if [ "$1" == "-h" ] || [ "$1" == "--help" ]
+ then
+ helpFuntion 0
+ else
+ echo "Error: Not enough arguments"
+ helpFuntion 1
+ fi
fi
-
-# Generate requirements.txt file, delete it if it already exists
-if [ -f requirements.txt ]; then
- rm requirements.txt
+# Check if number of arguments is greater than 7
+if [ $# -gt 7 ]
+then
+ echo "Error: Too many arguments"
+ helpFuntion 1
fi
-pip3 freeze > requirements.txt
-
-# Add files to be committed
-git add $files
-git add requirements.txt
-
-# Modify the message in the following format: <type>: <message>
-if [ "$type" != "" ]; then
- message="$type: $message"
+# Parse the arguments
+while [ "$1" != "" ]; do
+ case $1 in
+ -m | --message ) shift
+ message=$1
+ ;;
+ -t | --type ) shift
+ type=$1
+ ;;
+ -b | --branch ) shift
+ branch=$1
+ ;;
+ -h | --help ) helpFuntion 0
+ ;;
+ * ) files="$files $1"
+ esac
+ shift
+done
+
+# Check if the message is empty
+if [ -z "$message" ]
+then
+ # If the message is empty, prompt the user for a message
+ read -p "Enter commit message: " message
fi
-# Commit the changes
-git commit -m "$message"
+# Add type to the message
+message="$type: $message"
+
+# Check if the branch exists
+if [ -z "$(git branch --list $branch)" ]
+then
+ # If the branch does not exist, prompt if the user wants to create it
+ read -p "Branch $branch does not exist. Do you want to create it? (y/n): " createBranch
+ if [[ $createBranch =~ ^[Yy]$ ]] || [[ $createBranch =~ ^[Yy][Ee][Ss]$ ]]
+ then
+ git checkout -b $branch
+ else
+ echo "Commit aborted"
+ exit 1
+ fi
+fi
-# Push the changes to the remote branch
-if [ "$branch" != "" ]; then
- git push origin $branch
+# Check if type is valid
+if [[ $type =~ ^(feat|fix|docs|style|refactor|perf|test|chore|revert)$ ]]
+then
+ # If type is valid, run commit function
+ commit $files $message $branch
else
- git push origin $branch
+ # If type is invalid, prompt the to use the default type
+ read -p "Invalid type. Do you want to use the default type "feat"? (y/n): " useDefaultType
+ if [[ $useDefaultType =~ ^[Yy]$ ]] || [[ $useDefaultType =~ ^[Yy][Ee][Ss]$ ]]
+ then
+ commit $files $message $branch
+ else
+ echo "Commit aborted"
+ exit 1
+ fi
fi