2021-04-16 15:22:13 +03:00
{ lib
, writeShellScript
2020-10-19 20:57:12 +03:00
, coreutils
, git
, nix
, common-updater-scripts
} :
# This is an updater for unstable packages that should always use the latest
# commit.
2022-09-04 11:29:44 +03:00
{ url ? null # The git url, if empty it will be set to src.gitRepoUrl
2021-04-16 15:22:13 +03:00
, branch ? null
2024-01-12 14:04:56 +03: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
, tagPrefix ? " " # strip this prefix from a tag name
2023-07-17 06:48:26 +03:00
, shallowClone ? true
2020-10-19 20:57:12 +03:00
} :
let
updateScript = writeShellScript " u n s t a b l e - u p d a t e - s c r i p t . s h " ''
set - ex
2022-02-18 10:12:44 +03:00
url = " "
branch = " "
2024-01-12 14:04:56 +03:00
hardcode_zero_version = " "
2022-02-18 10:19:31 +03:00
tag_prefix = " "
2023-07-17 06:48:26 +03:00
shallow_clone = " "
2022-02-18 10:12:44 +03:00
while ( ( $ # > 0 )); do
flag = " $ 1 "
shift 1
case " $ f l a g " in
- - url = * )
url = " ' ' ${ flag #*=}"
; ;
- - branch = * )
branch = " ' ' ${ flag #*=}"
; ;
2024-01-12 14:04:56 +03:00
- - hardcode-zero-version )
hardcode_zero_version = 1
2022-02-18 10:19:31 +03:00
; ;
- - tag-prefix = * )
tag_prefix = " ' ' ${ flag #*=}"
; ;
2023-07-17 06:48:26 +03:00
- - shallow-clone )
shallow_clone = 1
; ;
2022-02-18 10:12:44 +03:00
* )
echo " $ 0 : u n k n o w n o p t i o n ‘ ' ' ${ flag } ’ "
exit 1
; ;
esac
done
2020-10-19 20:57:12 +03:00
2022-09-04 11:29:44 +03:00
# By default we set url to src.gitRepoUrl
2020-10-19 20:57:12 +03:00
if [ [ - z " $ u r l " ] ] ; then
url = " $ ( ${ nix } / b i n / 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 \
2022-05-24 19:03:46 +03:00
" 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 " \
2020-10-19 20:57:12 +03:00
| tr - d ' " ' ) "
fi
# Get info about HEAD from a shallow git clone
tmpdir = " $ ( ${ coreutils } / b i n / m k t e m p - d ) "
2022-02-18 10:12:44 +03:00
cloneArgs = (
- - bare
)
2023-07-17 06:48:26 +03:00
if [ [ " $ s h a l l o w _ c l o n e " == " 1 " ] ] ; then
cloneArgs + = ( - - depth = 1 )
fi
2022-02-18 10:12:44 +03:00
if [ [ - n " $ b r a n c h " ] ] ; then
cloneArgs + = ( - - branch = " $ b r a n c h " )
fi
$ { git } /bin/git clone " ' ' ${ cloneArgs [ @ ] } " " $ u r l " " $ t m p d i r "
2020-10-19 20:57:12 +03:00
pushd " $ t m p d i r "
commit_date = " $ ( ${ git } / b i n / g i t s h o w - s - - p r e t t y = ' f o r m a t : % c s ' ) "
commit_sha = " $ ( ${ git } / b i n / g i t s h o w - s - - p r e t t y = ' f o r m a t : % H ' ) "
2024-01-12 14:04:56 +03:00
last_tag = " "
if [ [ - z " $ h a r d c o d e _ z e r o _ v e r s i o n " ] ] ; then
2024-01-12 20:32:04 +03:00
if [ [ " $ s h a l l o w _ c l o n e " == " 1 " ] ] ; then
depth = 100
while ( ( $ depth < 10000 ) ) ; do
last_tag = " $ ( ${ git } / b i n / g i t d e s c r i b e - - t a g s - - a b b r e v = 0 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 } /bin/git fetch - - depth = " $ d e p t h " - - tags
depth = $ ( ( $ depth * 2 ) )
done
2024-02-07 18:46:56 +03: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 = " $ ( ${ git } / b i n / g i t d e s c r i b e - - t a g s - - a b b r e v = 0 2 > / d e v / n u l l | | t r u e ) "
fi
2024-01-12 20:32:04 +03:00
else
2024-02-07 18:46:56 +03:00
last_tag = " $ ( ${ git } / b i n / g i t d e s c r i b e - - t a g s - - a b b r e v = 0 2 > / d e v / n u l l | | t r u e ) "
2024-01-12 20:32:04 +03:00
fi
2022-02-18 10:19:31 +03:00
if [ [ - z " $ l a s t _ t a g " ] ] ; then
2024-02-07 18:46:56 +03:00
last_tag = " 0 "
2022-02-18 10:19:31 +03:00
fi
if [ [ - n " $ t a g _ p r e f i x " ] ] ; then
2024-02-07 18:46:56 +03:00
last_tag = " ' ' ${ last_tag #$tag_prefix}"
2022-02-18 10:19:31 +03:00
fi
2024-01-12 14:04:56 +03:00
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 ' ( a f t e r r e m o v i n g p r e f i x ' $ t a g _ p r e f i x ' ) 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
else
last_tag = " 0 "
2022-02-18 10:19:31 +03:00
fi
2024-01-12 14:04:56 +03:00
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 "
2020-10-19 20:57:12 +03:00
popd
2022-02-18 10:19:31 +03:00
# ${coreutils}/bin/rm -rf "$tmpdir"
2020-10-19 20:57:12 +03:00
# update the nix expression
$ { common-updater-scripts } /bin/update-source-version \
" $ U P D A T E _ N I X _ A T T R _ P A T H " \
2022-02-18 10:19:31 +03:00
" $ n e w _ v e r s i o n " \
2020-10-19 20:57:12 +03:00
- - rev = " $ c o m m i t _ s h a "
'' ;
2023-07-17 06:48:26 +03:00
in
[
2022-02-18 10:12:44 +03:00
updateScript
" - - u r l = ${ builtins . toString url } "
2024-01-12 14:04:56 +03:00
" - - t a g - p r e f i x = ${ tagPrefix } "
2022-02-18 10:12:44 +03:00
] ++ lib . optionals ( branch != null ) [
" - - b r a n c h = ${ branch } "
2024-01-12 14:04:56 +03: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 06:48:26 +03:00
] ++ lib . optionals shallowClone [
" - - s h a l l o w - c l o n e "
2022-02-18 10:12:44 +03:00
]