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