Skip to content

Commit

Permalink
git aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
n1amr committed Oct 25, 2024
1 parent 91ab3ca commit f938195
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
31 changes: 25 additions & 6 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,12 +58,18 @@ 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)"
Expand All @@ -70,10 +78,21 @@ if [[ -z "$repo_dir" ]]; then
fi
debug repo_dir="$repo_dir"

checkpoint_file="$repo_dir/.git/review/checkpoints/${pr_branch//\//%}"
git_dir="$repo_dir/.git"
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 +101,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 +132,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
18 changes: 18 additions & 0 deletions config/gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
df = diff
pick = cherry-pick
st = status
wt = worktree

cor = "!git checkout-remote"
cr = "!git review"
Expand All @@ -19,7 +20,10 @@
rrb = "!git redo-rebase-on"

# Simple
amend = commit --am --no-edit
current-branch = rev-parse --abbrev-ref HEAD
default-branch = config --get user.default-branch
set-default-branch = config user.default-branch
f = fetch -v --all
l = log --graph --pretty=longline
ll = log --graph
Expand All @@ -28,17 +32,31 @@
mtv = mergetool --tool customVimMergeTool
prev = checkout HEAD~
rb = rebase -i
rba = rebase --abort
rbc = rebase --continue
rl = log --walk-reflogs --pretty=reflog-short
rll = log --walk-reflogs --pretty=reflog-long
root-dir = rev-parse --show-cdup

# External scripts
commit-timestamp = "!git commit -m \"$(date -u -Iseconds)\""
commit-timestamp-all = "!git commit -am \"$(date -u -Iseconds)\""
copy-merge-base = "!git merge-base \"origin/$(git default-branch)\" HEAD | clipboard"
create-github = "!git-create-github"
create-gitlab = "!git-create-gitlab"
rebase-default = "!git rebase -i \"$(git merge-base \"$(git default-branch)\" HEAD)\""
rebase-default-latest = "!git rebase -i \"$(git merge-base \"origin/$(git default-branch)\" HEAD)\""
review = "!git-review"
review-save = "!git review --save"
review-checkpoint = "!git-review-checkpoint"

# Aliases
ct = commit-timestamp
cta = commit-timestamp-all
cmb = copy-merge-base
rbd = rebase-default
rbdl = rebase-default-latest

# 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
Expand Down

0 comments on commit f938195

Please sign in to comment.