Skip to content

Commit dee7c0f

Browse files
[bash_aliases] Made gitgrepreplace work on MacOS and Ubuntu
1 parent d285955 commit dee7c0f

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

MacOS/bash_aliases

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ function watch () {
1515
done
1616
}
1717

18-
function gitgrepreplace () {
19-
local OLD_TEXT="${1}"
20-
local NEW_TEXT="${2}"
21-
local GREP_PATH="${3:-.}"
22-
local SEPARATOR="${4:-!}"
23-
local REPLACE="s${SEPARATOR}${OLD_TEXT}${SEPARATOR}${NEW_TEXT}${SEPARATOR}g"
24-
git grep -l "${OLD_TEXT}" "${GREP_PATH}" | xargs gsed -i "${REPLACE}"
25-
}
26-
2718
function getent() {
2819
# Trying to make dscacheutil like getent
2920
# in macOS `man dscacheutil`

Ubuntu/bash_aliases

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ function cleansshagent () {
4646
pgrep -u "$(id -u)" 'ssh-agent'
4747
}
4848

49-
function gitgrepreplace () {
50-
local OLD_TEXT="${1}"
51-
local NEW_TEXT="${2}"
52-
local GREP_PATH="${3:-.}"
53-
local SEPARATOR="${4:-!}"
54-
local REPLACE="s${SEPARATOR}${OLD_TEXT}${SEPARATOR}${NEW_TEXT}${SEPARATOR}g"
55-
git grep -l "${OLD_TEXT}" "${GREP_PATH}" | xargs sed -i "${REPLACE}"
56-
}
57-
5849
function aptlistinrepo() {
5950
local REPO="${1:-ppa.launchpad}"
6051
grep ^Package \

bash_aliases

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,42 @@ function youtube-dl-best() {
154154
--merge-output-format mp4 "${YOUTUBE_URL}"
155155
}
156156

157+
function get_sed_args() {
158+
local -n args=$1
159+
case "$(uname)" in
160+
Darwin)
161+
args+=('-i' "''")
162+
;;
163+
Linux)
164+
args+=('-i')
165+
;;
166+
*)
167+
msg_error "Operating System $(uname) is not supported"
168+
;;
169+
esac
170+
}
171+
172+
function gitgrepreplace () {
173+
local OLD_TEXT="${1}"
174+
shift
175+
local NEW_TEXT="${1}"
176+
shift
177+
local SEPARATOR="${1:-!}"
178+
shift
179+
declare -a GREP_PATHS=("${1:-.}")
180+
shift
181+
if [[ "${GREP_PATHS[0]}" != '.' ]]; then
182+
while [[ $# -gt 0 ]]; do
183+
GREP_PATHS+=("${1}")
184+
shift
185+
done
186+
fi
187+
local sed_args
188+
get_sed_args sed_args
189+
local REPLACE="s${SEPARATOR}${OLD_TEXT}${SEPARATOR}${NEW_TEXT}${SEPARATOR}g"
190+
git grep -l "${OLD_TEXT}" "${GREP_PATHS[@]}" | xargs sed "${sed_args[@]}" "${REPLACE}"
191+
}
192+
157193
function gitgrepdiff() {
158194
local STRING="${1}"
159195
local DEFAULT_BRANCH="${2:-$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')}"

0 commit comments

Comments
 (0)