这是一个非常旧的但没有答案的问题。我决定调查,发现我可以证明Git的行为与问题所说的不同。 一种解释是,Git的改进算法的对开,或提问做出标记提交一个错误。
我想了解更多和git bisect,但我有这个历史的麻烦。我知道107ca95
是好的,3830e61
是坏的。当我运行git bisect时,提交107ca95..3e667f8
被忽略。我碰巧知道43a07b1
是犯介绍了一个回归,但它永远不会评估。
我写了一些代码来检查其是否评估或没有。我的测试显示它已被评估。运行下面的代码并验证提交消息Add menu styles.
出现。
进一步意见:
- “承诺
107ca95..3e667f8
被忽略”:请注意,提交你标记为“好”将不被评估,因为混帐已经知道它是好的。
- 请参阅this article by Christian Couder中的“二分算法”一节。另外“检查合并基础”部分可能是相关的。
- 如上所述,问题当然是使用不同的版本,然后我使用(问题是从2013年,Git 2.11是从2016年)。
平分运行输出
- 注意,第一“添加管理员通知”被检查(第4行),因为其提供最多信息。 (请阅读上述文章中的“检查合并基础”。)
- 从此,它会按预期平分线性历史。
# bad: [d7761d6f146eaca1d886f793ced4315539326866] Add data escaping. (Bad)
# good: [f555d9063a25a20a6ec7c3b0c0504ffe0a997e98] Add Responsive Nav. (Good)
git bisect start 'd7761d6f146eaca1d886f793ced4315539326866' 'f555d9063a25a20a6ec7c3b0c0504ffe0a997e98'
# good: [1b3b7f4952732fec0c68a37d5f313d6f4219e4ae] Add ‘Admin’ notice. (Good)
git bisect good 1b3b7f4952732fec0c68a37d5f313d6f4219e4ae
# bad: [f9a65fe9e6cde4358e5b8ef7569332abfb07675e] Add icons. (Bad)
git bisect bad f9a65fe9e6cde4358e5b8ef7569332abfb07675e
# bad: [165b8a6e5137c40ce8b90911e59d7ec8eec30f46] Add menu styles. (Bad)
git bisect bad 165b8a6e5137c40ce8b90911e59d7ec8eec30f46
# first bad commit: [165b8a6e5137c40ce8b90911e59d7ec8eec30f46] Add menu styles. (Bad)
代码
运行在的Python 3,使用Git 2.11.0。 命令来运行:python3 script.py
""" The following code creates a git repository in '/tmp/git-repo' and populates
it with the following commit graph. Each commit has a test.sh which can be used
as input to a git-bisect-run.
The code then tries to find the breaking change automatically.
And prints out the git bisect log.
Written in response to http://stackoverflow.com/questions/17267816/git-bisect-with-merged-commits
to test the claim that '107ca95..3e667f8 are never checked out'.
Needs Python 3!
"""
from itertools import chain
import os.path
import os
import sh
repo = {
0x3830e61: {'message': "Add data escaping.", 'parents': [ 0x0f5e148 ], 'test': False} , # Last: (Bad)
0x0f5e148: {'message': "Improve function for getting page template.", 'parents': [ 0xaaf8dc5], 'test': False},
0xaaf8dc5: {'message': "Merge branch 'navigation'", 'parents': [ 0x3e667f8, 0xea3d736], 'test': False},
0x3e667f8: {'message': "Add icons.", 'parents': [ 0x43a07b1], 'test': False},
0x43a07b1: {'message': "Add menu styles.", 'parents': [ 0x107ca95], 'test': False} , # First: (Breaks)
0x107ca95: {'message': "Add Responsive Nav.", 'parents': [ 0xf52cc34], 'test': True}, # First: (Good)
0xea3d736: {'message': "Add ‘Admin’ notice.", 'parents': [ 0x17ca0bb], 'test': True},
0x17ca0bb: {'message': "Update placeholder text.", 'parents': [ 0xf52cc34], 'test': True},
0xf52cc34: {'message': "Add featured image.", 'parents': [ 0x2abd954], 'test': True},
0x2abd954: {'message': "Style placeholders.", 'parents': [], 'test': True},
}
bad = 0x3830e61
good = 0x107ca95
def generate_queue(_dag, parents):
for prev in parents:
yield prev
yield from generate_queue(_dag, _dag[prev]['parents'])
def make_queue(_dag, inits):
""" Converts repo (a DAG) into a queue """
q = list(generate_queue(_dag, inits))
q.reverse()
seen = set()
r = [x for x in q if not (x in seen or seen.add(x))]
return r
if __name__ == '__main__':
pwd = '/tmp/git-repo'
sh.rm('-r', pwd)
sh.mkdir('-p', pwd)
g = sh.git.bake(_cwd=pwd)
g.init()
parents = set(chain.from_iterable((repo[c]['parents'] for c in repo)))
commits = set(repo)
inits = list(commits - parents)
queue = make_queue(repo, inits)
assert len(queue) == len(repo), "queue {} vs repo {}".format(len(queue), len(repo))
commit_ids = {}
# Create commits
for c in queue:
# Set up repo
parents = repo[c]['parents']
if len(parents) > 0:
g.checkout(commit_ids[parents[0]])
if len(parents) > 1:
if len(parents) > 2: raise NotImplementedError('Octopus merges not support yet.')
g.merge('--no-commit', '-s', 'ours', commit_ids[parents[1]]) # just force to use 'ours' strategy.
# Make changes
with open(os.path.join(pwd, 'test.sh'), 'w') as f:
f.write('exit {:d}\n'.format(0 if repo[c]['test'] else 1))
os.chmod(os.path.join(pwd, 'test.sh'), 0o0755)
with open(os.path.join(pwd, 'message'), 'w') as f:
f.write(repo[c]['message'])
g.add('test.sh', 'message')
g.commit('-m', '{msg} ({test})'.format(msg=repo[c]['message'], test='Good' if repo[c]['test'] else 'Bad'))
commit_ids[c] = g('rev-parse', 'HEAD').strip()
# Run git-bisect
g.bisect('start', commit_ids[bad], commit_ids[good])
g.bisect('run', './test.sh')
print(g.bisect('log'))
过去我google搜索了类似的问题
,发现标有所有“合并”分支的东西好,所以只留下合并提交自己的脚本。 –
@BalogPal - 我看到了类似的建议,但似乎这样会将分支中的所有内容都标记为好,但实际上它包含错误的提交。对我来说奇怪的是,我甚至无法解决合并提交问题。奇怪的是,它解决了一个甚至不在提交范围内的提交。 – tollmanz
没关系,如果第一个识别出合并,那么你做一个2-pass对分,然后你将它与它提交的提交合并。但是,如果你有更好的主意,只需将它应用于技术,关键是你可以使用脚本预先标记某些提交 –