Skip to content

Commit

Permalink
Update gco.fish
Browse files Browse the repository at this point in the history
  • Loading branch information
shinriyo authored Nov 19, 2024
1 parent 7a4cd0a commit 93fb0ca
Showing 1 changed file with 25 additions and 57 deletions.
82 changes: 25 additions & 57 deletions functions/gco.fish
Original file line number Diff line number Diff line change
@@ -1,77 +1,45 @@
function __git_checkout -a var
# is numeric
# Check if the argument is numeric
if [ "$var" -eq "$var" ] 2>/dev/null
# number
set length (count $arr)
# Argument is a number (branch index)
set branches (git branch --list --format="%(refname:short)")
set length (count $branches)

if [ $var -gt $length ]
echo 'Number is large.'
return
echo "Error: Branch index $var is out of range."
return 1
end

set toplevel (git rev-parse --show-toplevel)
set myarg $arr[$var]
git checkout $myarg
# to allow gco from subdirs, use:
# git checkout $toplevel/$myarg
set branch $branches[$var]
git checkout $branch
else
# not a number
git checkout $var
# Argument is not numeric, assume it's a branch name
git checkout "$var"
end
end

function __gco
# number
set res (string split "-" -- (string trim $argv))
# for branch names with hyphens, use:
# set res (string trim $argv)
set first $res[1]
set length (count $res)
set last ""
function __gco -a arg
# Check if the argument is a range (e.g., 1-3)
if echo $arg | grep -qE '^\d+-\d+$'
set start (echo $arg | string split "-" | head -n1)
set end (echo $arg | string split "-" | tail -n1)

# >
if [ $length -gt 1 ]
set last $res[2]
else
# just one
#set myarg $arr[$res]
#git checkout $myarg
__git_checkout $res
return
end

# last exists
if [ $last != '' ]
# first < last
if [ $first -lt $last ]
for i in (seq $first 1 $last)
__git_checkout $i
end
if [ "$start" -lt "$end" ]
for i in (seq $start $end)
__git_checkout $i
end
else
echo 'Argument is not valid.'
echo "Error: Invalid range. Start must be less than end."
end
else
__git_checkout $first
# Argument is a single value (number or branch name)
__git_checkout "$arg"
end
end

function gco
# TODO: space like, `gco 1 2 3`
set length (count $argv)

if [ $length -eq 2 ]
# more than 1
set fst (echo $argv[1] | string sub -l 1)
# if first string is -, it is option
if [ $fst = '-' ]
git checkout $argv
return
end
end

set res (string split " " -- (string trim $argv))
for i in $res
#echo $i
__gco $i
# Process multiple arguments
for arg in $argv
__gco "$arg"
end
end

0 comments on commit 93fb0ca

Please sign in to comment.