Skip to content

Commit 916e640

Browse files
committed
initial import
0 parents  commit 916e640

32 files changed

Lines changed: 5953 additions & 0 deletions

.bazelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build --workspace_status_command "tools/workspace_status.sh"
2+
build --noexperimental_convenience_symlinks
3+
build --java_runtime_version=remotejdk_11
4+
5+
test --test_output=errors
6+
test:ci --jobs=3

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5.1.0

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
bazel-diff
3+
*.txt

BUILD.bazel

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# gazelle:prefix github.com/ewhauser/bazel-differ
2+
# gazelle:exclude sql
3+
load("@bazel_gazelle//:def.bzl", "gazelle")
4+
5+
gazelle(
6+
name = "gazelle",
7+
)
8+
9+
# gazelle_ci is called from CI to verify the repo is up-to-date
10+
gazelle(
11+
name = "gazelle_ci",
12+
command = "fix",
13+
extra_args = ["--mode=diff"],
14+
)
15+
16+
gazelle(
17+
name = "gazelle-update-repos",
18+
args = [
19+
"-from_file=go.mod",
20+
"-to_macro=deps.bzl%go_dependencies",
21+
"-prune",
22+
],
23+
command = "update-repos",
24+
)
25+
26+
sh_binary(
27+
name = "buildifier",
28+
srcs = select(
29+
{
30+
"@bazel_tools//src/conditions:darwin": ["@buildifier_osx//file"],
31+
"@bazel_tools//src/conditions:linux": ["@buildifier//file"],
32+
},
33+
no_match_error = "Buildifier does not have a binary for your platform",
34+
),
35+
)
36+
37+
sh_binary(
38+
name = "buildozer",
39+
srcs = select(
40+
{
41+
"@bazel_tools//src/conditions:darwin": ["@buildozer_osx//file"],
42+
"@bazel_tools//src/conditions:linux": ["@buildozer//file"],
43+
},
44+
no_match_error = "Buildozer does not have a binary for your platform",
45+
),
46+
)

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# bazel-differ
2+
3+
`bazel-differ` is a command line interface for Bazel that helps you do incremental builds across different Git versions. `bazel-differ` is a mostly a pure Go port of the excellent [bazel-diff](https://github.com/tinder/bazel-diff) and should be able to function as a drop-in replacement for most use cases.
4+
5+
# FAQ
6+
7+
1. Why did you port `bazel-diff`?
8+
9+
A couple reasons:
10+
11+
* The projects I work with don't have any JVM dependencies so using `bazel-diff` was adding some additional build time I wanted to eliminate.
12+
* With the exception of the Bazel server itself, much of the tooling in the Bazel ecosystem is written in Go.
13+
14+
2. What are the differences from `bazel-diff`?
15+
16+
* Due to some implementation differences, the actual hashes generated by the two programs are different
17+
* `bazel-differ` isn't using `streamed_proto` output from Bazel query (I'm not sure if there is a Go implemntation of this?). In some minimal testing against some large repositories, `bazel-differ` still seems to outperform `bazel-diff` by 2x.

WORKSPACE

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file", "http_jar")
2+
3+
#--------
4+
# Go
5+
#--------
6+
## rules_go
7+
http_archive(
8+
name = "io_bazel_rules_go",
9+
sha256 = "f2dcd210c7095febe54b804bb1cd3a58fe8435a909db2ec04e31542631cf715c",
10+
urls = [
11+
"https://github.com/bazelbuild/rules_go/releases/download/v0.31.0/rules_go-v0.31.0.zip",
12+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.31.0/rules_go-v0.31.0.zip",
13+
],
14+
)
15+
16+
http_archive(
17+
name = "bazel_gazelle",
18+
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
19+
urls = [
20+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
21+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
22+
],
23+
)
24+
25+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
26+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
27+
load("//:deps.bzl", "go_dependencies")
28+
29+
# gazelle:repository_macro deps.bzl%go_dependencies
30+
go_dependencies()
31+
32+
go_rules_dependencies()
33+
34+
go_register_toolchains(version = "1.17.6")
35+
36+
gazelle_dependencies()
37+
38+
#------------
39+
# Bazel tools
40+
#------------
41+
# buildifier
42+
http_file(
43+
name = "buildifier",
44+
executable = True,
45+
sha256 = "3ed7358c7c6a1ca216dc566e9054fd0b97a1482cb0b7e61092be887d42615c5d",
46+
urls = ["https://github.com/bazelbuild/buildtools/releases/download/5.0.1/buildifier-linux-amd64"],
47+
)
48+
49+
http_file(
50+
name = "buildifier_osx",
51+
executable = True,
52+
sha256 = "2cb0a54683633ef6de4e0491072e22e66ac9c6389051432b76200deeeeaf93fb",
53+
urls = ["https://github.com/bazelbuild/buildtools/releases/download/5.0.1/buildifier-darwin-amd64"],
54+
)
55+
56+
http_file(
57+
name = "buildozer",
58+
executable = True,
59+
sha256 = "78204dac0ac6a94db499c57c5334b9c0c409d91de9779032c73ad42f2362e901",
60+
urls = ["https://github.com/bazelbuild/buildtools/releases/download/5.0.1/buildozer-linux-amd64"],
61+
)
62+
63+
http_file(
64+
name = "buildozer_osx",
65+
executable = True,
66+
sha256 = "17a093596f141ead6ff70ac217a063d7aebc86174faa8ab43620392c17b8ee61",
67+
urls = ["https://github.com/bazelbuild/buildtools/releases/download/5.0.1/buildozer-darwin-amd64"],
68+
)
69+
70+
http_file(
71+
name = "protoc_bin",
72+
executable = True,
73+
sha256 = "75d8a9d7a2c42566e46411750d589c51276242d8b6247a5724bac0f9283e05a8",
74+
urls = ["https://github.com/google/protobuf/releases/download/v3.20.0/protoc-3.20.0-linux-x86_64.zip"],
75+
)
76+
77+
http_file(
78+
name = "protoc_bin_osx",
79+
executable = True,
80+
sha256 = "8b35a679c99b36caef5899e596281fe0b943ed248f7d5f70b3e705684bf67cb4",
81+
urls = ["https://github.com/google/protobuf/releases/download/v3.20.0/protoc-3.20.0-osx-x86_64.zip"],
82+
)
83+
84+
#---------------
85+
# Bazel diff
86+
#---------------
87+
http_jar(
88+
name = "bazel_diff",
89+
sha256 = "0dc9166097c181796cfcbde4d16ab304e70cc853c536257c3dfa9af60ddd31e6",
90+
urls = [
91+
"https://github.com/Tinder/bazel-diff/releases/download/3.3.0/bazel-diff_deploy.jar",
92+
],
93+
)

cli/BUILD.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
2+
3+
go_library(
4+
name = "cli",
5+
srcs = ["main.go"],
6+
importpath = "github.com/ewhauser/bazel-differ/bazel-differ",
7+
visibility = ["//visibility:private"],
8+
deps = ["//cmd"],
9+
)
10+
11+
go_binary(
12+
name = "bazel-differ",
13+
embed = [":cli"],
14+
visibility = ["//visibility:public"],
15+
)
16+
17+
go_library(
18+
name = "cli_lib",
19+
srcs = ["main.go"],
20+
importpath = "github.com/ewhauser/bazel-differ/cli",
21+
visibility = ["//visibility:private"],
22+
deps = ["//cmd"],
23+
)

cli/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/ewhauser/bazel-differ/cmd"
4+
5+
func main() {
6+
cmd.Execute()
7+
}

cmd/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "cmd",
5+
srcs = [
6+
"diff.go",
7+
"generate_hashes.go",
8+
"root.go",
9+
],
10+
importpath = "github.com/ewhauser/bazel-differ/cmd",
11+
visibility = ["//visibility:public"],
12+
deps = [
13+
"//internal",
14+
"@com_github_spf13_cobra//:cobra",
15+
],
16+
)

cmd/diff.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cmd
2+
3+
import (
4+
"bufio"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"github.com/ewhauser/bazel-differ/internal"
9+
"github.com/spf13/cobra"
10+
"io/ioutil"
11+
"log"
12+
"os"
13+
)
14+
15+
// diffCmd represents the diff command
16+
var diffCmd = &cobra.Command{
17+
Use: "diff",
18+
Short: "Writes to a file the impacted targets between two Bazel graph JSON files",
19+
Long: `Writes to a file the impacted targets between two Bazel graph JSON files`,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
if StartingHashes == "" {
22+
fmt.Println("Starting hashes is required")
23+
os.Exit(1)
24+
}
25+
if _, err := os.Stat(StartingHashes); errors.Is(err, os.ErrNotExist) {
26+
fmt.Println("Starting hashes is required")
27+
os.Exit(1)
28+
}
29+
if FinalHashes == "" {
30+
fmt.Println("Final hashes is required")
31+
os.Exit(1)
32+
}
33+
if _, err := os.Stat(FinalHashes); errors.Is(err, os.ErrNotExist) {
34+
fmt.Println("Final hashes is required")
35+
os.Exit(1)
36+
}
37+
if Output == "" {
38+
fmt.Println("Output path is required")
39+
os.Exit(1)
40+
}
41+
targetHasher := internal.NewTargetHashingClient(GetBazelClient(), internal.Filesystem,
42+
internal.NewRuleProvider())
43+
44+
startingHashes := readHashFile(StartingHashes)
45+
finalHashes := readHashFile(FinalHashes)
46+
targets, err := targetHasher.GetImpactedTargets(startingHashes, finalHashes)
47+
if err != nil {
48+
panic(err)
49+
}
50+
writeFile(targets, Output)
51+
},
52+
}
53+
54+
func writeFile(targets map[string]bool, output string) {
55+
file, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
56+
defer file.Close()
57+
58+
if err != nil {
59+
log.Fatalf("failed creating file: %s", err)
60+
}
61+
62+
datawriter := bufio.NewWriter(file)
63+
defer datawriter.Flush()
64+
65+
for k, _ := range targets {
66+
_, _ = datawriter.WriteString(k + "\n")
67+
}
68+
}
69+
70+
func readHashFile(filename string) map[string]string {
71+
x := map[string]string{}
72+
startingContent, err := ioutil.ReadFile(filename)
73+
if err != nil {
74+
panic(err)
75+
}
76+
err = json.Unmarshal(startingContent, &x)
77+
if err != nil {
78+
panic(err)
79+
}
80+
return x
81+
}
82+
83+
func init() {
84+
rootCmd.AddCommand(diffCmd)
85+
}

0 commit comments

Comments
 (0)