summaryrefslogtreecommitdiff
path: root/skills/subagent-driven-development/scripts/sdd-repo
blob: b1756b975cf7df470bd4f8b8a0ed9e24f3d64c5f (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
#!/usr/bin/env bash
# Print the top-level directory of the git working tree the SDD scripts use:
# the current directory's, exactly as git itself picks one. A caller that
# means a different working tree says so with the scripts' -C DIR, which
# they apply (cd) before calling this.
#
# The plan file's location never picks the repository. An executor working
# in a linked worktree commonly holds an absolute path to the plan in the
# main checkout (writing-plans leaves the plan uncommitted there), and its
# workspace and commits belong to the worktree it is working in. And a
# caller standing outside any repository, such as the skill's own directory
# in an installed plugin, is in the wrong place: guessing a repository from
# the plan would silently act on whichever checkout the path points into.
#
# Outside any working tree, stop. --with-commit also stops on a repository
# with no commit yet, for callers that need HEAD as a BASE or a review
# range. The message covers both ways to get here: running from the wrong
# directory (where 'git init' would be the wrong fix) and a new project
# that has no repository or commit yet.
#
# Single source of truth for which repository the SDD and executing-plans
# scripts work in, so no two of them can disagree.
#
# Usage: sdd-repo [--with-commit]
set -euo pipefail

with_commit=false
if [ "${1:-}" = "--with-commit" ]; then
  with_commit=true
  shift
fi

if [ $# -ne 0 ]; then
  echo "usage: sdd-repo [--with-commit]" >&2
  exit 2
fi

if root=$(git rev-parse --show-toplevel 2>/dev/null); then
  if [ "$with_commit" = false ] || git -C "$root" rev-parse --verify --quiet HEAD >/dev/null; then
    printf '%s\n' "$root"
    exit 0
  fi
  said=$(git -C "$root" rev-parse --verify HEAD 2>&1 >/dev/null) || true
else
  said=$(git rev-parse --show-toplevel 2>&1 >/dev/null) || true
fi

# git's own first line always follows the shared message: where git refuses
# a working tree the caller is inside (dubious ownership, git missing from
# PATH, the cwd inside .git), the shared message alone would be wrong, and
# git's line names the real fix.
echo "SDD scripts must run from inside the git working tree you're executing in, with at least one commit (current directory: $PWD). If this is a new project, run 'git init' and make a first commit, then rerun." >&2
echo "git said: ${said%%$'\n'*}" >&2
exit 2