]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/git/pull
4dd7fe94ef70e533191e7ef9056154979c3264e7
[plan9front.git] / sys / src / cmd / git / pull
1 #!/bin/rc -e
2 rfork en
3 . /sys/lib/git/common.rc
4
5 fn update{
6         branch=$1
7         upstream=$2
8         url=$3
9         dir=$4
10         bflag=()
11         dflag=()
12         if(! ~ $#branch 0)
13                 bflag=(-b $branch)
14         if(! ~ $#debug 0)
15                 dflag='-d'
16         {git/fetch $dflag $bflag -u $upstream $url >[2=3] || die $status} | awk '
17         /^remote/{
18                 if($2=="HEAD")
19                         next
20                 ref=$2
21                 hash=$3
22                 gsub("^refs/heads", "refs/remotes/'$upstream'", ref)
23                 outfile = ".git/"ref
24                 system("mkdir -p `{basename -d "outfile"}");
25                 print hash > outfile;
26                 close(outfile);
27         }
28         ' |[3] tr '\x0d' '\x0a'
29 }
30
31 gitup
32
33 flagfmt='a:allbranch, b:branch branch, d:debug,
34         f:fetchonly, u:upstream upstream, q:quiet'
35 args=''
36 eval `''{aux/getflags $*} || exec aux/usage
37
38 if(~ $#branch 0)
39         branch=refs/`{git/branch}
40 if(~ $allbranch 1)
41         branch=''
42
43 if(~ $#upstream 0)
44         upstream=origin
45 remote=`$nl{git/conf 'remote "'$upstream'".url'}
46 if(~ $#remote 0){
47         remote=$upstream
48         upstream=THEM
49 }
50
51 update $branch $upstream $remote
52 if (~ $fetchonly 1)
53         exit
54
55 local=`{git/branch}
56 remote=`{git/branch | subst '^(refs/)?heads' 'remotes/'$upstream}
57
58 # we have local commits, but the remote hasn't changed.
59 # in this case, we want to keep the local commits untouched.
60 if(~ `{git/query HEAD $remote @} `{git/query $remote}){
61         echo 'up to date' >[1=2]
62         exit
63 }
64 # The remote repository and our HEAD have diverged: we
65 # need to merge.
66 if(! ~ `{git/query HEAD $remote @} `{git/query HEAD}){
67         >[1=2]{
68                 echo ours:      `{git/query HEAD}
69                 echo theirs:    `{git/query $remote}
70                 echo common:    `{git/query HEAD $remote @}
71                 echo git/merge $remote
72         }
73         exit diverged
74 }
75 # The remote is directly ahead of the local, and we have
76 # no local commits that need merging.
77 if(~ $#quiet 0)
78         git/log -s -e $local'..'$remote >[1=2]
79 echo
80 echo $remote':' `{git/query $local} '=>' `{git/query $remote}  >[1=2]
81 git/branch -mnb $remote $local
82 exit ''