|
| 1 | +# Patrol |
| 2 | + |
| 3 | +*Patrol* is a utility to help you understand what packages within a Go module |
| 4 | +changed between commits. It was created to be used within CI pipelines to only |
| 5 | +build what needs to be built, but maybe someone else can find a cool use for |
| 6 | +this :). Patrol currently detects changes in: |
| 7 | + |
| 8 | +- packages within the module itself |
| 9 | +- `go.mod` dependency |
| 10 | +- vendored dependencies |
| 11 | + |
| 12 | +To understand all (potential) changes, Patrol traverses the whole dependencies |
| 13 | +graph which means that if you have a structure that looks like this |
| 14 | + |
| 15 | +``` |
| 16 | +[email protected] -> yourModule/foo -> yourModule/bar |
| 17 | +``` |
| 18 | + |
| 19 | +and you update `external-package` to version `v1.0.1` Patrol will report |
| 20 | +`yourModule/foo` (depending on `external-package`) and `yourModule/bar` |
| 21 | +(depending on `yourModule/foo`) as changed. |
| 22 | + |
| 23 | +## Getting started |
| 24 | + |
| 25 | +### Install as binary |
| 26 | +Patrol can be installed as any other Go binary, you just need to run |
| 27 | + |
| 28 | +``` |
| 29 | +go install github.com/utilitywarehouse/patrol |
| 30 | +``` |
| 31 | + |
| 32 | +and you can use it like this |
| 33 | +``` patrol -from={commit hash} . ``` |
| 34 | + |
| 35 | +This is an example run against [My Services |
| 36 | +monorepo](https://github.com/utilitywarehouse/my-services-mono): |
| 37 | + |
| 38 | +``` |
| 39 | +$ patrol -from=0a359e246ba3c7c76b0ad0e1d734ae103455b7a9 . |
| 40 | +
|
| 41 | +github.com/utilitywarehouse/my-services-mono/services/broadband-services-api/cmd/broadband-services-api |
| 42 | +github.com/utilitywarehouse/my-services-mono/pkg/broadband |
| 43 | +github.com/utilitywarehouse/my-services-mono/services/energy-services-projector/cmd/energy-services-projector |
| 44 | +github.com/utilitywarehouse/my-services-mono/services/energy-services-projector/internal/handler |
| 45 | +``` |
| 46 | + |
| 47 | +Patrol does nothing more than reporting what packages (or other packages they |
| 48 | +depend on) changed in between commits. If for example your goal is to understand |
| 49 | +what Docker images you should build as part of your CI run, and you know your |
| 50 | +executables live under `services/`, you could by filtering your results with |
| 51 | +[`ripgrep`](https://github.com/BurntSushi/ripgrep): |
| 52 | + |
| 53 | +``` |
| 54 | +$ patrol -from=0a359e246ba3c7c76b0ad0e1d734ae103455b7a9 . | rg services |
| 55 | +
|
| 56 | +github.com/utilitywarehouse/my-services-mono/services/broadband-services-api/cmd/broadband-services-api |
| 57 | +github.com/utilitywarehouse/my-services-mono/services/energy-services-projector/cmd/energy-services-projector |
| 58 | +github.com/utilitywarehouse/my-services-mono/services/energy-services-projector/internal/handler |
| 59 | +``` |
| 60 | + |
| 61 | +### Use as a Go library |
| 62 | +If you want to integrate Patrol into your scripts, and your scripts are written |
| 63 | +in Go (maybe using something like [mage](https://magefile.org/)) you can easily do so: |
| 64 | + |
| 65 | +```golang |
| 66 | +package main |
| 67 | + |
| 68 | +import ( |
| 69 | + "fmt" |
| 70 | + |
| 71 | + "github.com/utilitywarehouse/patrol/patrol" |
| 72 | +) |
| 73 | + |
| 74 | +func main() { |
| 75 | + |
| 76 | + repo, err := patrol.NewRepo("path/to/your/repo") |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + |
| 81 | + revision := "a0e002f951f56d53d552f9427b3331b11ea66e92" |
| 82 | + |
| 83 | + changes, err := repo.ChangesFrom(revision) |
| 84 | + if err != nil { |
| 85 | + panic(err) |
| 86 | + } |
| 87 | + |
| 88 | + for _, c := range changes { |
| 89 | + fmt.Println(c) |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Contributing |
| 95 | +So did Patrol blow up on you or you finally saw an actual stack overflow? Graphs |
| 96 | +do that sometimes. Sorry if that happened, but if you found you want to improve |
| 97 | +or fix I'd suggest starting by writing a test. |
| 98 | + |
| 99 | +Tests in this are fairly peculiar, but we need other repositories to test a tool |
| 100 | +like Patrol. You can take a look at any of the case defined in |
| 101 | +[`testdata`](patrol/testdata) but here's a checklist that might help: |
| 102 | + |
| 103 | +- add a new folder within `patrol/testdata` with a name expressing what you're |
| 104 | + trying to test |
| 105 | +- add as many folders as you'd like to `patrol/testdata/{your-test}/commits`. |
| 106 | + Each folder represents an actual commit, and they will be applied on top of |
| 107 | + each other as real commits when tests are run. |
| 108 | +- add a new test case in [patrol/repo\_test.go](patrol/repo_test.go) |
0 commit comments