Skip to content
Merged
16 changes: 12 additions & 4 deletions debup-pkg/usr/local/bin/dbp
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ debup_search() {
local -a global_types=()
local -a global_release_ids=()
local -a global_assets=()
local -a global_expected_packages=()
local display_idx=1
local apt_results=""
local apt_search_failed=false
Expand All @@ -987,6 +988,7 @@ debup_search() {
global_types[display_idx]="APT"
global_release_ids[display_idx]=""
global_assets[display_idx]=""
global_expected_packages[display_idx]=""

printf "${YELLOW}[%d]${NC} ${BOLD}%-35s${NC} ${GRAY}(APT)${NC}\n ${GRAY}%s${NC}\n" \
"$display_idx" "$apt_pkg" "${apt_desc:0:70}"
Expand All @@ -1004,6 +1006,7 @@ debup_search() {
local -a candidate_repos=()
local -a candidate_stars=()
local -a candidate_descs=()
local -a candidate_expected_packages=()
local -A seen_repos=()
local catalog_file="${SCRIPT_DIR}/../share/debup/curated_db.txt"
local catalog_entries=""
Expand All @@ -1012,15 +1015,16 @@ debup_search() {
catalog_entries=$(catalog_search_repositories "$catalog_file" "$query") || catalog_entries=""
fi

local catalog_repo catalog_package repo_key
while IFS='|' read -r catalog_repo catalog_package; do
local catalog_repo catalog_package catalog_expected_package repo_key
while IFS='|' read -r catalog_repo catalog_package catalog_expected_package; do
[ -n "$catalog_repo" ] || continue
repo_key="${catalog_repo,,}"
[ -n "${seen_repos[$repo_key]+present}" ] && continue
seen_repos[$repo_key]=1
candidate_repos+=("$catalog_repo")
candidate_stars+=("-")
candidate_descs+=("Curated package: $catalog_package")
candidate_expected_packages+=("$catalog_expected_package")
done <<< "$catalog_entries"

local raw_results=""
Expand Down Expand Up @@ -1051,6 +1055,7 @@ debup_search() {
candidate_repos+=("$repo_name")
candidate_stars+=("$stars")
candidate_descs+=("$description")
candidate_expected_packages+=("")
done
else
github_search_failed=true
Expand All @@ -1074,11 +1079,12 @@ debup_search() {
local gh_valid=0
local release_check_failed=0
local candidate_index=0
local candidate_repo candidate_stars_value candidate_desc check_release deb_assets compatible_debs chosen_asset release_id release_tag
local candidate_repo candidate_stars_value candidate_desc candidate_expected_package check_release deb_assets compatible_debs chosen_asset release_id release_tag

for candidate_repo in "${candidate_repos[@]}"; do
candidate_stars_value="${candidate_stars[$candidate_index]}"
candidate_desc="${candidate_descs[$candidate_index]}"
candidate_expected_package="${candidate_expected_packages[$candidate_index]}"
candidate_index=$((candidate_index + 1))

if ! check_release=$(fetch_github_api "repos/${candidate_repo}/releases/latest" 10); then
Expand Down Expand Up @@ -1110,6 +1116,7 @@ debup_search() {
global_types[display_idx]="GH"
global_release_ids[display_idx]="$release_id"
global_assets[display_idx]="$chosen_asset"
global_expected_packages[display_idx]="$candidate_expected_package"

if [ "$candidate_stars_value" = "-" ]; then
printf " ${YELLOW}[%d]${NC} ${BOLD}%-35s${NC} ${CYAN}(curated)${NC}\n ${GRAY}%s${NC}\n" \
Expand Down Expand Up @@ -1175,10 +1182,11 @@ debup_search() {
local selected_repo="$selected_target"
local selected_release_id="${global_release_ids[choice]}"
local selected_asset="${global_assets[choice]}"
local selected_expected_package="${global_expected_packages[choice]}"
printf "${CYAN}%b${NC} ${GREEN}%s${NC}...\n" "$(printf "$MSG_INSPECTING_ASSETS" "")" "$selected_repo"
printf "${CYAN}Selected release ID: %s; asset: %s${NC}\n" "$selected_release_id" "$selected_asset"

check_and_update "$selected_repo" "" false "$selected_release_id" "$selected_asset"
check_and_update "$selected_repo" "$selected_expected_package" false "$selected_release_id" "$selected_asset"
local selected_install_status=$?
if [ "$selected_install_status" -eq 1 ]; then
install_failed=1
Expand Down
16 changes: 12 additions & 4 deletions debup-pkg/usr/local/bin/debup
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ debup_search() {
local -a global_types=()
local -a global_release_ids=()
local -a global_assets=()
local -a global_expected_packages=()
local display_idx=1
local apt_results=""
local apt_search_failed=false
Expand All @@ -987,6 +988,7 @@ debup_search() {
global_types[display_idx]="APT"
global_release_ids[display_idx]=""
global_assets[display_idx]=""
global_expected_packages[display_idx]=""

printf "${YELLOW}[%d]${NC} ${BOLD}%-35s${NC} ${GRAY}(APT)${NC}\n ${GRAY}%s${NC}\n" \
"$display_idx" "$apt_pkg" "${apt_desc:0:70}"
Expand All @@ -1004,6 +1006,7 @@ debup_search() {
local -a candidate_repos=()
local -a candidate_stars=()
local -a candidate_descs=()
local -a candidate_expected_packages=()
local -A seen_repos=()
local catalog_file="${SCRIPT_DIR}/../share/debup/curated_db.txt"
local catalog_entries=""
Expand All @@ -1012,15 +1015,16 @@ debup_search() {
catalog_entries=$(catalog_search_repositories "$catalog_file" "$query") || catalog_entries=""
fi

local catalog_repo catalog_package repo_key
while IFS='|' read -r catalog_repo catalog_package; do
local catalog_repo catalog_package catalog_expected_package repo_key
while IFS='|' read -r catalog_repo catalog_package catalog_expected_package; do
[ -n "$catalog_repo" ] || continue
repo_key="${catalog_repo,,}"
[ -n "${seen_repos[$repo_key]+present}" ] && continue
seen_repos[$repo_key]=1
candidate_repos+=("$catalog_repo")
candidate_stars+=("-")
candidate_descs+=("Curated package: $catalog_package")
candidate_expected_packages+=("$catalog_expected_package")
done <<< "$catalog_entries"

local raw_results=""
Expand Down Expand Up @@ -1051,6 +1055,7 @@ debup_search() {
candidate_repos+=("$repo_name")
candidate_stars+=("$stars")
candidate_descs+=("$description")
candidate_expected_packages+=("")
done
else
github_search_failed=true
Expand All @@ -1074,11 +1079,12 @@ debup_search() {
local gh_valid=0
local release_check_failed=0
local candidate_index=0
local candidate_repo candidate_stars_value candidate_desc check_release deb_assets compatible_debs chosen_asset release_id release_tag
local candidate_repo candidate_stars_value candidate_desc candidate_expected_package check_release deb_assets compatible_debs chosen_asset release_id release_tag

for candidate_repo in "${candidate_repos[@]}"; do
candidate_stars_value="${candidate_stars[$candidate_index]}"
candidate_desc="${candidate_descs[$candidate_index]}"
candidate_expected_package="${candidate_expected_packages[$candidate_index]}"
candidate_index=$((candidate_index + 1))

if ! check_release=$(fetch_github_api "repos/${candidate_repo}/releases/latest" 10); then
Expand Down Expand Up @@ -1110,6 +1116,7 @@ debup_search() {
global_types[display_idx]="GH"
global_release_ids[display_idx]="$release_id"
global_assets[display_idx]="$chosen_asset"
global_expected_packages[display_idx]="$candidate_expected_package"

if [ "$candidate_stars_value" = "-" ]; then
printf " ${YELLOW}[%d]${NC} ${BOLD}%-35s${NC} ${CYAN}(curated)${NC}\n ${GRAY}%s${NC}\n" \
Expand Down Expand Up @@ -1175,10 +1182,11 @@ debup_search() {
local selected_repo="$selected_target"
local selected_release_id="${global_release_ids[choice]}"
local selected_asset="${global_assets[choice]}"
local selected_expected_package="${global_expected_packages[choice]}"
printf "${CYAN}%b${NC} ${GREEN}%s${NC}...\n" "$(printf "$MSG_INSPECTING_ASSETS" "")" "$selected_repo"
printf "${CYAN}Selected release ID: %s; asset: %s${NC}\n" "$selected_release_id" "$selected_asset"

check_and_update "$selected_repo" "" false "$selected_release_id" "$selected_asset"
check_and_update "$selected_repo" "$selected_expected_package" false "$selected_release_id" "$selected_asset"
local selected_install_status=$?
if [ "$selected_install_status" -eq 1 ]; then
install_failed=1
Expand Down
8 changes: 6 additions & 2 deletions debup-pkg/usr/local/share/debup/search.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ catalog_search_repositories() {
BEGIN {
query = tolower(query)
}
NF != 3 || $2 != "github" {
(NF != 3 && NF != 4) || $2 != "github" {
next
}
$1 !~ /^[a-z0-9][a-z0-9+.-]*$/ {
Expand All @@ -22,12 +22,16 @@ catalog_search_repositories() {
$3 !~ /^[A-Za-z0-9][A-Za-z0-9_.-]*\/[A-Za-z0-9][A-Za-z0-9_.-]*$/ {
next
}
NF == 4 && $4 !~ /^[a-z0-9][a-z0-9+.-]*$/ {
next
}
{
package_name = tolower($1)
repository = tolower($3)
if (index(package_name, query) || index(repository, query)) {
if (!seen[repository]++) {
print $3 "|" $1
expected_package = (NF == 4 ? $4 : "")
print $3 "|" $1 "|" expected_package
count++
if (count >= 10) {
exit
Expand Down
29 changes: 25 additions & 4 deletions scripts/sync_catalog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ readonly PACKAGE_DIR="${WORK_DIR}/deb-get/01-main/packages"

printf '[*] Parsing package recipes...\n'

# Preserve explicit Debian Package: identity mappings across catalog refreshes.
declare -A expected_package_mappings=()
if [[ -r "$CATALOG_FILE" ]]; then
while IFS='|' read -r existing_package existing_provider existing_repo existing_expected extra || [[ -n "${existing_package:-}" ]]; do
[[ -z "${extra:-}" &&
"$existing_provider" == "github" &&
"$existing_package" =~ ^[a-z0-9][a-z0-9+.-]*$ &&
"$existing_repo" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*$ &&
"$existing_expected" =~ ^[a-z0-9][a-z0-9+.-]*$ ]] || continue
expected_package_mappings["$existing_package"]="${existing_repo}|${existing_expected}"
done < "$CATALOG_FILE"
fi

shopt -s nullglob
recipe_files=("${PACKAGE_DIR}"/*)
shopt -u nullglob
Expand All @@ -49,6 +62,7 @@ shopt -u nullglob
for recipe in "${recipe_files[@]}"; do
[[ -f "$recipe" ]] || continue
package_name="$(basename -- "$recipe")"
[[ "$package_name" =~ ^[a-z0-9][a-z0-9+.-]*$ ]] || continue

# Only accept literal, quoted upstream values. Dynamic shell expressions
# are intentionally ignored instead of being evaluated.
Expand All @@ -65,7 +79,13 @@ for recipe in "${recipe_files[@]}"; do
' "$recipe")"

if [[ -n "$github_repo" && "$github_repo" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then
printf '%s|github|%s\n' "$package_name" "$github_repo" >> "$TMP_CATALOG"
mapped_identity="${expected_package_mappings[$package_name]:-}"
if [[ "$mapped_identity" == "${github_repo}|"* ]]; then
expected_deb_package="${mapped_identity#*|}"
printf '%s|github|%s|%s\n' "$package_name" "$github_repo" "$expected_deb_package" >> "$TMP_CATALOG"
else
printf '%s|github|%s\n' "$package_name" "$github_repo" >> "$TMP_CATALOG"
fi
continue
fi

Expand All @@ -84,14 +104,15 @@ fi

# Validate every generated record before replacing the tracked catalog.
awk -F'|' '
NF != 3 || $1 !~ /^[a-z0-9][a-z0-9+.-]*$/ || $2 != "github" || $3 !~ /^[A-Za-z0-9][A-Za-z0-9_.-]*\/[A-Za-z0-9][A-Za-z0-9_.-]*$/ {
(NF != 3 && NF != 4) || $1 !~ /^[a-z0-9][a-z0-9+.-]*$/ || $2 != "github" || $3 !~ /^[A-Za-z0-9][A-Za-z0-9_.-]*\/[A-Za-z0-9][A-Za-z0-9_.-]*$/ || (NF == 4 && $4 !~ /^[a-z0-9][a-z0-9+.-]*$/) {
exit 1
}
seen[$1] && seen[$1] != $3 {
identity = $3 "|" (NF == 4 ? $4 : "")
seen[$1] && seen[$1] != identity {
exit 1
}
{
seen[$1] = $3
seen[$1] = identity
}
' "$TMP_CATALOG" || die "Generated catalog failed validation or contains ambiguous package names."

Expand Down
20 changes: 17 additions & 3 deletions tests/test_search.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,42 @@ trap 'rm -rf -- "$TEST_DIR"' EXIT
CATALOG="${TEST_DIR}/curated_db.txt"

cat > "$CATALOG" <<'EOF'
alpha-app|github|Acme/Alpha
alpha-app|github|Acme/Alpha|alpha-deb
shared-tool|github|Acme/Shared
alpha-cli|github|Acme/Alpha
direct-alpha|direct|https://example.org/alpha.deb
malformed|github
invalid-repo|github|not a valid repository
uppercase-name|GITHUB|Acme/Uppercase
invalid-package-map|github|Acme/Invalid|INVALID_NAME
extra-field|github|Acme/Extra|valid-name|unexpected
EOF

actual="$(catalog_search_repositories "$CATALOG" "ALPHA")"
expected='Acme/Alpha|alpha-app'
expected='Acme/Alpha|alpha-app|alpha-deb'
[[ "$actual" == "$expected" ]] || {
printf 'FAIL: case-insensitive matching or repository deduplication failed\nExpected: %s\nActual: %s\n' "$expected" "$actual" >&2
exit 1
}

actual="$(catalog_search_repositories "$CATALOG" "shared")"
[[ "$actual" == 'Acme/Shared|shared-tool' ]] || {
[[ "$actual" == 'Acme/Shared|shared-tool|' ]] || {
printf 'FAIL: repository-name matching failed: %s\n' "$actual" >&2
exit 1
}

actual="$(catalog_search_repositories "$CATALOG" "shared-tool")"
[[ "$actual" == 'Acme/Shared|shared-tool|' ]] || {
printf 'FAIL: legacy three-field entries must keep an empty expected package identity: %s\n' "$actual" >&2
exit 1
}

actual="$(catalog_search_repositories "$CATALOG" "invalid-package-map")"
[[ -z "$actual" ]] || {
printf 'FAIL: invalid Debian package identity mapping was accepted: %s\n' "$actual" >&2
exit 1
}

actual="$(catalog_search_repositories "$CATALOG" '[')"
[[ -z "$actual" ]] || {
printf 'FAIL: literal query matching treated punctuation as a pattern: %s\n' "$actual" >&2
Expand Down
11 changes: 6 additions & 5 deletions tests/test_sync_catalog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,24 @@ export PATH="${BIN_DIR}:${PATH}"
CATALOG="${REPO_DIR}/curated_db.txt"

# A failed upstream clone must leave the previous catalog untouched.
printf 'preserve|github|owner/repository\n' > "$CATALOG"
printf 'preserve|github|owner/repository\ngithub-app|github|vendor/github-app|github-app-deb\n' > "$CATALOG"
if MOCK_GIT_FAIL=1 "${REPO_DIR}/scripts/sync_catalog.sh" >/dev/null 2>&1; then
printf 'FAIL: expected clone failure\n' >&2
exit 1
fi
[[ "$(cat "$CATALOG")" == 'preserve|github|owner/repository' ]] || {
expected_preserved=$'preserve|github|owner/repository\ngithub-app|github|vendor/github-app|github-app-deb'
[[ "$(cat "$CATALOG")" == "$expected_preserved" ]] || {
printf 'FAIL: existing catalog was modified after clone failure\n' >&2
exit 1
}

# Successful synchronization must include literal supported entries only.
# Successful synchronization must include literal supported entries and preserve explicit mappings.
MOCK_GIT_FAIL=0 "${REPO_DIR}/scripts/sync_catalog.sh" >/dev/null
expected='github-app|github|vendor/github-app'
expected='github-app|github|vendor/github-app|github-app-deb'
actual="$(cat "$CATALOG")"
[[ "$actual" == "$expected" ]] || {
printf 'FAIL: unexpected generated catalog\nExpected:\n%s\nActual:\n%s\n' "$expected" "$actual" >&2
exit 1
}

printf 'PASS: catalog generation, filtering, sorting, and failure preservation\n'
printf 'PASS: catalog generation, filtering, sorting, mapping preservation, and failure preservation\n'
Loading