2021-04-16 14:22:13 +02:00
{ lib
2024-03-03 16:38:43 +01:00
, writeShellApplication
2020-10-19 19:57:12 +02:00
, coreutils
, git
, nix
, common-updater-scripts
} :
# This is an updater for unstable packages that should always use the latest
# commit.
2024-05-11 14:57:38 +02:00
# To use this updater, add the following to your package set:
# passthru.updateScript = unstableGitUpdater { };
# relevant attributes can be passed as below:
2022-09-04 10:29:44 +02:00
{ url ? null # The git url, if empty it will be set to src.gitRepoUrl
2021-04-16 14:22:13 +02:00
, branch ? null
2024-05-31 23:26:24 +02:00
, hardcodeZeroVersion ? false # Use a made-up version "0" instead of latest tag. Use when the project's tagging system is incompatible with what we expect from versions
2024-03-03 16:38:43 +01:00
, tagFormat ? " * " # A `git describe --tags --match '<format>'` pattern that tags must match to be considered
, tagPrefix ? null # strip this prefix from a tag name
2024-02-28 10:19:51 +01:00
, tagConverter ? null # A command to convert more complex tag formats. It receives the git tag via stdin and should convert it into x.y.z format to stdout
2023-07-17 05:48:26 +02:00
, shallowClone ? true
2020-10-19 19:57:12 +02:00
} :
2024-03-03 16:38:43 +01:00
assert lib . asserts . assertMsg ( tagPrefix == null || tagConverter == null ) " C a n o n l y u s e e i t h e r t a g P r e f i x o r t a g C o n v e r t e r ! " ;
2020-10-19 19:57:12 +02:00
let
2024-03-03 16:38:43 +01:00
updateScript = writeShellApplication {
name = " u n s t a b l e - u p d a t e - s c r i p t " ;
runtimeInputs = [
common-updater-scripts
coreutils
git
nix
] ;
text = ''
set - ex
2020-10-19 19:57:12 +02:00
2024-03-03 16:38:43 +01:00
url = " "
branch = " "
hardcode_zero_version = " "
tag_format = " "
tag_prefix = " "
tag_converter = " "
shallow_clone = " "
: " ' ' ${ systemArg:= } "
2022-02-18 08:12:44 +01:00
2024-03-03 16:38:43 +01:00
while ( ( $ # > 0 )); do
flag = " $ 1 "
shift 1
case " $ f l a g " in
- - url = * )
url = " ' ' ${ flag #*=}"
; ;
- - branch = * )
branch = " ' ' ${ flag #*=}"
; ;
- - hardcode-zero-version )
hardcode_zero_version = 1
; ;
- - tag-format = * )
tag_format = " ' ' ${ flag #*=}"
; ;
- - tag-prefix = * )
tag_prefix = " ' ' ${ flag #*=}"
; ;
- - tag-converter = * )
tag_converter = " ' ' ${ flag #*=}"
; ;
- - shallow-clone )
shallow_clone = 1
; ;
* )
echo " $ 0 : u n k n o w n o p t i o n ‘ ' ' ${ flag } ’ "
exit 1
; ;
esac
done
2020-10-19 19:57:12 +02:00
2024-03-03 16:38:43 +01:00
# By default we set url to src.gitRepoUrl
if [ [ - z " $ u r l " ] ] ; then
# system argument cannot be passed as 1 argument
# shellcheck disable=SC2086
url = " $ ( n i x - i n s t a n t i a t e $ s y s t e m A r g - - e v a l - E \
" w i t h i m p o r t . / . { } ; $ U P D A T E _ N I X _ A T T R _ P A T H . s r c . g i t R e p o U r l " \
| tr - d ' " ' ) "
fi
2020-10-19 19:57:12 +02:00
2024-03-03 16:38:43 +01:00
# Get info about HEAD from a shallow git clone
tmpdir = " $ ( m k t e m p - d ) "
2022-02-18 08:12:44 +01:00
2024-03-03 18:40:47 +01:00
cloneArgs = ( )
2022-02-18 08:12:44 +01:00
2024-03-03 16:38:43 +01:00
if [ [ " $ s h a l l o w _ c l o n e " == " 1 " ] ] ; then
cloneArgs + = ( - - depth = 1 )
fi
2023-07-17 05:48:26 +02:00
2024-03-03 16:38:43 +01:00
if [ [ - n " $ b r a n c h " ] ] ; then
cloneArgs + = ( - - branch = " $ b r a n c h " )
fi
2022-02-18 08:12:44 +01:00
2024-03-03 16:38:43 +01:00
git clone " ' ' ${ cloneArgs [ @ ] } " " $ u r l " " $ t m p d i r "
2024-03-25 12:50:47 +01:00
getLatestVersion ( ) {
git describe - - tags - - abbrev = 0 - - match " ' ' ${ tag_format } " 2 > /dev/null || true
}
2022-02-18 08:12:44 +01:00
2024-03-03 16:38:43 +01:00
pushd " $ t m p d i r "
commit_date = " $ ( g i t s h o w - s - - p r e t t y = ' f o r m a t : % c s ' ) "
commit_sha = " $ ( g i t s h o w - s - - p r e t t y = ' f o r m a t : % H ' ) "
last_tag = " "
if [ [ - z " $ h a r d c o d e _ z e r o _ v e r s i o n " ] ] ; then
if [ [ " $ s h a l l o w _ c l o n e " == " 1 " ] ] ; then
depth = 100
while ( ( depth < 10000 ) ) ; do
2024-03-25 12:50:47 +01:00
last_tag = " $ ( g e t L a t e s t V e r s i o n ) "
2024-03-03 16:38:43 +01:00
if [ [ - n " $ l a s t _ t a g " ] ] ; then
break
fi
git fetch - - depth = " $ d e p t h " - - tags
depth = $ ( ( depth * 2 ) )
done
2024-02-07 16:46:56 +01:00
2024-03-03 16:38:43 +01:00
if [ [ - z " $ l a s t _ t a g " ] ] ; then
# To be extra sure, check if full history helps with finding a tag
git fetch - - tags
2024-03-25 12:50:47 +01:00
last_tag = " $ ( g e t L a t e s t V e r s i o n ) "
2024-03-03 16:38:43 +01:00
fi
else
2024-03-25 12:50:47 +01:00
last_tag = " $ ( g e t L a t e s t V e r s i o n ) "
2024-03-03 16:38:43 +01:00
fi
if [ [ - z " $ l a s t _ t a g " ] ] ; then
last_tag = " 0 "
fi
if [ [ - n " $ t a g _ p r e f i x " ] ] ; then
echo " S t r i p p i n g p r e f i x ' $ t a g _ p r e f i x ' f r o m t a g ' $ l a s t _ t a g ' "
last_tag = " ' ' ${ last_tag #"''${tag_prefix}"}"
fi
if [ [ - n " $ t a g _ c o n v e r t e r " ] ] ; then
echo " R u n n i n g ' $ l a s t _ t a g ' t h r o u g h : $ t a g _ c o n v e r t e r "
last_tag = " $ ( e c h o " '' ${ last_tag } " | ''$ { t a g _ c o n v e r t e r } ) "
fi
else
last_tag = " 0 "
fi
if [ [ ! " $ l a s t _ t a g " = ~ ^ [ [ : digit : ] ] ] ] ; then
echo " L a s t t a g ' $ l a s t _ t a g ' d o e s n o t s t a r t w i t h a d i g i t " > /dev/stderr
exit 1
fi
new_version = " $ l a s t _ t a g - u n s t a b l e - $ c o m m i t _ d a t e "
popd
# rm -rf "$tmpdir"
2020-10-19 19:57:12 +02:00
2024-03-03 16:38:43 +01:00
# update the nix expression
update-source-version \
" $ U P D A T E _ N I X _ A T T R _ P A T H " \
" $ n e w _ v e r s i o n " \
- - rev = " $ c o m m i t _ s h a "
'' ;
} ;
2020-10-19 19:57:12 +02:00
2023-07-17 05:48:26 +02:00
in
[
2024-03-03 16:38:43 +01:00
( lib . getExe updateScript )
2022-02-18 08:12:44 +01:00
" - - u r l = ${ builtins . toString url } "
2024-02-28 10:29:26 +01:00
" - - t a g - f o r m a t = ${ tagFormat } "
2022-02-18 08:12:44 +01:00
] ++ lib . optionals ( branch != null ) [
" - - b r a n c h = ${ branch } "
2024-03-03 16:38:43 +01:00
] ++ lib . optionals ( tagPrefix != null ) [
" - - t a g - p r e f i x = ${ tagPrefix } "
2024-02-28 10:19:51 +01:00
] ++ lib . optionals ( tagConverter != null ) [
" - - t a g - c o n v e r t e r = ${ tagConverter } "
2024-01-12 12:04:56 +01:00
] ++ lib . optionals hardcodeZeroVersion [
" - - h a r d c o d e - z e r o - v e r s i o n "
2023-07-17 05:48:26 +02:00
] ++ lib . optionals shallowClone [
" - - s h a l l o w - c l o n e "
2022-02-18 08:12:44 +01:00
]