ref: f633a247589b6de4b058f66ad6b014db57bad3dd
dir: /branch/
#!/bin/rc -e
rfork en
fn usage{
echo usage: $0 '[-b base] [-o origin] new
-b base: use "base" for branch (default: current branch)
-o origin: use "origin" for remote branch
-u: update branch if it exists
new: name of new branch' >[1=2]
exit usage
}
if(! cd `{git/conf -r}){
exit 'not in git repository'
exit notgit
}
git/fs
nl='
'
stay=()
create=()
update=()
base=`{awk '$1=="branch"{print $2}' < /mnt/git/ctl}
while(~ $1 -* && ! ~ $1 --){
switch($1){
case -c; create=true
case -s; stay=true
case -b; shift; base=$1
case -u; update=true
case *
usage
}
shift
}
if(~ $1 --) shift
if(~ $#* 0){
echo $base
exit
}
if(! ~ $#* 1)
usage
if(~ $new refs/heads/*)
trim='s@^refs/heads/@@g'
if not if(~ $new heads/*)
trim='s@^heads/@@g'
if not
trim='s@^@@g'
new=`{echo $1 | sed $trim}
if(~ $#create 0){
if(! test -e .git/refs/heads/$new){
echo branch $new: does not exist >[1=2]
exit exists
}
}
if not{
if(~ $#update 0 && test -e .git/refs/heads/$new){
echo could not create $new: already exists >[1=2]
exit exists
}
branched=()
candidates=(.git/refs/$base .git/refs/heads/$base .git/refs/remotes/$base .git/refs/remotes/*/$base)
for(br in $candidates){
if(test -f $br){
echo 'creating new branch '$new
cp $br .git/refs/heads/$new
branched="ok"
}
}
if(~ $#branched 0){
echo 'could not find branch '$base >[1=2]
exit notfound
}
}
if(! ~ $#stay 0)
exit
modified=`$nl{git/query -c HEAD $base | grep '^[+~]' | sed 's/^..//'}
deleted=`$nl{git/query -c HEAD $base | grep '^-' | sed 's/^..//'}
echo here-- mod $#modified del $#deleted
if(! ~ $#modified 0 || ! ~ $#deleted 0){
if(! git/walk -q $modified $deleted){
echo remote changes would clobber local changes >[1=2]
exit dirty
}
}
if(! ~ $#modified 0){
# Modifications can turn a file into
# a directory, so we need to walk to
# check the blobs out correctly.
for(m in `{walk -f $modified}){
gm=/mnt/git/HEAD/tree/$m
d=`{basename -d $m}
mkdir -p $d
mkdir -p .git/index9/tracked/$d
cp $gm $m
walk -eq $m > .git/index9/tracked/$m
}
}
if(! ~ $#deleted 0){
rm -f $deleted
rm -f .git/index9/tracked/$deleted
}