]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/git/common.rc
b98987d758f390c8219de8cf97f5d18fc0fd538c
[plan9front.git] / sys / lib / git / common.rc
1 nl='
2 '
3
4 fn die{
5         >[1=2] echo $0: $*
6         exit $"*
7 }
8
9 fn usage{
10         >[1=2] echo -n 'usage:' $usage
11         exit 'usage'
12 }
13
14 # subst [-g] this [that]
15 fn subst{
16         awk 'BEGIN{
17                 global = 0
18                 for(i = 1; ARGV[i] ~ /^-/; i++){
19                         if(ARGV[i] == "-g")
20                                 global = 1
21                         ARGC--
22                 }
23                 this = ARGV[i++]; ARGC--
24                 that = ARGV[i++]; ARGC--
25         }
26         {
27                 if(global) gsub(this, that)
28                 else sub(this, that)
29                 print
30         }' $*
31 }
32
33 fn present {
34         if(~ $1 /dev/null && cmp $2 $3>/dev/null)
35                 status=gone
36         if not if (~ $3 /dev/null && cmp $1 $2>/dev/null)
37                 status=gone
38         if not
39                 status=()
40 }
41
42 # merge1 out theirs base ours
43 fn merge1 {@{
44         rfork e
45         n=$pid
46         out=$1
47         ours=$2
48         base=$3
49         theirs=$4
50         tmp=$out.tmp
51         while(test -f $tmp){
52                 tmp=$tmp.$n
53                 n=`{echo $n + 1 | hoc}
54         }
55
56         if(! test -f $ours)
57                 ours=/dev/null
58         if(! test -f $base)
59                 base=/dev/null
60         if(! test -f $theirs)
61                 theirs=/dev/null
62         if(! ape/diff3 -3 -m $ours $base $theirs > $tmp)
63                 echo merge needed: $out >[1=2]
64
65         if(present $ours $base $theirs){
66                 mv $tmp $out
67                 git/add $out
68         }
69         if not {
70                 rm -f $tmp $out
71                 git/rm $out
72         }
73 }}
74
75 fn gitup{
76         gitroot=`{git/conf -r >[2]/dev/null}
77         if(~ $#gitroot 0)
78                 die 'not a git repository'
79         gitfs=$gitroot/.git/fs
80         gitrel=`{pwd | subst '^'$"gitroot'/?'}
81         if(~ $#gitrel 0)
82                 gitrel='.'
83         cd $gitroot
84         startfs=()
85         if(! test -d $gitfs)
86                 mkdir -p $gitfs
87         if(! test -e $gitfs/ctl)
88                 startfs=true
89         if(! grep -s '^repo '$gitroot'$' $gitfs/ctl >[2]/dev/null)
90                 startfs=true
91         if(~ $#startfs 1)
92                 git/fs
93         if not
94                 status=''
95 }