summaryrefslogtreecommitdiff
path: root/git-stack
blob: c5828f6719d4c70a5a66df2330e761c2ff299aab (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env bash
set -euo pipefail

app_name="git-stack"

die() {
  echo "$app_name: $*" >&2
  exit 1
}

in_repo() {
  git rev-parse --git-dir >/dev/null 2>&1
}

require_repo() {
  in_repo || die "not inside a Git repository"
}

git_path() {
  git rev-parse --git-path "$1"
}

stack_file() {
  local p
  p="$(git_path "$app_name/messages")"
  mkdir -p "$(dirname "$p")"
  touch "$p"
  printf '%s\n' "$p"
}

install_hook() {
  local hooks_dir hook tmp
  hooks_dir="$(git_path hooks)"
  hook="$hooks_dir/pre-commit"
  mkdir -p "$hooks_dir"

  if [ -f "$hook" ] && ! grep -Fq "# managed-by: $app_name" "$hook"; then
    return 0
  fi

  tmp="$(mktemp)"
  cat >"$tmp" <<'EOF'
#!/usr/bin/env bash
# managed-by: git-stack
set -euo pipefail

stack_file="$(git rev-parse --git-path git-stack/messages)"
if [ -f "$stack_file" ] && [ -s "$stack_file" ]; then
  echo "git-stack: commit blocked; message stack is not empty" >&2
  echo "git-stack: pending messages:" >&2
  nl -ba "$stack_file" >&2
  echo "git-stack: clear with: git stack pop / git stack clear" >&2
  exit 1
fi
EOF

  chmod +x "$tmp"
  mv "$tmp" "$hook"
}

ensure_ready() {
  require_repo
  install_hook
}

cmd_push() {
  ensure_ready
  [ "$#" -ge 1 ] || die 'usage: git stack push "message"'
  local file msg
  file="$(stack_file)"
  msg="$*"
  printf '%s\n' "$msg" >>"$file"
  echo "$msg"
}

cmd_peek() {
  ensure_ready
  local file
  file="$(stack_file)"
  [ -s "$file" ] || die "stack is empty"
  tail -n 1 "$file"
}

cmd_list() {
  ensure_ready
  local file
  file="$(stack_file)"
  [ -s "$file" ] || exit 0
  nl -ba "$file"
}

cmd_pop() {
  ensure_ready
  local file last tmp
  file="$(stack_file)"
  [ -s "$file" ] || die "stack is empty"

  last="$(tail -n 1 "$file")"
  echo "$last"

  tmp="$(mktemp)"
  sed '$d' "$file" >"$tmp" || true
  mv "$tmp" "$file"
}

cmd_clear() {
  ensure_ready
  : >"$(stack_file)"
}

cmd_install() {
  ensure_ready
  echo "hook installed in $(git_path hooks)/pre-commit"
}

usage() {
  cat <<'EOF'
Usage:
  git stack push "message"
  git stack peek
  git stack list
  git stack pop
  git stack clear
  git stack install
EOF
}

main() {
  [ "$#" -ge 1 ] || {
    usage
    exit 1
  }

  case "$1" in
    push)
      shift
      cmd_push "$@"
      ;;
    peek)
      shift
      cmd_peek "$@"
      ;;
    list)
      shift
      cmd_list "$@"
      ;;
    pop)
      shift
      cmd_pop "$@"
      ;;
    clear)
      shift
      cmd_clear "$@"
      ;;
    install)
      shift
      cmd_install "$@"
      ;;
    *)
      usage
      exit 1
      ;;
  esac
}

main "$@"