Skip to content

Commit fd4db23

Browse files
committed
fix: bsd compatibility issues
- iconv //TRANSLIT is effectively GNU-only - BSD sed reads \t literally
1 parent ba63f66 commit fd4db23

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# GitNow — Speed up your Git workflow. 🐠
22
# https://github.com/joseluisq/gitnow
33

4-
function __gitnow_current_branch_list
5-
command git branch --list --no-color | LC_ALL=C command sed -E "s/^(\*?[ \t]*)//g" 2>/dev/null
4+
function __gitnow_current_branch_list -d "Lists local branch names, one per line"
5+
# Ask git for clean names instead of parsing `git branch` output: no
6+
# `*` marker, no padding, no "(HEAD detached at ...)" line — and no
7+
# sed, whose `\t` handling differs between GNU and BSD (macOS).
8+
command git for-each-ref --format='%(refname:short)' refs/heads/ 2>/dev/null
69
end

functions/__gitnow_slugify.fish

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
# GitNow — Speed up your Git workflow. 🐠
22
# https://github.com/joseluisq/gitnow
33

4-
# adapted from https://gist.github.com/oneohthree/f528c7ae1e701ad990e6
5-
function __gitnow_slugify
6-
# Lowercase + ASCII transliteration; `/` and `.` are preserved since they
7-
# are valid in git branch names (e.g. `fix/v1.2`).
8-
string join ' ' -- $argv | command iconv -f UTF-8 -t ascii//TRANSLIT | string lower | string replace -ra '[^a-z0-9./-]+' '_' | string replace -ra '^[-_./]+|[-_./]+$' ''
4+
function __gitnow_slugify -d "Turns free text into a git-safe, lowercase branch slug"
5+
# Pure Fish, no iconv: `iconv //TRANSLIT` is GNU-only in practice (macOS
6+
# iconv rejects or drops accented characters). Transliterate the common
7+
# Latin accents by hand, lowercase, then map anything else non-ASCII to
8+
# `_`. `/` and `.` are preserved since they are valid in branch names.
9+
set -l s (string join ' ' -- $argv)
10+
11+
set s (string replace -ra '[àáâäãåāÀÁÂÄÃÅĀ]' a -- $s)
12+
set s (string replace -ra '[èéêëēÈÉÊËĒ]' e -- $s)
13+
set s (string replace -ra '[ìíîïīÌÍÎÏĪ]' i -- $s)
14+
set s (string replace -ra '[òóôöõøōÒÓÔÖÕØŌ]' o -- $s)
15+
set s (string replace -ra '[ùúûüūÙÚÛÜŪ]' u -- $s)
16+
set s (string replace -ra '[ýÿÝŸ]' y -- $s)
17+
set s (string replace -ra '[ñÑ]' n -- $s)
18+
set s (string replace -ra '[çÇ]' c -- $s)
19+
set s (string replace -ra '[ß]' ss -- $s)
20+
set s (string replace -ra '[æÆ]' ae -- $s)
21+
set s (string replace -ra '[œŒ]' oe -- $s)
22+
23+
string lower -- $s | string replace -ra '[^a-z0-9./-]+' '_' | string replace -ra '^[-_./]+|[-_./]+$' ''
924
end

tests/integration/commands.test.fish

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ it "keeps slash hierarchy and dots"
9595
branch "Feat/Login.Page" >/dev/null 2>&1
9696
expect (current_branch) --to-equal feat/login.page
9797

98+
it "lists branch names verbatim (regression: BSD sed stripped leading t)"
99+
expect (__gitnow_current_branch_list) --to-contain master my_branch feat/login.page
100+
expect (__gitnow_current_branch_list) --not-to-match '^\*|^\s'
101+
branch tricky >/dev/null 2>&1
102+
expect (__gitnow_check_if_branch_exist tricky) --to-equal 1
103+
expect (__gitnow_check_if_branch_exist opic) --to-equal 0
104+
move master >/dev/null 2>&1
105+
98106
describe "gitflow branches"
99107

100108
it "feature creates a lowercase prefixed branch"

tests/unit/slugify.test.fish

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ expect "$(__gitnow_slugify "My FEATURE")" --to-equal my_feature
1313

1414
it "transliterates unicode to ascii"
1515
expect "$(__gitnow_slugify "Ñandú Café")" --to-equal nandu_cafe
16+
expect "$(__gitnow_slugify "Ærøskøbing Straße Œuvre")" --to-equal aeroskobing_strasse_oeuvre
17+
expect "$(__gitnow_slugify "ÀÉÎÕÜ")" --to-equal aeiou
18+
19+
it "maps other non-ascii characters to underscores"
20+
expect "$(__gitnow_slugify "日本 x")" --to-equal x
21+
expect "$(__gitnow_slugify "a→b")" --to-equal a_b
1622

1723
it "preserves slash and dot (valid in git refs)"
1824
expect "$(__gitnow_slugify "Fix/v1.2")" --to-equal fix/v1.2

0 commit comments

Comments
 (0)