Socialify

Folder ..

Viewing commit.sh
158 lines (136 loc) • 4.4 KB

  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# This script is used to process certain files or run certain commands
# before a commit is made.  You should use this script instead of
# using the any of the git commands directly.

# This script is called with the following arguments:
# $1 - files to be committed (space separated) or '.' if all files
# $2 - commit message preceded by a '-m' 
# $3 - change type (optional) - valid values are 'feat', 'fix', 
#       'docs', 'style', 'refactor', 'perf', 'test', 'chore' or 
#       'revert'. If not specified, 'feat' is assumed. Must be
#       specified preceded by a '-t' flag.
# $4 - branch name (optional) - if not specified, the current branch
#       is assumed. Must be specified preceded by a '-b' flag.

# This script should return 0 if the commit should proceed or 1 if
# the commit should be aborted.

set -e

# Get the current branch name
branch=$(git rev-parse --abbrev-ref HEAD)


# 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"

    # Sort imports
    python3 -m isort .

    # Format code
    python3 -m black .

    # Generate Maintainers List
    python3 scripts/maintainers.py
}

# Default values for optional arguments
type="feat"
branch=$(git rev-parse --abbrev-ref HEAD)

# 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
        msg="$3: $2"
        # If the branch is the current branch, commit the files
        git add $1
        git commit -m "$msg"
        git push origin $branch
    else
        # If the branch is not the current branch, checkout the branch and commit the files
        git checkout $branch
        msg="$3: $2"
        git add $1
        git commit -m "$msg"
        git push origin $branch
    fi

    # exit with success
    exit 0
}

# 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

# Check if number of arguments is greater than 7
if [ $# -gt 7 ]
then
    echo "Error: Too many arguments"
    helpFuntion 1
fi

# 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

# 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

# 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" $type $branch
else
    # 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" $type $branch
    else
        echo "Commit aborted"
        exit 1
    fi
fi