新的“branch”参数在 git.latest 状态下意味着什么,我该如何使用它?

What does the new `branch` argument mean in git.latest states, and how do I use it?

我们最近刚从 Salt 2014.7 升级到 2015.8,我对 git.latest 状态下的新行为感到困惑,即新的 branch 参数。

在我们的暂存环境中,我们习惯于部署到一个命名分支,git.latest state 从支柱上获取并进入 rev 参数。像这样:

{% set branch = salt['pillar.get']('myapp:branch', 'master') %}
{% set code_prefix = "/tmp/myapp" %}

{{ code_prefix }}:
  file.directory:
    - user: myapp
    - group: myapp

myapp-repo:
  git.latest:
    - name: git@github.com:MyOrg/myrepo.git
    - rev: {{ branch }}
    - force_checkout: true
    - force_clone: true
    - force_fetch: true
    - force_reset: true
    - target: {{ code_prefix }}
    - user: myapp
    - require:
        - file: {{ code_prefix }}

升级后,我们开始在一些回购协议中出现奇怪的 git 错误;我不确定到底是什么导致了错误,但它似乎与工作目录的 rev 更改但本地命名分支没有更改的新行为有关,因此它与远程版本不匹配本地分支机构。

长话短说,将 branch 参数设置为 git.latest 似乎可以解决问题:

myapp-repo:
  git.latest:
    - name: git@github.com:MyOrg/myrepo.git
    - rev: {{ branch }}
    - branch: {{ branch }}
    - force_checkout: true
    - force_clone: true
    - force_fetch: true
    - force_reset: true
    - target: {{ code_prefix }}
    - user: myapp
    - require:
        - file: {{ code_prefix }}

但我想知道:为什么我突然需要 branch 参数?如果我部署 sha 或标签而不是分支名称会发生​​什么?

我应该担心吗?我做错了吗?

branch 参数指定在目标中使用哪个分支来检出 rev branch/tag/commit。如果您不使用此参数,则 rev 将在目标中处于活动状态的任何分支中检出。

我认为文档对此非常清楚:

Name of the branch into which to checkout the specified rev. If not specified, then Salt will not care what branch is being used locally and will just use whatever branch is currently there.

Note: If not specified, this means that the local branch name will not be changed if the repository is reset to another branch/tag/SHA1.

我不知道以前的行为是什么。现在,如果您在未指定本地分支的情况下签出提交或标记,则本地签出分支将硬重置为指定的 rev,无论其先前状态如何(前提是您使用 force_reset=True )

就个人而言,我使用远程存储库中不存在的本地分支名称,这样就不会混淆签出的内容。