github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/makelib/git.mk (about) 1 # This file does a shallow clone of a given repository into a given 2 # directory and does a checkout to a given branch. The end result is a 3 # pristine state of a repository. 4 # 5 # Inputs: 6 # 7 # GCL_REPOSITORY - a git repository to clone 8 # 9 # GCL_DIRECTORY - a directory where the repository is supposed to be cloned 10 # 11 # GCL_COMMITTISH - a committish in a repository we want to check 12 # out. A committish is basically either a SHA, or a tag or a branch. 13 # Be careful with using SHAs - shallow fetches of a specified SHA are 14 # quite a new feature on the server side and not all git providers 15 # have this implemented or enabled. 16 # 17 # GCL_EXPECTED_FILE - a file relative to GCL_DIRECTORY that is 18 # expected to exist after the repository is cloned 19 # 20 # GCL_TARGET - the target that will depend on GCL_EXPECTED_FILE 21 # directly or indirectly, depending on whether we want to fetch the 22 # changes from git repo or not, see GCL_DO_CHECK. Also will depend on 23 # some deps stamps. 24 # 25 # GCL_DO_CHECK - whether we should try to fetch the new changes from 26 # the repository and invalidate the GCL_TARGET in case of new changes 27 # availability; should not be used for SHA or tags. 28 29 _GCL_GIT_ := "$(GIT)" -C "$(GCL_DIRECTORY)" 30 _GCL_FULL_PATH_ := $(GCL_DIRECTORY)/$(GCL_EXPECTED_FILE) 31 32 # init, set remote, fetch, reset --hard and clean -ffdx 33 $(call forward-vars,$(_GCL_FULL_PATH_), \ 34 _GCL_GIT_ GCL_REPOSITORY GCL_DIRECTORY GCL_COMMITTISH) 35 $(_GCL_FULL_PATH_): | $(GCL_DIRECTORY) 36 $(VQ) \ 37 set -e; \ 38 $(_GCL_GIT_) init $(call vl3,--quiet); \ 39 if ! $(_GCL_GIT_) remote | grep --silent origin; \ 40 then \ 41 $(_GCL_GIT_) remote add origin "$(GCL_REPOSITORY)"; \ 42 fi; \ 43 saved_committish=''; \ 44 if $(_GCL_GIT_) config --list | grep --silent '^rkt\.committish='; then \ 45 saved_committish="$$($(_GCL_GIT_) config rkt.committish)"; \ 46 fi; \ 47 if [ "$${saved_committish}" != "$(GCL_COMMITTISH)" ]; then \ 48 $(call vb,vt,GIT CLONE,$(GCL_REPOSITORY) ($(GCL_COMMITTISH)) => $(call vsp,$(GCL_DIRECTORY))) \ 49 $(_GCL_GIT_) fetch $(call vl3,--quiet) --depth=1 origin $(GCL_COMMITTISH); \ 50 $(_GCL_GIT_) config rkt.committish "$(GCL_COMMITTISH)"; \ 51 $(_GCL_GIT_) config rkt.fetch-head $$($(_GCL_GIT_) rev-parse FETCH_HEAD); \ 52 fi; \ 53 rev="$$($(_GCL_GIT_) config rkt.fetch-head)"; \ 54 $(call vb,vt,GIT RESET,$(call vsp,$(GCL_DIRECTORY)) => $(GCL_COMMITTISH)) \ 55 $(_GCL_GIT_) reset --hard $(call vl3,--quiet) "$${rev}"; \ 56 $(call vb,vt,GIT CLEAN,$(call vsp,$(GCL_DIRECTORY))) \ 57 $(_GCL_GIT_) clean -ffdx $(call vl3,--quiet); \ 58 touch "$@" 59 60 # remove the GCL_DIRECTORY if GCL_REPOSITORY changes 61 # also invalidate the _GCL_FULL_PATH_ as make seems not to check for 62 # nonexistence of a file after its prerequisites are remade 63 $(call setup-stamp-file,_GCL_RM_DIR_STAMP,gcl-$(GCL_DIRECTORY)-rm-dir) 64 $(call setup-stamp-file,_GCL_KV_DEPMK_STAMP,gcl-$(GCL_DIRECTORY)-kv-depmk) 65 $(call setup-dep-file,_GCL_KV_DEPMK,gcl-$(GCL_DIRECTORY)-kv) 66 67 $(call generate-rm-dir-rule,$(_GCL_RM_DIR_STAMP),$(GCL_DIRECTORY)) 68 $(call generate-kv-deps,$(_GCL_KV_DEPMK_STAMP),$(_GCL_RM_DIR_STAMP) $(_GCL_FULL_PATH_),$(_GCL_KV_DEPMK),GCL_REPOSITORY) 69 70 $(GCL_TARGET): $(_GCL_KV_DEPMK_STAMP) 71 72 # invalidate the _GCL_FULL_PATH_ if GCL_COMMITTISH changed 73 $(call setup-stamp-file,_GCL_KV_COMMITTISH_DEPMK_STAMP,gcl-$(GCL_DIRECTORY)-kv-committish-depmk) 74 $(call setup-dep-file,_GCL_KV_COMMITTISH_DEPMK,gcl-$(GCL_DIRECTORY)-kv-committish) 75 76 $(call generate-kv-deps,$(_GCL_KV_COMMITTISH_DEPMK_STAMP),$(_GCL_FULL_PATH_),$(_GCL_KV_COMMITTISH_DEPMK),GCL_COMMITTISH) 77 78 $(GCL_TARGET): $(_GCL_KV_COMMITTISH_DEPMK_STAMP) 79 80 # perform updates if wanted 81 ifneq ($(GCL_DO_CHECK),) 82 83 GR_TARGET := $(GCL_TARGET) 84 GR_SRCDIR := $(GCL_DIRECTORY) 85 GR_BRANCH := $(GCL_COMMITTISH) 86 GR_PREREQS := $(_GCL_FULL_PATH_) 87 88 include makelib/git-refresh.mk 89 90 else 91 92 $(GCL_TARGET): $(_GCL_FULL_PATH_) 93 94 endif 95 96 $(call undefine-namespaces,GCL _GCL)