Compare commits
10 Commits
c253b61fb0
...
4dd974484f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4dd974484f | ||
|
|
ffd0ae92d3 | ||
| bc42a3f8ef | |||
| a9ec765b5d | |||
| 4bdd001fb1 | |||
| d02733bff1 | |||
|
|
815f54be68 | ||
| f452ff65ac | |||
| 56f3fbac81 | |||
| 5dcd2cf0d2 |
@@ -65,10 +65,6 @@ function parse_git_branch {
|
||||
return
|
||||
}
|
||||
|
||||
if [ -f `brew --prefix`/etc/bash_completion ]; then
|
||||
. `brew --prefix`/etc/bash_completion
|
||||
fi
|
||||
|
||||
export TERM="xterm-color"
|
||||
alias ls="ls -G"
|
||||
export PS1="$PURPLE\u@\h$WHITE:$BLUE\w$YELLOW\$(parse_git_branch)$WHITE\$ "
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
bi = bisect
|
||||
ci = commit
|
||||
co = checkout
|
||||
fu = commit --fixup
|
||||
cp = cherry-pick
|
||||
di = diff
|
||||
dc = diff --cached
|
||||
amend = commit --amend
|
||||
@@ -18,15 +20,17 @@
|
||||
gnc = goodness --cached
|
||||
fa = fetch --all
|
||||
pom = push origin master
|
||||
b = branch
|
||||
b = "!git for-each-ref --sort=-committerdate refs/heads --format='%(authordate:short) %(color:yellow)%(refname:short) %(color:red)%(objectname:short) %(color:reset) (%(color:green)%(committerdate:relative)%(color:reset))'"
|
||||
ll = log --pretty='%C(yellow)%h %C(cyan)%cd %Cblue%aN%C(auto)%d %Creset%s' --graph --date=relative --topo-order --decorate
|
||||
ds = diff --stat=160,120
|
||||
dh1 = diff HEAD~1
|
||||
|
||||
head = !git l -1
|
||||
h = !git head
|
||||
hp = "!source ~/.githelpers && show_git_head"
|
||||
hs = !git head --show-signature
|
||||
r = !git l -30
|
||||
ra = !git r --all
|
||||
rs = !git log --oneline --show-signature
|
||||
l = "!source ~/.githelpers && pretty_git_log"
|
||||
la = !git l --all
|
||||
sync = "!source ~/.githelpers && sync_to_remote"
|
||||
@@ -34,12 +38,19 @@
|
||||
bp = !git backport
|
||||
backport = "!source ~/.githelpers && backport_pr"
|
||||
track = "!source ~/.githelpers && track_remote"
|
||||
sign = "!git commit -S --amend --no-edit"
|
||||
|
||||
todo = grep --heading --break --ignore-case -e ' FIX: *' -e ' TODO: *'
|
||||
pr = "!source ~/.githelpers && get_pr"
|
||||
prd = "!source ~/.githelpers && del_pr"
|
||||
brc = "!source ~/.githelpers && clean_branches"
|
||||
prx = "!source ~/.githelpers && del_pr"
|
||||
brx = "!source ~/.githelpers && clean_branches"
|
||||
lbr = for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/heads
|
||||
snc = "!source ~/.githelpers && create_snap"
|
||||
sna = "!source ~/.githelpers && apply_snap"
|
||||
snl = "!source ~/.githelpers && list_snaps"
|
||||
snS = "!source ~/.githelpers && show_snap"
|
||||
snr = "!source ~/.githelpers && revert_snap"
|
||||
snx = "!source ~/.githelpers && remove_snap"
|
||||
[merge]
|
||||
tool = vimdiff
|
||||
ff = true
|
||||
@@ -48,3 +59,5 @@
|
||||
followTags = true
|
||||
[branch]
|
||||
autosetuprebase = always
|
||||
[rebase]
|
||||
autosquash = true
|
||||
|
||||
240
git/githelpers
240
git/githelpers
@@ -18,18 +18,21 @@ RELATIVE_TIME="%Cgreen(%ar)%Creset"
|
||||
AUTHOR="%C(bold blue)<%an>%Creset"
|
||||
REFS="%C(red)%d%Creset"
|
||||
SUBJECT="%s"
|
||||
DEFAULT_REMOTE="upstream"
|
||||
DEFAULT_REMOTE="origin"
|
||||
ROOT_BRANCH="master"
|
||||
MAIN_BRANCH="main"
|
||||
DEVELOP_BRANCH="development"
|
||||
|
||||
FORMAT="$HASH}$RELATIVE_TIME}$AUTHOR}$REFS $SUBJECT"
|
||||
|
||||
show_git_head() {
|
||||
pretty_git_log -1
|
||||
git show -p --pretty="tformat:"
|
||||
}
|
||||
RED="\033[0;31m"
|
||||
GREEN="\033[0;32m"
|
||||
YELLOW="\033[0;33m"
|
||||
CYAN="\033[0;36m"
|
||||
CRESET="\033[0m"
|
||||
|
||||
pretty_git_log() {
|
||||
git log --graph --pretty="tformat:${FORMAT}" $* |
|
||||
git log --graph --pretty="tformat:${FORMAT}" "$@" |
|
||||
# Replace (2 years ago) with (2 years)
|
||||
#sed -Ee 's/(^[^<]*) ago)/\1)/' |
|
||||
sed -e 's/ ago//' |
|
||||
@@ -43,92 +46,207 @@ pretty_git_log() {
|
||||
}
|
||||
|
||||
sync_to_remote() {
|
||||
REMOTE=$1
|
||||
: ${REMOTE:=$DEFAULT_REMOTE}
|
||||
BRANCH=`git rev-parse --abbrev-ref HEAD`
|
||||
echo "Synching with ${REMOTE}"
|
||||
git fetch ${REMOTE} && git rebase ${REMOTE}/${BRANCH};
|
||||
REMOTE="${1:-${DEFAULT_REMOTE}}"
|
||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
|
||||
echo "Synching with ${YELLOW}${REMOTE}/${BRANCH}${CRESET}"
|
||||
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 -
|
||||
# if the stash added to the stack, pop it back off
|
||||
[ "$old_stash" != "$new_stash" ] && git stash pop
|
||||
BRANCH="${1:-${MAIN_BRANCH}}"
|
||||
REMOTE="${2:-${DEFAULT_REMOTE}}"
|
||||
|
||||
echo "Fetching ${YELLOW}${REMOTE}/${BRANCH}${CRESET} and rebasing with autostash"
|
||||
|
||||
# 1. Update the local ref for the target branch (worktree-safe)
|
||||
git fetch "$REMOTE" "$BRANCH"
|
||||
|
||||
# 2. Rebase with integrated stash management
|
||||
# --autostash: hides dirty files, rebases, then pops them back
|
||||
if git rebase --autostash "$REMOTE/$BRANCH"; then
|
||||
echo "${GREEN}Successfully updated and reapplied changes${CRESET}"
|
||||
git status --short
|
||||
else
|
||||
echo "${RED}Rebase conflict detected!${CRESET}"
|
||||
echo "1. Fix conflicts -> git rebase --continue"
|
||||
echo "2. Or abort -> git rebase --abort"
|
||||
echo " If aborting, don't forget your stashed changes in the stash list"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
get_pr() {
|
||||
PR=$1
|
||||
REMOTE=$2
|
||||
: ${REMOTE:=$DEFAULT_REMOTE}
|
||||
REMOTE="${2:-${DEFAULT_REMOTE}}"
|
||||
|
||||
if [ -z "$PR" ]; then
|
||||
echo "Please specify a PR to checkout"
|
||||
echo "${RED}Please specify a PR to checkout${CRESET}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo Checking out PR ${PR} from ${REMOTE}
|
||||
git fetch ${REMOTE} pull/${PR}/head:pr/${PR}
|
||||
git checkout pr/${PR}
|
||||
echo Checking out PR "${CYAN}${PR}${CRESET}" from "${YELLOW}${REMOTE}${CRESET}"
|
||||
git fetch "${REMOTE}" "pull/${PR}/head:pr/${PR}"
|
||||
|
||||
# Optimized count using grep -c
|
||||
WT_COUNT=$(git worktree list --porcelain | grep -c "^worktree")
|
||||
|
||||
if [ "$WT_COUNT" -gt 1 ]; then
|
||||
WT_NAME="PR-$PR"
|
||||
echo "Worktrees detected. ${CYAN}Creating ${WT_NAME}${CRESET}"
|
||||
git worktree add "PR-$PR" "pr/${PR}"
|
||||
return
|
||||
fi
|
||||
|
||||
# Fallback to normal checkout if no worktrees or user opts out
|
||||
git checkout "pr/$PR"
|
||||
}
|
||||
|
||||
del_pr() {
|
||||
BRANCH=`git rev-parse --abbrev-ref HEAD`
|
||||
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
|
||||
if [[ ! ${BRANCH} =~ ^pr\/ ]]; then
|
||||
echo "Not a PR branch, aborting!"
|
||||
echo "${RED}Not a PR branch, aborting!${CRESET}"
|
||||
exit 1
|
||||
fi
|
||||
git checkout - && git branch -D ${BRANCH}
|
||||
|
||||
if [ "$(git worktree list --porcelain | grep -c "^worktree")" -gt 1 ]; then
|
||||
WT_PATH=$(git rev-parse --show-toplevel)
|
||||
# echo "Worktrees detected. ${CYAN}Removing ${WT_PATH}${CRESET}"
|
||||
git worktree remove "$WT_PATH" --force && git branch -D "$BRANCH"
|
||||
|
||||
echo "PR branch ${CYAN}${BRANCH}${CRESET} deleted and worktree ${CYAN}$(basename "$WT_PATH")${CRESET} removed"
|
||||
echo "** ${GREEN}'cd ..' to return to the main repo${CRESET} **"
|
||||
return
|
||||
fi
|
||||
|
||||
# Fallback to normal branch deletion if no worktrees or user opts out
|
||||
git checkout - && git branch -D "${BRANCH}"
|
||||
}
|
||||
|
||||
clean_branches() {
|
||||
BRANCHES=`git branch | grep -v 'master\|develop\|\*'`
|
||||
BRANCHES=$(git branch | grep -v "${ROOT_BRANCH}\|${MAIN_BRANCH}\|${DEVELOP_BRANCH}\|\*")
|
||||
for i in ${BRANCHES}; do
|
||||
git branch -d $i
|
||||
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"
|
||||
echo "THIS NEEDS WORK - DO NOT USE YET"
|
||||
return 1
|
||||
|
||||
if [ -z "$PR" ]; then
|
||||
echo "Please specify a PR to backport"
|
||||
exit 1
|
||||
fi
|
||||
# PR=$1
|
||||
# BRANCH=$2
|
||||
# [ -z "$BRANCH" ] && BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
# REMOTE="${3:-${DEFAULT_REMOTE}}"
|
||||
|
||||
echo Backporting ${PR} to ${BRANCH} from ${REMOTE}
|
||||
git checkout ${BRANCH} && git pull ${REMOTE} ${BRANCH} && curl -L -s "$URL" | git am
|
||||
# THISBRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
# URL="https://patch-diff.githubusercontent.com/raw/elastic/kibana/pull/${PR}.patch"
|
||||
|
||||
# if the backport couldn't be cleanly applied, tell the user and exit
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "FAILED - Backport could not be cleanly applied!"
|
||||
echo "FAILED - Fix by hand or run 'git am --abort'"
|
||||
exit 2
|
||||
fi
|
||||
# if [ -z "$PR" ]; then
|
||||
# echo "Please specify a PR to backport"
|
||||
# exit 1
|
||||
# fi
|
||||
|
||||
# switch back if we didn't start in the target branch
|
||||
[ "$THISBRANCH" != "$BRANCH" ] && git checkout -
|
||||
# echo "Backporting ${CYAN}${PR}${CRESET} to ${YELLOW}${BRANCH}${CRESET} from ${YELLOW}${REMOTE}${CRESET}"
|
||||
|
||||
|
||||
# # 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"}
|
||||
REMOTE="${1:-${DEFAULT_REMOTE}}"
|
||||
BRANCH=$2
|
||||
[ -z "$BRANCH" ] && BRANCH=`git rev-parse --abbrev-ref HEAD`
|
||||
git branch --set-upstream-to=${REMOTE}/${BRANCH} ${BRANCH}
|
||||
}
|
||||
|
||||
[ -z "$BRANCH" ] && BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
||||
echo "Setting ${CYAN}${BRANCH}${CRESET} to track ${YELLOW}${REMOTE}/${BRANCH}${CRESET}"
|
||||
git branch --set-upstream-to="${REMOTE}/${BRANCH}" "${BRANCH}"
|
||||
}
|
||||
|
||||
SNAPSHOT_DIR="$HOME/.git-snapshots"
|
||||
|
||||
create_snap() {
|
||||
NAME=$1
|
||||
if [ -z "$NAME" ]; then
|
||||
echo "${RED}Error: Please specify a name for the snap.${CRESET}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
mkdir -p "$SNAPSHOT_DIR"
|
||||
FILE="$SNAPSHOT_DIR/${NAME}.patch"
|
||||
|
||||
if [ -f "$FILE" ]; then
|
||||
echo "${RED}Error: Snap '$NAME' already exists.${CRESET}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
git diff --cached > "$FILE" && git reset
|
||||
}
|
||||
|
||||
apply_snap() {
|
||||
NAME=$1
|
||||
FILE="$SNAPSHOT_DIR/${NAME}.patch"
|
||||
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "${RED}Error: Snap '$NAME' not found.${CRESET}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "Applying snap ${CYAN}${NAME}${CRESET}"
|
||||
git apply "$FILE"
|
||||
}
|
||||
|
||||
list_snaps() {
|
||||
echo "Snapshots in ${CYAN}$SNAPSHOT_DIR${CRESET}"
|
||||
find "$SNAPSHOT_DIR" -maxdepth 1 -mindepth 1 -print0 | while IFS= read -r -d '' file; do
|
||||
# $file is now safe to use, even with spaces or newlines
|
||||
echo " $(basename "$file" .patch)"
|
||||
done
|
||||
|
||||
}
|
||||
|
||||
revert_snap() {
|
||||
NAME=$1
|
||||
FILE="$SNAPSHOT_DIR/${NAME}.patch"
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "${RED}Error: Snap '$NAME' not found.${CRESET}"
|
||||
return 1
|
||||
fi
|
||||
echo "Reverting snap ${CYAN}${NAME}${CRESET}"
|
||||
git apply --reverse "$FILE"
|
||||
}
|
||||
|
||||
remove_snap() {
|
||||
NAME=$1
|
||||
FILE="$SNAPSHOT_DIR/${NAME}.patch"
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "${RED}Error: Snap '$NAME' not found.${CRESET}"
|
||||
return 1
|
||||
fi
|
||||
echo "Remove snap ${CYAN}${NAME}${CRESET} ? (y/n)"
|
||||
read -r a
|
||||
if [[ "$a" == [yY]* ]]; then
|
||||
rm "$FILE"
|
||||
else
|
||||
echo "-- Aborted"
|
||||
fi
|
||||
}
|
||||
|
||||
show_snap() {
|
||||
NAME=$1
|
||||
FILE="$SNAPSHOT_DIR/${NAME}.patch"
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "${RED}Error: Snap '$NAME' not found.${CRESET}"
|
||||
return 1
|
||||
fi
|
||||
cat "$FILE"
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ fi
|
||||
mkdir -p "${HOME}/bin" "${HOME}/.bash_aliases.d" "${HOME}/.bash_completion.d" "${HOME}/.bash_functions.d"
|
||||
|
||||
cd "${ROOT}/bash/bash_magic/bash_aliases.d";
|
||||
for i in color refresh git less brew dev sublime top; do cp "${i}.sh" "${HOME}/.bash_aliases.d"; done
|
||||
for i in color refresh git less dev sublime docker misc; do cp "${i}.sh" "${HOME}/.bash_aliases.d"; done
|
||||
cd "${ROOT}/bash/bash_magic/bash_completion.d";
|
||||
for i in etc; do cp "${i}.sh" "${HOME}/.bash_completion.d"; done
|
||||
cd "${ROOT}/bash/bash_magic/bash_functions.d";
|
||||
|
||||
Reference in New Issue
Block a user