Autogenerated HTML docs for v1.7.6-rc1
[git/jnareb-git.git] / git-bisect-lk2009.html
blob1cd54833acac7bd051559929471572e56b4d4a1b
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <meta name="generator" content="AsciiDoc 8.4.5" />
7 <title>Fighting regressions with git bisect</title>
8 <style type="text/css">
9 /* Debug borders */
10 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
12 border: 1px solid red;
16 body {
17 margin: 1em 5% 1em 5%;
20 a {
21 color: blue;
22 text-decoration: underline;
24 a:visited {
25 color: fuchsia;
28 em {
29 font-style: italic;
30 color: navy;
33 strong {
34 font-weight: bold;
35 color: #083194;
38 tt {
39 color: navy;
42 h1, h2, h3, h4, h5, h6 {
43 color: #527bbd;
44 font-family: sans-serif;
45 margin-top: 1.2em;
46 margin-bottom: 0.5em;
47 line-height: 1.3;
50 h1, h2, h3 {
51 border-bottom: 2px solid silver;
53 h2 {
54 padding-top: 0.5em;
56 h3 {
57 float: left;
59 h3 + * {
60 clear: left;
63 div.sectionbody {
64 font-family: serif;
65 margin-left: 0;
68 hr {
69 border: 1px solid silver;
72 p {
73 margin-top: 0.5em;
74 margin-bottom: 0.5em;
77 ul, ol, li > p {
78 margin-top: 0;
81 pre {
82 padding: 0;
83 margin: 0;
86 span#author {
87 color: #527bbd;
88 font-family: sans-serif;
89 font-weight: bold;
90 font-size: 1.1em;
92 span#email {
94 span#revnumber, span#revdate, span#revremark {
95 font-family: sans-serif;
98 div#footer {
99 font-family: sans-serif;
100 font-size: small;
101 border-top: 2px solid silver;
102 padding-top: 0.5em;
103 margin-top: 4.0em;
105 div#footer-text {
106 float: left;
107 padding-bottom: 0.5em;
109 div#footer-badges {
110 float: right;
111 padding-bottom: 0.5em;
114 div#preamble {
115 margin-top: 1.5em;
116 margin-bottom: 1.5em;
118 div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
119 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
120 div.admonitionblock {
121 margin-top: 1.5em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.5em;
126 margin-bottom: 2.5em;
129 div.content { /* Block element content. */
130 padding: 0;
133 /* Block element titles. */
134 div.title, caption.title {
135 color: #527bbd;
136 font-family: sans-serif;
137 font-weight: bold;
138 text-align: left;
139 margin-top: 1.0em;
140 margin-bottom: 0.5em;
142 div.title + * {
143 margin-top: 0;
146 td div.title:first-child {
147 margin-top: 0.0em;
149 div.content div.title:first-child {
150 margin-top: 0.0em;
152 div.content + div.title {
153 margin-top: 0.0em;
156 div.sidebarblock > div.content {
157 background: #ffffee;
158 border: 1px solid silver;
159 padding: 0.5em;
162 div.listingblock > div.content {
163 border: 1px solid silver;
164 background: #f4f4f4;
165 padding: 0.5em;
168 div.quoteblock {
169 padding-left: 2.0em;
170 margin-right: 10%;
172 div.quoteblock > div.attribution {
173 padding-top: 0.5em;
174 text-align: right;
177 div.verseblock {
178 padding-left: 2.0em;
179 margin-right: 10%;
181 div.verseblock > div.content {
182 white-space: pre;
184 div.verseblock > div.attribution {
185 padding-top: 0.75em;
186 text-align: left;
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div.verseblock + div.attribution {
190 text-align: left;
193 div.admonitionblock .icon {
194 vertical-align: top;
195 font-size: 1.1em;
196 font-weight: bold;
197 text-decoration: underline;
198 color: #527bbd;
199 padding-right: 0.5em;
201 div.admonitionblock td.content {
202 padding-left: 0.5em;
203 border-left: 2px solid silver;
206 div.exampleblock > div.content {
207 border-left: 2px solid silver;
208 padding: 0.5em;
211 div.imageblock div.content { padding-left: 0; }
212 span.image img { border-style: none; }
213 a.image:visited { color: white; }
215 dl {
216 margin-top: 0.8em;
217 margin-bottom: 0.8em;
219 dt {
220 margin-top: 0.5em;
221 margin-bottom: 0;
222 font-style: normal;
223 color: navy;
225 dd > *:first-child {
226 margin-top: 0.1em;
229 ul, ol {
230 list-style-position: outside;
232 ol.arabic {
233 list-style-type: decimal;
235 ol.loweralpha {
236 list-style-type: lower-alpha;
238 ol.upperalpha {
239 list-style-type: upper-alpha;
241 ol.lowerroman {
242 list-style-type: lower-roman;
244 ol.upperroman {
245 list-style-type: upper-roman;
248 div.compact ul, div.compact ol,
249 div.compact p, div.compact p,
250 div.compact div, div.compact div {
251 margin-top: 0.1em;
252 margin-bottom: 0.1em;
255 div.tableblock > table {
256 border: 3px solid #527bbd;
258 thead {
259 font-family: sans-serif;
260 font-weight: bold;
262 tfoot {
263 font-weight: bold;
265 td > div.verse {
266 white-space: pre;
268 p.table {
269 margin-top: 0;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div.tableblock > table[frame="void"] {
273 border-style: none;
275 div.tableblock > table[frame="hsides"] {
276 border-left-style: none;
277 border-right-style: none;
279 div.tableblock > table[frame="vsides"] {
280 border-top-style: none;
281 border-bottom-style: none;
285 div.hdlist {
286 margin-top: 0.8em;
287 margin-bottom: 0.8em;
289 div.hdlist tr {
290 padding-bottom: 15px;
292 dt.hdlist1.strong, td.hdlist1.strong {
293 font-weight: bold;
295 td.hdlist1 {
296 vertical-align: top;
297 font-style: normal;
298 padding-right: 0.8em;
299 color: navy;
301 td.hdlist2 {
302 vertical-align: top;
304 div.hdlist.compact tr {
305 margin: 0;
306 padding-bottom: 0;
309 .comment {
310 background: yellow;
313 @media print {
314 div#footer-badges { display: none; }
317 div#toctitle {
318 color: #527bbd;
319 font-family: sans-serif;
320 font-size: 1.1em;
321 font-weight: bold;
322 margin-top: 1.0em;
323 margin-bottom: 0.1em;
326 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
327 margin-top: 0;
328 margin-bottom: 0;
330 div.toclevel2 {
331 margin-left: 2em;
332 font-size: 0.9em;
334 div.toclevel3 {
335 margin-left: 4em;
336 font-size: 0.9em;
338 div.toclevel4 {
339 margin-left: 6em;
340 font-size: 0.9em;
342 /* Workarounds for IE6's broken and incomplete CSS2. */
344 div.sidebar-content {
345 background: #ffffee;
346 border: 1px solid silver;
347 padding: 0.5em;
349 div.sidebar-title, div.image-title {
350 color: #527bbd;
351 font-family: sans-serif;
352 font-weight: bold;
353 margin-top: 0.0em;
354 margin-bottom: 0.5em;
357 div.listingblock div.content {
358 border: 1px solid silver;
359 background: #f4f4f4;
360 padding: 0.5em;
363 div.quoteblock-attribution {
364 padding-top: 0.5em;
365 text-align: right;
368 div.verseblock-content {
369 white-space: pre;
371 div.verseblock-attribution {
372 padding-top: 0.75em;
373 text-align: left;
376 div.exampleblock-content {
377 border-left: 2px solid silver;
378 padding-left: 0.5em;
381 /* IE6 sets dynamically generated links as visited. */
382 div#toc a:visited { color: blue; }
383 </style>
384 </head>
385 <body>
386 <div id="header">
387 <h1>Fighting regressions with git bisect</h1>
388 <span id="author">Christian Couder</span><br />
389 <span id="email"><tt>&lt;<a href="mailto:chriscool@tuxfamily.org">chriscool@tuxfamily.org</a>&gt;</tt></span><br />
390 <span id="revdate">2009/11/08</span>
391 </div>
392 <h2 id="_abstract">Abstract</h2>
393 <div class="sectionbody">
394 <div class="paragraph"><p>"git bisect" enables software users and developers to easily find the
395 commit that introduced a regression. We show why it is important to
396 have good tools to fight regressions. We describe how "git bisect"
397 works from the outside and the algorithms it uses inside. Then we
398 explain how to take advantage of "git bisect" to improve current
399 practices. And we discuss how "git bisect" could improve in the
400 future.</p></div>
401 </div>
402 <h2 id="_introduction_to_git_bisect">Introduction to "git bisect"</h2>
403 <div class="sectionbody">
404 <div class="paragraph"><p>Git is a Distributed Version Control system (DVCS) created by Linus
405 Torvalds and maintained by Junio Hamano.</p></div>
406 <div class="paragraph"><p>In Git like in many other Version Control Systems (VCS), the different
407 states of the data that is managed by the system are called
408 commits. And, as VCS are mostly used to manage software source code,
409 sometimes "interesting" changes of behavior in the software are
410 introduced in some commits.</p></div>
411 <div class="paragraph"><p>In fact people are specially interested in commits that introduce a
412 "bad" behavior, called a bug or a regression. They are interested in
413 these commits because a commit (hopefully) contains a very small set
414 of source code changes. And it&#8217;s much easier to understand and
415 properly fix a problem when you only need to check a very small set of
416 changes, than when you don&#8217;t know where look in the first place.</p></div>
417 <div class="paragraph"><p>So to help people find commits that introduce a "bad" behavior, the
418 "git bisect" set of commands was invented. And it follows of course
419 that in "git bisect" parlance, commits where the "interesting
420 behavior" is present are called "bad" commits, while other commits are
421 called "good" commits. And a commit that introduce the behavior we are
422 interested in is called a "first bad commit". Note that there could be
423 more than one "first bad commit" in the commit space we are searching.</p></div>
424 <div class="paragraph"><p>So "git bisect" is designed to help find a "first bad commit". And to
425 be as efficient as possible, it tries to perform a binary search.</p></div>
426 </div>
427 <h2 id="_fighting_regressions_overview">Fighting regressions overview</h2>
428 <div class="sectionbody">
429 <h3 id="_regressions_a_big_problem">Regressions: a big problem</h3><div style="clear:left"></div>
430 <div class="paragraph"><p>Regressions are a big problem in the software industry. But it&#8217;s
431 difficult to put some real numbers behind that claim.</p></div>
432 <div class="paragraph"><p>There are some numbers about bugs in general, like a NIST study in
433 2002 <a href="#1">[1]</a> that said:</p></div>
434 <div class="quoteblock">
435 <div class="quoteblock-content">
436 <div class="paragraph"><p>Software bugs, or errors, are so prevalent and so detrimental that
437 they cost the U.S. economy an estimated $59.5 billion annually, or
438 about 0.6 percent of the gross domestic product, according to a newly
439 released study commissioned by the Department of Commerce&#8217;s National
440 Institute of Standards and Technology (NIST). At the national level,
441 over half of the costs are borne by software users and the remainder
442 by software developers/vendors. The study also found that, although
443 all errors cannot be removed, more than a third of these costs, or an
444 estimated $22.2 billion, could be eliminated by an improved testing
445 infrastructure that enables earlier and more effective identification
446 and removal of software defects. These are the savings associated with
447 finding an increased percentage (but not 100 percent) of errors closer
448 to the development stages in which they are introduced. Currently,
449 over half of all errors are not found until "downstream" in the
450 development process or during post-sale software use.</p></div>
451 </div>
452 <div class="quoteblock-attribution">
453 </div></div>
454 <div class="paragraph"><p>And then:</p></div>
455 <div class="quoteblock">
456 <div class="quoteblock-content">
457 <div class="paragraph"><p>Software developers already spend approximately 80 percent of
458 development costs on identifying and correcting defects, and yet few
459 products of any type other than software are shipped with such high
460 levels of errors.</p></div>
461 </div>
462 <div class="quoteblock-attribution">
463 </div></div>
464 <div class="paragraph"><p>Eventually the conclusion started with:</p></div>
465 <div class="quoteblock">
466 <div class="quoteblock-content">
467 <div class="paragraph"><p>The path to higher software quality is significantly improved software
468 testing.</p></div>
469 </div>
470 <div class="quoteblock-attribution">
471 </div></div>
472 <div class="paragraph"><p>There are other estimates saying that 80% of the cost related to
473 software is about maintenance <a href="#2">[2]</a>.</p></div>
474 <div class="paragraph"><p>Though, according to Wikipedia <a href="#3">[3]</a>:</p></div>
475 <div class="quoteblock">
476 <div class="quoteblock-content">
477 <div class="paragraph"><p>A common perception of maintenance is that it is merely fixing
478 bugs. However, studies and surveys over the years have indicated that
479 the majority, over 80%, of the maintenance effort is used for
480 non-corrective actions (Pigosky 1997). This perception is perpetuated
481 by users submitting problem reports that in reality are functionality
482 enhancements to the system.</p></div>
483 </div>
484 <div class="quoteblock-attribution">
485 </div></div>
486 <div class="paragraph"><p>But we can guess that improving on existing software is very costly
487 because you have to watch out for regressions. At least this would
488 make the above studies consistent among themselves.</p></div>
489 <div class="paragraph"><p>Of course some kind of software is developed, then used during some
490 time without being improved on much, and then finally thrown away. In
491 this case, of course, regressions may not be a big problem. But on the
492 other hand, there is a lot of big software that is continually
493 developed and maintained during years or even tens of years by a lot
494 of people. And as there are often many people who depend (sometimes
495 critically) on such software, regressions are a really big problem.</p></div>
496 <div class="paragraph"><p>One such software is the linux kernel. And if we look at the linux
497 kernel, we can see that a lot of time and effort is spent to fight
498 regressions. The release cycle start with a 2 weeks long merge
499 window. Then the first release candidate (rc) version is tagged. And
500 after that about 7 or 8 more rc versions will appear with around one
501 week between each of them, before the final release.</p></div>
502 <div class="paragraph"><p>The time between the first rc release and the final release is
503 supposed to be used to test rc versions and fight bugs and especially
504 regressions. And this time is more than 80% of the release cycle
505 time. But this is not the end of the fight yet, as of course it
506 continues after the release.</p></div>
507 <div class="paragraph"><p>And then this is what Ingo Molnar (a well known linux kernel
508 developer) says about his use of git bisect:</p></div>
509 <div class="quoteblock">
510 <div class="quoteblock-content">
511 <div class="paragraph"><p>I most actively use it during the merge window (when a lot of trees
512 get merged upstream and when the influx of bugs is the highest) - and
513 yes, there have been cases that i used it multiple times a day. My
514 average is roughly once a day.</p></div>
515 </div>
516 <div class="quoteblock-attribution">
517 </div></div>
518 <div class="paragraph"><p>So regressions are fought all the time by developers, and indeed it is
519 well known that bugs should be fixed as soon as possible, so as soon
520 as they are found. That&#8217;s why it is interesting to have good tools for
521 this purpose.</p></div>
522 <h3 id="_other_tools_to_fight_regressions">Other tools to fight regressions</h3><div style="clear:left"></div>
523 <div class="paragraph"><p>So what are the tools used to fight regressions? They are nearly the
524 same as those used to fight regular bugs. The only specific tools are
525 test suites and tools similar as "git bisect".</p></div>
526 <div class="paragraph"><p>Test suites are very nice. But when they are used alone, they are
527 supposed to be used so that all the tests are checked after each
528 commit. This means that they are not very efficient, because many
529 tests are run for no interesting result, and they suffer from
530 combinational explosion.</p></div>
531 <div class="paragraph"><p>In fact the problem is that big software often has many different
532 configuration options and that each test case should pass for each
533 configuration after each commit. So if you have for each release: N
534 configurations, M commits and T test cases, you should perform:</p></div>
535 <div class="listingblock">
536 <div class="content">
537 <pre><tt>N * M * T tests</tt></pre>
538 </div></div>
539 <div class="paragraph"><p>where N, M and T are all growing with the size your software.</p></div>
540 <div class="paragraph"><p>So very soon it will not be possible to completely test everything.</p></div>
541 <div class="paragraph"><p>And if some bugs slip through your test suite, then you can add a test
542 to your test suite. But if you want to use your new improved test
543 suite to find where the bug slipped in, then you will either have to
544 emulate a bisection process or you will perhaps bluntly test each
545 commit backward starting from the "bad" commit you have which may be
546 very wasteful.</p></div>
547 </div>
548 <h2 id="_git_bisect_overview">"git bisect" overview</h2>
549 <div class="sectionbody">
550 <h3 id="_starting_a_bisection">Starting a bisection</h3><div style="clear:left"></div>
551 <div class="paragraph"><p>The first "git bisect" subcommand to use is "git bisect start" to
552 start the search. Then bounds must be set to limit the commit
553 space. This is done usually by giving one "bad" and at least one
554 "good" commit. They can be passed in the initial call to "git bisect
555 start" like this:</p></div>
556 <div class="listingblock">
557 <div class="content">
558 <pre><tt>$ git bisect start [BAD [GOOD...]]</tt></pre>
559 </div></div>
560 <div class="paragraph"><p>or they can be set using:</p></div>
561 <div class="listingblock">
562 <div class="content">
563 <pre><tt>$ git bisect bad [COMMIT]</tt></pre>
564 </div></div>
565 <div class="paragraph"><p>and:</p></div>
566 <div class="listingblock">
567 <div class="content">
568 <pre><tt>$ git bisect good [COMMIT...]</tt></pre>
569 </div></div>
570 <div class="paragraph"><p>where BAD, GOOD and COMMIT are all names that can be resolved to a
571 commit.</p></div>
572 <div class="paragraph"><p>Then "git bisect" will checkout a commit of its choosing and ask the
573 user to test it, like this:</p></div>
574 <div class="listingblock">
575 <div class="content">
576 <pre><tt>$ git bisect start v2.6.27 v2.6.25
577 Bisecting: 10928 revisions left to test after this (roughly 14 steps)
578 [2ec65f8b89ea003c27ff7723525a2ee335a2b393] x86: clean up using max_low_pfn on 32-bit</tt></pre>
579 </div></div>
580 <div class="paragraph"><p>Note that the example that we will use is really a toy example, we
581 will be looking for the first commit that has a version like
582 "2.6.26-something", that is the commit that has a "SUBLEVEL = 26" line
583 in the top level Makefile. This is a toy example because there are
584 better ways to find this commit with git than using "git bisect" (for
585 example "git blame" or "git log -S&lt;string&gt;").</p></div>
586 <h3 id="_driving_a_bisection_manually">Driving a bisection manually</h3><div style="clear:left"></div>
587 <div class="paragraph"><p>At this point there are basically 2 ways to drive the search. It can
588 be driven manually by the user or it can be driven automatically by a
589 script or a command.</p></div>
590 <div class="paragraph"><p>If the user is driving it, then at each step of the search, the user
591 will have to test the current commit and say if it is "good" or "bad"
592 using the "git bisect good" or "git bisect bad" commands respectively
593 that have been described above. For example:</p></div>
594 <div class="listingblock">
595 <div class="content">
596 <pre><tt>$ git bisect bad
597 Bisecting: 5480 revisions left to test after this (roughly 13 steps)
598 [66c0b394f08fd89236515c1c84485ea712a157be] KVM: kill file-&gt;f_count abuse in kvm</tt></pre>
599 </div></div>
600 <div class="paragraph"><p>And after a few more steps like that, "git bisect" will eventually
601 find a first bad commit:</p></div>
602 <div class="listingblock">
603 <div class="content">
604 <pre><tt>$ git bisect bad
605 2ddcca36c8bcfa251724fe342c8327451988be0d is the first bad commit
606 commit 2ddcca36c8bcfa251724fe342c8327451988be0d
607 Author: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
608 Date: Sat May 3 11:59:44 2008 -0700
610 Linux 2.6.26-rc1
612 :100644 100644 5cf8258195331a4dbdddff08b8d68642638eea57 4492984efc09ab72ff6219a7bc21fb6a957c4cd5 M Makefile</tt></pre>
613 </div></div>
614 <div class="paragraph"><p>At this point we can see what the commit does, check it out (if it&#8217;s
615 not already checked out) or tinker with it, for example:</p></div>
616 <div class="listingblock">
617 <div class="content">
618 <pre><tt>$ git show HEAD
619 commit 2ddcca36c8bcfa251724fe342c8327451988be0d
620 Author: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
621 Date: Sat May 3 11:59:44 2008 -0700
623 Linux 2.6.26-rc1
625 diff --git a/Makefile b/Makefile
626 index 5cf8258..4492984 100644
627 --- a/Makefile
628 +++ b/Makefile
629 @@ -1,7 +1,7 @@
630 VERSION = 2
631 PATCHLEVEL = 6
632 -SUBLEVEL = 25
633 -EXTRAVERSION =
634 +SUBLEVEL = 26
635 +EXTRAVERSION = -rc1
636 NAME = Funky Weasel is Jiggy wit it
638 # *DOCUMENTATION*</tt></pre>
639 </div></div>
640 <div class="paragraph"><p>And when we are finished we can use "git bisect reset" to go back to
641 the branch we were in before we started bisecting:</p></div>
642 <div class="listingblock">
643 <div class="content">
644 <pre><tt>$ git bisect reset
645 Checking out files: 100% (21549/21549), done.
646 Previous HEAD position was 2ddcca3... Linux 2.6.26-rc1
647 Switched to branch 'master'</tt></pre>
648 </div></div>
649 <h3 id="_driving_a_bisection_automatically">Driving a bisection automatically</h3><div style="clear:left"></div>
650 <div class="paragraph"><p>The other way to drive the bisection process is to tell "git bisect"
651 to launch a script or command at each bisection step to know if the
652 current commit is "good" or "bad". To do that, we use the "git bisect
653 run" command. For example:</p></div>
654 <div class="listingblock">
655 <div class="content">
656 <pre><tt>$ git bisect start v2.6.27 v2.6.25
657 Bisecting: 10928 revisions left to test after this (roughly 14 steps)
658 [2ec65f8b89ea003c27ff7723525a2ee335a2b393] x86: clean up using max_low_pfn on 32-bit
660 $ git bisect run grep '^SUBLEVEL = 25' Makefile
661 running grep ^SUBLEVEL = 25 Makefile
662 Bisecting: 5480 revisions left to test after this (roughly 13 steps)
663 [66c0b394f08fd89236515c1c84485ea712a157be] KVM: kill file-&gt;f_count abuse in kvm
664 running grep ^SUBLEVEL = 25 Makefile
665 SUBLEVEL = 25
666 Bisecting: 2740 revisions left to test after this (roughly 12 steps)
667 [671294719628f1671faefd4882764886f8ad08cb] V4L/DVB(7879): Adding cx18 Support for mxl5005s
670 running grep ^SUBLEVEL = 25 Makefile
671 Bisecting: 0 revisions left to test after this (roughly 0 steps)
672 [2ddcca36c8bcfa251724fe342c8327451988be0d] Linux 2.6.26-rc1
673 running grep ^SUBLEVEL = 25 Makefile
674 2ddcca36c8bcfa251724fe342c8327451988be0d is the first bad commit
675 commit 2ddcca36c8bcfa251724fe342c8327451988be0d
676 Author: Linus Torvalds &lt;torvalds@linux-foundation.org&gt;
677 Date: Sat May 3 11:59:44 2008 -0700
679 Linux 2.6.26-rc1
681 :100644 100644 5cf8258195331a4dbdddff08b8d68642638eea57 4492984efc09ab72ff6219a7bc21fb6a957c4cd5 M Makefile
682 bisect run success</tt></pre>
683 </div></div>
684 <div class="paragraph"><p>In this example, we passed "grep <em>^SUBLEVEL = 25</em> Makefile" as
685 parameter to "git bisect run". This means that at each step, the grep
686 command we passed will be launched. And if it exits with code 0 (that
687 means success) then git bisect will mark the current state as
688 "good". If it exits with code 1 (or any code between 1 and 127
689 included, except the special code 125), then the current state will be
690 marked as "bad".</p></div>
691 <div class="paragraph"><p>Exit code between 128 and 255 are special to "git bisect run". They
692 make it stop immediately the bisection process. This is useful for
693 example if the command passed takes too long to complete, because you
694 can kill it with a signal and it will stop the bisection process.</p></div>
695 <div class="paragraph"><p>It can also be useful in scripts passed to "git bisect run" to "exit
696 255" if some very abnormal situation is detected.</p></div>
697 <h3 id="_avoiding_untestable_commits">Avoiding untestable commits</h3><div style="clear:left"></div>
698 <div class="paragraph"><p>Sometimes it happens that the current state cannot be tested, for
699 example if it does not compile because there was a bug preventing it
700 at that time. This is what the special exit code 125 is for. It tells
701 "git bisect run" that the current commit should be marked as
702 untestable and that another one should be chosen and checked out.</p></div>
703 <div class="paragraph"><p>If the bisection process is driven manually, you can use "git bisect
704 skip" to do the same thing. (In fact the special exit code 125 makes
705 "git bisect run" use "git bisect skip" in the background.)</p></div>
706 <div class="paragraph"><p>Or if you want more control, you can inspect the current state using
707 for example "git bisect visualize". It will launch gitk (or "git log"
708 if the DISPLAY environment variable is not set) to help you find a
709 better bisection point.</p></div>
710 <div class="paragraph"><p>Either way, if you have a string of untestable commits, it might
711 happen that the regression you are looking for has been introduced by
712 one of these untestable commits. In this case it&#8217;s not possible to
713 tell for sure which commit introduced the regression.</p></div>
714 <div class="paragraph"><p>So if you used "git bisect skip" (or the run script exited with
715 special code 125) you could get a result like this:</p></div>
716 <div class="listingblock">
717 <div class="content">
718 <pre><tt>There are only 'skip'ped commits left to test.
719 The first bad commit could be any of:
720 15722f2fa328eaba97022898a305ffc8172db6b1
721 78e86cf3e850bd755bb71831f42e200626fbd1e0
722 e15b73ad3db9b48d7d1ade32f8cd23a751fe0ace
723 070eab2303024706f2924822bfec8b9847e4ac1b
724 We cannot bisect more!</tt></pre>
725 </div></div>
726 <h3 id="_saving_a_log_and_replaying_it">Saving a log and replaying it</h3><div style="clear:left"></div>
727 <div class="paragraph"><p>If you want to show other people your bisection process, you can get a
728 log using for example:</p></div>
729 <div class="listingblock">
730 <div class="content">
731 <pre><tt>$ git bisect log &gt; bisect_log.txt</tt></pre>
732 </div></div>
733 <div class="paragraph"><p>And it is possible to replay it using:</p></div>
734 <div class="listingblock">
735 <div class="content">
736 <pre><tt>$ git bisect replay bisect_log.txt</tt></pre>
737 </div></div>
738 </div>
739 <h2 id="_git_bisect_details">"git bisect" details</h2>
740 <div class="sectionbody">
741 <h3 id="_bisection_algorithm">Bisection algorithm</h3><div style="clear:left"></div>
742 <div class="paragraph"><p>As the Git commits form a directed acyclic graph (DAG), finding the
743 best bisection commit to test at each step is not so simple. Anyway
744 Linus found and implemented a "truly stupid" algorithm, later improved
745 by Junio Hamano, that works quite well.</p></div>
746 <div class="paragraph"><p>So the algorithm used by "git bisect" to find the best bisection
747 commit when there are no skipped commits is the following:</p></div>
748 <div class="paragraph"><p>1) keep only the commits that:</p></div>
749 <div class="paragraph"><p>a) are ancestor of the "bad" commit (including the "bad" commit itself),
750 b) are not ancestor of a "good" commit (excluding the "good" commits).</p></div>
751 <div class="paragraph"><p>This means that we get rid of the uninteresting commits in the DAG.</p></div>
752 <div class="paragraph"><p>For example if we start with a graph like this:</p></div>
753 <div class="listingblock">
754 <div class="content">
755 <pre><tt>G-Y-G-W-W-W-X-X-X-X
757 W-W-B
759 Y---G-W---W
760 \ / \
761 Y-Y X-X-X-X
763 -&gt; time goes this way -&gt;</tt></pre>
764 </div></div>
765 <div class="paragraph"><p>where B is the "bad" commit, "G" are "good" commits and W, X, and Y
766 are other commits, we will get the following graph after this first
767 step:</p></div>
768 <div class="listingblock">
769 <div class="content">
770 <pre><tt>W-W-W
772 W-W-B
774 W---W</tt></pre>
775 </div></div>
776 <div class="paragraph"><p>So only the W and B commits will be kept. Because commits X and Y will
777 have been removed by rules a) and b) respectively, and because commits
778 G are removed by rule b) too.</p></div>
779 <div class="paragraph"><p>Note for git users, that it is equivalent as keeping only the commit
780 given by:</p></div>
781 <div class="listingblock">
782 <div class="content">
783 <pre><tt>git rev-list BAD --not GOOD1 GOOD2...</tt></pre>
784 </div></div>
785 <div class="paragraph"><p>Also note that we don&#8217;t require the commits that are kept to be
786 descendants of a "good" commit. So in the following example, commits W
787 and Z will be kept:</p></div>
788 <div class="listingblock">
789 <div class="content">
790 <pre><tt>G-W-W-W-B
792 Z-Z</tt></pre>
793 </div></div>
794 <div class="paragraph"><p>2) starting from the "good" ends of the graph, associate to each
795 commit the number of ancestors it has plus one</p></div>
796 <div class="paragraph"><p>For example with the following graph where H is the "bad" commit and A
797 and D are some parents of some "good" commits:</p></div>
798 <div class="listingblock">
799 <div class="content">
800 <pre><tt>A-B-C
802 F-G-H
804 D---E</tt></pre>
805 </div></div>
806 <div class="paragraph"><p>this will give:</p></div>
807 <div class="listingblock">
808 <div class="content">
809 <pre><tt>1 2 3
810 A-B-C
811 \6 7 8
812 F-G-H
813 1 2/
814 D---E</tt></pre>
815 </div></div>
816 <div class="paragraph"><p>3) associate to each commit: min(X, N - X)</p></div>
817 <div class="paragraph"><p>where X is the value associated to the commit in step 2) and N is the
818 total number of commits in the graph.</p></div>
819 <div class="paragraph"><p>In the above example we have N = 8, so this will give:</p></div>
820 <div class="listingblock">
821 <div class="content">
822 <pre><tt>1 2 3
823 A-B-C
824 \2 1 0
825 F-G-H
826 1 2/
827 D---E</tt></pre>
828 </div></div>
829 <div class="paragraph"><p>4) the best bisection point is the commit with the highest associated
830 number</p></div>
831 <div class="paragraph"><p>So in the above example the best bisection point is commit C.</p></div>
832 <div class="paragraph"><p>5) note that some shortcuts are implemented to speed up the algorithm</p></div>
833 <div class="paragraph"><p>As we know N from the beginning, we know that min(X, N - X) can&#8217;t be
834 greater than N/2. So during steps 2) and 3), if we would associate N/2
835 to a commit, then we know this is the best bisection point. So in this
836 case we can just stop processing any other commit and return the
837 current commit.</p></div>
838 <h3 id="_bisection_algorithm_debugging">Bisection algorithm debugging</h3><div style="clear:left"></div>
839 <div class="paragraph"><p>For any commit graph, you can see the number associated with each
840 commit using "git rev-list --bisect-all".</p></div>
841 <div class="paragraph"><p>For example, for the above graph, a command like:</p></div>
842 <div class="listingblock">
843 <div class="content">
844 <pre><tt>$ git rev-list --bisect-all BAD --not GOOD1 GOOD2</tt></pre>
845 </div></div>
846 <div class="paragraph"><p>would output something like:</p></div>
847 <div class="listingblock">
848 <div class="content">
849 <pre><tt>e15b73ad3db9b48d7d1ade32f8cd23a751fe0ace (dist=3)
850 15722f2fa328eaba97022898a305ffc8172db6b1 (dist=2)
851 78e86cf3e850bd755bb71831f42e200626fbd1e0 (dist=2)
852 a1939d9a142de972094af4dde9a544e577ddef0e (dist=2)
853 070eab2303024706f2924822bfec8b9847e4ac1b (dist=1)
854 a3864d4f32a3bf5ed177ddef598490a08760b70d (dist=1)
855 a41baa717dd74f1180abf55e9341bc7a0bb9d556 (dist=1)
856 9e622a6dad403b71c40979743bb9d5be17b16bd6 (dist=0)</tt></pre>
857 </div></div>
858 <h3 id="_bisection_algorithm_discussed">Bisection algorithm discussed</h3><div style="clear:left"></div>
859 <div class="paragraph"><p>First let&#8217;s define "best bisection point". We will say that a commit X
860 is a best bisection point or a best bisection commit if knowing its
861 state ("good" or "bad") gives as much information as possible whether
862 the state of the commit happens to be "good" or "bad".</p></div>
863 <div class="paragraph"><p>This means that the best bisection commits are the commits where the
864 following function is maximum:</p></div>
865 <div class="listingblock">
866 <div class="content">
867 <pre><tt>f(X) = min(information_if_good(X), information_if_bad(X))</tt></pre>
868 </div></div>
869 <div class="paragraph"><p>where information_if_good(X) is the information we get if X is good
870 and information_if_bad(X) is the information we get if X is bad.</p></div>
871 <div class="paragraph"><p>Now we will suppose that there is only one "first bad commit". This
872 means that all its descendants are "bad" and all the other commits are
873 "good". And we will suppose that all commits have an equal probability
874 of being good or bad, or of being the first bad commit, so knowing the
875 state of c commits gives always the same amount of information
876 wherever these c commits are on the graph and whatever c is. (So we
877 suppose that these commits being for example on a branch or near a
878 good or a bad commit does not give more or less information).</p></div>
879 <div class="paragraph"><p>Let&#8217;s also suppose that we have a cleaned up graph like one after step
880 1) in the bisection algorithm above. This means that we can measure
881 the information we get in terms of number of commit we can remove from
882 the graph..</p></div>
883 <div class="paragraph"><p>And let&#8217;s take a commit X in the graph.</p></div>
884 <div class="paragraph"><p>If X is found to be "good", then we know that its ancestors are all
885 "good", so we want to say that:</p></div>
886 <div class="listingblock">
887 <div class="content">
888 <pre><tt>information_if_good(X) = number_of_ancestors(X) (TRUE)</tt></pre>
889 </div></div>
890 <div class="paragraph"><p>And this is true because at step 1) b) we remove the ancestors of the
891 "good" commits.</p></div>
892 <div class="paragraph"><p>If X is found to be "bad", then we know that its descendants are all
893 "bad", so we want to say that:</p></div>
894 <div class="listingblock">
895 <div class="content">
896 <pre><tt>information_if_bad(X) = number_of_descendants(X) (WRONG)</tt></pre>
897 </div></div>
898 <div class="paragraph"><p>But this is wrong because at step 1) a) we keep only the ancestors of
899 the bad commit. So we get more information when a commit is marked as
900 "bad", because we also know that the ancestors of the previous "bad"
901 commit that are not ancestors of the new "bad" commit are not the
902 first bad commit. We don&#8217;t know if they are good or bad, but we know
903 that they are not the first bad commit because they are not ancestor
904 of the new "bad" commit.</p></div>
905 <div class="paragraph"><p>So when a commit is marked as "bad" we know we can remove all the
906 commits in the graph except those that are ancestors of the new "bad"
907 commit. This means that:</p></div>
908 <div class="listingblock">
909 <div class="content">
910 <pre><tt>information_if_bad(X) = N - number_of_ancestors(X) (TRUE)</tt></pre>
911 </div></div>
912 <div class="paragraph"><p>where N is the number of commits in the (cleaned up) graph.</p></div>
913 <div class="paragraph"><p>So in the end this means that to find the best bisection commits we
914 should maximize the function:</p></div>
915 <div class="listingblock">
916 <div class="content">
917 <pre><tt>f(X) = min(number_of_ancestors(X), N - number_of_ancestors(X))</tt></pre>
918 </div></div>
919 <div class="paragraph"><p>And this is nice because at step 2) we compute number_of_ancestors(X)
920 and so at step 3) we compute f(X).</p></div>
921 <div class="paragraph"><p>Let&#8217;s take the following graph as an example:</p></div>
922 <div class="listingblock">
923 <div class="content">
924 <pre><tt> G-H-I-J
926 A-B-C-D-E-F O
928 K-L-M-N</tt></pre>
929 </div></div>
930 <div class="paragraph"><p>If we compute the following non optimal function on it:</p></div>
931 <div class="listingblock">
932 <div class="content">
933 <pre><tt>g(X) = min(number_of_ancestors(X), number_of_descendants(X))</tt></pre>
934 </div></div>
935 <div class="paragraph"><p>we get:</p></div>
936 <div class="listingblock">
937 <div class="content">
938 <pre><tt> 4 3 2 1
939 G-H-I-J
940 1 2 3 4 5 6/ \0
941 A-B-C-D-E-F O
943 K-L-M-N
944 4 3 2 1</tt></pre>
945 </div></div>
946 <div class="paragraph"><p>but with the algorithm used by git bisect we get:</p></div>
947 <div class="listingblock">
948 <div class="content">
949 <pre><tt> 7 7 6 5
950 G-H-I-J
951 1 2 3 4 5 6/ \0
952 A-B-C-D-E-F O
954 K-L-M-N
955 7 7 6 5</tt></pre>
956 </div></div>
957 <div class="paragraph"><p>So we chose G, H, K or L as the best bisection point, which is better
958 than F. Because if for example L is bad, then we will know not only
959 that L, M and N are bad but also that G, H, I and J are not the first
960 bad commit (since we suppose that there is only one first bad commit
961 and it must be an ancestor of L).</p></div>
962 <div class="paragraph"><p>So the current algorithm seems to be the best possible given what we
963 initially supposed.</p></div>
964 <h3 id="_skip_algorithm">Skip algorithm</h3><div style="clear:left"></div>
965 <div class="paragraph"><p>When some commits have been skipped (using "git bisect skip"), then
966 the bisection algorithm is the same for step 1) to 3). But then we use
967 roughly the following steps:</p></div>
968 <div class="paragraph"><p>6) sort the commit by decreasing associated value</p></div>
969 <div class="paragraph"><p>7) if the first commit has not been skipped, we can return it and stop
970 here</p></div>
971 <div class="paragraph"><p>8) otherwise filter out all the skipped commits in the sorted list</p></div>
972 <div class="paragraph"><p>9) use a pseudo random number generator (PRNG) to generate a random
973 number between 0 and 1</p></div>
974 <div class="paragraph"><p>10) multiply this random number with its square root to bias it toward
975 0</p></div>
976 <div class="paragraph"><p>11) multiply the result by the number of commits in the filtered list
977 to get an index into this list</p></div>
978 <div class="paragraph"><p>12) return the commit at the computed index</p></div>
979 <h3 id="_skip_algorithm_discussed">Skip algorithm discussed</h3><div style="clear:left"></div>
980 <div class="paragraph"><p>After step 7) (in the skip algorithm), we could check if the second
981 commit has been skipped and return it if it is not the case. And in
982 fact that was the algorithm we used from when "git bisect skip" was
983 developed in git version 1.5.4 (released on February 1st 2008) until
984 git version 1.6.4 (released July 29th 2009).</p></div>
985 <div class="paragraph"><p>But Ingo Molnar and H. Peter Anvin (another well known linux kernel
986 developer) both complained that sometimes the best bisection points
987 all happened to be in an area where all the commits are
988 untestable. And in this case the user was asked to test many
989 untestable commits, which could be very inefficient.</p></div>
990 <div class="paragraph"><p>Indeed untestable commits are often untestable because a breakage was
991 introduced at one time, and that breakage was fixed only after many
992 other commits were introduced.</p></div>
993 <div class="paragraph"><p>This breakage is of course most of the time unrelated to the breakage
994 we are trying to locate in the commit graph. But it prevents us to
995 know if the interesting "bad behavior" is present or not.</p></div>
996 <div class="paragraph"><p>So it is a fact that commits near an untestable commit have a high
997 probability of being untestable themselves. And the best bisection
998 commits are often found together too (due to the bisection algorithm).</p></div>
999 <div class="paragraph"><p>This is why it is a bad idea to just chose the next best unskipped
1000 bisection commit when the first one has been skipped.</p></div>
1001 <div class="paragraph"><p>We found that most commits on the graph may give quite a lot of
1002 information when they are tested. And the commits that will not on
1003 average give a lot of information are the one near the good and bad
1004 commits.</p></div>
1005 <div class="paragraph"><p>So using a PRNG with a bias to favor commits away from the good and
1006 bad commits looked like a good choice.</p></div>
1007 <div class="paragraph"><p>One obvious improvement to this algorithm would be to look for a
1008 commit that has an associated value near the one of the best bisection
1009 commit, and that is on another branch, before using the PRNG. Because
1010 if such a commit exists, then it is not very likely to be untestable
1011 too, so it will probably give more information than a nearly randomly
1012 chosen one.</p></div>
1013 <h3 id="_checking_merge_bases">Checking merge bases</h3><div style="clear:left"></div>
1014 <div class="paragraph"><p>There is another tweak in the bisection algorithm that has not been
1015 described in the "bisection algorithm" above.</p></div>
1016 <div class="paragraph"><p>We supposed in the previous examples that the "good" commits were
1017 ancestors of the "bad" commit. But this is not a requirement of "git
1018 bisect".</p></div>
1019 <div class="paragraph"><p>Of course the "bad" commit cannot be an ancestor of a "good" commit,
1020 because the ancestors of the good commits are supposed to be
1021 "good". And all the "good" commits must be related to the bad commit.
1022 They cannot be on a branch that has no link with the branch of the
1023 "bad" commit. But it is possible for a good commit to be related to a
1024 bad commit and yet not be neither one of its ancestor nor one of its
1025 descendants.</p></div>
1026 <div class="paragraph"><p>For example, there can be a "main" branch, and a "dev" branch that was
1027 forked of the main branch at a commit named "D" like this:</p></div>
1028 <div class="listingblock">
1029 <div class="content">
1030 <pre><tt>A-B-C-D-E-F-G &lt;--main
1032 H-I-J &lt;--dev</tt></pre>
1033 </div></div>
1034 <div class="paragraph"><p>The commit "D" is called a "merge base" for branch "main" and "dev"
1035 because it&#8217;s the best common ancestor for these branches for a merge.</p></div>
1036 <div class="paragraph"><p>Now let&#8217;s suppose that commit J is bad and commit G is good and that
1037 we apply the bisection algorithm like it has been previously
1038 described.</p></div>
1039 <div class="paragraph"><p>As described in step 1) b) of the bisection algorithm, we remove all
1040 the ancestors of the good commits because they are supposed to be good
1041 too.</p></div>
1042 <div class="paragraph"><p>So we would be left with only:</p></div>
1043 <div class="listingblock">
1044 <div class="content">
1045 <pre><tt>H-I-J</tt></pre>
1046 </div></div>
1047 <div class="paragraph"><p>But what happens if the first bad commit is "B" and if it has been
1048 fixed in the "main" branch by commit "F"?</p></div>
1049 <div class="paragraph"><p>The result of such a bisection would be that we would find that H is
1050 the first bad commit, when in fact it&#8217;s B. So that would be wrong!</p></div>
1051 <div class="paragraph"><p>And yes it can happen in practice that people working on one branch
1052 are not aware that people working on another branch fixed a bug! It
1053 could also happen that F fixed more than one bug or that it is a
1054 revert of some big development effort that was not ready to be
1055 released.</p></div>
1056 <div class="paragraph"><p>In fact development teams often maintain both a development branch and
1057 a maintenance branch, and it would be quite easy for them if "git
1058 bisect" just worked when they want to bisect a regression on the
1059 development branch that is not on the maintenance branch. They should
1060 be able to start bisecting using:</p></div>
1061 <div class="listingblock">
1062 <div class="content">
1063 <pre><tt>$ git bisect start dev main</tt></pre>
1064 </div></div>
1065 <div class="paragraph"><p>To enable that additional nice feature, when a bisection is started
1066 and when some good commits are not ancestors of the bad commit, we
1067 first compute the merge bases between the bad and the good commits and
1068 we chose these merge bases as the first commits that will be checked
1069 out and tested.</p></div>
1070 <div class="paragraph"><p>If it happens that one merge base is bad, then the bisection process
1071 is stopped with a message like:</p></div>
1072 <div class="listingblock">
1073 <div class="content">
1074 <pre><tt>The merge base BBBBBB is bad.
1075 This means the bug has been fixed between BBBBBB and [GGGGGG,...].</tt></pre>
1076 </div></div>
1077 <div class="paragraph"><p>where BBBBBB is the sha1 hash of the bad merge base and [GGGGGG,&#8230;]
1078 is a comma separated list of the sha1 of the good commits.</p></div>
1079 <div class="paragraph"><p>If some of the merge bases are skipped, then the bisection process
1080 continues, but the following message is printed for each skipped merge
1081 base:</p></div>
1082 <div class="listingblock">
1083 <div class="content">
1084 <pre><tt>Warning: the merge base between BBBBBB and [GGGGGG,...] must be skipped.
1085 So we cannot be sure the first bad commit is between MMMMMM and BBBBBB.
1086 We continue anyway.</tt></pre>
1087 </div></div>
1088 <div class="paragraph"><p>where BBBBBB is the sha1 hash of the bad commit, MMMMMM is the sha1
1089 hash of the merge base that is skipped and [GGGGGG,&#8230;] is a comma
1090 separated list of the sha1 of the good commits.</p></div>
1091 <div class="paragraph"><p>So if there is no bad merge base, the bisection process continues as
1092 usual after this step.</p></div>
1093 </div>
1094 <h2 id="_best_bisecting_practices">Best bisecting practices</h2>
1095 <div class="sectionbody">
1096 <h3 id="_using_test_suites_and_git_bisect_together">Using test suites and git bisect together</h3><div style="clear:left"></div>
1097 <div class="paragraph"><p>If you both have a test suite and use git bisect, then it becomes less
1098 important to check that all tests pass after each commit. Though of
1099 course it is probably a good idea to have some checks to avoid
1100 breaking too many things because it could make bisecting other bugs
1101 more difficult.</p></div>
1102 <div class="paragraph"><p>You can focus your efforts to check at a few points (for example rc
1103 and beta releases) that all the T test cases pass for all the N
1104 configurations. And when some tests don&#8217;t pass you can use "git
1105 bisect" (or better "git bisect run"). So you should perform roughly:</p></div>
1106 <div class="listingblock">
1107 <div class="content">
1108 <pre><tt>c * N * T + b * M * log2(M) tests</tt></pre>
1109 </div></div>
1110 <div class="paragraph"><p>where c is the number of rounds of test (so a small constant) and b is
1111 the ratio of bug per commit (hopefully a small constant too).</p></div>
1112 <div class="paragraph"><p>So of course it&#8217;s much better as it&#8217;s O(N * T) vs O(N * T * M) if
1113 you would test everything after each commit.</p></div>
1114 <div class="paragraph"><p>This means that test suites are good to prevent some bugs from being
1115 committed and they are also quite good to tell you that you have some
1116 bugs. But they are not so good to tell you where some bugs have been
1117 introduced. To tell you that efficiently, git bisect is needed.</p></div>
1118 <div class="paragraph"><p>The other nice thing with test suites, is that when you have one, you
1119 already know how to test for bad behavior. So you can use this
1120 knowledge to create a new test case for "git bisect" when it appears
1121 that there is a regression. So it will be easier to bisect the bug and
1122 fix it. And then you can add the test case you just created to your
1123 test suite.</p></div>
1124 <div class="paragraph"><p>So if you know how to create test cases and how to bisect, you will be
1125 subject to a virtuous circle:</p></div>
1126 <div class="paragraph"><p>more tests &#8658; easier to create tests &#8658; easier to bisect &#8658; more tests</p></div>
1127 <div class="paragraph"><p>So test suites and "git bisect" are complementary tools that are very
1128 powerful and efficient when used together.</p></div>
1129 <h3 id="_bisecting_build_failures">Bisecting build failures</h3><div style="clear:left"></div>
1130 <div class="paragraph"><p>You can very easily automatically bisect broken builds using something
1131 like:</p></div>
1132 <div class="listingblock">
1133 <div class="content">
1134 <pre><tt>$ git bisect start BAD GOOD
1135 $ git bisect run make</tt></pre>
1136 </div></div>
1137 <h3 id="_passing_sh_c_some_commands_to_git_bisect_run">Passing sh -c "some commands" to "git bisect run"</h3><div style="clear:left"></div>
1138 <div class="paragraph"><p>For example:</p></div>
1139 <div class="listingblock">
1140 <div class="content">
1141 <pre><tt>$ git bisect run sh -c "make || exit 125; ./my_app | grep 'good output'"</tt></pre>
1142 </div></div>
1143 <div class="paragraph"><p>On the other hand if you do this often, then it can be worth having
1144 scripts to avoid too much typing.</p></div>
1145 <h3 id="_finding_performance_regressions">Finding performance regressions</h3><div style="clear:left"></div>
1146 <div class="paragraph"><p>Here is an example script that comes slightly modified from a real
1147 world script used by Junio Hamano <a href="#4">[4]</a>.</p></div>
1148 <div class="paragraph"><p>This script can be passed to "git bisect run" to find the commit that
1149 introduced a performance regression:</p></div>
1150 <div class="listingblock">
1151 <div class="content">
1152 <pre><tt>#!/bin/sh
1154 # Build errors are not what I am interested in.
1155 make my_app || exit 255
1157 # We are checking if it stops in a reasonable amount of time, so
1158 # let it run in the background...
1160 ./my_app &gt;log 2&gt;&amp;1 &amp;
1162 # ... and grab its process ID.
1163 pid=$!
1165 # ... and then wait for sufficiently long.
1166 sleep $NORMAL_TIME
1168 # ... and then see if the process is still there.
1169 if kill -0 $pid
1170 then
1171 # It is still running -- that is bad.
1172 kill $pid; sleep 1; kill $pid;
1173 exit 1
1174 else
1175 # It has already finished (the $pid process was no more),
1176 # and we are happy.
1177 exit 0
1178 fi</tt></pre>
1179 </div></div>
1180 <h3 id="_following_general_best_practices">Following general best practices</h3><div style="clear:left"></div>
1181 <div class="paragraph"><p>It is obviously a good idea not to have commits with changes that
1182 knowingly break things, even if some other commits later fix the
1183 breakage.</p></div>
1184 <div class="paragraph"><p>It is also a good idea when using any VCS to have only one small
1185 logical change in each commit.</p></div>
1186 <div class="paragraph"><p>The smaller the changes in your commit, the most effective "git
1187 bisect" will be. And you will probably need "git bisect" less in the
1188 first place, as small changes are easier to review even if they are
1189 only reviewed by the committer.</p></div>
1190 <div class="paragraph"><p>Another good idea is to have good commit messages. They can be very
1191 helpful to understand why some changes were made.</p></div>
1192 <div class="paragraph"><p>These general best practices are very helpful if you bisect often.</p></div>
1193 <h3 id="_avoiding_bug_prone_merges">Avoiding bug prone merges</h3><div style="clear:left"></div>
1194 <div class="paragraph"><p>First merges by themselves can introduce some regressions even when
1195 the merge needs no source code conflict resolution. This is because a
1196 semantic change can happen in one branch while the other branch is not
1197 aware of it.</p></div>
1198 <div class="paragraph"><p>For example one branch can change the semantic of a function while the
1199 other branch add more calls to the same function.</p></div>
1200 <div class="paragraph"><p>This is made much worse if many files have to be fixed to resolve
1201 conflicts. That&#8217;s why such merges are called "evil merges". They can
1202 make regressions very difficult to track down. It can even be
1203 misleading to know the first bad commit if it happens to be such a
1204 merge, because people might think that the bug comes from bad conflict
1205 resolution when it comes from a semantic change in one branch.</p></div>
1206 <div class="paragraph"><p>Anyway "git rebase" can be used to linearize history. This can be used
1207 either to avoid merging in the first place. Or it can be used to
1208 bisect on a linear history instead of the non linear one, as this
1209 should give more information in case of a semantic change in one
1210 branch.</p></div>
1211 <div class="paragraph"><p>Merges can be also made simpler by using smaller branches or by using
1212 many topic branches instead of only long version related branches.</p></div>
1213 <div class="paragraph"><p>And testing can be done more often in special integration branches
1214 like linux-next for the linux kernel.</p></div>
1215 <h3 id="_adapting_your_work_flow">Adapting your work-flow</h3><div style="clear:left"></div>
1216 <div class="paragraph"><p>A special work-flow to process regressions can give great results.</p></div>
1217 <div class="paragraph"><p>Here is an example of a work-flow used by Andreas Ericsson:</p></div>
1218 <div class="ulist"><ul>
1219 <li>
1221 write, in the test suite, a test script that exposes the regression
1222 </p>
1223 </li>
1224 <li>
1226 use "git bisect run" to find the commit that introduced it
1227 </p>
1228 </li>
1229 <li>
1231 fix the bug that is often made obvious by the previous step
1232 </p>
1233 </li>
1234 <li>
1236 commit both the fix and the test script (and if needed more tests)
1237 </p>
1238 </li>
1239 </ul></div>
1240 <div class="paragraph"><p>And here is what Andreas said about this work-flow <a href="#5">[5]</a>:</p></div>
1241 <div class="quoteblock">
1242 <div class="quoteblock-content">
1243 <div class="paragraph"><p>To give some hard figures, we used to have an average report-to-fix
1244 cycle of 142.6 hours (according to our somewhat weird bug-tracker
1245 which just measures wall-clock time). Since we moved to git, we&#8217;ve
1246 lowered that to 16.2 hours. Primarily because we can stay on top of
1247 the bug fixing now, and because everyone&#8217;s jockeying to get to fix
1248 bugs (we&#8217;re quite proud of how lazy we are to let git find the bugs
1249 for us). Each new release results in ~40% fewer bugs (almost certainly
1250 due to how we now feel about writing tests).</p></div>
1251 </div>
1252 <div class="quoteblock-attribution">
1253 </div></div>
1254 <div class="paragraph"><p>Clearly this work-flow uses the virtuous circle between test suites
1255 and "git bisect". In fact it makes it the standard procedure to deal
1256 with regression.</p></div>
1257 <div class="paragraph"><p>In other messages Andreas says that they also use the "best practices"
1258 described above: small logical commits, topic branches, no evil
1259 merge,&#8230; These practices all improve the bisectability of the commit
1260 graph, by making it easier and more useful to bisect.</p></div>
1261 <div class="paragraph"><p>So a good work-flow should be designed around the above points. That
1262 is making bisecting easier, more useful and standard.</p></div>
1263 <h3 id="_involving_qa_people_and_if_possible_end_users">Involving QA people and if possible end users</h3><div style="clear:left"></div>
1264 <div class="paragraph"><p>One nice about "git bisect" is that it is not only a developer
1265 tool. It can effectively be used by QA people or even end users (if
1266 they have access to the source code or if they can get access to all
1267 the builds).</p></div>
1268 <div class="paragraph"><p>There was a discussion at one point on the linux kernel mailing list
1269 of whether it was ok to always ask end user to bisect, and very good
1270 points were made to support the point of view that it is ok.</p></div>
1271 <div class="paragraph"><p>For example David Miller wrote <a href="#6">[6]</a>:</p></div>
1272 <div class="quoteblock">
1273 <div class="quoteblock-content">
1274 <div class="paragraph"><p>What people don&#8217;t get is that this is a situation where the "end node
1275 principle" applies. When you have limited resources (here: developers)
1276 you don&#8217;t push the bulk of the burden upon them. Instead you push
1277 things out to the resource you have a lot of, the end nodes (here:
1278 users), so that the situation actually scales.</p></div>
1279 </div>
1280 <div class="quoteblock-attribution">
1281 </div></div>
1282 <div class="paragraph"><p>This means that it is often "cheaper" if QA people or end users can do
1283 it.</p></div>
1284 <div class="paragraph"><p>What is interesting too is that end users that are reporting bugs (or
1285 QA people that reproduced a bug) have access to the environment where
1286 the bug happens. So they can often more easily reproduce a
1287 regression. And if they can bisect, then more information will be
1288 extracted from the environment where the bug happens, which means that
1289 it will be easier to understand and then fix the bug.</p></div>
1290 <div class="paragraph"><p>For open source projects it can be a good way to get more useful
1291 contributions from end users, and to introduce them to QA and
1292 development activities.</p></div>
1293 <h3 id="_using_complex_scripts">Using complex scripts</h3><div style="clear:left"></div>
1294 <div class="paragraph"><p>In some cases like for kernel development it can be worth developing
1295 complex scripts to be able to fully automate bisecting.</p></div>
1296 <div class="paragraph"><p>Here is what Ingo Molnar says about that <a href="#7">[7]</a>:</p></div>
1297 <div class="quoteblock">
1298 <div class="quoteblock-content">
1299 <div class="paragraph"><p>i have a fully automated bootup-hang bisection script. It is based on
1300 "git-bisect run". I run the script, it builds and boots kernels fully
1301 automatically, and when the bootup fails (the script notices that via
1302 the serial log, which it continuously watches - or via a timeout, if
1303 the system does not come up within 10 minutes it&#8217;s a "bad" kernel),
1304 the script raises my attention via a beep and i power cycle the test
1305 box. (yeah, i should make use of a managed power outlet to 100%
1306 automate it)</p></div>
1307 </div>
1308 <div class="quoteblock-attribution">
1309 </div></div>
1310 <h3 id="_combining_test_suites_git_bisect_and_other_systems_together">Combining test suites, git bisect and other systems together</h3><div style="clear:left"></div>
1311 <div class="paragraph"><p>We have seen that test suites an git bisect are very powerful when
1312 used together. It can be even more powerful if you can combine them
1313 with other systems.</p></div>
1314 <div class="paragraph"><p>For example some test suites could be run automatically at night with
1315 some unusual (or even random) configurations. And if a regression is
1316 found by a test suite, then "git bisect" can be automatically
1317 launched, and its result can be emailed to the author of the first bad
1318 commit found by "git bisect", and perhaps other people too. And a new
1319 entry in the bug tracking system could be automatically created too.</p></div>
1320 </div>
1321 <h2 id="_the_future_of_bisecting">The future of bisecting</h2>
1322 <div class="sectionbody">
1323 <h3 id="_git_replace">"git replace"</h3><div style="clear:left"></div>
1324 <div class="paragraph"><p>We saw earlier that "git bisect skip" is now using a PRNG to try to
1325 avoid areas in the commit graph where commits are untestable. The
1326 problem is that sometimes the first bad commit will be in an
1327 untestable area.</p></div>
1328 <div class="paragraph"><p>To simplify the discussion we will suppose that the untestable area is
1329 a simple string of commits and that it was created by a breakage
1330 introduced by one commit (let&#8217;s call it BBC for bisect breaking
1331 commit) and later fixed by another one (let&#8217;s call it BFC for bisect
1332 fixing commit).</p></div>
1333 <div class="paragraph"><p>For example:</p></div>
1334 <div class="listingblock">
1335 <div class="content">
1336 <pre><tt>...-Y-BBC-X1-X2-X3-X4-X5-X6-BFC-Z-...</tt></pre>
1337 </div></div>
1338 <div class="paragraph"><p>where we know that Y is good and BFC is bad, and where BBC and X1 to
1339 X6 are untestable.</p></div>
1340 <div class="paragraph"><p>In this case if you are bisecting manually, what you can do is create
1341 a special branch that starts just before the BBC. The first commit in
1342 this branch should be the BBC with the BFC squashed into it. And the
1343 other commits in the branch should be the commits between BBC and BFC
1344 rebased on the first commit of the branch and then the commit after
1345 BFC also rebased on.</p></div>
1346 <div class="paragraph"><p>For example:</p></div>
1347 <div class="listingblock">
1348 <div class="content">
1349 <pre><tt> (BBC+BFC)-X1'-X2'-X3'-X4'-X5'-X6'-Z'
1351 ...-Y-BBC-X1-X2-X3-X4-X5-X6-BFC-Z-...</tt></pre>
1352 </div></div>
1353 <div class="paragraph"><p>where commits quoted with ' have been rebased.</p></div>
1354 <div class="paragraph"><p>You can easily create such a branch with Git using interactive rebase.</p></div>
1355 <div class="paragraph"><p>For example using:</p></div>
1356 <div class="listingblock">
1357 <div class="content">
1358 <pre><tt>$ git rebase -i Y Z</tt></pre>
1359 </div></div>
1360 <div class="paragraph"><p>and then moving BFC after BBC and squashing it.</p></div>
1361 <div class="paragraph"><p>After that you can start bisecting as usual in the new branch and you
1362 should eventually find the first bad commit.</p></div>
1363 <div class="paragraph"><p>For example:</p></div>
1364 <div class="listingblock">
1365 <div class="content">
1366 <pre><tt>$ git bisect start Z' Y</tt></pre>
1367 </div></div>
1368 <div class="paragraph"><p>If you are using "git bisect run", you can use the same manual fix up
1369 as above, and then start another "git bisect run" in the special
1370 branch. Or as the "git bisect" man page says, the script passed to
1371 "git bisect run" can apply a patch before it compiles and test the
1372 software <a href="#8">[8]</a>. The patch should turn a current untestable commits
1373 into a testable one. So the testing will result in "good" or "bad" and
1374 "git bisect" will be able to find the first bad commit. And the script
1375 should not forget to remove the patch once the testing is done before
1376 exiting from the script.</p></div>
1377 <div class="paragraph"><p>(Note that instead of a patch you can use "git cherry-pick BFC" to
1378 apply the fix, and in this case you should use "git reset --hard
1379 HEAD^" to revert the cherry-pick after testing and before returning
1380 from the script.)</p></div>
1381 <div class="paragraph"><p>But the above ways to work around untestable areas are a little bit
1382 clunky. Using special branches is nice because these branches can be
1383 shared by developers like usual branches, but the risk is that people
1384 will get many such branches. And it disrupts the normal "git bisect"
1385 work-flow. So, if you want to use "git bisect run" completely
1386 automatically, you have to add special code in your script to restart
1387 bisection in the special branches.</p></div>
1388 <div class="paragraph"><p>Anyway one can notice in the above special branch example that the Z'
1389 and Z commits should point to the same source code state (the same
1390 "tree" in git parlance). That&#8217;s because Z' result from applying the
1391 same changes as Z just in a slightly different order.</p></div>
1392 <div class="paragraph"><p>So if we could just "replace" Z by Z' when we bisect, then we would
1393 not need to add anything to a script. It would just work for anyone in
1394 the project sharing the special branches and the replacements.</p></div>
1395 <div class="paragraph"><p>With the example above that would give:</p></div>
1396 <div class="listingblock">
1397 <div class="content">
1398 <pre><tt> (BBC+BFC)-X1'-X2'-X3'-X4'-X5'-X6'-Z'-...
1400 ...-Y-BBC-X1-X2-X3-X4-X5-X6-BFC-Z</tt></pre>
1401 </div></div>
1402 <div class="paragraph"><p>That&#8217;s why the "git replace" command was created. Technically it
1403 stores replacements "refs" in the "refs/replace/" hierarchy. These
1404 "refs" are like branches (that are stored in "refs/heads/") or tags
1405 (that are stored in "refs/tags"), and that means that they can
1406 automatically be shared like branches or tags among developers.</p></div>
1407 <div class="paragraph"><p>"git replace" is a very powerful mechanism. It can be used to fix
1408 commits in already released history, for example to change the commit
1409 message or the author. And it can also be used instead of git "grafts"
1410 to link a repository with another old repository.</p></div>
1411 <div class="paragraph"><p>In fact it&#8217;s this last feature that "sold" it to the git community, so
1412 it is now in the "master" branch of git&#8217;s git repository and it should
1413 be released in git 1.6.5 in October or November 2009.</p></div>
1414 <div class="paragraph"><p>One problem with "git replace" is that currently it stores all the
1415 replacements refs in "refs/replace/", but it would be perhaps better
1416 if the replacement refs that are useful only for bisecting would be in
1417 "refs/replace/bisect/". This way the replacement refs could be used
1418 only for bisecting, while other refs directly in "refs/replace/" would
1419 be used nearly all the time.</p></div>
1420 <h3 id="_bisecting_sporadic_bugs">Bisecting sporadic bugs</h3><div style="clear:left"></div>
1421 <div class="paragraph"><p>Another possible improvement to "git bisect" would be to optionally
1422 add some redundancy to the tests performed so that it would be more
1423 reliable when tracking sporadic bugs.</p></div>
1424 <div class="paragraph"><p>This has been requested by some kernel developers because some bugs
1425 called sporadic bugs do not appear in all the kernel builds because
1426 they are very dependent on the compiler output.</p></div>
1427 <div class="paragraph"><p>The idea is that every 3 test for example, "git bisect" could ask the
1428 user to test a commit that has already been found to be "good" or
1429 "bad" (because one of its descendants or one of its ancestors has been
1430 found to be "good" or "bad" respectively). If it happens that a commit
1431 has been previously incorrectly classified then the bisection can be
1432 aborted early, hopefully before too many mistakes have been made. Then
1433 the user will have to look at what happened and then restart the
1434 bisection using a fixed bisect log.</p></div>
1435 <div class="paragraph"><p>There is already a project called BBChop created by Ealdwulf Wuffinga
1436 on Github that does something like that using Bayesian Search Theory
1437 <a href="#9">[9]</a>:</p></div>
1438 <div class="quoteblock">
1439 <div class="quoteblock-content">
1440 <div class="paragraph"><p>BBChop is like <em>git bisect</em> (or equivalent), but works when your bug
1441 is intermittent. That is, it works in the presence of false negatives
1442 (when a version happens to work this time even though it contains the
1443 bug). It assumes that there are no false positives (in principle, the
1444 same approach would work, but adding it may be non-trivial).</p></div>
1445 </div>
1446 <div class="quoteblock-attribution">
1447 </div></div>
1448 <div class="paragraph"><p>But BBChop is independent of any VCS and it would be easier for Git
1449 users to have something integrated in Git.</p></div>
1450 </div>
1451 <h2 id="_conclusion">Conclusion</h2>
1452 <div class="sectionbody">
1453 <div class="paragraph"><p>We have seen that regressions are an important problem, and that "git
1454 bisect" has nice features that complement very well practices and
1455 other tools, especially test suites, that are generally used to fight
1456 regressions. But it might be needed to change some work-flows and
1457 (bad) habits to get the most out of it.</p></div>
1458 <div class="paragraph"><p>Some improvements to the algorithms inside "git bisect" are possible
1459 and some new features could help in some cases, but overall "git
1460 bisect" works already very well, is used a lot, and is already very
1461 useful. To back up that last claim, let&#8217;s give the final word to Ingo
1462 Molnar when he was asked by the author how much time does he think
1463 "git bisect" saves him when he uses it:</p></div>
1464 <div class="quoteblock">
1465 <div class="quoteblock-content">
1466 <div class="paragraph"><p>a <em>lot</em>.</p></div>
1467 <div class="paragraph"><p>About ten years ago did i do my first <em>bisection</em> of a Linux patch
1468 queue. That was prior the Git (and even prior the BitKeeper) days. I
1469 literally days spent sorting out patches, creating what in essence
1470 were standalone commits that i guessed to be related to that bug.</p></div>
1471 <div class="paragraph"><p>It was a tool of absolute last resort. I&#8217;d rather spend days looking
1472 at printk output than do a manual <em>patch bisection</em>.</p></div>
1473 <div class="paragraph"><p>With Git bisect it&#8217;s a breeze: in the best case i can get a ~15 step
1474 kernel bisection done in 20-30 minutes, in an automated way. Even with
1475 manual help or when bisecting multiple, overlapping bugs, it&#8217;s rarely
1476 more than an hour.</p></div>
1477 <div class="paragraph"><p>In fact it&#8217;s invaluable because there are bugs i would never even
1478 <em>try</em> to debug if it wasn&#8217;t for git bisect. In the past there were bug
1479 patterns that were immediately hopeless for me to debug - at best i
1480 could send the crash/bug signature to lkml and hope that someone else
1481 can think of something.</p></div>
1482 <div class="paragraph"><p>And even if a bisection fails today it tells us something valuable
1483 about the bug: that it&#8217;s non-deterministic - timing or kernel image
1484 layout dependent.</p></div>
1485 <div class="paragraph"><p>So git bisect is unconditional goodness - and feel free to quote that
1486 ;-)</p></div>
1487 </div>
1488 <div class="quoteblock-attribution">
1489 </div></div>
1490 </div>
1491 <h2 id="_acknowledgements">Acknowledgements</h2>
1492 <div class="sectionbody">
1493 <div class="paragraph"><p>Many thanks to Junio Hamano for his help in reviewing this paper, for
1494 reviewing the patches I sent to the git mailing list, for discussing
1495 some ideas and helping me improve them, for improving "git bisect" a
1496 lot and for his awesome work in maintaining and developing Git.</p></div>
1497 <div class="paragraph"><p>Many thanks to Ingo Molnar for giving me very useful information that
1498 appears in this paper, for commenting on this paper, for his
1499 suggestions to improve "git bisect" and for evangelizing "git bisect"
1500 on the linux kernel mailing lists.</p></div>
1501 <div class="paragraph"><p>Many thanks to Linus Torvalds for inventing, developing and
1502 evangelizing "git bisect", Git and Linux.</p></div>
1503 <div class="paragraph"><p>Many thanks to the many other great people who helped one way or
1504 another when I worked on git, especially to Andreas Ericsson, Johannes
1505 Schindelin, H. Peter Anvin, Daniel Barkalow, Bill Lear, John Hawley,
1506 Shawn O. Pierce, Jeff King, Sam Vilain, Jon Seymour.</p></div>
1507 <div class="paragraph"><p>Many thanks to the Linux-Kongress program committee for choosing the
1508 author to given a talk and for publishing this paper.</p></div>
1509 </div>
1510 <h2 id="_references">References</h2>
1511 <div class="sectionbody">
1512 <div class="ulist"><ul>
1513 <li>
1515 <a id="1"></a>[1] <a href="http://www.nist.gov/public_affairs/releases/n02-10.htm"><em>Software Errors Cost U.S. Economy $59.5 Billion Annually</em>. Nist News Release.</a>
1516 </p>
1517 </li>
1518 <li>
1520 <a id="2"></a>[2] <a href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc.html#16712"><em>Code Conventions for the Java Programming Language</em>. Sun Microsystems.</a>
1521 </p>
1522 </li>
1523 <li>
1525 <a id="3"></a>[3] <a href="http://en.wikipedia.org/wiki/Software_maintenance"><em>Software maintenance</em>. Wikipedia.</a>
1526 </p>
1527 </li>
1528 <li>
1530 <a id="4"></a>[4] <a href="http://article.gmane.org/gmane.comp.version-control.git/45195/">Junio C Hamano. <em>Automated bisect success story</em>. Gmane.</a>
1531 </p>
1532 </li>
1533 <li>
1535 <a id="5"></a>[5] <a href="http://lwn.net/Articles/317154/">Christian Couder. <em>Fully automated bisecting with "git bisect run"</em>. LWN.net.</a>
1536 </p>
1537 </li>
1538 <li>
1540 <a id="6"></a>[6] <a href="http://lwn.net/Articles/277872/">Jonathan Corbet. <em>Bisection divides users and developers</em>. LWN.net.</a>
1541 </p>
1542 </li>
1543 <li>
1545 <a id="7"></a>[7] <a href="http://article.gmane.org/gmane.linux.scsi/36652/">Ingo Molnar. <em>Re: BUG 2.6.23-rc3 can&#8217;t see sd partitions on Alpha</em>. Gmane.</a>
1546 </p>
1547 </li>
1548 <li>
1550 <a id="8"></a>[8] <a href="http://www.kernel.org/pub/software/scm/git/docs/git-bisect.html">Junio C Hamano and the git-list. <em>git-bisect(1) Manual Page</em>. Linux Kernel Archives.</a>
1551 </p>
1552 </li>
1553 <li>
1555 <a id="9"></a>[9] <a href="http://github.com/Ealdwulf/bbchop">Ealdwulf. <em>bbchop</em>. GitHub.</a>
1556 </p>
1557 </li>
1558 </ul></div>
1559 </div>
1560 <div id="footer">
1561 <div id="footer-text">
1562 Last updated 2010-09-03 21:29:54 UTC
1563 </div>
1564 </div>
1565 </body>
1566 </html>