|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
| 3 | +_is_hb_source_repository() { |
| 4 | + local candidate="$1" |
| 5 | + local remote root candidate_root |
| 6 | + [ -e "$candidate/.git" ] && [ -f "$candidate/scripts/sync-home.sh" ] || return 1 |
| 7 | + remote="$(git -C "$candidate" remote get-url origin 2>/dev/null)" || return 1 |
| 8 | + [ -n "$remote" ] || return 1 |
| 9 | + root="$(git -C "$candidate" rev-parse --show-toplevel 2>/dev/null)" || return 1 |
| 10 | + candidate_root="$(cd -- "$candidate" && pwd -P)" || return 1 |
| 11 | + [ "$(cd -- "$root" && pwd -P)" = "$candidate_root" ] |
| 12 | +} |
| 13 | + |
3 | 14 | resolve_hb_source_repository() { |
4 | 15 | local start_path="${1:-${BASH_SOURCE[0]}}" |
5 | 16 | local allow_legacy="${2:-0}" |
6 | | - local candidate state_path configured |
| 17 | + local candidate state_path configured home_root |
| 18 | + |
| 19 | + home_root="$(cd -- "$HOME" && pwd -P)" |
7 | 20 |
|
8 | 21 | candidate="$(cd -- "$(dirname -- "$start_path")" 2>/dev/null && pwd -P)" || return 2 |
9 | 22 | while [ "$candidate" != "/" ]; do |
10 | | - if [ -e "$candidate/.git" ] && [ -f "$candidate/scripts/sync-home.sh" ]; then |
| 23 | + if [ "$candidate" != "$home_root" ] && _is_hb_source_repository "$candidate"; then |
11 | 24 | printf '%s\n' "$candidate" |
12 | 25 | return 0 |
13 | 26 | fi |
14 | 27 | candidate="$(dirname -- "$candidate")" |
15 | 28 | done |
16 | 29 |
|
17 | 30 | if [ -n "${HOME_BASELINE_SOURCE:-}" ] && |
18 | | - [ -e "${HOME_BASELINE_SOURCE}/.git" ] && |
19 | | - [ -f "${HOME_BASELINE_SOURCE}/scripts/sync-home.sh" ]; then |
| 31 | + _is_hb_source_repository "$HOME_BASELINE_SOURCE"; then |
20 | 32 | (cd -- "$HOME_BASELINE_SOURCE" && pwd -P) |
21 | 33 | return 0 |
22 | 34 | fi |
23 | 35 |
|
24 | 36 | state_path="${HOME}/.home-baseline/source-repository.json" |
25 | 37 | if [ -f "$state_path" ] && command -v jq >/dev/null 2>&1; then |
26 | 38 | configured="$(jq -er '.sourcePath | select(type == "string" and length > 0)' "$state_path" 2>/dev/null || true)" |
27 | | - if [ -n "$configured" ] && [ -e "$configured/.git" ] && |
28 | | - [ -f "$configured/scripts/sync-home.sh" ]; then |
| 39 | + if [ -n "$configured" ] && _is_hb_source_repository "$configured"; then |
29 | 40 | (cd -- "$configured" && pwd -P) |
30 | 41 | return 0 |
31 | 42 | fi |
32 | 43 | fi |
33 | 44 |
|
34 | | - if [ -e "${HOME}/home-baseline-source/.git" ]; then |
| 45 | + if _is_hb_source_repository "${HOME}/home-baseline-source"; then |
35 | 46 | (cd -- "${HOME}/home-baseline-source" && pwd -P) |
36 | 47 | return 0 |
37 | 48 | fi |
38 | | - if [ "$allow_legacy" -eq 1 ] && [ -e "${HOME}/home-baseline-tmp/.git" ]; then |
| 49 | + if [ "$allow_legacy" -eq 1 ] && |
| 50 | + _is_hb_source_repository "${HOME}/home-baseline-tmp"; then |
39 | 51 | printf '%s\n' 'WARN: ~/home-baseline-tmp is deprecated; migrate to ~/home-baseline-source.' >&2 |
40 | 52 | (cd -- "${HOME}/home-baseline-tmp" && pwd -P) |
41 | 53 | return 0 |
|
0 commit comments