sha1_to_hex: properly terminate the SHA1
[git.git] / git-ls-remote.sh
1 #!/bin/sh
2 #
3
4 usage () {
5     echo >&2 "usage: $0 [--heads] [--tags] <repository> <refs>..."
6     exit 1;
7 }
8
9 die () {
10     echo >&2 "$*"
11     exit 1
12 }
13
14 while case "$#" in 0) break;; esac
15 do
16   case "$1" in
17   -h|--h|--he|--hea|--head|--heads)
18   heads=heads; shift ;;
19   -t|--t|--ta|--tag|--tags)
20   tags=tags; shift ;;
21   --)
22   shift; break ;;
23   -*)
24   usage ;;
25   *)
26   break ;;
27   esac
28 done
29
30 case "$#" in 0) usage ;; esac
31
32 case ",$heads,$tags," in
33 ,,,) heads=heads tags=tags other=other ;;
34 esac
35
36 . git-parse-remote
37 peek_repo="$(get_remote_url "$@")"
38 shift
39
40 tmp=.ls-remote-$$
41 trap "rm -fr $tmp-*" 0 1 2 3 15
42 tmpdir=$tmp-d
43
44 case "$peek_repo" in
45 http://* | https://* )
46         if [ -n "$GIT_SSL_NO_VERIFY" ]; then
47             curl_extra_args="-k"
48         fi
49         curl -nsf $curl_extra_args "$peek_repo/info/refs" ||
50                 echo "failed    slurping"
51         ;;
52
53 rsync://* )
54         mkdir $tmpdir
55         rsync -rq "$peek_repo/refs" $tmpdir || {
56                 echo "failed    slurping"
57                 exit
58         }
59         (cd $tmpdir && find refs -type f) |
60         while read path
61         do
62                 cat "$tmpdir/$path" | tr -d '\012'
63                 echo "  $path"
64         done &&
65         rm -fr $tmpdir
66         ;;
67
68 * )
69         git-peek-remote "$peek_repo" ||
70                 echo "failed    slurping"
71         ;;
72 esac |
73 sort -t '       ' -k 2 |
74 while read sha1 path
75 do
76         case "$sha1" in
77         failed)
78                 die "Failed to find remote refs"
79         esac
80         case "$path" in
81         refs/heads/*)
82                 group=heads ;;
83         refs/tags/*)
84                 group=tags ;;
85         *)
86                 group=other ;;
87         esac
88         case ",$heads,$tags,$other," in
89         *,$group,*)
90                 ;;
91         *)
92                 continue;;
93         esac
94         case "$#" in
95         0)
96                 match=yes ;;
97         *)
98                 match=no
99                 for pat
100                 do
101                         case "/$path" in
102                         */$pat )
103                                 match=yes
104                                 break ;;
105                         esac
106                 done
107         esac
108         case "$match" in
109         no)
110                 continue ;;
111         esac
112         echo "$sha1     $path"
113 done