Blame view

RIOT/dist/tools/git/git-cache 5.6 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  #!/bin/sh
  
  git_cache() {
      git -C "${GIT_CACHE_DIR}" $*
  }
  
  git_cache_initialized() {
      local _git_dir="$(git_cache rev-parse --git-dir 2>/dev/null)"
      test "$_git_dir" = "." -o "$_git_dir" = ".git"
  }
  
  init() {
      set -e
      git_cache_initialized || {
          mkdir -p "${GIT_CACHE_DIR}"
  
          git_cache init --bare
          git_cache config core.compression 1
      }
      set +e
  }
  
  add() {
      local repo="$1"
  
      _locked "$GIT_CACHE_DIR/$name.addlock" _add "$repo"
  }
  
  _add() {
      set -e
      local repo="$1"
      local name="$(_remote_name $repo)"
  
      if ! is_cached "$repo"; then
          git_cache remote add "$name" "$repo"
          git_cache config --add remote.${name}.fetch "+refs/tags/*:refs/tags/${name}/*"
      else
          echo "git-cache: $url already in cache"
      fi
      set +e
  }
  
  if [ "$(uname)" = Darwin ]; then
      _locked() {
          local lockfile="$1"
          shift
  
          while ! shlock -p $$ -f $lockfile; do
              sleep 0.2
          done
  
          $*
  
          rm $lockfile
      }
  else
      _locked() {
          local lockfile="$1"
          shift
  
          (
          flock -w 600 9 || exit 1
          $*
          ) 9>"$lockfile"
      }
  fi
  
  update() {
      set -e
  
      local REMOTE=${1}
      if [ -n "$REMOTE" ]; then
          local REMOTES=$(_remote_name $REMOTE)
      else
          local REMOTES="$(git_cache remote show)"
      fi
  
      for remote in $REMOTES; do
          echo "git-cache: updating remote $remote"
          _locked "$GIT_CACHE_DIR/$remote.lock" git_cache --namespace $remote fetch -n $remote
      done
  
      set +e
  }
  
  is_cached() {
      set +e
      local url="$1"
      local REMOTES="$(git_cache remote show)"
      for remote in $REMOTES; do
          [ "$(git_cache ls-remote --get-url $remote)" = "$url" ] && return 0
      done
      set -e
      return 1
  }
  
  list() {
      local REMOTES="$(git_cache remote show)"
      for remote in $REMOTES; do
          echo "$(git_cache ls-remote --get-url $remote)"
      done
  }
  
  drop() {
      set -e
      local REMOTE=${1}
      [ -z "$REMOTE" ] && {
          echo "usage: git cache drop <url>"
          exit 1
      }
      local REMOTES="$(git_cache remote show)"
      for remote in $REMOTES; do
          [ "$(git_cache ls-remote --get-url $remote)" = "$REMOTE" ] && {
              git_cache remote remove $remote
              break
          }
      done
      set +e
  }
  
  _check_commit() {
      git_cache cat-file -e ${1}^{commit} 2>/dev/null
  }
  
  _remote_name() {
      echo "$*" | git hash-object --stdin
  }
  
  _tag_to_sha1() {
      local out="$(git_cache log -n 1 --pretty=oneline $1 -- 2>/dev/null)"
      [ -n "$out" ] && echo $out | cut -f 1 -d" "
  }
  
  _check_tag_or_commit() {
      local SHA1=$1
      local REMOTE_NAME=$2
  
      if _check_commit $SHA1 ; then
          git_cache tag commit$SHA1 $SHA1 2> /dev/null || true # ignore possibly already existing tag
          echo "commit$SHA1"
      elif _tag_to_sha1 ${REMOTE_NAME}/$SHA1 > /dev/null; then
          echo "${REMOTE_NAME}/$SHA1"
      fi
  }
  
  clone() {
      set -e
      local REMOTE="${1}"
      local SHA1="${2}"
      local REMOTE_NAME="$(_remote_name $REMOTE)"
      local TARGET_PATH="${3}"
  
      [ -z "$TARGET_PATH" ] && TARGET_PATH="$(basename $REMOTE)"
  
      # make sure git won't ask for credentials
      export GIT_TERMINAL_PROMPT=0
  
      if git_cache_initialized; then
          if ! is_cached "$REMOTE"; then
              echo "git cache: auto-adding $REMOTE"
              add "$REMOTE"
          fi
  
          local tag="$(_check_tag_or_commit $SHA1 $REMOTE_NAME)"
          if [ -z "$tag" ]; then
              # commit / tag not in cache, try updating repo
              update "$REMOTE" || true
              tag="$(_check_tag_or_commit $SHA1 $REMOTE_NAME)"
          fi
  
          if [ -n "$tag" ]; then
              echo "git-cache: cloning from cache."
              git -c advice.detachedHead=false clone --reference "${GIT_CACHE_DIR}" --shared "${GIT_CACHE_DIR}" "${TARGET_PATH}" --branch $tag
              git -C "${TARGET_PATH}" fetch origin "refs/tags/${REMOTE_NAME}/*:refs/tags/*" > /dev/null
          else
              echo "git-cache: trying checkout from source"
              git clone --reference "${GIT_CACHE_DIR}" --shared "${REMOTE}" "${TARGET_PATH}"
              git -c advice.detachedHead=false -C "${TARGET_PATH}" checkout $SHA1
          fi
      else
              git clone "${REMOTE}" "${TARGET_PATH}"
              git -c advice.detachedHead=false -C "${TARGET_PATH}" checkout $SHA1
      fi
      set +e
  }
  
  usage() {
      echo "git cache uses a bare git repository containing all objects from multiple"
      echo "upstream git repositories."
      echo ""
      echo "usage:"
      echo ""
      echo "    git cache init                initialize git cache"
      echo "    git cache add <url>           add repository <url>"
      echo "    git cache list                list cached repositories"
      echo "    git cache drop <url>          drop repo from cache"
      echo "    git cache update [<url>]      fetch repo <url> (or all)"
      echo "    git cache clone <url> <SHA1>  clone repository <url> from cache"
      echo "    git cache show-path           print's the path that can be used as "
      echo "                                  '--reference' parameter"
      echo ""
      echo "To retrieve objects from cache (will use remote repository if needed):"
      echo '    git clone --reference $(git cache show-path) <repo>'
  }
  
  [ $# -eq 0 ] && {
      usage
      exit 1
  }
  
  ACTION=$1
  shift 1
  
  export GIT_CACHE_DIR=${GIT_CACHE_DIR:-${HOME}/.gitcache}
  
  case $ACTION in
      init)
          init $*
          ;;
      add)
          add $*
          ;;
      update)
          update $*
          ;;
      list)
          list $*
          ;;
      drop)
          drop $*
          ;;
      show-path)
          echo ${GIT_CACHE_DIR}
          ;;
      clone)
          clone $*
          ;;
      *)
          usage
          ;;
  esac