├── .env.example ├── .gitignore ├── LICENSE ├── README.md ├── colors.sh └── git.sh /.env.example: -------------------------------------------------------------------------------- 1 | # ex.: https://github.com/wilcorrea/dev-utils/issues/${COMMIT_ISSUE} 2 | ISSUE_TRACKER="" 3 | # ex.: use 1 to use the branch name as issue id, or 0 not to use 4 | BRANCH_AS_ISSUE_ID="1" 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .env -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 William Correa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dev-utils 2 | 3 | Only a set of functions to standardize common dev operations 4 | 5 | ## How to configure 6 | 7 | Clone (or download) the repo in your local project folder, ex.: 8 | ``` 9 | git clone https://github.com/wilcorrea/dev-utils.git ~/projects/dev-utils 10 | ``` 11 | 12 | Add the script to your .bashrc, ex.: 13 | ``` 14 | echo "source ~/projects/dev-utils/git.sh" >> ~/.bashrc 15 | ``` 16 | 17 | ## How to use 18 | 19 | After add the script in .bashrc make sure you created a new session in your terminal and use the commands available in the script you added. 20 | 21 | ### git.sh 22 | 23 | [![asciicast](https://asciinema.org/a/242553.svg)](https://asciinema.org/a/242553) 24 | 25 | #### status 26 | 27 | The status function will basically execute the `git status` with the parameter` --short` 28 | 29 | #### commit 30 | 31 | The commit function will run an interactive menu to execute a `git commit -m "standardize commit message"`. 32 | 33 | #### log 34 | 35 | Use the log command to get a `git log` with a preconfigured format and the option to pass as argument a text that will be used in the `grep` parameter. 36 | 37 | #### push 38 | 39 | Using the push command will be executed `git config credential.helper store` before `git push`. 40 | This can be useful to store the credentials of repository. 41 | 42 | #### pull 43 | 44 | The command pull has the same features than `push` command. 45 | 46 | ## Personalization 47 | 48 | Create a copy of .env.example named .env in the project root folder and customize the properties as shown in the examples 49 | 50 | ---- 51 | 52 | shared with ❤ by [@wilcorrea](https://github.com/wilcorrea) 53 | -------------------------------------------------------------------------------- /colors.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Copyright 2013 Manuel Gutierrez 4 | # https://github.com/xr09/rainbow.sh 5 | # Bash helper functions to put colors on your scripts 6 | # 7 | # Usage example: 8 | # green=$(green "Grass is green") 9 | # echo "Coming next: $green" 10 | # 11 | 12 | __RAINBOWPALETTE="38;5" 13 | 14 | function __color() 15 | { 16 | echo -e " \e[$__RAINBOWPALETTE;$2m$1\e[0m" 17 | } 18 | 19 | function red() 20 | { 21 | echo $(__color "$1" "1") 22 | } 23 | 24 | function _red() 25 | { 26 | echo -n $(__color "$1" "1") 27 | } 28 | 29 | function green() 30 | { 31 | echo $(__color "$1" "2") 32 | } 33 | 34 | function _green() 35 | { 36 | echo -n $(__color "$1" "2") 37 | } 38 | 39 | function yellow() 40 | { 41 | echo $(__color "$1" "3") 42 | } 43 | 44 | function _yellow() 45 | { 46 | echo -n $(__color "$1" "3") 47 | } 48 | 49 | function blue() 50 | { 51 | echo $(__color "$1" "27") 52 | } 53 | 54 | function _blue() 55 | { 56 | echo -n $(__color "$1" "27") 57 | } 58 | 59 | function cyan() 60 | { 61 | echo $(__color "$1" "6") 62 | } 63 | 64 | function _cyan() 65 | { 66 | echo -n $(__color "$1" "6") 67 | } 68 | 69 | function purple() 70 | { 71 | echo $(__color "$1" "171") 72 | } 73 | 74 | function _purple() 75 | { 76 | echo -n $(__color "$1" "171") 77 | } 78 | 79 | -------------------------------------------------------------------------------- /git.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 4 | source ${SCRIPT_PATH}/colors.sh 5 | 6 | # ascii art :) 7 | function __presentation 8 | { 9 | echo " _____ ______ __ __ __ __ ______ __ __ ______" 10 | echo "/\ __-. /\ ___\ /\ \ / / /\ \/\ \ /\__ _\ /\ \ /\ \ /\ ___\\" 11 | echo "\ \ \/\ \ \ \ __\ \ \ \'/ \ \ \_\ \ \/_/\ \/ \ \ \ \ \ \____ \ \___ \\" 12 | echo " \ \____- \ \_____\ \ \__| \ \_____\ \ \_\ \ \_\ \ \_____\ \/\_____\\" 13 | echo " \/____/ \/_____/ \/_/ \/_____/ \/_/ \/_/ \/_____/ \/_____/" 14 | 15 | green "# ${1}" 16 | } 17 | 18 | # perform log commit command 19 | function commit() 20 | { 21 | __presentation "commit" 22 | 23 | __commitPerform 24 | 25 | if [[ ! -d .git ]]; then 26 | red "## This folder is not a working tree" 27 | return 28 | fi 29 | 30 | # check if repo is empty 31 | if [[ ! "$(git branch -vv)" ]];then 32 | red "## No branches available" 33 | _red "## Create the first commit? [y/n] $ " 34 | read -n 1 READ_START 35 | echo "" 36 | if [[ ${READ_START} = "y" ]]; then 37 | git commit -m "[init] Just init the repo" --allow-empty >/dev/null 2>&1 38 | else 39 | return 40 | fi 41 | fi 42 | } 43 | 44 | # commit command body instructions 45 | function __commitPerform 46 | { 47 | ISSUE_TRACKER="" 48 | BRANCH_AS_ISSUE_ID="0" 49 | if [[ -f ${SCRIPT_PATH}/.env ]]; then 50 | source ${SCRIPT_PATH}/.env 51 | fi 52 | if [[ -f .du ]]; then 53 | source .du 54 | fi 55 | 56 | MODIFIED=$(git status --short) 57 | if [[ ! ${MODIFIED} ]]; then 58 | red "## No changes available" 59 | return 60 | fi 61 | 62 | # get modified files 63 | COMMIT_FILES_OPTIONS=($(git ls-files --others --exclude-standard --modified)) 64 | # check if repo is empty 65 | COMMIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) 66 | # start COMMIT_FILES_CHOSEN as empty array 67 | COMMIT_FILES_CHOSEN=() 68 | # start selected file with EMPTY 69 | SELECTED_FILE="EMPTY" 70 | # start index with 0 71 | INDEX=0 72 | # flag control to select all 73 | SELECT_ALL=0 74 | 75 | # while the user select a file 76 | while [[ "${SELECTED_FILE}" ]]; do 77 | # show menu (COMMIT_FILES_OPTIONS and COMMIT_FILES_CHOSEN are used as global) 78 | __commitMenu 79 | 80 | yellow "# Enter the file number in the list to toggle selection" 81 | yellow "# [separate with ',' when more than 9 options]" 82 | _yellow "# [type enter with empty to go ahead] $ " 83 | 84 | # if there is more than 10 files will be necessary type enter 85 | if (( ${#COMMIT_FILES_OPTIONS[@]} > 10 )); then 86 | read SELECTED_FILE 87 | # otherwise the value will be picked up automatically after typing 88 | else 89 | read -n 1 SELECTED_FILE 90 | fi 91 | 92 | echo "" 93 | 94 | # stop selection if the value entered is empty 95 | if [[ ! "${SELECTED_FILE}" ]]; then 96 | break 97 | fi 98 | 99 | # stop selection if the value entered is 0 100 | if [[ "${SELECTED_FILE}" = "0" ]]; then 101 | SELECT_ALL=1 102 | break 103 | fi 104 | 105 | # scroll through the selected files input to apply the option that came from the menu 106 | while IFS=',' read -ra ITEM; do 107 | for i in "${ITEM[@]}"; do 108 | INDEX=${i} 109 | 110 | # test if selected option is valid 111 | ((INDEX--)) 112 | if (( INDEX < 0 || INDEX >= ${#COMMIT_FILES_OPTIONS[@]} )); then 113 | # show a message of invalid input 114 | red " ~> Invalid option: ${SELECTED_FILE}" 115 | continue 116 | fi 117 | 118 | # if the option is already selected... 119 | if [[ "${COMMIT_FILES_CHOSEN[INDEX]}" ]]; then 120 | # ...unselected the option 121 | COMMIT_FILES_CHOSEN[INDEX]="" 122 | continue 123 | fi 124 | # select the option 125 | COMMIT_FILES_CHOSEN[INDEX]="x" 126 | done 127 | # apply the SELECTED_FILE to for 128 | done <<< "${SELECTED_FILE}" 129 | done 130 | 131 | # if SELECT_ALL was selected... 132 | if [[ "${SELECT_ALL}" = 1 ]]; then 133 | # ...clear the selection 134 | SELECT_ALL=0 135 | # ...scroll through all file options... 136 | for i in ${!COMMIT_FILES_OPTIONS[@]}; do 137 | # ...and select each one 138 | COMMIT_FILES_CHOSEN[i]="x" 139 | done 140 | fi 141 | 142 | yellow "# Log" 143 | # scroll through all file options 144 | for i in ${!COMMIT_FILES_OPTIONS[@]}; do 145 | # if is not selected ignore 146 | if [[ ! "${COMMIT_FILES_CHOSEN[i]}" ]]; then 147 | continue; 148 | fi 149 | # else perform git add command 150 | MUTE=$(git add ${COMMIT_FILES_OPTIONS[i]}) 151 | done 152 | 153 | # get the git changes to create a log with the changes in commit 154 | COMMIT_CHANGES=$(git diff --cached --name-only) 155 | # convert the changes in an array 156 | ADDED=(${COMMIT_CHANGES}) 157 | # if there is no changes... 158 | if [[ ! "$ADDED" ]]; then 159 | # ...abort commit 160 | red "## No changes available to perform a commit" 161 | return 162 | fi 163 | 164 | # default log message 165 | message=" ~> nothing of anything" 166 | # scroll through the added files in git 167 | for i in ${!ADDED[@]}; do 168 | # show message with the name of file listed to be in commit 169 | cyan " ~> git add ${ADDED[i]}" 170 | # reset the default log message 171 | message="" 172 | done 173 | # show the default log message 174 | cyan "$message" 175 | 176 | yellow "# Commit type (use the numbers for the options listed below)" 177 | yellow "# [if the option you entered is not in the list, the 'feature' option will be used] " 178 | _yellow "# 1:feature, 2:fix, 3:review, 4:doc, 5:test, 6:devops, 7:merge or 8:finish $ " 179 | read -n 1 READ_COMMIT_TYPE 180 | # get commit type 181 | case ${READ_COMMIT_TYPE} in 182 | 2) 183 | COMMIT_TYPE="fix" 184 | ;; 185 | 3) 186 | COMMIT_TYPE="review" 187 | ;; 188 | 4) 189 | COMMIT_TYPE="doc" 190 | ;; 191 | 5) 192 | COMMIT_TYPE="test" 193 | ;; 194 | 6) 195 | COMMIT_TYPE="devops" 196 | ;; 197 | 7) 198 | COMMIT_TYPE="merge" 199 | ;; 200 | 8) 201 | COMMIT_TYPE="finish" 202 | ;; 203 | *) 204 | COMMIT_TYPE="feature" 205 | ;; 206 | esac 207 | if [[ ${READ_COMMIT_TYPE} ]]; then 208 | echo "" 209 | fi 210 | cyan " ~> selected type: ${COMMIT_TYPE}" 211 | 212 | # initialize message of commit 213 | COMMIT_MESSAGE="" 214 | # repeat until get a message 215 | until [[ "${COMMIT_MESSAGE}" ]] 216 | do 217 | _yellow "# Commit message $ " 218 | read COMMIT_MESSAGE 219 | # validate message 220 | if [[ ! ${COMMIT_MESSAGE} ]]; then 221 | red "## Message is required" 222 | fi 223 | done 224 | 225 | # init commit reference with the branch name 226 | COMMIT_REFERENCE=$(echo $(basename ${COMMIT_BRANCH}) | tr a-z A-Z) 227 | 228 | # if the flag BRANCH_AS_ISSUE_ID is active ... 229 | if [[ ${BRANCH_AS_ISSUE_ID} = "1" ]]; then 230 | # ... use the branch name as reference 231 | READ_COMMIT_RELATED=${COMMIT_REFERENCE} 232 | 233 | # else ask user the issue ID 234 | else 235 | yellow "# Related issue" 236 | yellow "# [enter the ID of issue]" 237 | _yellow "# [if empty will use the branch name as reference in commit] $ " 238 | 239 | # concat related with a text to be human readable 240 | COMMIT_RELATED="> current branch: ${COMMIT_BRANCH}" 241 | 242 | # read commit related info 243 | read READ_COMMIT_RELATED 244 | fi 245 | 246 | # if commit related was entered... 247 | if [[ ${READ_COMMIT_RELATED} ]]; then 248 | # convert related to uppercase 249 | READ_COMMIT_RELATED=$(echo ${READ_COMMIT_RELATED} | tr a-z A-Z) 250 | # use related as commit reference 251 | COMMIT_REFERENCE="#${READ_COMMIT_RELATED}" 252 | 253 | COMMIT_RELATED_ISSUE_LINK="#${COMMIT_RELATED}" 254 | ## get issue tracker from .env 255 | if [[ ${ISSUE_TRACKER} ]]; then 256 | # concat related with a text to be human readable 257 | COMMIT_RELATED_ISSUE_LINK="${ISSUE_TRACKER}/${READ_COMMIT_RELATED}" 258 | fi 259 | COMMIT_RELATED="> current branch: ${COMMIT_BRANCH}, issue link: ${COMMIT_RELATED_ISSUE_LINK}" 260 | fi 261 | 262 | COMMIT_MESSAGE="[${COMMIT_REFERENCE}/${COMMIT_TYPE}] ${COMMIT_MESSAGE}" 263 | green " ~> git commit -m '${COMMIT_MESSAGE}'" 264 | 265 | git commit -q -F- <