Skip to content

Commit

Permalink
More git fixes and aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
n1amr committed Oct 27, 2024
1 parent e9b7786 commit 25dfd84
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 89 deletions.
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"
5 changes: 3 additions & 2 deletions bin/git-review
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ if [[ -z "$pr_branch" ]]; then
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"

git_dir="$repo_dir/.git"
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
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
120 changes: 60 additions & 60 deletions config/gitconfig
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
[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)\""
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"
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)\""
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 main-worktree-git-dir)\""
worktree-main-git-dir = rev-parse --path-format=absolute --git-common-dir

# Aliases
a = add
ap = add --patch
br = branch
c = commit
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
wt = worktree

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
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
dfa = diff-upstream
f = fetch -v --all
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
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)\""
rebase-upstream = "!git rebase -i '@{upstream}'"
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
rbon = rebase-on
rbu = rebase-upstream

# 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"
rl = log --walk-reflogs --pretty=reflog-short
rll = log --walk-reflogs --pretty=reflog-long
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

0 comments on commit 25dfd84

Please sign in to comment.