forked from huaixv/git-tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.tracer
64 lines (53 loc) · 2.71 KB
/
Makefile.tracer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## Usage
#
# TLDR:
# Run `make -f <path_to_this_makefile>`
# and take a look at your 'auto-tracer' branch
#
# Long:
# 1. Include this Makefile at the end of your project Makefile
# 2. Add `$(call git_commit, "commit message")` to your target
# 3. Run `make` and your modifications will be traced when your target is run
# See also the attached `Makefile` for an example
## Configuration
#
# TRACER_BRANCH will be the branch name, change it to any name you like :)
TRACER_BRANCH = auto-tracer
# GITFLAGS is the flags for git commit, without commit message
GITFLAGS = -q --author='auto-tracer <auto-tracer@saves.me>' --no-verify --allow-empty
THIS_PATH = $(abspath $(lastword $(MAKEFILE_LIST)))
# PROJ_HOME defaluts to the toplevel dir of current Git repo, which will be $PWD of following `git` runs
PROJ_HOME = $(abspath $(shell git rev-parse --show-toplevel))
# GIT_DIR is the `.git` dir of current Git repo, this should work fine with submodules
GIT_DIR = $(abspath $(shell git rev-parse --git-dir))
# WORK_BRANCH is the previous branch we were working on, or a commit hash if detached
# this is used to checkout back to where we depart, it cannot be empty or just literally HEAD
WORK_BRANCH = $(shell git symbolic-ref --short HEAD 2>/dev/null || git rev-parse HEAD)
INDEX_LOCK = $(GIT_DIR)/index.lock
CURR_INDEX = $(GIT_DIR)/index
WORK_INDEX = $(GIT_DIR)/index-$(WORK_BRANCH)
# Default target of this Makefile
trace:
$(call git_commit, "tracer called")
# prototype: git_soft_checkout(branch)
define git_soft_checkout
git checkout --detach -q && git reset --soft $(1) -q -- && git checkout $(1) -q --
endef
# prototype: git_commit(msg)
define git_commit
-@flock $(GIT_DIR) $(MAKE) -C $(PROJ_HOME) -f $(THIS_PATH) .git_commit MSG='$(1)'
-@sync $(GIT_DIR)
endef
.git_commit:
-@while (test -e $(INDEX_LOCK)); do sleep 0.1; done; `# wait for other git instances`
-@git branch $(TRACER_BRANCH) -q 2>/dev/null || true `# create tracer branch if not existent`
-@cp -a $(CURR_INDEX) $(WORK_INDEX) `# backup git index`
-@$(call git_soft_checkout, $(TRACER_BRANCH)) `# switch to tracer branch`
-@git add . -A --ignore-errors `# add files to commit`
-@(echo "> $(MSG)" && echo "$$(whoami) $$(pwd)" && uname -a && uptime `# generate commit msg`) \
| git commit -F - $(GITFLAGS) `# commit changes in tracer branch`
-@$(call git_soft_checkout, $(WORK_BRANCH)) `# switch to work branch`
-@mv $(WORK_INDEX) $(CURR_INDEX) `# restore git index`
.clean_index:
rm -f $(WORK_INDEX)
.PHONY: .git_commit .clean_index trace