#!/bin/bash # Log output: # # * 51c333e (12 days) add vim-eunuch # # The time massaging regexes start with ^[^<]* because that ensures that they # only operate before the first "<". That "<" will be the beginning of the # author name, ensuring that we don't destroy anything in the commit message # that looks like time. # # The log format uses } characters between each field, and `column` is later # used to split on them. A } in the commit subject or any other field will # break this. HASH="%C(yellow)%h%Creset" RELATIVE_TIME="%Cgreen(%ar)%Creset" AUTHOR="%C(bold blue)<%an>%Creset" REFS="%C(red)%d%Creset" SUBJECT="%s" DEFAULT_REMOTE="upstream" ROOT_BRANCH="master" MAIN_BRANCH="main" DEVELOP_BRANCH="development" FORMAT="$HASH}$RELATIVE_TIME}$AUTHOR}$REFS $SUBJECT" pretty_git_log() { git log --graph --pretty="tformat:${FORMAT}" "$@" | # Replace (2 years ago) with (2 years) #sed -Ee 's/(^[^<]*) ago)/\1)/' | sed -e 's/ ago//' | # Replace (2 years, 5 months) with (2 years) #sed -Ee 's/(^[^<]*), [[:digit:]]+ .*months?)/\1)/' | sed -e 's/(^[^<]*), [[:digit:]]+ .*months?)/)/' | # Line columns up based on } delimiter column -s '}' -t | # Page only if we need to less -FXRS } sync_to_remote() { REMOTE=$1 : "${REMOTE:=$DEFAULT_REMOTE}" BRANCH=$(git rev-parse --abbrev-ref HEAD) echo "Synching with ${REMOTE}/${BRANCH}" git fetch "${REMOTE}" && git rebase "${REMOTE}/${BRANCH}"; } update_branch() { BRANCH=$1 : "${BRANCH:=$ROOT_BRANCH}" REMOTE=$2 : "${REMOTE:=$DEFAULT_REMOTE}" echo Updating ${BRANCH} from ${REMOTE} # check stash stack before and after old_stash=$(git rev-parse -q --verify refs/stash) git stash new_stash=$(git rev-parse -q --verify refs/stash) # checkout branch and sync to remote, then come back git checkout ${BRANCH} && git sync ${REMOTE} && git checkout - && git rebase ${BRANCH} # if the stash added to the stack, pop it back off [ "$old_stash" != "$new_stash" ] && git stash pop } get_pr() { PR=$1 REMOTE=$2 : "${REMOTE:=$DEFAULT_REMOTE}" if [ -z "$PR" ]; then echo "Please specify a PR to checkout" exit 1 fi echo Checking out PR "${PR}" from ${REMOTE} git fetch ${REMOTE} "pull/${PR}/head:pr/${PR}" git checkout "pr/${PR}" } del_pr() { BRANCH=$(git rev-parse --abbrev-ref HEAD) if [[ ! ${BRANCH} =~ ^pr\/ ]]; then echo "Not a PR branch, aborting!" exit 1 fi git checkout - && git branch -D "${BRANCH}" } clean_branches() { BRANCHES=$(git branch | grep -v "${ROOT_BRANCH}\|${MAIN_BRANCH}\|${DEVELOP_BRANCH}\|\*") for i in ${BRANCHES}; do git branch -d "$i" done } backport_pr() { PR=$1 BRANCH=$2 [ -z "$BRANCH" ] && BRANCH=$(git rev-parse --abbrev-ref HEAD) REMOTE=$3 : "${REMOTE:=$DEFAULT_REMOTE}" THISBRANCH=$(git rev-parse --abbrev-ref HEAD) URL="https://patch-diff.githubusercontent.com/raw/elastic/kibana/pull/${PR}.patch" if [ -z "$PR" ]; then echo "Please specify a PR to backport" exit 1 fi echo "Backporting ${PR} to ${BRANCH} from ${REMOTE}" # if the backport couldn't be cleanly applied, tell the user and exit if ! git checkout "${BRANCH}" && git pull ${REMOTE} "${BRANCH}" && curl -L -s "$URL" | git am then echo "FAILED - Backport could not be cleanly applied!" echo "FAILED - Fix by hand or run 'git am --abort'" exit 2 fi # switch back if we didn't start in the target branch [ "$THISBRANCH" != "$BRANCH" ] && git checkout - } track_remote() { REMOTE=$1 : "${REMOTE:="origin"}" BRANCH=$2 [ -z "$BRANCH" ] && BRANCH=$(git rev-parse --abbrev-ref HEAD) git branch --set-upstream-to="${REMOTE}/${BRANCH}" "${BRANCH}" }