From f93819532de1f3bac36b2dd3c31674cec27b9749 Mon Sep 17 00:00:00 2001 From: Amr Alaa Date: Fri, 25 Oct 2024 22:04:57 +0300 Subject: [PATCH] git aliases --- bin/git-review | 31 +++++++++++++++++++++++++------ config/gitconfig | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/bin/git-review b/bin/git-review index 9edad234..4de7fb30 100755 --- a/bin/git-review +++ b/bin/git-review @@ -7,6 +7,7 @@ target_branch='' remote_name='origin' from_checkpoint='' save_checkpoint='false' +reset_checkpoint='false' fetch='true' local_branch='false' @@ -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' ;; @@ -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 @@ -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)" @@ -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" @@ -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" @@ -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" diff --git a/config/gitconfig b/config/gitconfig index 16ce2b62..4cbb6af4 100644 --- a/config/gitconfig +++ b/config/gitconfig @@ -7,6 +7,7 @@ df = diff pick = cherry-pick st = status + wt = worktree cor = "!git checkout-remote" cr = "!git review" @@ -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 @@ -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