GitLabgitlab-org

cli

main
ply5 git calls24 ms99 KiB3 cachedprotocol v2
260 lines10 KiBRaw
1OS = $(shell uname | tr A-Z a-z)
2ARCH ?= $(shell uname -m | sed -e 's/x86_64/amd64/' | sed -e 's/aarch64/arm64/')
3DEBUG ?= false
4export PATH := $(abspath bin/):${PATH}
6# A git hook (such as lefthook's pre-push) exports these; left set, git commands
7# that tests run in temporary repositories act on the repository running the hook.
8unexport GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
10# Build variables
11export CGO_ENABLED ?= 0
12ifeq (${VERBOSE}, 1)
13ifeq ($(filter -v,${GOARGS}),)
14 GOARGS += -v
15endif
16TEST_FORMAT = short-verbose
17endif
19GLAB_VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || git rev-parse --short HEAD)
20BUILD_COMMIT_SHA ?= $(shell git rev-parse --short HEAD)
22ifndef CGO_CPPFLAGS
23 export CGO_CPPFLAGS := $(CPPFLAGS)
24endif
25ifndef CGO_CFLAGS
26 export CGO_CFLAGS := $(CFLAGS)
27endif
28ifndef CGO_LDFLAGS
29 export CGO_LDFLAGS := $(LDFLAGS)
30endif
32# Probe by running the tool, not by looking it up on PATH. A version manager
33# leaves a shim on PATH for every tool it knows about, including ones it has no
34# version configured for, so `which` finds a name that then fails to execute and
35# the bin/ fallback below never gets a chance to build a working copy.
36HASGOTESTSUM := $(shell gotestsum --version > /dev/null 2>&1 && echo 1)
37HASGOCILINT := $(shell golangci-lint --version > /dev/null 2>&1 && echo 1)
39ifdef HASGOTESTSUM
40 GOTEST=gotestsum
41else
42 GOTEST=bin/gotestsum
43endif
45ifdef HASGOCILINT
46 GOLINT=golangci-lint
47else
48 GOLINT=bin/golangci-lint
49endif
51GO_LDFLAGS := -X main.commit=$(BUILD_COMMIT_SHA) $(GO_LDFLAGS)
52GO_LDFLAGS := $(GO_LDFLAGS) -X main.version=$(GLAB_VERSION)
53GOURL ?= gitlab.com/gitlab-org/cli
54BUILDLOC ?= ./bin/glab
56# Dependency versions
57GOTESTSUM_VERSION = 1.13.0
58GOLANGCI_LINT_VERSION := $(shell awk '$$1 == "golangci-lint" { print $$2 }' .tool-versions)
60# Add the ability to override some variables
61# Use with care
62-include override.mk
64.PHONY: build
65.DEFAULT_GOAL := build
66build:
67 go build -trimpath -ldflags "$(GO_LDFLAGS) -X main.debugMode=$(DEBUG) -w -s" -o $(BUILDLOC) $(GOURL)/cmd/glab
69clean: ## Clear the working area and the project
70 rm -rf ./bin ./.glab-cli ./test/testdata-* ./coverage.txt coverage-*
71.PHONY: clean
73.PHONY: install
74install: ## Install glab in $GOPATH/bin
75 GO111MODULE=on go install -trimpath -ldflags "$(GO_LDFLAGS) -X main.debugMode=$(DEBUG) -w -s" $(GOURL)/cmd/glab
77.PHONY: run
78run:
79 go run -trimpath -ldflags "$(GO_LDFLAGS) -X main.debugMode=$(DEBUG)" ./cmd/glab $(run)
81.PHONY: rt
82rt: ## Test release without publishing
83 goreleaser release --snapshot --clean
85.PHONY: rtdebug
86rtdebug: ## Test release with debug info
87 goreleaser release --snapshot --clean --verbose
89.PHONY: release
90release:
91 goreleaser $(run)
93.PHONY: manpage
94manpage: ## Generate manual pages
95 go run ./cmd/gen-docs/docs.go --manpage --path ./share/man/man1
97.PHONY: gen-docs
98gen-docs: ## Generate web docs and the GitLab Docs navigation submenu for glab
99 go run ./cmd/gen-docs/docs.go
101.PHONY: check
102check: test lint check-embed check-args ## Run tests and linters
104.PHONY: check-embed
105# grep --include is GNU-only; use find|xargs so this works with BusyBox grep in CI (alpine:3).
106check-embed: ## Forbid embedding .md files as help text; use heredoc.Doc instead
107 @matches=$$(find internal/ -name '*.go' -print0 | xargs -0 grep -nE '^[[:space:]]*//go:embed[[:space:]]+[^[:space:]]*\.md([[:space:]]|$$)' 2>/dev/null || true); \
108 if [ -n "$$matches" ]; then \
109 echo "Forbidden pattern: //go:embed of a .md file. Inline help text via heredoc.Doc:"; \
110 echo "$$matches"; \
111 exit 1; \
112 fi
114.PHONY: check-args
115# golangci-lint cannot express this: forbidigo matches call names, not argument
116# values, so it cannot distinguish ExactArgs(0) from a legitimate ExactArgs(1).
117check-args: ## Forbid cobra.ExactArgs(0); use cobra.NoArgs instead
118 @matches=$$(find internal/ cmd/ -name '*.go' -print0 | xargs -0 grep -nE 'cobra\.ExactArgs\([[:space:]]*0[[:space:]]*\)' 2>/dev/null || true); \
119 if [ -n "$$matches" ]; then \
120 echo "Forbidden pattern: cobra.ExactArgs(0). Use cobra.NoArgs, which reports unexpected arguments by name:"; \
121 echo "$$matches"; \
122 exit 1; \
123 fi
125ifdef HASGOTESTSUM
126bin/gotestsum:
127 @echo "Skip this"
128else
129bin/gotestsum: bin/gotestsum-${GOTESTSUM_VERSION}
130 @ln -sf gotestsum-${GOTESTSUM_VERSION} bin/gotestsum
131endif
133bin/gotestsum-${GOTESTSUM_VERSION}:
134 @mkdir -p bin
135 curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_${ARCH}.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION}
137TEST_PKGS ?= ./internal/... ./cmd/...
139# Recipe prefix that unsets the variables glab reads, so a CI job or a
140# configured shell does not change unit test results. Tests that need one set
141# it with t.Setenv.
142UNSET_GLAB_ENV = unset $$(compgen -e | grep -E '^(GITLAB_|GLAB_|CI_|(CI|GL_HOST)$$)' | grep -vx CI_PROJECT_PATH);
144.PHONY: test
145# NOTE: some tests require uncustomized environment variables for VISUAL, EDITOR, and PAGER to test
146# certain behaviors related to glab output preferences. Also, the CI_PROJECT_PATH environment variable
147# is set to support forked clones that will have a different origin remote url.
148test: TEST_FORMAT ?= short
149test: SHELL = /bin/bash # set environment variables to ensure consistent test behavior
150test: VISUAL=
151test: EDITOR=
152test: PAGER=
153test: export CI_PROJECT_PATH=$(shell git remote get-url origin)
154test: bin/gotestsum ## Run tests
155 $(UNSET_GLAB_ENV) $(GOTEST) --no-summary=skipped --format-hide-empty-pkg --junitfile ./coverage-unit.xml --format ${TEST_FORMAT} -- -coverprofile=./coverage-unit.txt -covermode=atomic $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...)
157.PHONY: test-changed
158test-changed: bin/gotestsum ## Run tests on packages changed vs origin/main (including reverse dependencies)
159 $(eval CHANGED_PKGS := $(shell ./scripts/changed-test-pkgs.sh))
160 @if [ -z "$(CHANGED_PKGS)" ]; then \
161 echo "No Go packages changed vs origin/main — skipping tests"; \
162 else \
163 $(MAKE) test TEST_PKGS="$(CHANGED_PKGS)"; \
164 fi
166.PHONY: test-race
167test-race: TEST_FORMAT ?= short
168test-race: SHELL = /bin/bash # set environment variables to ensure consistent test behavior
169test-race: VISUAL=
170test-race: EDITOR=
171test-race: PAGER=
172test-race: export CI_PROJECT_PATH=$(shell git remote get-url origin)
173test-race: bin/gotestsum ## Run tests with race detection
174 $(UNSET_GLAB_ENV) $(GOTEST) --no-summary=skipped --format-hide-empty-pkg --junitfile ./coverage-unit.xml --format ${TEST_FORMAT} -- -coverprofile=./coverage-unit.txt -covermode=atomic -race $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...)
176# Only the packages that hold integration tests. Derived rather than listed so
177# a new *_integration_test.go is picked up without editing this file. The
178# TEST_PKGS default covers every package, which made this target re-run the
179# whole unit suite under -race on top of the tests:unit job that already does.
180# A command-line TEST_PKGS= still wins over this target-specific default.
181INTEGRATION_PKGS = $(shell find ./internal ./cmd -name '*_integration_test.go' -exec dirname {} \; | sort -u)
183.PHONY: integration-test-race
184integration-test-race: TEST_FORMAT ?= short
185integration-test-race: SHELL = /bin/bash # set environment variables to ensure consistent test behavior
186integration-test-race: VISUAL=
187integration-test-race: EDITOR=
188integration-test-race: PAGER=
189integration-test-race: TEST_PKGS = $(INTEGRATION_PKGS)
190integration-test-race: export CI_PROJECT_PATH=$(shell git remote get-url origin)
191integration-test-race: bin/gotestsum ## Run tests with race detection
192 $(GOTEST) --no-summary=skipped --format-hide-empty-pkg --junitfile ./coverage-integration.xml --format ${TEST_FORMAT} -- -coverprofile=./coverage-integration.txt -covermode=atomic -race -tags=integration $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...) -count=1
194ifdef HASGOCILINT
195bin/golangci-lint:
196 @echo "Skip this"
197else
198bin/golangci-lint: bin/golangci-lint-${GOLANGCI_LINT_VERSION}
199 @ln -sf golangci-lint-${GOLANGCI_LINT_VERSION} bin/golangci-lint
200endif
202bin/golangci-lint-${GOLANGCI_LINT_VERSION}:
203 @mkdir -p bin
204 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- -b ./bin v${GOLANGCI_LINT_VERSION}
205 @mv bin/golangci-lint $@
207.PHONY: coverage-merge
208coverage-merge: ## Merge coverage profiles from unit and integration tests
209 @[ -s coverage-unit.txt ] && [ -s coverage-integration.txt ] || { echo "coverage-merge: both coverage-unit.txt and coverage-integration.txt must exist and be non-empty" >&2; exit 1; }
210 @[ "$$(awk 'FNR==1' coverage-unit.txt coverage-integration.txt | sort -u)" = "mode: atomic" ] || { echo "coverage-merge: profiles must share a single 'mode: atomic' header" >&2; exit 1; }
211 { echo "mode: atomic"; tail -q -n +2 coverage-unit.txt coverage-integration.txt; } > coverage.txt
213COVERAGE_PROFILE ?= coverage.txt
215.PHONY: coverage-report
216coverage-report: ## Run coverage report
217 go tool cover -func $(COVERAGE_PROFILE)
219.PHONY: lint
220lint: bin/golangci-lint ## Run linter
221 $(GOLINT) run
223.PHONY: fix
224fix: bin/golangci-lint ## Fix lint violations
225 $(GOLINT) fmt
226 $(GOLINT) run --fix
228.PHONY: generate
229generate: ## Run go generate
230 go generate ./...
232.PHONY: list-todo
233list-todo: ## Detect FIXME, TODO and other comment keywords
234 golangci-lint run --enable=godox --disable-all
236.PHONY: bootstrap
237bootstrap: ## Install development tools for git hooks
238 @./scripts/bootstrap.sh
240.PHONY: lint-shell
241lint-shell: ## Lint all shell scripts
242 @if command -v shellcheck > /dev/null 2>&1; then \
243 echo "Linting shell scripts..."; \
244 find . -name "*.sh" -type f -not -path "*/node_modules/*" -exec shellcheck {} \;; \
245 echo "✓ Shell script linting complete"; \
246 else \
247 echo "⚠️ shellcheck not found. Run 'make bootstrap' to install tools."; \
248 exit 1; \
249 fi
251# Add custom targets here
252-include custom.mk
254.PHONY: list
255list: ## List all make targets
256 @${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
258.PHONY: help
259help:
260 @grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Diagnostics5 git calls · 24 ms
CallStatusSizeTime024 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=19
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetch2003.9 KiB · hit19 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3997
git.response.cache=HIT
git.response.duration_ms=19
git.fetch.wants=c5c93a85070cca1538378e9727adb85e44aaac15
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3997
git.pkt.payload.max=3970
git.sideband.band1.bytes=3970
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e1056c3c03910d
Request
79b18bec…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format