git bisect run for automated bisect - Time & Space Complexity
When using git bisect run, we want to know how the time to find a bad commit grows as the number of commits increases.
We ask: How many steps does it take to find the problem as the commit list gets bigger?
Analyze the time complexity of this git bisect automation snippet.
git bisect start
git bisect bad HEAD
git bisect good v1.0
git bisect run ./test_script.sh
# test_script.sh returns 0 if good, 1 if bad
This code starts bisecting between a known good and bad commit, then runs a test script automatically on each commit to find the first bad one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running the test script on selected commits.
- How many times: The test runs on a subset of commits chosen by bisect, reducing the search space each time.
Each test run cuts the number of commits to check roughly in half.
| Input Size (n commits) | Approx. Test Runs |
|---|---|
| 10 | 4 |
| 100 | 7 |
| 1000 | 10 |
Pattern observation: The number of test runs grows slowly, about doubling the commits adds only one more test run.
Time Complexity: O(log n)
This means the number of test runs grows slowly as the number of commits grows, making bisect efficient.
[X] Wrong: "git bisect runs the test on every commit one by one."
[OK] Correct: Bisect smartly picks commits to test, cutting the search space in half each time, so it does not test all commits.
Understanding how bisect reduces work helps you explain efficient problem-solving in code history, a useful skill in real projects.
"What if the test script takes longer as the commit list grows? How would that affect the overall time complexity?"