下面的方法使用分而治之在程序代码中定位错误(不要与git bisect混淆)。我使用了一次,它很有用。在git中有更好的方法吗?到处拖动主分支是好的形式吗?
错误可能存在于代码的任何部分。具有所有分支的2级二分树看起来像这样:
* 452a8ff (4/4) i
| * 32d0498 (3/4) h
|/
* 86de3c2 (2/2) g
| * 3740f7e (2/4) f
| | * 4cf909d (1/4) e
| |/
| * e9dc3a4 (1/2) d
|/
* 832edd8 (1/1) c
* a4ca550 'b'
* eafd06c 'a'
分数是分支名称。分支1/1包含整个代码。分支1/2包含前半部分,分支2/2包含后半部分。分支1 / 4、2 / 4、3 / 4和4/4包含四分之一。
每个分治阶段都有4个步骤:
该方法使用git reset。警告:每次重置git之前,请确保使用指向该提交的分支来提交文件
下面的示例使用分而治之在代码中查找错误。
提交并创建初始分支:
(already on branch to be debugged, reset not needed)
(edit,compile,test)
$ git commit -am "error: description of error we want to locate the source of"
$ git branch 1/1
$ git log --oneline --decorate --graph --all
* 832edd8 (HEAD, master, 1/1) error: description of error
* a4ca550 'b'
* eafd06c 'a'
错误在分支1/1中。测试分支1/1的第一部分:
(already on branch 1/1, reset not needed)
(edit,compile,test)
$ git commit -am "noError"
$ git branch 1/2
$ git log --oneline --decorate --graph --all
* e9dc3a4 (HEAD, master, 1/2) noError
* 832edd8 (1/1) error: description of error
* a4ca550 'b'
* eafd06c 'a'
分支1/2中没有错误。测试分支1/1的其他部分:
$ git reset --hard 1/1
(edit,compile,test)
$ git commit -am "error"
$ git branch 2/2
$ git log --oneline --decorate --graph --all
* 86de3c2 (HEAD, master, 2/2) error
| * e9dc3a4 (1/2) noError
|/
* 832edd8 (1/1) error: description of error
* a4ca550 'b'
* eafd06c 'a'
分支2/2中有错误。测试分支2/2的第一部分:
(already on branch 2/2, reset not needed)
(edit,compile,test)
$ git commit -am "noError"
$ git branch 3/4
$ git log --oneline --decorate --graph --all
* 32d0498 (HEAD, master, 3/4) noError
* 86de3c2 (2/2) error
| * e9dc3a4 (1/2) noError
|/
* 832edd8 (1/1) error: description of error
* a4ca550 'b'
* eafd06c 'a'
分支3/4中没有错误。测试分支2/2的其他部分:
$ git reset --hard 2/2
(edit,compile,test)
$ git commit -am "errorFound"
$ git branch 4/4
$ git log --oneline --decorate --graph --all
* 452a8ff (HEAD, master, 4/4) errorFound
| * 32d0498 (3/4) noError
|/
* 86de3c2 (2/2) error
| * e9dc3a4 (1/2) noError
|/
* 832edd8 (1/1) error: description of error
* a4ca550 'b'
* eafd06c 'a'
在分支4/4中发现错误。
4步分治制:
reset & edit
commit & branch
例子:
1/1 error: description
1/2 noError
2/2 error
3/4 noError
4/4 errorFound
分数是分支名称。分支1/1包含整个代码。分支1/2包含前半部分,分支2/2包含后半部分。分支1 / 4、2 / 4、3 / 4和4/4包含四分之一。
拥有一半(或四分之一或...)的代码通常无助于查明错误,因为该错误可能取决于代码库的上下文(而不是数量)。含义:提交可以引入功能上依赖于其他提交的其他更改的更改。
简单地删除一半的代码可能会损害那些功能上的依赖关系,并且代码可能仅由于缺少所述依赖关系而无法工作。
git repo按提交而不是按文件集查找错误。
如上所述,使用提交时“分而治之”方法的正确工具是git bisect
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句