Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
search: add search -Scomment-description
Baptiste Daroussin committed 1 month ago
commit a478b48f0669890cf323ba325271d08fa1f75eb7
parent 5151477
5 files changed +116 -9
modified docs/pkg-search.8
@@ -234,6 +234,18 @@ include the package name, equivalent to
When chosen as a search option, only the
.Sy comment
field is used for sorting the output.
+
.It Sy comment-description
+
Both the one line comment and the multi-line description fields.
+
When chosen as a search option, a package will match if
+
.Ar pattern
+
is found in either field.
+
The output format will include the package name, comment and
+
description.
+
Results are sorted by comment.
+
This option is only available as a search field
+
.Fl ( S ) ,
+
not as a label option
+
.Fl ( L ) .
.It Sy description
The multi-line package description from the
.Fa pkg-descr
modified libpkg/pkg.h.in
@@ -222,6 +222,7 @@ typedef enum {
	FIELD_NAMEVER,
	FIELD_COMMENT,
	FIELD_DESC,
+
	FIELD_COMMENT_DESC,
	FIELD_FLAVOR,
} pkgdb_field;

modified libpkg/repo/binary/query.c
@@ -429,9 +429,17 @@ pkg_repo_binary_build_search_query(xstring *sql, match_t match,
	case FIELD_DESC:
		what = "desc";
		break;
+
	case FIELD_COMMENT_DESC:
+
		break;
	}

-
	if (what != NULL && how != NULL)
+
	if (field == FIELD_COMMENT_DESC && how != NULL) {
+
		fprintf(sql->fp, "(");
+
		fprintf(sql->fp, how, "comment");
+
		fprintf(sql->fp, " OR ");
+
		fprintf(sql->fp, how, "desc");
+
		fprintf(sql->fp, ")");
+
	} else if (what != NULL && how != NULL)
		fprintf(sql->fp, how, what);

	switch (sort) {
@@ -455,6 +463,9 @@ pkg_repo_binary_build_search_query(xstring *sql, match_t match,
	case FIELD_DESC:
		orderby = " ORDER BY desc";
		break;
+
	case FIELD_COMMENT_DESC:
+
		orderby = " ORDER BY comment";
+
		break;
	}

	if (orderby != NULL)
modified src/search.c
@@ -46,12 +46,13 @@ typedef struct _cliopt {

/* an option string should not be a prefix of any other option string */
static const cliopt search_label[] = {
-
	{ "comment",     'c'  },
-
	{ "description", 'd'  },
-
	{ "name",        'n'  },
-
	{ "origin",      'o'  },
-
	{ "pkg-name",    'p'  },
-
	{ NULL,          '\0' },
+
	{ "comment",             'c'  },
+
	{ "comment-description", 'D'  },
+
	{ "description",         'd'  },
+
	{ "name",                'n'  },
+
	{ "origin",              'o'  },
+
	{ "pkg-name",            'p'  },
+
	{ NULL,                  '\0' },
};

static const cliopt modifiers[] = {
@@ -92,7 +93,13 @@ match_optarg(const cliopt *optlist, const char *opt)
	for (i = 0; optlist[i].option != NULL; i++) {
		if (strncmp(opt, optlist[i].option, optlen) != 0)
			continue;
-
		if (matched > 0) {
+
		/* Exact match: use it immediately */
+
		if (strlen(optlist[i].option) == optlen) {
+
			matched = i;
+
			key = optlist[i].key;
+
			break;
+
		}
+
		if (matched >= 0) {
			warnx("\"%s\" is ambiguous. Was "
			      "\"%s\" or \"%s\" meant?", opt,
			      optlist[matched].option, optlist[i].option);
@@ -127,6 +134,9 @@ search_label_opt(const char *optionarg)
	case 'd':
		field = FIELD_DESC;
		break;
+
	case 'D':
+
		field = FIELD_COMMENT_DESC;
+
		break;
	default:
		usage_search();
		errx(EXIT_FAILURE, "Unknown search/label option: %s", optionarg);
@@ -408,6 +418,9 @@ exec_search(int argc, char **argv)
	case FIELD_DESC:
		opt |= INFO_TAG_NAMEVER|INFO_DESCR;
		break;
+
	case FIELD_COMMENT_DESC:
+
		opt |= INFO_TAG_NAMEVER|INFO_COMMENT|INFO_DESCR;
+
		break;
	}

	if (quiet) {
modified tests/frontend/search.sh
@@ -4,7 +4,8 @@

tests_init \
	search \
-
	search_options
+
	search_options \
+
	search_comment_description

search_body() {
	export REPOS_DIR=/nonexistent
@@ -69,3 +70,72 @@ EOF
		-s exit:0 \
	pkg -o REPOS_DIR="${TMPDIR}/reposconf" search -q pkgA
}
+

+
search_comment_description_body() {
+
	# Test for issue #2118: search in both comment and description fields
+

+
	cat << EOF > alpha.ucl
+
name: alpha
+
origin: misc/alpha
+
version: "1.0"
+
maintainer: test
+
categories: [test]
+
comment: networking library
+
www: http://test
+
prefix: /usr/local
+
desc: <<EOD
+
A generic utility package
+
EOD
+
EOF
+

+
	cat << EOF > beta.ucl
+
name: beta
+
origin: misc/beta
+
version: "2.0"
+
maintainer: test
+
categories: [test]
+
comment: a generic tool
+
www: http://test
+
prefix: /usr/local
+
desc: <<EOD
+
Provides networking functions
+
EOD
+
EOF
+

+
	mkdir reposconf
+
	cat << EOF > reposconf/repos.conf
+
repo: {
+
	url: file://${TMPDIR}/repo,
+
	enabled: true
+
}
+
EOF
+

+
	for p in alpha beta; do
+
		pkg create -o ${TMPDIR}/repo -M ./${p}.ucl
+
	done
+
	pkg repo -o ${TMPDIR}/repo ${TMPDIR}/repo
+

+
	# Search by comment only: "networking" matches alpha's comment
+
	atf_check \
+
		-o match:"alpha" \
+
		-o not-match:"beta" \
+
		-e ignore \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR="${TMPDIR}/reposconf" search -S comment networking
+

+
	# Search by description only: "networking" matches beta's desc
+
	atf_check \
+
		-o match:"beta" \
+
		-o not-match:"alpha" \
+
		-e ignore \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR="${TMPDIR}/reposconf" search -S description networking
+

+
	# Search by comment-description: "networking" matches both
+
	atf_check \
+
		-o match:"alpha" \
+
		-o match:"beta" \
+
		-e ignore \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR="${TMPDIR}/reposconf" search -S comment-description networking
+
}