git-flow を初めて利用する際には git flow init
で初期設定を行う必要があります。
チーム開発で利用するための CI 用のシェルスクリプトを作成していた際、この初期設定が完了しているか確認するスクリプトを書いたのでメモしておきます。
そもそも Git Flow って何?という方は以下の記事が参考になるかと思います。
スクリプト
#!/bin/bash
function warn() { echo "$@" >&2; }
function die() { warn "$@"; exit 1; }
function checkIfGitFlowBranchExists() {
local branch_config
local existed_in_local
branch_config=${1}
existed_in_local=$(git config --get ${branch_config})
if [[ -z ${existed_in_local} ]]; then
die "${branch_config} が存在しません。 Git Flow の初期化を行ってください。"
fi
if ! git branch | egrep "\s+${existed_in_local}" > /dev/null; then
die "${branch_config} に指定したブランチが存在しません。"
fi
}
function checkIfGitFlowInitialized() {
# cf. https://stackoverflow.com/questions/28464229/programmatically-determine-if-git-flow-is-initialized
local master_branch_config
local develop_branch_config
master_branch_config="gitflow.branch.master"
develop_branch_config="gitflow.branch.develop"
# 1. gitflow.branch.master が設定されており、設定したブランチが存在すること
checkIfGitFlowBranchExists ${master_branch_config}
# 2. gitflow.branch.develop が設定されており、設定したブランチが存在すること
checkIfGitFlowBranchExists ${develop_branch_config}
# 3. gitflow.branch.master と gitflow.branch.develop に指定したブランチが別のブランチであること
if [[ $(git config --get ${master_branch_config}) == $(git config --get ${develop_branch_config}) ]]; then
die "master ブランチと develop ブランチを分けてください。"
fi
# 4. release/hotfix ブランチが設定されていること
if [[ -z $(git config --get "gitflow.prefix.release") || -z $(git config --get "gitflow.prefix.hotfix") ]]; then
die "release ブランチまたは hotfix ブランチが設定されていません。"
fi
}
checkIfGitFlowInitialized # 初期設定チェック
git flow init
が実行済みかどうかを確認する手順については Stack Overflow の関連 Q&A を参考にしています。
以下に引用した回答の内容をスクリプトとして落とし込んでいます。
Answered here. Basically:
- Check config for gitflow.branch.master and if the branch does exist in the repo
- Check config for gitflow.branch.develop and if the branch does exist in the repo
- Master branch can not be the same as develop branch.
- Make sure all the prefixes are configured.
1 と 2 では .git/config
の以下の設定およびそのブランチが存在することを確認し、加えて 3 で2つのブランチが別々のものであることを確認しています。
[gitflow "branch"]
master = master
develop = develop
4 は以下の prefix 設定があるかどうかの確認です。
[gitflow "prefix"]
feature = feature/
release = release/
hotfix = hotfix/
support = support/
versiontag =
上記のスクリプトでは release と hotfix しか見ていませんが、必要に応じて feature ブランチなども確認するようにスクリプトを修正してください。
ちなみに
git-flow をデフォルト設定で運用することをチーム内で合意できるのであれば、 git flow init -df
で強制的に初期設定を行ってしまった方が早いかもしれません。