Skip to content

Commit 3ad32c3

Browse files
Add home/.git_aliases & move git aliases from .bash_aliases to there
1 parent 3877da9 commit 3ad32c3

2 files changed

Lines changed: 87 additions & 77 deletions

File tree

home/.bash_aliases

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,10 @@ gs_git_show_branch() {
5757
PS1="\e[7m\$(gs_git_show_branch)\e[m\n$PS1" # comment out to NOT show git branch!
5858
PS1='\$SHLVL'":$SHLVL $PS1" # comment out to NOT show shell level!
5959

60-
# See which files have changed since some prior commit named `MY_FIRST_COMMIT`.
61-
# Usage:
62-
# gs_git_files_changed MY_FIRST_COMMIT~
63-
# OR (same thing):
64-
# gs_git_files_changed BASE_COMMIT
65-
# Known limitations: works only on filenames which have no spaces or special bash chars. To make
66-
# it handle these chars, it will require using `git diff --name-only -z`, with some more
67-
# fancy bash trickery. See my ans:
68-
# https://stackoverflow.com/questions/28109520/how-to-cope-with-spaces-in-file-names-when-iterating-results-from-git-diff-nam/62853776#62853776
69-
gs_git_list_files_changed() {
70-
files="$(git diff --name-only "$1")"
71-
echo "These are the changed files:"
72-
echo "$files"
73-
# Now optionally create a new function from this and do something with these files here if you
74-
# want!
75-
}
60+
# Import this ".git_aliases" file, if it exists.
61+
if [ -f "$SCRIPT_DIRECTORY/.git_aliases" ]; then
62+
. "$SCRIPT_DIRECTORY/.git_aliases"
63+
fi
7664

7765
# Find a file built by Bazel and therefor sitting in the "build/bin" dir.
7866
alias gs_find_bazel_build_file='find -L build/bin | grep'
@@ -83,67 +71,6 @@ alias gs_find_bazel_build_file_i='find -L build/bin | grep -i'
8371
# See: https://askubuntu.com/questions/1792/how-can-i-suspend-hibernate-from-command-line/1795#1795
8472
alias gs_suspend='systemctl suspend'
8573

86-
############ TODO: fix this up!
87-
# 1. make it stand-alone
88-
# 2. make it work as `git branch_hash_bak [optional message]`
89-
# - let the optional message just be the remainder of the arguments, so it doesn't require a quote
90-
# - however, force it to not contain spaces, so replace spaces with underscores
91-
# - add the optional message into the filename itself at the end
92-
############
93-
# GS: git branch backups: useful to back up git branch hashes before deleting branches, so you can
94-
# always have their hashes to go back to to checkout rather than having to dig through your `git
95-
# reflog` forever.
96-
# - Note that this currently requires that the GIT_BRANCH_HASH_BAK_DIR directory already exists.
97-
# - TODO: fail more gracefully: make it check to see if this dir exists & prompt the user for
98-
# permission to auto-create it with `mkdir -p ${GIT_BRANCH_HASH_BAK_DIR}` if it does not.
99-
#
100-
# Syntax: `gs_git_branch_hash_bak [dir]` = back up to a backup file in directory "dir" if a dir is
101-
# passed in.
102-
GIT_BRANCH_HASH_BAK_DEFAULT_DIR="./git_branch_hash_backups"
103-
gs_git_branch_hash_bak () {
104-
CMD="gs_git_branch_hash_bak"
105-
GIT_BRANCH_HASH_BAK_DIR="$GIT_BRANCH_HASH_BAK_DEFAULT_DIR"
106-
EXIT_SUCCESS=0
107-
EXIT_ERROR=1
108-
109-
# Help menu
110-
if [ "$1" == "-h" ] || [ "$1" == "-?" ]; then
111-
echo "This is a bash function in \"~/.bash_aliases\" which backs up git branch"
112-
echo "names & short hashes to your local \"${GIT_BRANCH_HASH_BAK_DEFAULT_DIR}\" (or other"
113-
echo "specified) dir."
114-
echo ""
115-
echo "Usage: $CMD [dir]"
116-
echo " Back up branch names and hashes to a backup file in directory \"dir\"."
117-
return $EXIT_SUCCESS
118-
fi
119-
120-
if [ -n "$1" ]; then
121-
# If an arg is passed in, then use it instead of the default directory!
122-
GIT_BRANCH_HASH_BAK_DIR="$1"
123-
fi
124-
125-
DATE=`date +%Y%m%d-%H%Mhrs-%Ssec`
126-
BRANCH="$(gs_git_show_branch)"
127-
DIR=$(pwd)
128-
REPO=$(basename "$DIR") # repository name
129-
# Replace any spaces in the repository name with underscores
130-
# See: https://stackoverflow.com/questions/19661267/replace-spaces-with-underscores-via-bash/19661428#19661428
131-
REPO="${REPO// /_}"
132-
FILE="${GIT_BRANCH_HASH_BAK_DIR}/${REPO}_git_branch_bak--${DATE}.txt"
133-
134-
echo "Backing up 'git branch -vv' info to \"$FILE\"."
135-
echo -e "date = \"$DATE\"" > $FILE
136-
echo -e "repo (folder) name = \"$REPO\"" >> $FILE
137-
echo -e "pwd = \"$DIR\"" >> $FILE
138-
echo -e "current branch name = \"$BRANCH\"" >> $FILE
139-
echo -e "\n=== \`git branch -vv\` ===\n" >> $FILE
140-
git branch -vv >> $FILE
141-
echo "Done!"
142-
}
143-
# Alias to do the git hash backups in a directory one higher so you don't have to add this backup
144-
# dir to this git project's .gitignore file
145-
alias gs_git_branch_hash_bak_up1="gs_git_branch_hash_bak \"../git_branch_hash_backups\""
146-
14774
# ssh into another computer. Override this ssh command by re-defining it in
14875
# "~/.bash_aliases_private"! Make the username, domain_name, & options what they should be for you.
14976
# Notes:

home/.git_aliases

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
2+
3+
# These are bash "git aliases", meaning that they are bash aliases that are intended to be related
4+
# to or used by `git`. This file is sourced by the ".bash_aliases" file.
5+
6+
# See which files have changed since some prior commit named `MY_FIRST_COMMIT`.
7+
# Usage:
8+
# gs_git_files_changed MY_FIRST_COMMIT~
9+
# OR (same thing):
10+
# gs_git_files_changed BASE_COMMIT
11+
# Known limitations: works only on filenames which have no spaces or special bash chars. To make
12+
# it handle these chars, it will require using `git diff --name-only -z`, with some more
13+
# fancy bash trickery. See my ans:
14+
# https://stackoverflow.com/questions/28109520/how-to-cope-with-spaces-in-file-names-when-iterating-results-from-git-diff-nam/62853776#62853776
15+
gs_git_list_files_changed() {
16+
files="$(git diff --name-only "$1")"
17+
echo "These are the changed files:"
18+
echo "$files"
19+
# Now optionally create a new function from this and do something with these files here if you
20+
# want!
21+
}
22+
23+
############ TODO: fix this up!
24+
# 1. make it stand-alone
25+
# 2. make it work as `git branch_hash_bak [optional message]`
26+
# - let the optional message just be the remainder of the arguments, so it doesn't require a quote
27+
# - however, force it to not contain spaces, so replace spaces with underscores
28+
# - add the optional message into the filename itself at the end
29+
############
30+
# GS: git branch backups: useful to back up git branch hashes before deleting branches, so you can
31+
# always have their hashes to go back to to checkout rather than having to dig through your `git
32+
# reflog` forever.
33+
# - Note that this currently requires that the GIT_BRANCH_HASH_BAK_DIR directory already exists.
34+
# - TODO: fail more gracefully: make it check to see if this dir exists & prompt the user for
35+
# permission to auto-create it with `mkdir -p ${GIT_BRANCH_HASH_BAK_DIR}` if it does not.
36+
#
37+
# Syntax: `gs_git_branch_hash_bak [dir]` = back up to a backup file in directory "dir" if a dir is
38+
# passed in.
39+
GIT_BRANCH_HASH_BAK_DEFAULT_DIR="./git_branch_hash_backups"
40+
gs_git_branch_hash_bak () {
41+
CMD="gs_git_branch_hash_bak"
42+
GIT_BRANCH_HASH_BAK_DIR="$GIT_BRANCH_HASH_BAK_DEFAULT_DIR"
43+
EXIT_SUCCESS=0
44+
EXIT_ERROR=1
45+
46+
# Help menu
47+
if [ "$1" == "-h" ] || [ "$1" == "-?" ]; then
48+
echo "This is a bash function in \"~/.bash_aliases\" which backs up git branch"
49+
echo "names & short hashes to your local \"${GIT_BRANCH_HASH_BAK_DEFAULT_DIR}\" (or other"
50+
echo "specified) dir."
51+
echo ""
52+
echo "Usage: $CMD [dir]"
53+
echo " Back up branch names and hashes to a backup file in directory \"dir\"."
54+
return $EXIT_SUCCESS
55+
fi
56+
57+
if [ -n "$1" ]; then
58+
# If an arg is passed in, then use it instead of the default directory!
59+
GIT_BRANCH_HASH_BAK_DIR="$1"
60+
fi
61+
62+
DATE=`date +%Y%m%d-%H%Mhrs-%Ssec`
63+
BRANCH="$(gs_git_show_branch)"
64+
DIR=$(pwd)
65+
REPO=$(basename "$DIR") # repository name
66+
# Replace any spaces in the repository name with underscores
67+
# See: https://stackoverflow.com/questions/19661267/replace-spaces-with-underscores-via-bash/19661428#19661428
68+
REPO="${REPO// /_}"
69+
FILE="${GIT_BRANCH_HASH_BAK_DIR}/${REPO}_git_branch_bak--${DATE}.txt"
70+
71+
echo "Backing up 'git branch -vv' info to \"$FILE\"."
72+
echo -e "date = \"$DATE\"" > $FILE
73+
echo -e "repo (folder) name = \"$REPO\"" >> $FILE
74+
echo -e "pwd = \"$DIR\"" >> $FILE
75+
echo -e "current branch name = \"$BRANCH\"" >> $FILE
76+
echo -e "\n=== \`git branch -vv\` ===\n" >> $FILE
77+
git branch -vv >> $FILE
78+
echo "Done!"
79+
}
80+
# Alias to do the git hash backups in a directory one higher so you don't have to add this backup
81+
# dir to this git project's .gitignore file
82+
alias gs_git_branch_hash_bak_up1="gs_git_branch_hash_bak \"../git_branch_hash_backups\""
83+

0 commit comments

Comments
 (0)