Skip to content

Commit

Permalink
Correctly use gnu-sed on macos
Browse files Browse the repository at this point in the history
- corrects outdated gnu-sed installation instructions on macos
- uses gsed on macos
- display error message if gnu-sed is missing
  • Loading branch information
ssbarnea committed Mar 27, 2024
1 parent 744f22f commit c04ee67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,11 @@ Options:
Similar to a distributed
`git ls-files -z -- FILENAMES | xargs -0 sed -i EXPRESSION`.

_note_: this assumes GNU sed. If you're on macOS, install `gnu-sed` with Homebrew:
_note_: this assumes GNU sed. If you're on macOS, install `gnu-sed` with
Homebrew, which will expose `gsed` executable:

```bash
brew install gnu-sed

# Add to .bashrc / .zshrc
export PATH="$(brew --prefix)/opt/gnu-sed/libexec/gnubin:$PATH"
```

Arguments:
Expand Down
17 changes: 14 additions & 3 deletions all_repos/sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import argparse
import functools
import os.path
import platform
import shlex
import shutil
import subprocess
import sys
from typing import Generator
from typing import Sequence

Expand All @@ -14,6 +17,14 @@
from all_repos.config import Config
from all_repos.util import zsplit

SED_EXEC = "sed"
if platform.system() == 'Darwin':
if not shutil.which("gsed"):
print('MacOS specific prerequisite missing, please install it with: brew install gnu-sed')
sys.exit(2)

SED_EXEC = "gsed"


def find_repos(
config: Config,
Expand Down Expand Up @@ -44,7 +55,7 @@ def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description=(
'Similar to a distributed '
'`git ls-files -z -- FILENAMES | xargs -0 sed -i EXPRESSION`.'
f'`git ls-files -z -- FILENAMES | xargs -0 {SED_EXEC} -i EXPRESSION`.'
),
usage='%(prog)s [options] EXPRESSION FILENAMES',
)
Expand All @@ -62,7 +73,7 @@ def main(argv: Sequence[str] | None = None) -> int:
'--commit-msg', '--commit-message',
help=(
'override the autofixer commit message. (default '
'`git ls-files -z -- FILENAMES | xargs -0 sed -i ... EXPRESSION`).'
f'`git ls-files -z -- FILENAMES | xargs -0 {SED_EXEC} -i ... EXPRESSION`).'
),
)
parser.add_argument(
Expand All @@ -79,7 +90,7 @@ def main(argv: Sequence[str] | None = None) -> int:
dash_r = ('-r',)
else:
dash_r = ()
sed_cmd = ('sed', '-i', *dash_r, args.expression)
sed_cmd = (SED_EXEC, '-i', *dash_r, args.expression)
ls_files_cmd = ('git', 'ls-files', '-z', '--', args.filenames)

msg = f'{shlex.join(ls_files_cmd)} | xargs -0 {shlex.join(sed_cmd)}'
Expand Down

0 comments on commit c04ee67

Please sign in to comment.