|
| 1 | +#!/usr/bin/env bash |
| 2 | +#make ticket name with dashes |
| 3 | + |
| 4 | +publish() { |
| 5 | + #if no remote is provided, tell user how to configure it |
| 6 | + # if [[ -z $(git remote) ]]; then |
| 7 | + # echo "No remote repository found. Please configure one using:" |
| 8 | + # echo "git remote add origin <remote repository URL>" |
| 9 | + # exit 1 |
| 10 | + # fi |
| 11 | + |
| 12 | + #get ticket name if provided otherwise use current ticket |
| 13 | + if [[ -n "$1" ]]; then |
| 14 | + ticket="$1" |
| 15 | + #if current ticket is not the same as the one provided, start the provided ticket |
| 16 | + if [[ "$ticket" != "$(get_current_ticket)" ]]; then |
| 17 | + start "$ticket" |
| 18 | + fi |
| 19 | + else |
| 20 | + if [[ ! -f .git/current-ticket ]]; then |
| 21 | + #start a ticket if none is started |
| 22 | + echo "You did not start a ticket yet, creating one for you..." |
| 23 | + start "$1" |
| 24 | + fi |
| 25 | + ticket=$(get_current_ticket) |
| 26 | + fi |
| 27 | + |
| 28 | + git add -A #mark fixed conflicts as resolved (should be done in refresh?) |
| 29 | + |
| 30 | + refresh |
| 31 | + #double_check_conflicts |
| 32 | + |
| 33 | + #commit and push |
| 34 | + git add -A |
| 35 | + git commit -m "$ticket" |
| 36 | + git push &>/dev/null && finish_ticket && echo "Work on \"$ticket\" published successfully." |
| 37 | + |
| 38 | + #finish work on current ticket |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +unpublish() { |
| 43 | + # Safely update your local repository |
| 44 | + git update |
| 45 | + |
| 46 | + # Get the commit hash or reference associated with the ticket |
| 47 | + ticket=${1:-$(get_current_ticket)} |
| 48 | + |
| 49 | + # Find the commit with the matching ticket name |
| 50 | + commit_hash=$(git log --grep="$ticket" --pretty=format:%H -n 1) |
| 51 | + |
| 52 | + if [[ -z $commit_hash ]]; then |
| 53 | + echo "No ticket created with the name \"$ticket\"." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + # Revert the commit |
| 58 | + git revert --no-commit "$commit_hash" |
| 59 | + |
| 60 | + # Check for conflicts after reverting |
| 61 | + conflict_files=$(grep -r '<<<<<<<|=======|>>>>>>>' .) |
| 62 | + if [[ -n $conflict_files ]]; then |
| 63 | + echo "Please fix conflicts in the following files after reverting:" |
| 64 | + echo "$conflict_files" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + |
| 68 | + # Add all changes to the staging area |
| 69 | + git add . |
| 70 | + |
| 71 | + # Create a revert commit with the ticket name |
| 72 | + git commit -m "revert of $ticket" |
| 73 | + |
| 74 | + # Push the changes to the remote repository |
| 75 | + git push |
| 76 | + |
| 77 | + echo "Unpublishing of \"$ticket\" completed successfully." |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +start() { |
| 82 | + if [[ ! -d .git ]]; then |
| 83 | + echo "Git repository not found. Initializing a new repository..." |
| 84 | + git init |
| 85 | + fi |
| 86 | + |
| 87 | + git add -A #mark fixed conflicts as resolved |
| 88 | + |
| 89 | + #if no ticket name is provided, use the next ticket number |
| 90 | + ticket=${1:-"WIP-$(get_next_ticket_number)"} |
| 91 | + # Replace spaces with dashes in the ticket name |
| 92 | + ticket=${ticket// /-} |
| 93 | + |
| 94 | + |
| 95 | + #if another ticket is in progress, pause it (stash it) |
| 96 | + if [ -f .git/current-ticket ]; then |
| 97 | + existing_current_ticket=$(get_current_ticket) |
| 98 | + if [ "$ticket" != "$existing_current_ticket" ]; then |
| 99 | + pause "$existing_current_ticket" |
| 100 | + fi |
| 101 | + fi |
| 102 | + |
| 103 | + #resume any work in progress of that ticket |
| 104 | + stash_ref=$(git stash list | grep -w "$1" | cut -d "{" -f2 | cut -d "}" -f1) |
| 105 | + git stash apply stash@{$stash_ref} &>/dev/null |
| 106 | + git stash drop stash@{$stash_ref} &>/dev/null |
| 107 | + |
| 108 | + |
| 109 | + #update codebase |
| 110 | + refresh |
| 111 | + |
| 112 | + #update current ticket value |
| 113 | + echo "$ticket" > .git/current-ticket |
| 114 | + echo "Work on \"$ticket\" started successfully." |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +pause() { |
| 119 | + current_ticket=${1:-$(get_current_ticket)} |
| 120 | + git stash push -m "$current_ticket" &>/dev/null |
| 121 | + echo "Work on \"$current_ticket\" paused successfully." |
| 122 | + finish_ticket |
| 123 | +} |
| 124 | + |
| 125 | +refresh() { |
| 126 | + #get current ticket name if started otherwise use temp name for stash |
| 127 | + current_ticket=${get_current_ticket:-"temp-WIP-$(date +%s)"} |
| 128 | + |
| 129 | + #stash before pulling |
| 130 | + git stash push -m "$current_ticket" -u &>/dev/null |
| 131 | + git pull &>/dev/null |
| 132 | + |
| 133 | + #apply and than delete stash after pulling |
| 134 | + stash_ref=$(git stash list | grep -w "$current_ticket" | cut -d "{" -f2 | cut -d "}" -f1) |
| 135 | + git stash apply stash@{$stash_ref} &>/dev/null |
| 136 | + git stash drop stash@{$stash_ref} &>/dev/null |
| 137 | + |
| 138 | + check_conflicts |
| 139 | +} |
| 140 | + |
| 141 | +current() { |
| 142 | + if [ -f .git/current-ticket ]; then |
| 143 | + echo "You are currently working on: $(get_current_ticket)" |
| 144 | + else |
| 145 | + echo "You are not currently working on anything." |
| 146 | + fi |
| 147 | +} |
| 148 | +check_conflicts() { |
| 149 | + unresolved_files=$(git diff --name-only --diff-filter=U) |
| 150 | + if [[ -n $unresolved_files ]]; then |
| 151 | + echo "BEFORE DOING ANYTHING ELSE, FIX CONFLICTS IN THE FOLLOWING FILES:" |
| 152 | + echo "$unresolved_files" |
| 153 | + exit 1 |
| 154 | + fi |
| 155 | +} |
| 156 | + |
| 157 | +double_check_conflicts() { |
| 158 | + conflict_files=$(grep -r '<<<<<<<|=======|>>>>>>>' .) |
| 159 | + if [[ -n $conflict_files ]]; then |
| 160 | + echo "Please fix conflicts in the following files and try again:" |
| 161 | + echo "$conflict_files" |
| 162 | + exit 1 |
| 163 | + fi |
| 164 | +} |
| 165 | + |
| 166 | +get_current_ticket() { |
| 167 | + ticket=$(cat .git/current-ticket) |
| 168 | + echo $ticket |
| 169 | +} |
| 170 | + |
| 171 | +get_next_ticket_number() { |
| 172 | + last_ticket_number=0 |
| 173 | + if [ -f .git/last-ticket-number ]; then |
| 174 | + last_ticket_number=$(cat .git/last-ticket-number) |
| 175 | + fi |
| 176 | + next_ticket_number=$((last_ticket_number + 1)) |
| 177 | + |
| 178 | + echo "$next_ticket_number" > .git/last-ticket-number |
| 179 | + |
| 180 | + echo "$next_ticket_number" |
| 181 | +} |
| 182 | + |
| 183 | +finish_ticket() { |
| 184 | + #finish work on current ticket |
| 185 | + rm -f .git/current-ticket 2>/dev/null &>/dev/null |
| 186 | +} |
| 187 | + |
| 188 | +# Invoke the appropriate function based on the command |
| 189 | +"$@" |
| 190 | + |
| 191 | + |
| 192 | +#publish |
| 193 | +#show files that will be published + ai description that tries to tell you what you are about to publish |
| 194 | +#ask for confirmation (makemsure you test your code before publishing) |
| 195 | +#git update send update to remote even if there are no changes |
| 196 | + |
| 197 | +#git undo to remove current changes when not yet published (not permantely deleted, you can still recover them with git redo) |
| 198 | +#git reload, pause every ticket, with -f should reset repository to server version |
0 commit comments