Skip to content

Commit

Permalink
git aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
n1amr committed Oct 31, 2024
1 parent 8dc7630 commit 74d7efa
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 80 deletions.
9 changes: 9 additions & 0 deletions bin/completion/gwtcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

results=()

while IFS= read dir; do
results+=("$dir")
done < <( git worktree list --porcelain | grep '^worktree' | sed 's/^worktree //' )

printf '%s\n' "${results[@]}"
19 changes: 10 additions & 9 deletions bin/git-browse
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ remote_name=origin
while [ $# != 0 ]; do
case $1 in
-r)
[[ $# -lt 2 ]] && echo "Error: No remote name" && exit 1
remote_name="$2"
shift
[[ $# -lt 2 ]] && echo "Error: No remote name" && exit 1
remote_name="$2"
shift
;;

*)
if [[ -n "$action" ]]; then
echo "Only one action is required."
exit 1
fi
action="$1"
if [[ -n "$action" ]]; then
echo "Only one action is required."
exit 1
fi
action="$1"
;;
esac
shift
Expand Down Expand Up @@ -45,6 +45,7 @@ else
echo "ERROR: Remote $remote_name is invalid"
exit 1
fi

url="${git_url}"

git_branch="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)" || git_branch=''
Expand Down Expand Up @@ -116,5 +117,5 @@ case "$action" in
;;
esac

echo "Opening $url"
echo "Opening URL: $url"
open-url "$url"
34 changes: 27 additions & 7 deletions bin/git-review
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target_branch=''
remote_name='origin'
from_checkpoint=''
save_checkpoint='false'
reset_checkpoint='false'
fetch='true'
local_branch='false'

Expand All @@ -18,6 +19,7 @@ while [ $# != 0 ]; do
--remote-name|-r) remote_name="$2"; shift ;;

--save-checkpoint|--save|-s) save_checkpoint='true' ;;
--reset-checkpoint|--reset) reset_checkpoint='true' ;;
--no-fetch) fetch='false' ;;
--local) local_branch='true' ;;

Expand All @@ -44,7 +46,7 @@ debug() {
}

if [[ -z "$target_branch" ]]; then
target_branch="$(git config --get user.defaultReviewBranch)"
target_branch="$(git config --get user.default-branch)"
fi

if [[ -z "$target_branch" ]]; then
Expand All @@ -56,24 +58,42 @@ debug target_branch="$target_branch"
debug remote_name="$remote_name"
debug from_checkpoint="$from_checkpoint"
debug save_checkpoint="$save_checkpoint"
debug reset_checkpoint="$reset_checkpoint"
debug fetch="$fetch"
debug local_branch="$local_branch"

if [[ -z "$pr_branch" ]]; then
echo "Specify branch to review" >&2
return 1
pr_branch="$(git current-branch | sed 's|^review/||')"
if [[ -z "$pr_branch" ]]; then
echo "Specify branch to review" >&2
exit 1
else
echo "No PR branch is specified. Using default branch: $pr_branch"
fi
fi

repo_dir="$(git root-dir)"
repo_dir="$(git repo-root)"
if [[ -z "$repo_dir" ]]; then
repo_dir='.'
fi
debug repo_dir="$repo_dir"

checkpoint_file="$repo_dir/.git/review/checkpoints/${pr_branch//\//%}"
git_dir="$(git rev-parse --path-format=absolute --git-common-dir)"

if [[ -f "$git_dir" ]]; then
git_dir="$(cat "$git_dir" | grep 'gitdir:' | sed 's/^gitdir: //')"
fi

checkpoint_file="$git_dir/review/checkpoints/${pr_branch//\//%}"
mkdir -p "$(dirname "$checkpoint_file")"
debug checkpoint_file="$checkpoint_file"

if [[ "$reset_checkpoint" == 'true' ]]; then
rm -f "$checkpoint_file"

exit 0
fi

if [[ -z "$from_checkpoint" ]] && [[ -f "$checkpoint_file" ]]; then
from_checkpoint="$(cat "$checkpoint_file" | tail -n 1)"
debug from_checkpoint="$from_checkpoint"
Expand All @@ -82,7 +102,7 @@ fi
if [[ "$save_checkpoint" == 'true' ]]; then
git status

git commit -m "Reviewed (from checkpoint $from_checkpoint)"
git commit -m "Reviewed (from checkpoint $from_checkpoint)" || true

new_checkpoint="$(git rev-parse --verify HEAD)"
echo "$new_checkpoint" >> "$checkpoint_file"
Expand Down Expand Up @@ -113,7 +133,7 @@ if [[ -n "$from_checkpoint" ]] && [[ "$from_checkpoint" != 'null' ]]; then
git restore --no-overlay --source "$from_checkpoint" -- "$repo_dir"
git add "$repo_dir"
git status
git commit -am "Reviewed (from checkpoint $from_checkpoint)"
git commit -am "Reviewed (from checkpoint $from_checkpoint)" || true
fi

# git restore --overlay --source "$pr_branch_ref" -- "$repo_dir"
Expand Down
16 changes: 0 additions & 16 deletions bin/git-review-checkpoint

This file was deleted.

13 changes: 11 additions & 2 deletions bin/open-url
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ while [ $# -gt 0 ]; do
shift
done

open () { xdg-open "$1" & }
open_with_redirect () { open "$@" > /dev/null 2>&1 & }
_open () {
if type open > /dev/null 2>&1; then
open "$1" &
else
xdg-open "$1" &
fi
}

open_with_redirect () {
_open "$@" # > /dev/null 2>&1 &
}

for url in "$@"; do
open_with_redirect "$url"
Expand Down
119 changes: 73 additions & 46 deletions config/gitconfig
Original file line number Diff line number Diff line change
@@ -1,62 +1,89 @@
[alias]
# Short names
# author-date-old = "!f() { git filter-branch -f --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE' HEAD~\"${1:-1}\"..HEAD; }; f" # git versoin < 2.22.0
amend = commit --am --no-edit
author-date = "!f() { git filter-repo --force --commit-callback 'commit.committer_date = commit.author_date' --refs HEAD~\"${1:-1}\"..HEAD; }; f"
backup = "!f() { local branch_name=\"${1:-$(git rev-parse --abbrev-ref HEAD)}\"; local tag_name=\"BACKUP/${branch_name}-$(date +\"%Y-%m-%d_%H-%M-%S\")\"; git tag \"$tag_name\" \"${1:-HEAD}\" && echo \"$tag_name\"; }; f"
checkout-remote = "!f() { remote_name=\"$1\"; git stash-all && git is-clean && git fetch -v origin \"$1\" && git checkout -f -B \"$1\" \"origin/$1\"; }; f"
cleanup = "!f() { git fsck && git prune && git reflog expire --expire=now --all && git repack -ad && git prune; }; f"
code = "!f() { cd \"$(git repo-root)\"; code \"$@\" .; }; f"
commit-timestamp = "!git commit -m \"$(date -u -Iseconds)\""
commit-timestamp-all = "!git commit -am \"$(date -u -Iseconds)\""
config-edit = config --edit
config-path = "!echo \"$(git worktree-main-git-dir)/config\""
copy-commit-hash = "!git rev-parse HEAD | clipboard"
copy-current-branch = "!git current-branch | clipboard"
copy-merge-base = "!git merge-base \"origin/$(git default-branch)\" HEAD | clipboard"
create-github = "!git-create-github"
create-gitlab = "!git-create-gitlab"
create-stash = "!f() { git is-clean && echo \"Nothing to stash\" && return 0; ! git is-index-empty && echo >&2 \"FAILED: Index is not empty\"; return 1; git add -A && git stash push --keep-index && git reset; }; f"
current-branch = rev-parse --abbrev-ref HEAD
default-branch = config --get user.default-branch
diff-upstream = diff '@{upstream}'
init-root = "!f() { git init && git commit --allow-empty -m \"Root Commit\"; }; f"
is-clean = "!f() { test -z \"$(git status --short )\"; }; f"
is-index-empty = "!f() { test -z \"$(git diff-index --cached --name-only HEAD)\"; }; f"
push-pr = "!f() { git push origin \"HEAD~${1:-0}:refs/heads/$(git current-branch | sed 's/-dev$//')\" -f; }; f"
rebase-default = "!git rebase -i \"$(git merge-base \"$(git default-branch)\" HEAD)\""
rebase-default-latest = "!git rebase -i \"origin/$(git default-branch)\""
rebase-on = "!f() { local remote_branch_name=\"${1:-$(git rev-parse --abbrev-ref HEAD)}\"; git rebase -i --autosquash --onto \"$remote_branch_name\" \"$(git merge-base \"$remote_branch_name\" HEAD)\" \"$(git rev-parse --abbrev-ref HEAD)\"; }; f"
rebase-upstream = rebase -i '@{upstream}'
recommit = "!f() { echo \"$(git l -1)\" && old_msg=\"$(git log -1 --format='%s')\" && git reset --soft HEAD~ && git commit -m \"$old_msg\"; }; f"
redo-rebase-on = "!f() { local remote_branch_name=\"${1:-$(git rev-parse --abbrev-ref HEAD)}\"; git rebase -i --autosquash \"$(git merge-base \"$remote_branch_name\" HEAD)\"; }; f"
repo-root = rev-parse --path-format=absolute --show-toplevel
repo-root-relative = rev-parse --show-cdup
review = "!git-review"
review-save = review --save
set-default-branch = config user.default-branch
stash-all = "!f() { ! git is-index-empty && echo >&2 \"FAILED: Index is not empty\" && return 1; git stash -u; }; f"
vim = "!f() { cd \"$(git repo-root)\"; vim \"$@\" .; }; f"
worktree-main = "!git worktree-main-git-dir | sed 's/\\/*.git$//'"
worktree-main-exec = "!git -C \"$(git worktree-main-git-dir)\""
worktree-main-git-dir = rev-parse --path-format=absolute --git-common-dir

# Aliases
a = add
ap = add --patch
br = branch
c = commit
ccb = copy-current-branch
cch = copy-commit-hash
cmb = copy-merge-base
co = checkout
cor = checkout-remote
cr = review
crc = review-checkpoint
cs = create-stash
ct = commit-timestamp
cta = commit-timestamp-all
df = diff
pick = cherry-pick
st = status

cor = "!git checkout-remote"
cr = "!git review"
crc = "!git review-checkpoint"
cs = "!git create-stash"
go = "!f() { git-browse \"$@\"; }; f"
next = "!f() { branch=\"${1:-master}\"; git checkout \"$(git log --reverse --ancestry-path --oneline HEAD..\"${branch}\" | head -1 | awk '{print $1}')\"; }; f"
rbb = "!f() { count=\"$1\"; shift; git rebase -i \"$@\" \"HEAD~${count}\"; }; f"
rbon = "!git rebase-on"
rrb = "!git redo-rebase-on"

# Simple
current-branch = rev-parse --abbrev-ref HEAD
f = fetch -v --all
dfa = diff-upstream
f = fetch --all --prune
go = "!git-browse"
l = log --graph --pretty=longline
ll = log --graph
lp = log --graph --patch
mt = mergetool --tool customVimMergeToolMin
mtv = mergetool --tool customVimMergeTool
next = "!f() { branch=\"${1:-master}\"; git checkout \"$(git log --reverse --ancestry-path --oneline HEAD..\"${branch}\" | head -1 | awk '{print $1}')\"; }; f"
pick = cherry-pick
prev = checkout HEAD~
rb = rebase -i
rba = rebase --abort
rbb = "!f() { count=\"$1\"; shift; git rebase -i \"$@\" \"HEAD~${count}\"; }; f"
rbc = rebase --continue
rbd = rebase-default
rbdl = rebase-default-latest
rbon = rebase-on
rbu = rebase-upstream
rl = log --walk-reflogs --pretty=reflog-short
rll = log --walk-reflogs --pretty=reflog-long
root-dir = rev-parse --show-cdup

# External scripts
create-github = "!git-create-github"
create-gitlab = "!git-create-gitlab"
review = "!git-review"
review-checkpoint = "!git-review-checkpoint"

# Functions
author-date = "!f() { git filter-repo --force --commit-callback 'commit.committer_date = commit.author_date' --refs HEAD~\"${1:-1}\"..HEAD; }; f"
author-date-old = "!f() { git filter-branch -f --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE' HEAD~\"${1:-1}\"..HEAD; }; f" # git versoin < 2.22.0
backup = "!f() { local branch_name=\"${1:-$(git rev-parse --abbrev-ref HEAD)}\"; local tag_name=\"BACKUP/${branch_name}-$(date +\"%Y-%m-%d_%H-%M-%S\")\"; git tag \"$tag_name\" \"${1:-HEAD}\" && echo \"$tag_name\"; }; f"
stash-all = "!f() { ! git is-index-empty && echo >&2 \"FAILED: Index is not empty\" && return 1; git stash -u; }; f"
checkout-remote = "!f() { remote_name=\"$1\"; git stash-all && git is-clean && git fetch -v origin \"$1\" && git checkout -f -B \"$1\" \"origin/$1\"; }; f"
cleanup = "!f() { git fsck && git prune && git reflog expire --expire=now --all && git repack -ad && git prune; }; f"
create-stash = "!f() { git is-clean && echo \"Nothing to stash\" && return 0; ! git is-index-empty && echo >&2 \"FAILED: Index is not empty\"; return 1; git add -A && git stash push --keep-index && git reset; }; f"
init-root = "!f() { git init && git commit --allow-empty -m \"Root Commit\"; }; f"
is-clean = "!f() { test -z \"$(git status --short )\"; }; f"
is-index-empty = "!f() { test -z \"$(git diff-index --cached --name-only HEAD)\"; }; f"
rebase-on = "!f() { local remote_branch_name=\"${1:-$(git rev-parse --abbrev-ref HEAD)}\"; git rebase -i --autosquash --onto \"$remote_branch_name\" \"$(git merge-base \"$remote_branch_name\" HEAD)\" \"$(git rev-parse --abbrev-ref HEAD)\"; }; f"
recommit = "!f() { echo \"$(git l -1)\" && old_msg=\"$(git log -1 --format='%s')\" && git reset --soft HEAD~ && git commit -m \"$old_msg\"; }; f"
redo-rebase-on = "!f() { local remote_branch_name=\"${1:-$(git rev-parse --abbrev-ref HEAD)}\"; git rebase -i --autosquash \"$(git merge-base \"$remote_branch_name\" HEAD)\"; }; f"

# TODO: Remove
old-review = "!f() { set -x; branch=\"$1\" && test -n \"$branch\" || { echo \"Specify branch to review\" >&2; return 1; } && last_checkpoint=\"$2\" && git stash-all && git is-clean && git fetch -v origin \"$branch\" && merge_base=\"$(git merge-base \"origin/$branch\" \"origin/${GIT_TARGET_BRANCH:-master}\")\" && git checkout -B review \"$merge_base\" && { test -z \"$last_checkpoint\" || git checkout -f \"$last_checkpoint\" -- .; } && git restore --source \"origin/$branch\" -- .; }; f"
old-review-checkpoint = "!f() { set -x; git commit -m \"reviewed: $(date -u -Iseconds)\" && git rev-parse --verify HEAD; }; f"
rrb = redo-rebase-on
st = status
wt = worktree
wta = worktree add
wtl = worktree list
wtmv = worktree move
wtrm = worktree remove

[core]
editor = vim
Expand Down Expand Up @@ -171,7 +198,7 @@ Committer Date: %ci %C(green)(%cr)%C(reset)%n\

[credential "azrepos:org/msasg"]
username = [email protected]
azureAuthority = https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47
azureAuthority = https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47
[credential "azrepos:org/msblox"]
azureAuthority = https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47
username = [email protected]
azureAuthority = https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47
username = [email protected]
4 changes: 4 additions & 0 deletions config/shellrc
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ findword () {
grep -P "$1" /usr/share/dict/words
}

gwtcd () {
cd "$@"
}

init_fzf () {
export PATH="$DOTFILES_HOME/thirdparty/fzf/bin:$PATH"
[[ $- == *i* ]] && source "$DOTFILES_HOME/thirdparty/fzf/shell/completion.$SHELLNAME" 2> /dev/null
Expand Down

0 comments on commit 74d7efa

Please sign in to comment.