Filter coverage results to only directories with modified files

By default, kunit.sh now only looks at directories with modified files
when generating the HTML coverage report.
In the future, we would like to make it possible to toggle this on/off.

Used `git diff --name-only` to compute the list of affected files
(and added the `git` package to the dependencies list)

Signed-off-by: Maria Del Carmen Ignacio <mariaignacio@google.com>
Change-Id: I2bea878824772a63cf02c5551df1ec74e0e21757
diff --git a/Dockerfile b/Dockerfile
index b8f5803..b3fbb69 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -4,6 +4,7 @@
   build-essential \
   bc \
   m4 \
+  git \
   bison \
   flex \
   python3 \
diff --git a/debs.bzl b/debs.bzl
index c2dfdaf..dbda02a 100644
--- a/debs.bzl
+++ b/debs.bzl
@@ -28,6 +28,7 @@
         "gcc",
         "gcc-6",
         "gcc-6-base",
+        "git",
         "guile-2.0-libs",
         "install-info",
         "lcov",
diff --git a/kunit.sh b/kunit.sh
index 4ef46d8..f3552df 100755
--- a/kunit.sh
+++ b/kunit.sh
@@ -34,7 +34,20 @@
 KUNIT_EXIT_CODE=$?
 
 # Generate an HTML coverage report using LCOV
-lcov -t "kunit_presubmit_tests" -o coverage.info -c -d . > "$ARTIFACTS/lcov.log" 2>&1
+# TODO(mariaignacio): Have LCOV_FILTER be default until we can specify the state
+LCOV_FILTER=1
+
+if [[ $LCOV_FILTER -eq 1 ]]; then
+  # Generate a list of recently modified directories, "-d <dirname> ..."
+  # Note: the final `xargs` joins all the lines together.
+  DIRNAMES=$(git diff --name-only HEAD~ | xargs dirname | sort -u | sed -e 's/^/-d /' | xargs)
+
+  lcov -t "kunit_presubmit_tests" -o coverage.info -c ${DIRNAMES} --no-recursion \
+  > "$ARTIFACTS/lcov.log" 2>&1
+else
+  lcov -t "kunit_presubmit_tests" -o coverage.info -c -d . > "$ARTIFACTS/lcov.log" 2>&1
+fi
+
 genhtml -o "$ARTIFACTS/coverage_html" coverage.info --css-file /lcov/lcov.css > "$ARTIFACTS/genhtml.log" 2>&1
 GENHTML_EXIT_CODE=$?