alchemist: Drop support for injecting .git directories

We no longer need to inject the LLVM git repository to build the
versioned LLVM packages. When we decide to support building the 9999
ebuilds, we should figure out how to compute the SVN revision inside a
repository rule.

BUG=b:267209587
TEST=cargo test
TEST=tar tvvf bazel-bin/external/_main~portage~portage/internal/sources/chromite/__tarball__.tar.zst | grep git

Change-Id: Ib846a7526b780059a7ecbdc93c428989d8c9c4fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/bazel/+/4851809
Auto-Submit: Raul Rangel <rrangel@chromium.org>
Reviewed-by: Shuhei Takahashi <nya@chromium.org>
Commit-Queue: Shuhei Takahashi <nya@chromium.org>
Tested-by: Raul Rangel <rrangel@chromium.org>
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/mod.rs b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/mod.rs
index 9dc245b..c076a51 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/mod.rs
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/mod.rs
@@ -197,12 +197,6 @@
     /// should be excluded to avoid confusing Bazel, thus they appear here.
     /// `excludes` includes `symlinks` and `renames`.
     excludes: Vec<PathBuf>,
-
-    /// Whether to include .git in the source tarball.
-    /// This must not be enabled except for a few packages under active fixing
-    /// because .git structure is not canonicalized and thus harms caching.
-    /// TODO: Remove this hack.
-    include_git: bool,
 }
 
 impl SourcePackage {
@@ -248,14 +242,15 @@
             let rel_path = entry.path().strip_prefix(&source_dir)?.to_owned();
             let file_name_str = &*rel_path.file_name().unwrap_or_default().to_string_lossy();
 
-            // Skip .git directory. Note that this is also hard-coded in
-            // the template BUILD.bazel.
+            // Skip .git directory because it is non-deterministic.
             if file_name_str == ".git" {
                 // Note that .git can be a symlink, in which case we must not
                 // call WalkDir::skip_current_dir.
                 if entry.file_type().is_dir() {
                     walk.skip_current_dir();
                 }
+
+                excludes.push(rel_path);
                 continue;
             }
 
@@ -292,29 +287,6 @@
             }
         }
 
-        // HACK: We intentionally include the .git repo for llvm because it's
-        // required to calculate which patches to apply. We really need to
-        // figure out another way of doing this.
-        // TODO: Remove this hack.
-        let include_git = layout.prefix.to_string_lossy() == "src/third_party/llvm-project";
-
-        if include_git {
-            // On including .git, explicitly scan directories as it may contain
-            // mandatory empty directories (e.g. .git/refs). We don't bother to
-            // handle file names with special characters under .git; if such
-            // files exist, they will just cause the build to fail.
-            let walk = WalkDir::new(source_dir.join(".git"))
-                .follow_links(true)
-                .sort_by_file_name();
-            for entry in walk {
-                let entry = entry?;
-                if entry.file_type().is_dir() {
-                    let rel_path = entry.path().strip_prefix(&source_dir)?.to_owned();
-                    dirs.push(rel_path);
-                }
-            }
-        }
-
         Ok(Self {
             layout,
             source_dir,
@@ -323,7 +295,6 @@
             symlinks,
             renames,
             excludes,
-            include_git,
         })
     }
 }
@@ -349,7 +320,6 @@
     symlinks: Vec<SymlinkEntry>,
     renames: Vec<RenameEntry>,
     excludes: Vec<String>,
-    include_git: bool,
 }
 
 /// Generates `BUILD.bazel` file for a source package.
@@ -399,7 +369,6 @@
             .iter()
             .map(|path| path.to_string_lossy().into_owned())
             .collect(),
-        include_git: package.include_git,
     };
 
     let mut file = File::create(package.output_dir.join("BUILD.bazel"))?;
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/templates/source.BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/templates/source.BUILD.bazel
index a0427b0..ff881ed 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/templates/source.BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/templates/source.BUILD.bazel
@@ -10,10 +10,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            {%- if not include_git %}
-            "**/.git",
-            "**/.git/**",
-            {%- endif %}
             {%- for path in excludes %}
             "{{ path }}",
             {%- endfor %}
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs.golden.BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs.golden.BUILD.bazel
index 51d7588..09ed7c2 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs.golden.BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs.golden.BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.llvm-project.golden.BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.llvm-project.golden.BUILD.bazel
index 2cb7e8f..dc67bf2 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.llvm-project.golden.BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.llvm-project.golden.BUILD.bazel
@@ -12,6 +12,7 @@
     srcs = glob(
         ["**"],
         exclude = [
+            ".git",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
@@ -27,19 +28,10 @@
     visibility = ["//visibility:private"],
 )
 
-pkg_mkdirs(
-    name = "__dirs__",
-    dirs = [
-        ".git",
-        ".git/refs",
-    ],
-)
-
 pkg_filegroup(
     name = "__files__",
     srcs = [
         ":__files_regular__",
-        ":__dirs__",
     ],
     prefix = "/mnt/host/source/src/third_party/llvm-project",
     visibility = ["//visibility:private"],
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.platform2.golden.BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.platform2.golden.BUILD.bazel
index 3e74c21..df5ba2f 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.platform2.golden.BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/empty_dirs_git.platform2.golden.BUILD.bazel
@@ -12,8 +12,7 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
+            ".git",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/BUILD.bazel
index feb668b..0df04c9 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/BUILD.bazel
index 0dee865..4d5005c 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/baz/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/baz/BUILD.bazel
index b824d07..e481e92 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/baz/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/nested/golden/foo/bar/baz/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/simple/golden/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/simple/golden/BUILD.bazel
index 909f0ab..2e30cf1 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/simple/golden/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/simple/golden/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_dirs/golden/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_dirs/golden/BUILD.bazel
index 6796fa0..be96211 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_dirs/golden/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_dirs/golden/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "hello world/file.txt",
             "BUILD.bazel",
             "BUILD",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_files/golden/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_files/golden/BUILD.bazel
index e051b2c..2da3e21 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_files/golden/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/special_files/golden/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "WORKSPACE.bazel",
             "hello world.txt",
diff --git a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/symlinks/golden/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/symlinks/golden/BUILD.bazel
index bb59a7d..398f08e 100644
--- a/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/symlinks/golden/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/generate_repo/internal/sources/testdata/symlinks/golden/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "another symlink",
             "symlink",
             "BUILD.bazel",
diff --git a/portage/bin/alchemist/src/bin/alchemist/testdata/golden/internal/sources/src/scripts/hooks/BUILD.bazel b/portage/bin/alchemist/src/bin/alchemist/testdata/golden/internal/sources/src/scripts/hooks/BUILD.bazel
index a1f3bd1..6f885b4 100644
--- a/portage/bin/alchemist/src/bin/alchemist/testdata/golden/internal/sources/src/scripts/hooks/BUILD.bazel
+++ b/portage/bin/alchemist/src/bin/alchemist/testdata/golden/internal/sources/src/scripts/hooks/BUILD.bazel
@@ -12,8 +12,6 @@
     srcs = glob(
         ["**"],
         exclude = [
-            "**/.git",
-            "**/.git/**",
             "BUILD.bazel",
             "BUILD",
             "WORKSPACE.bazel",