8

リポジトリに解決する必要のある競合があるかどうかを人間として確認する方法はたくさんあります。

ただし、これをスクリプトで確認する方法を探しています。つまり、リポジトリが何かを開始するのに適した状態にあるかどうか、またはユーザーが競合を修正する必要がある段階にあるかどうかを確認します。

私は次のような方法を考えることができます:

__git_ps1 "%s" | grep MERGING > /dev/null 2>&1 && echo "In merge state"

ただし、これは推奨される方法ではないと思います。第一に、Cプログラマーとして、私__git_ps1__それが自分の用途ではないと思う傾向があるためです。第二に、次のようなより適切な方法があると推測しています。

git repo-status --is-merging

戻り値などがあります。

では、リポジトリがマージ状態にあるかどうかをgitに(スクリプトとして)尋ねるにはどうすればよいですか?

4

3 に答える 3

14

作業コピー全体のステータスとインデックスを確認する必要があるため、大規模なリポジトリでは使用git statusまたは同様の処理が遅くなります。インデックスのみに関心があるため、インデックスの状態をチェックするだけのはるかに高速なコマンドを使用できます。

具体的には、 を使用できますgit ls-files --unmerged。競合状態のファイルがない場合、そのコマンドは出力を生成せず、存在する場合は次のようなものを生成します。

$ git ls-files --unmerged
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 1       filename
100644 4a58007052a65fbc2fc3f910f2855f45a4058e74 2       filename
100644 65b2df87f7df3aeedef04be96703e55ac19c2cfb 3       filename

したがって、そのファイルが出力を生成するかどうかを確認できます: [[ -z $(git ls-files --unmerged) ]]. そのコマンドは、リポジトリがクリーンな場合はゼロのリターン コードを返し、リポジトリに競合がある場合はゼロ以外を返します。逆の動作には に-z置き換えます。-n

以下を に追加できます~/.gitconfig

[alias]
    conflicts = ![[ -n $(git ls-files --unmerged) ]]
    list-conflicts = "!cd ${GIT_PREFIX:-.}; git ls-files --unmerged | cut -f2 | sort -u"

これにより、次のような動作が得られます。

$ git st
# On branch master
nothing to commit (working directory clean)

$ git conflicts && echo 'Conflicts exist' || echo 'No conflicts'
No conflicts

$ git merge other-branch
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.

$ git conflicts && echo 'Conflicts exist' || echo 'No conflicts'
Conflicts exist

$ git list-conflicts
file

( cd ${GIT_PREFIX:-.}2 番目のエイリアスの部分は、リポジトリ全体ではなく、現在のディレクトリ内の競合するファイルのリストのみを取得することを意味します。)

于 2012-12-17T18:16:59.233 に答える
4

GNU/私がすることなら何でも

$ if { git status --porcelain | sed -nr '/^U.|.U|AA|DD/q1'; }
> then # no merge conflicts
> else # merge conflicts
> fi
于 2012-12-15T16:26:00.073 に答える
1

これはうまくいきますか?

$ git merge origin/master
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.

$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 1 different commit each, respectively.
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both modified:      file
#
no changes added to commit (use "git add" and/or "git commit -a")

$ git status -s         
UU file

ファイルに 2 つの変更があり、両方がマージされていないことを示しているため、マージ状態にあることがわかります。実際、XY filewhereXYare の両方が文字になっている場合は、解決が必要な競合が発生している可能性があります。

于 2012-12-15T16:27:13.707 に答える