github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/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  	human_rev="$$($(_GCL_GIT_) describe --always)"; \
    59  	$(call vb,vt,GIT DESCRIBE,$(GCL_REPOSITORY) ($(GCL_COMMITTISH)) => "$${human_rev}") \
    60  	touch "$@"
    61  
    62  # remove the GCL_DIRECTORY if GCL_REPOSITORY changes
    63  # also invalidate the _GCL_FULL_PATH_ as make seems not to check for
    64  # nonexistence of a file after its prerequisites are remade
    65  $(call setup-stamp-file,_GCL_RM_DIR_STAMP,gcl-$(GCL_DIRECTORY)-rm-dir)
    66  $(call setup-stamp-file,_GCL_KV_DEPMK_STAMP,gcl-$(GCL_DIRECTORY)-kv-depmk)
    67  $(call setup-dep-file,_GCL_KV_DEPMK,gcl-$(GCL_DIRECTORY)-kv)
    68  
    69  $(call generate-rm-dir-rule,$(_GCL_RM_DIR_STAMP),$(GCL_DIRECTORY))
    70  $(call generate-kv-deps,$(_GCL_KV_DEPMK_STAMP),$(_GCL_RM_DIR_STAMP) $(_GCL_FULL_PATH_),$(_GCL_KV_DEPMK),GCL_REPOSITORY)
    71  
    72  $(GCL_TARGET): $(_GCL_KV_DEPMK_STAMP)
    73  
    74  # invalidate the _GCL_FULL_PATH_ if GCL_COMMITTISH changed
    75  $(call setup-stamp-file,_GCL_KV_COMMITTISH_DEPMK_STAMP,gcl-$(GCL_DIRECTORY)-kv-committish-depmk)
    76  $(call setup-dep-file,_GCL_KV_COMMITTISH_DEPMK,gcl-$(GCL_DIRECTORY)-kv-committish)
    77  
    78  $(call generate-kv-deps,$(_GCL_KV_COMMITTISH_DEPMK_STAMP),$(_GCL_FULL_PATH_),$(_GCL_KV_COMMITTISH_DEPMK),GCL_COMMITTISH)
    79  
    80  $(GCL_TARGET): $(_GCL_KV_COMMITTISH_DEPMK_STAMP)
    81  
    82  # perform updates if wanted
    83  ifneq ($(GCL_DO_CHECK),)
    84  
    85  GR_TARGET := $(GCL_TARGET)
    86  GR_SRCDIR := $(GCL_DIRECTORY)
    87  GR_BRANCH := $(GCL_COMMITTISH)
    88  GR_PREREQS := $(_GCL_FULL_PATH_)
    89  
    90  include makelib/git-refresh.mk
    91  
    92  else
    93  
    94  $(GCL_TARGET): $(_GCL_FULL_PATH_)
    95  
    96  endif
    97  
    98  $(call undefine-namespaces,GCL _GCL)