69 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env bash
 | 
						|
#
 | 
						|
# ghpc (GitHub Push Current)
 | 
						|
#
 | 
						|
# - Push current branch to its remote. Try the following until it works:
 | 
						|
#   - Plain 'git push'
 | 
						|
#   - 'git push -f'
 | 
						|
#   - Try the 'git push' command from the 'git push' error message
 | 
						|
#   - Try adding '-f' to that command
 | 
						|
#
 | 
						|
 | 
						|
yay() { echo "SUCCESS" ; }
 | 
						|
boo() { echo "FAIL" ; }
 | 
						|
 | 
						|
FORCE=$([[ "$1" == "--force" || "$1" == "-f" ]] && echo 1)
 | 
						|
 | 
						|
if [[ ! $FORCE ]]; then
 | 
						|
  echo -n "trying 'git push' ...... "
 | 
						|
  git push >/dev/null 2>&1 && { yay ; exit ; }
 | 
						|
  boo
 | 
						|
fi
 | 
						|
 | 
						|
echo -n "trying 'git push -f' ... "
 | 
						|
 | 
						|
# Get the error output from the failed push
 | 
						|
# and get the recommended 'git push' line
 | 
						|
git push -f 2>&1 | {
 | 
						|
  CMD=""
 | 
						|
 | 
						|
  ltrim() {
 | 
						|
    [[ "$1" =~ [^[:space:]].* ]]
 | 
						|
    printf "%s" "$BASH_REMATCH"
 | 
						|
  }
 | 
						|
 | 
						|
  while IFS= read -r line
 | 
						|
  do
 | 
						|
    #echo "$line"
 | 
						|
    if [[ -z "$CMD" && $line =~ "git push" ]]; then
 | 
						|
      CMD=$(ltrim "$line")
 | 
						|
    fi
 | 
						|
  done
 | 
						|
 | 
						|
  # if a command was found try it
 | 
						|
  if [[ -n "$CMD" ]]; then
 | 
						|
 | 
						|
    boo
 | 
						|
 | 
						|
    if [[ ! $FORCE ]]; then
 | 
						|
      echo -n "trying '$CMD' ...... "
 | 
						|
      $CMD >/dev/null 2>&1 && { yay ; exit ; }
 | 
						|
      boo
 | 
						|
    fi
 | 
						|
 | 
						|
    fCMD=${CMD/ push / push -f }
 | 
						|
    echo -n "trying '$fCMD' ... "
 | 
						|
    $fCMD >/dev/null 2>&1 && { yay ; exit ; }
 | 
						|
    boo
 | 
						|
 | 
						|
    exit 1
 | 
						|
 | 
						|
  else
 | 
						|
 | 
						|
    yay
 | 
						|
 | 
						|
  fi
 | 
						|
}
 | 
						|
 | 
						|
[[ ${PIPESTATUS[1]} == 1 ]] && echo "Sorry! Failed to push current branch."
 |