Skip to content

Commit

Permalink
fix: dirty check (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrop committed Jan 8, 2024
1 parent ebcade5 commit f6267eb
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ else
endif

GOBIN ?= $(shell go env GOPATH)/bin
GOMAKE ?= github.com/tkrop/[email protected].23
GOMAKE ?= github.com/tkrop/[email protected].24
TARGETS := $(shell command -v go-make >/dev/null || \
go install $(GOMAKE) && go-make targets)

Expand Down
46 changes: 29 additions & 17 deletions Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ TOOLS_GO := $(TOOLS_GO) \
github.com/icholy/gomajor \
github.com/golang/mock/mockgen \
github.com/tkrop/go-testing/cmd/mock \
github.com/tkrop/go-make
github.com/tkrop/go-make@v0.0.24
TOOLS_SH := $(TOOLS_SH) \
github.com/anchore/syft \
github.com/anchore/grype

# function for correct gol-package handling.
go-pkg = $(shell awk -v mode="$(1)" -v filter="$(3)" -v not="$(4)" ' \
# function for correct go-package handling.
go-pkg = $(shell awk -v mode="$(2)" -v filter="$(3)" -v not="$(4)" ' \
BEGIN { FS = "[/@]"; RS = "[ \n\r]" } { \
field = NF; \
if (version = index($$0, "@")) { field-- } \
Expand Down Expand Up @@ -98,8 +98,15 @@ go-pkg = $(shell awk -v mode="$(1)" -v filter="$(3)" -v not="$(4)" ' \
if (version) { \
return substr($$0, 1, index($$0, "@") - 1) \
} else { return $$0 } \
}' <<<"$(2)")

}' <<<"$(1)")
git-pkg = $(shell awk -v mode="$(2)" ' \
BEGIN { RS = "[ \n\r]" } { \
if (mode == "repo") { \
print "git@" gensub("/",":",1,substr($$0, 1, index($$0, "@") - 1)) ".git" \
} else if (mode == "tag") { \
print substr($$0, index($$0, "@") + 1) \
} \
}' <<<"$(1)")

BROWSER ?= xdg-open
VERSION ?= snapshot
Expand Down Expand Up @@ -220,8 +227,8 @@ COMMANDS := $(shell $(FIND) -name "main.go" | xargs -r readlink -f | \
COMMANDS_PKG := $(shell $(FIND) -name "main.go" | xargs -r readlink -f | \
sed -E "s/^(.*\/([^/]*))\/main.go$$/\2=\1/; s|$(CURDIR)|.|")
COMMANDS_REGEX := $(shell echo "^$(COMMANDS)$$" | tr ' ' '|' )
COMMANDS_GO := $(call go-pkg,cmd,$(TOOLS_GO),$(COMMANDS_REGEX),not)
COMMANDS_SH := $(call go-pkg,cmd,$(TOOLS_SH),$(COMMANDS_REGEX),not)
COMMANDS_GO := $(call go-pkg,$(TOOLS_GO),cmd,$(COMMANDS_REGEX),not)
COMMANDS_SH := $(call go-pkg,$(TOOLS_SH),cmd,$(COMMANDS_REGEX),not)


# Setup optimized golang mock setup environment.
Expand Down Expand Up @@ -368,15 +375,20 @@ targets::
else cat -; fi || true;

#@ create a clone of the base repository to update from.
BASE ?= git@github.com:tkrop/go-make.git
setup/base:: # TODO: support cloning of a distinct revision.
GOMAKE ?= github.com/tkrop/go-make@v0.0.24
setup/base::
@$(eval BASEDIR = $(shell mktemp -d)) ( \
trap "rm -rf $${DIR}" INT TERM EXIT; \
while pgrep $(MAKE) > /dev/null; do sleep 1; done; \
rm -rf $${DIR} \
) & \
REPO=$(call git-pkg,$(GOMAKE),repo); TAG=$(call git-pkg,$(GOMAKE),tag); \
git clone --no-checkout --depth 2 --single-branch \
--branch main $(BASE) $(BASEDIR) 2> /dev/null; \
--branch main $${REPO} $(BASEDIR) 2> /dev/null && \
if [ "$${TAG}" != "latest" ]; then \
cd $(BASEDIR) && git checkout tags/$${TAG} 2> /dev/null || \
(echo "error: checking out $${REPO} [$${TAG}]" >"/dev/sdterr"; exit 1) \
fi || (echo "error: cloning $${REPO} [$${TAG}]" >"/dev/sdterr"; exit 1) \


## Git-Support: targets for standard git commands (experimental).
Expand All @@ -397,23 +409,23 @@ git-graph::
--pretty=format:"%Cred%h %Cgreen%ad%Creset | %s $${SUFFIX}";
#@ cleans up all local branches except the default and current branch.
git-clean::
@BRANCH="$$(git branch --show-current)"; \
@git fetch; BRANCH="$$(git branch --show-current)"; \
MAIN="$$(git remote show origin | grep "HEAD branch:" | cut -d ' ' -f 5)"; \
BRANCHES=($$(git branch | cut -c 3- | grep -vw "$${MAIN}\|$${BRANCH}")); \
for BRANCH in $"$${BRANCHES[@]}"; do \
git branch --delete "$${BRANCH}"; \
done;
#@ checks out default branch and cleans up all local branches except the default.
git-reset::
@BRANCH="$$(git branch --show-current)"; \
@git fetch; BRANCH="$$(git branch --show-current)"; \
MAIN="$$(git remote show origin | grep "HEAD branch:" | cut -d ' ' -f 5)"; \
if [ "$${BRANCH}" != "$${MAIN}" ]; then git checkout "$${MAIN}" || exit 1; fi; \
for BRANCH in $$(git branch | cut -c 3- | grep -vw "$${MAIN}"); do \
git branch --delete "$${BRANCH}"; \
done;

#@ <branch> <message> # creates a branch with the current change set using next issue.
git-create:: git-create-feature
# TODO: check whether I'm on the default branch and on remote HEAD.
git-create-fix git-create-chore git-create-feature:: git-create-%:
@BRANCH="$(firstword $(RUNARGS))"; \
MESSAGE="$(wordlist 2,$(words $(RUNARGS)),$(RUNARGS))"; \
Expand Down Expand Up @@ -555,7 +567,7 @@ test-go = \
fi; \
echo -e "\t$${GOCHANGE} your local go version to $${VERSION} or "; \
echo -e "\trun 'make update-go $${VERSION}' to adjust the project!"; \
exit -1; \
exit 1; \
fi; \
done;

Expand Down Expand Up @@ -1011,7 +1023,7 @@ install-all:: $(TARGETS_INSTALL_ALL)
# install go tools used by the project.
$(TARGETS_INSTALL_GO):: install-%: $(GOBIN)/%
$(addprefix $(GOBIN)/,$(COMMANDS_GO)): $(GOBIN)/%:
go install $(call go-pkg,install,$(TOOLS_GO),^$*$$);
go install $(call go-pkg,$(TOOLS_GO),install,^$*$$);
# install go tools providing an install.sh script.
$(TARGETS_INSTALL_SH):: install-%: $(GOBIN)/%
$(addprefix $(GOBIN)/,$(COMMANDS_SH)): $(GOBIN)/%:
Expand Down Expand Up @@ -1041,7 +1053,7 @@ uninstall-all:: $(TARGETS_UNINSTALL_ALL)

# uninstall go tools used by the project.
$(TARGETS_UNINSTALL_GO):: uninstall-%:
@#PACKAGE=$(call go-pkg,strip,$(TOOLS_GO),^$*$$); \
@#PACKAGE=$(call go-pkg,$(TOOLS_GO),strip,^$*$$); \
rm -rf $(wildcard $(GOBIN)/$* $(GOBIN)/$*.config);
# uninstall npm based tools used by the project.
$(TARGETS_UNINSTALL_NPM):: uninstall-%:
Expand Down Expand Up @@ -1359,7 +1371,7 @@ update-tools:: $(TARGETS_UPDATE_GO) $(TARGETS_INSTALL_SH) $(TARGETS_INSTALL_NPM)

# update go tools used by the project.
$(TARGETS_UPDATE_GO):: update-%:
go install $(call go-pkg,update,$(TOOLS_GO),^$*$$);
go install $(call go-pkg,$(TOOLS_GO),update,^$*$$);

## Custom: custom extension targets.

Expand Down
3 changes: 2 additions & 1 deletion Makefile.ext
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ clean::

bump::
@VERSION="$$(cat VERSION)"; \
sed -i -e "s#\(github.com/tkrop/go-make@v\)[^ ]*#\1$${VERSION}#" Makefile;
sed -i -e "s#\(github.com[/:]tkrop/go-make@v\)[^ ]*#\1$${VERSION}#" \
Makefile Makefile.base; \
cp Makefile Makefile.base config;

commit::
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.23
0.0.24
2 changes: 1 addition & 1 deletion config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ else
endif

GOBIN ?= $(shell go env GOPATH)/bin
GOMAKE ?= github.com/tkrop/[email protected].23
GOMAKE ?= github.com/tkrop/[email protected].24
TARGETS := $(shell command -v go-make >/dev/null || \
go install $(GOMAKE) && go-make targets)

Expand Down
46 changes: 29 additions & 17 deletions config/Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ TOOLS_GO := $(TOOLS_GO) \
github.com/icholy/gomajor \
github.com/golang/mock/mockgen \
github.com/tkrop/go-testing/cmd/mock \
github.com/tkrop/go-make
github.com/tkrop/go-make@v0.0.24
TOOLS_SH := $(TOOLS_SH) \
github.com/anchore/syft \
github.com/anchore/grype

# function for correct gol-package handling.
go-pkg = $(shell awk -v mode="$(1)" -v filter="$(3)" -v not="$(4)" ' \
# function for correct go-package handling.
go-pkg = $(shell awk -v mode="$(2)" -v filter="$(3)" -v not="$(4)" ' \
BEGIN { FS = "[/@]"; RS = "[ \n\r]" } { \
field = NF; \
if (version = index($$0, "@")) { field-- } \
Expand Down Expand Up @@ -98,8 +98,15 @@ go-pkg = $(shell awk -v mode="$(1)" -v filter="$(3)" -v not="$(4)" ' \
if (version) { \
return substr($$0, 1, index($$0, "@") - 1) \
} else { return $$0 } \
}' <<<"$(2)")

}' <<<"$(1)")
git-pkg = $(shell awk -v mode="$(2)" ' \
BEGIN { RS = "[ \n\r]" } { \
if (mode == "repo") { \
print "git@" gensub("/",":",1,substr($$0, 1, index($$0, "@") - 1)) ".git" \
} else if (mode == "tag") { \
print substr($$0, index($$0, "@") + 1) \
} \
}' <<<"$(1)")

BROWSER ?= xdg-open
VERSION ?= snapshot
Expand Down Expand Up @@ -220,8 +227,8 @@ COMMANDS := $(shell $(FIND) -name "main.go" | xargs -r readlink -f | \
COMMANDS_PKG := $(shell $(FIND) -name "main.go" | xargs -r readlink -f | \
sed -E "s/^(.*\/([^/]*))\/main.go$$/\2=\1/; s|$(CURDIR)|.|")
COMMANDS_REGEX := $(shell echo "^$(COMMANDS)$$" | tr ' ' '|' )
COMMANDS_GO := $(call go-pkg,cmd,$(TOOLS_GO),$(COMMANDS_REGEX),not)
COMMANDS_SH := $(call go-pkg,cmd,$(TOOLS_SH),$(COMMANDS_REGEX),not)
COMMANDS_GO := $(call go-pkg,$(TOOLS_GO),cmd,$(COMMANDS_REGEX),not)
COMMANDS_SH := $(call go-pkg,$(TOOLS_SH),cmd,$(COMMANDS_REGEX),not)


# Setup optimized golang mock setup environment.
Expand Down Expand Up @@ -368,15 +375,20 @@ targets::
else cat -; fi || true;

#@ create a clone of the base repository to update from.
BASE ?= git@github.com:tkrop/go-make.git
setup/base:: # TODO: support cloning of a distinct revision.
GOMAKE ?= github.com/tkrop/go-make@v0.0.24
setup/base::
@$(eval BASEDIR = $(shell mktemp -d)) ( \
trap "rm -rf $${DIR}" INT TERM EXIT; \
while pgrep $(MAKE) > /dev/null; do sleep 1; done; \
rm -rf $${DIR} \
) & \
REPO=$(call git-pkg,$(GOMAKE),repo); TAG=$(call git-pkg,$(GOMAKE),tag); \
git clone --no-checkout --depth 2 --single-branch \
--branch main $(BASE) $(BASEDIR) 2> /dev/null; \
--branch main $${REPO} $(BASEDIR) 2> /dev/null && \
if [ "$${TAG}" != "latest" ]; then \
cd $(BASEDIR) && git checkout tags/$${TAG} 2> /dev/null || \
(echo "error: checking out $${REPO} [$${TAG}]" >"/dev/sdterr"; exit 1) \
fi || (echo "error: cloning $${REPO} [$${TAG}]" >"/dev/sdterr"; exit 1) \


## Git-Support: targets for standard git commands (experimental).
Expand All @@ -397,23 +409,23 @@ git-graph::
--pretty=format:"%Cred%h %Cgreen%ad%Creset | %s $${SUFFIX}";
#@ cleans up all local branches except the default and current branch.
git-clean::
@BRANCH="$$(git branch --show-current)"; \
@git fetch; BRANCH="$$(git branch --show-current)"; \
MAIN="$$(git remote show origin | grep "HEAD branch:" | cut -d ' ' -f 5)"; \
BRANCHES=($$(git branch | cut -c 3- | grep -vw "$${MAIN}\|$${BRANCH}")); \
for BRANCH in $"$${BRANCHES[@]}"; do \
git branch --delete "$${BRANCH}"; \
done;
#@ checks out default branch and cleans up all local branches except the default.
git-reset::
@BRANCH="$$(git branch --show-current)"; \
@git fetch; BRANCH="$$(git branch --show-current)"; \
MAIN="$$(git remote show origin | grep "HEAD branch:" | cut -d ' ' -f 5)"; \
if [ "$${BRANCH}" != "$${MAIN}" ]; then git checkout "$${MAIN}" || exit 1; fi; \
for BRANCH in $$(git branch | cut -c 3- | grep -vw "$${MAIN}"); do \
git branch --delete "$${BRANCH}"; \
done;

#@ <branch> <message> # creates a branch with the current change set using next issue.
git-create:: git-create-feature
# TODO: check whether I'm on the default branch and on remote HEAD.
git-create-fix git-create-chore git-create-feature:: git-create-%:
@BRANCH="$(firstword $(RUNARGS))"; \
MESSAGE="$(wordlist 2,$(words $(RUNARGS)),$(RUNARGS))"; \
Expand Down Expand Up @@ -555,7 +567,7 @@ test-go = \
fi; \
echo -e "\t$${GOCHANGE} your local go version to $${VERSION} or "; \
echo -e "\trun 'make update-go $${VERSION}' to adjust the project!"; \
exit -1; \
exit 1; \
fi; \
done;

Expand Down Expand Up @@ -1011,7 +1023,7 @@ install-all:: $(TARGETS_INSTALL_ALL)
# install go tools used by the project.
$(TARGETS_INSTALL_GO):: install-%: $(GOBIN)/%
$(addprefix $(GOBIN)/,$(COMMANDS_GO)): $(GOBIN)/%:
go install $(call go-pkg,install,$(TOOLS_GO),^$*$$);
go install $(call go-pkg,$(TOOLS_GO),install,^$*$$);
# install go tools providing an install.sh script.
$(TARGETS_INSTALL_SH):: install-%: $(GOBIN)/%
$(addprefix $(GOBIN)/,$(COMMANDS_SH)): $(GOBIN)/%:
Expand Down Expand Up @@ -1041,7 +1053,7 @@ uninstall-all:: $(TARGETS_UNINSTALL_ALL)

# uninstall go tools used by the project.
$(TARGETS_UNINSTALL_GO):: uninstall-%:
@#PACKAGE=$(call go-pkg,strip,$(TOOLS_GO),^$*$$); \
@#PACKAGE=$(call go-pkg,$(TOOLS_GO),strip,^$*$$); \
rm -rf $(wildcard $(GOBIN)/$* $(GOBIN)/$*.config);
# uninstall npm based tools used by the project.
$(TARGETS_UNINSTALL_NPM):: uninstall-%:
Expand Down Expand Up @@ -1359,7 +1371,7 @@ update-tools:: $(TARGETS_UPDATE_GO) $(TARGETS_INSTALL_SH) $(TARGETS_INSTALL_NPM)

# update go tools used by the project.
$(TARGETS_UPDATE_GO):: update-%:
go install $(call go-pkg,update,$(TOOLS_GO),^$*$$);
go install $(call go-pkg,$(TOOLS_GO),update,^$*$$);

## Custom: custom extension targets.

Expand Down
7 changes: 4 additions & 3 deletions internal/make/fixtures/targets-trace.err
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
call: targets --trace
info: {"path":"github.com/tkrop/go-make","repo":"[email protected]:tkrop/go-make","build":"0001-01-01T00:00:00Z","commit":"0001-01-01T00:00:00Z","dirty":true,"go":"{{GOVERSION}}","platform":"{{PLATFORM}}","compiler":"{{COMPILER}}"}
exec: make --file Makefile.base targets --trace [go-make/internal/make]
Makefile.base:18: info: please customize variables in Makefile.vars
Makefile.base:211: info: please define custom functions in Makefile.defs
exec: git status --short [make.test.config]
exec: make --file make.test.config/Makefile.base targets --trace [go-make/internal/make]
make.test.config/Makefile.base:18: info: please customize variables in Makefile.vars
make.test.config/Makefile.base:218: info: please define custom functions in Makefile.defs
4 changes: 2 additions & 2 deletions internal/make/fixtures/targets-trace.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Makefile.base:362: target 'targets' does not exist
make.test.config/Makefile.base:369: target 'targets' does not exist
make --no-builtin-rules --no-builtin-variables --print-data-base \
--question --makefile=Makefile.base 2>/dev/null | \
--question --makefile=make.test.config/Makefile.base 2>/dev/null | \
if [ "" != "raw" ]; then awk -v RS= -F: ' \
/(^|\n)# Files(\n|$)/,/(^|\n)# Finished Make data base/ { \
if ($1 !~ "^[#.]") { print $1 } \
Expand Down
4 changes: 2 additions & 2 deletions internal/make/fixtures/targets.err
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Makefile.base:18: info: please customize variables in Makefile.vars
Makefile.base:211: info: please define custom functions in Makefile.defs
make.test.config/Makefile.base:18: info: please customize variables in Makefile.vars
make.test.config/Makefile.base:218: info: please define custom functions in Makefile.defs
21 changes: 20 additions & 1 deletion internal/make/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ var (
cmdGitClone = []string{"git", "clone", "--depth=1"}
// Base command array for `git fetch` arguments.
cmdGitFetch = []string{"git", "fetch"}
// Base command array for `git status`` arguments.
cmdGitStatus = []string{"git", "status", "--short"}
// Base command array for `git reset --hard` arguments.
cmdGitHashReset = []string{"git", "reset", "--hard"}
// Base command array for `git rev-list --max-count=1` arguments.
Expand All @@ -102,6 +104,11 @@ func CmdGitClone(repo, dir string) []string {
return append(cmdGitClone, repo, dir)
}

// CmdGitStatus creates the argument array of a `git status` command.
func CmdGitStatus() []string {
return cmdGitStatus
}

// CmdGitFetch creates the argument array of a `git fetch` command using the
// given source repository.
func CmdGitFetch(repo string) []string {
Expand Down Expand Up @@ -189,7 +196,9 @@ func (gm *GoMake) updateGoMakeRepo() error {
}

// Do never update on dirty revisions.
if gm.Info.Dirty {
if ok, err := gm.repoIsDirty(); err != nil {
return err
} else if ok {
return nil
}

Expand Down Expand Up @@ -225,6 +234,16 @@ func (gm *GoMake) cloneGoMakeExec(repo string) error {
CmdGitClone(repo, gm.MakeDir)...)
}

// repoIsDirty returns whether the go-make command repository is dirty.
func (gm *GoMake) repoIsDirty() (bool, error) {
builder := strings.Builder{}
if err := gm.exec(&builder, gm.Stderr, gm.MakeDir,
CmdGitStatus()...); err != nil {
return false, err
}
return strings.TrimSpace(builder.String()) != "", nil
}

// updateRevision updates the current revision of the go-make command
// repository as required by the go-make command. If the update fails the
// an error is returned.
Expand Down
Loading

0 comments on commit f6267eb

Please sign in to comment.