2021-04-16 12:22:13 +00:00
{ lib
2024-03-03 15:38:43 +00:00
, writeShellApplication
2020-10-19 17:57:12 +00:00
, coreutils
, git
, nix
, common-updater-scripts
} :
# This is an updater for unstable packages that should always use the latest
# commit.
2022-09-04 08:29:44 +00:00
{ url ? null # The git url, if empty it will be set to src.gitRepoUrl
2021-04-16 12:22:13 +00:00
, branch ? null
2024-01-12 11:04:56 +00:00
, hardcodeZeroVersion ? false # Use a made-up version "0" instead of latest tag. Use when there is no previous release, or the project's tagging system is incompatible with what we expect from versions
2024-03-03 15:38:43 +00: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 09:19:51 +00: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 03:48:26 +00:00
, shallowClone ? true
2020-10-19 17:57:12 +00:00
} :
2024-03-03 15:38:43 +00: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 17:57:12 +00:00
let
2024-03-03 15:38:43 +00: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 17:57:12 +00:00
2024-03-03 15:38:43 +00:00
url = " "
branch = " "
hardcode_zero_version = " "
tag_format = " "
tag_prefix = " "
tag_converter = " "
shallow_clone = " "
: " ' ' ${ systemArg:= } "
2022-02-18 07:12:44 +00:00
2024-03-03 15:38:43 +00: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 17:57:12 +00:00
2024-03-03 15:38:43 +00: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 17:57:12 +00:00
2024-03-03 15:38:43 +00:00
# Get info about HEAD from a shallow git clone
tmpdir = " $ ( m k t e m p - d ) "
2022-02-18 07:12:44 +00:00
2024-03-03 15:38:43 +00:00
cloneArgs = (
- - bare
)
2022-02-18 07:12:44 +00:00
2024-03-03 15:38:43 +00:00
if [ [ " $ s h a l l o w _ c l o n e " == " 1 " ] ] ; then
cloneArgs + = ( - - depth = 1 )
fi
2023-07-17 03:48:26 +00:00
2024-03-03 15:38:43 +00:00
if [ [ - n " $ b r a n c h " ] ] ; then
cloneArgs + = ( - - branch = " $ b r a n c h " )
fi
2022-02-18 07:12:44 +00:00
2024-03-03 15:38:43 +00:00
git clone " ' ' ${ cloneArgs [ @ ] } " " $ u r l " " $ t m p d i r "
2022-02-18 07:12:44 +00:00
2024-03-03 15:38:43 +00: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
last_tag = " $ ( g i t d e s c r i b e - - t a g s - - a b b r e v = 0 - - m a t c h " '' ${ tag_format } " 2 > / d e v / n u l l | | t r u e ) "
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 15:46:56 +00:00
2024-03-03 15:38:43 +00: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
last_tag = " $ ( g i t d e s c r i b e - - t a g s - - a b b r e v = 0 - - m a t c h " '' ${ tag_format } " 2 > / d e v / n u l l | | t r u e ) "
fi
else
last_tag = " $ ( g i t d e s c r i b e - - t a g s - - a b b r e v = 0 2 > - - m a t c h " '' ${ tag_format } " / d e v / n u l l | | t r u e ) "
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 17:57:12 +00:00
2024-03-03 15:38:43 +00: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 17:57:12 +00:00
2023-07-17 03:48:26 +00:00
in
[
2024-03-03 15:38:43 +00:00
( lib . getExe updateScript )
2022-02-18 07:12:44 +00:00
" - - u r l = ${ builtins . toString url } "
2024-02-28 09:29:26 +00:00
" - - t a g - f o r m a t = ${ tagFormat } "
2022-02-18 07:12:44 +00:00
] ++ lib . optionals ( branch != null ) [
" - - b r a n c h = ${ branch } "
2024-03-03 15:38:43 +00:00
] ++ lib . optionals ( tagPrefix != null ) [
" - - t a g - p r e f i x = ${ tagPrefix } "
2024-02-28 09:19:51 +00:00
] ++ lib . optionals ( tagConverter != null ) [
" - - t a g - c o n v e r t e r = ${ tagConverter } "
2024-01-12 11:04:56 +00: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 03:48:26 +00:00
] ++ lib . optionals shallowClone [
" - - s h a l l o w - c l o n e "
2022-02-18 07:12:44 +00:00
]