-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.bash_git_specific
147 lines (128 loc) · 4.27 KB
/
.bash_git_specific
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
[ -f "$HOME/src/setup/completions/git.bash" ] && source "$HOME/src/setup/completions/git.bash"
GITHUB_SSH_KEY="$HOME/.ssh/github"
DEFAULT_GIT_AUTHOR_NAME="Carlos Nunez"
DEFAULT_GIT_AUTHOR_EMAIL="[email protected]"
alias a="git add . -N"
alias c="git cs"
alias ce="git cs --allow-empty"
alias cm="git cs -m"
alias commit="git -c commit.gpgsign=false commit"
alias commitm="git -c commit.gpgsign=false commit -m"
alias p="git add -p"
alias st="git status"
alias r="squash_outstanding_commits"
alias f="git fs"
alias sq="git -c sequence.editor=: rebase --autosquash --interactive"
alias squash="git -c sequence.editor=: -c commit.gpgsign=false rebase --autosquash --interactive"
alias push="sq && git push"
alias pushnosign="squash && git push"
alias pull="git pull --rebase"
git_config_hook() {
if ! test -e "$PWD/.gitconfig"
then
test -h "$PWD/.git/config" || return 0
log_info "[git-config-hook] Project-level gitconfig removed; restoring original"
mv "$PWD/.git/config.original" "$PWD/.git/config"
return 0
fi
test "$PWD" == "$HOME" && return 0
if ! is_current_directory_tracked_by_git
then
log_warning ".gitconfig file found, but this repo isn't being tracked by Git?"
return 0
fi
test "$(readlink "$PWD/.git/config")" == "$PWD/.gitconfig" && return 0
log_info "[git-config-hook] Linking Git config to project .gitconfig; hang on"
(
set -eo pipefail
trap 'rc=$?; set +eo pipefail; exit $rc' INT HUP
cat "$PWD/.git/config" >> "$PWD/.gitconfig"
mv "$PWD/.git/config" "$PWD/.git/config.original"
ln -s "$PWD/.gitconfig" "$PWD/.git/config"
log_info "[git-config-hook] Done. Original config located here: $PWD/.git/config.original"
)
return "$?"
}
branch() {
git branch --show-current
}
dont_use_master_for_default_branches() {
git config --global init.defaultBranch main
}
view_unpushed_commits_across_branches() {
# Thanks, StackOverflow!
# https://stackoverflow.com/a/20499690/314212
git for-each-ref --format="%(refname:short) %(upstream:track)" refs/heads
}
update_origin_url() {
if grep -q 'url = http.*github.com' .git/config
then
# match repos cloned by http
gsed -i 's/url = http.*github.com\//url = [email protected]:/' .git/config
elif grep -q 'url = [email protected]' .git/config
then
# match repos cloned by ssh
gsed -i 's/[email protected]/[email protected]/' .git/config
else
log_error "Unsupported clone scheme detected."
fi
}
create_new_git_branch() {
branch_name="${1?Please provide a Git branch to create.}"
upstream="${2:-origin}"
git checkout -b "$branch_name" && git push -u "$upstream" "$branch_name"
}
refresh_github_ssh_key() {
if [ "$(ssh-add -l | grep '.github')" == "" ]
then
eval $(ssh-agent -s) > /dev/null
ssh-add "$GITHUB_SSH_KEY" > /dev/null
fi
}
is_current_directory_tracked_by_git() {
[ "$(git rev-parse --git-dir 2>/dev/null)" != "" ]
}
get_current_branch_name() {
git rev-parse --abbrev-ref HEAD 2>/dev/null
}
get_number_of_commits_ahead_of_origin() {
current_branch="$(get_current_branch_name)"
_get_number_of_commits_between_refs "origin/$current_branch..$current_branch"
}
squash_commits_starting_from_root() {
git rebase -i --root
}
squash_outstanding_commits() {
_squash_commits "$(get_number_of_commits_ahead_of_origin)"
}
alias reorder_commits='squash_outstanding_commits'
squash_these_many_commits() {
number_of_commits_to_squash="$1"
if [ "$number_of_commits_to_squash" != "" ] &&
[ "$number_of_commits_to_squash" -gt 0 ]
then
_squash_commits "$number_of_commits_to_squash"
log_info "You might need to force-push this changeset into your remote. \
To do so, use \`git push -f\`. Note that not every repository will allow you \
to do this."
else
log_error "Please enter the number of commits to squash."
return 1
fi
}
_squash_commits() {
number_of_commits_to_squash="$1"
if is_current_directory_tracked_by_git &&
[ "$number_of_commits_to_squash" != "" ]
then
git rebase --allow-empty -i "HEAD~$number_of_commits_to_squash"
fi
}
_get_number_of_commits_between_refs() {
git log --pretty=oneline "$1" 2>/dev/null | wc -l | tr -d ' '
}
remove_orig_files() {
git status --porcelain | grep '.orig' | cut -f2 -d ' ' | xargs rm
}
dont_use_master_for_default_branches