From 080d76ce2cbe6883941b01fb46b5dbee3b0d72c9 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 6 Jan 2024 02:42:16 +0100 Subject: Added prefix and -d option --- pkgrel.1 | 26 ++++++++++++++++++++++---- pkgrel.c | 30 +++++++++++++++++++++++++++--- test.sh | 20 ++++++++++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/pkgrel.1 b/pkgrel.1 index a292d83..5d1965d 100644 --- a/pkgrel.1 +++ b/pkgrel.1 @@ -1,9 +1,12 @@ -.TH pkgrel 1 "2022-07-08" +.TH pkgrel 1 "2024-01-06" .SH NAME pkgrel \- print out pkgrel number according to git repository history .SH SYNOPSIS -.SY pkgrel -.YS +.B pkgrel +.RI [ prefix ] +.br +.B pkgrel +.RB [ \-d ] .SH DESCRIPTION This program will check the history of the .I PKGBUILD @@ -13,6 +16,19 @@ The pkgrel is guessed based on the state of the working directory, index, and hi until the end of history. The pkgrel starts at 1. If pkgver is changed, the currently stored pkgrel is printed to standard output and the program exits. If the working directory, index or a commit contains any other changes that are not affecting pkgver value, then pkgrel is incremented and the program continues. +.PP +.I Prefix +and +.B \-d +allow to specify text that will be prepended to the output. With +.I prefix +as argument user can prepend any value before the regular output. +.B \-d +option sets +.I prefix +to a handy constant +.BR pkgrel= +making the final output take a form of a shell variable assignment. .SH EXIT STATUS 0 will be returned on successful run and the pkgrel number will be printed to standard output. .PP @@ -32,4 +48,6 @@ This program was implemented as a result of a simple experiment to see if it wou pkgrel inside the git repository. The reason to avoid it was to disconnect meta information related to versioning from the content of the version-controlled files. .SH SEE ALSO -.BR PKGBUILD (5),\ makepkg (8),\ git (1) +.BR PKGBUILD (5), +.BR makepkg (8), +.BR git (1) diff --git a/pkgrel.c b/pkgrel.c index ecbfcea..961ecbd 100644 --- a/pkgrel.c +++ b/pkgrel.c @@ -112,11 +112,35 @@ match_line(const git_diff_delta* delta, const git_diff_hunk* hunk, const git_dif } +/// Parses an optional output prefix from *args* and returns it. +const char* +parse_args(const int argc, char* argv[]) +{ + int opt; + const char* prefix = NULL; + while (-1 != (opt = getopt(argc, argv, ":d"))) { + switch (opt) { + case 'd': + prefix = "pkgrel="; + break; + default: + break; // Accept anything but -d without prior -- + } + } + if (NULL == prefix) { + if (argc > optind) + prefix = argv[optind]; + else + prefix = ""; + } + return prefix; +} + + int main(int argc, char* argv[]) { - (void) argc; - (void) argv; + const char* prefix = parse_args(argc, argv); FILE* file = fopen("PKGBUILD", "r"); if (NULL == file) { dprintf(2, "PKGBUILD not found or cannot be opened\n"); @@ -176,5 +200,5 @@ main(int argc, char* argv[]) exit(1); } else - printf("%d\n", relpkg); + printf("%s%d\n", prefix, relpkg); } diff --git a/test.sh b/test.sh index 28d33f7..9c1e8c7 100755 --- a/test.sh +++ b/test.sh @@ -93,6 +93,25 @@ test_nested() { } +test_prefix() { + local pkgrel="$1" + local repo="$2" + cd "$repo" + git init + cat >PKGBUILD <<-PKGBUILD_initial + pkgname=test + pkgver=1.0.0 + pkgdesc="Package intended for testing" + PKGBUILD_initial + git add PKGBUILD + git commit -m "Initial commit" + test "$($pkgrel)" = "1" || exit 1 + test "$($pkgrel pkgrel=)" = "pkgrel=1" || exit 1 + test "$($pkgrel -d)" = "pkgrel=1" || exit 1 + test "$($pkgrel a= b= c=)" = "a=1" || exit 1 +} + + ok() { echo -e "${1:-unknown}... \033[32;1mok\033[0m" } @@ -113,3 +132,4 @@ execute_test() { execute_test test_increments execute_test test_root execute_test test_nested +execute_test test_prefix -- cgit v1.1