This repository has been archived by the owner on May 14, 2023. It is now read-only.
/
.bash_functions
99 lines (81 loc) · 2.54 KB
/
.bash_functions
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
# vim: set ft=sh:
# # put below lines to '.bashrc'
# if [ -f ~/.bash_functions ]; then
# . ~/.bash_functions
# fi
pecorake() {
local rake="bundle exec rake"
local task=$($rake --tasks | peco --prompt="task>" | cut -d " " -f 2)
[ ! -z $task ] && $rake $task
}
alias prake='pecorake'
follow_upstream() {
git fetch --prune upstream && \
git checkout master && \
git merge --ff-only upstream/master && \
git push origin master
}
follow_upstream_staging() {
git fetch --prune upstream && \
git checkout staging && \
git merge --ff-only upstream/staging && \
git push origin staging
}
_peco_checkout_branch() {
branches=$(git for-each-ref --format="%(refname:short)" --sort=-committerdate refs/heads)
branch=$(echo "$branches" | peco --prompt="Branch>")
git checkout $branch
git branch --verbose
}
alias co='_peco_checkout_branch'
_peco_checkout_branch_and_bundle_install() {
_peco_checkout_branch
echo
echo '[bundle install]'
echo
bundle install
}
alias cob='_peco_checkout_branch_and_bundle_install'
_peco_change_directory_ghq() {
dir=$(ghq list -p | peco --prompt="Repository>")
test "$dir" && cd $dir
}
alias gd='_peco_change_directory_ghq'
# Edit last committed files
function _last_committed_files() {
local ref=${@:-HEAD}
local base=$(git rev-parse --show-toplevel 2> /dev/null)
local last_committed_files=$(git diff-tree --no-commit-id --name-only -r $ref 2> /dev/null)
for file in $last_committed_files; do
echo $base/$file
done
}
function _edit_last_committed_files() {
local files=$(_last_committed_files $@)
test "$files" && vim -p $files
}
alias el='_edit_last_committed_files'
function _edit_last_committed_files_by_vscode() {
local files=$(_last_committed_files $@)
test "$files" && code -p $files
}
alias elc='_edit_last_committed_files_by_vscode'
# Stage unstaged files using peco
function _peco_select_unstaged_files() {
local base=$(git rev-parse --show-toplevel 2> /dev/null)
local unstaged_files=$(git status --porcelain | grep -v "??" | peco | awk -F ' ' '{print $NF}')
local files=$(for file in $unstaged_files; do echo $base/$file; done)
test "$files" && git add $files
}
alias pdd='_peco_select_unstaged_files'
function _rename_current_branch() {
local tmpfile=$(mktemp 2> /dev/null || mktemp -t tmp)
trap "rm -f $tmpfile" EXIT
local branch=$(git rev-parse --abbrev-ref HEAD)
echo "$branch" > $tmpfile
EDITOR="vim"
$EDITOR $tmpfile
git branch -m $branch $(cat $tmpfile)
}
alias rename_current_branch='_rename_current_branch'
alias mws='cd ~/src/github.com/meganemura/'