2021-06-28 23:13:55 +00:00
{ lib
2024-05-15 15:35:15 +00:00
, writeShellApplication
2020-11-21 19:51:51 +00:00
, coreutils
, git
, nix
, common-updater-scripts
} :
# This is an updater for unstable packages that should always use the latest
# commit.
2024-05-15 15:35:15 +00:00
# To use this updater, add the following to your package set:
# passthru.updateScript = unstableGitUpdater { };
# relevant attributes can be passed as below:
2022-09-09 14:08:57 +00:00
{ url ? null # The git url, if empty it will be set to src.gitRepoUrl
2021-06-28 23:13:55 +00:00
, branch ? null
2024-05-15 15:35:15 +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
, tagFormat ? " * " # A `git describe --tags --match '<format>'` pattern that tags must match to be considered
, tagPrefix ? null # strip this prefix from a tag name
, 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-08-04 22:07:22 +00:00
, shallowClone ? true
2020-11-21 19:51:51 +00:00
} :
2024-05-15 15:35:15 +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-11-21 19:51:51 +00:00
let
2024-05-15 15:35:15 +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
url = " "
branch = " "
hardcode_zero_version = " "
tag_format = " "
tag_prefix = " "
tag_converter = " "
shallow_clone = " "
: " ' ' ${ systemArg:= } "
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
# 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
# Get info about HEAD from a shallow git clone
tmpdir = " $ ( m k t e m p - d ) "
cloneArgs = ( )
if [ [ " $ s h a l l o w _ c l o n e " == " 1 " ] ] ; then
cloneArgs + = ( - - depth = 1 )
fi
if [ [ - n " $ b r a n c h " ] ] ; then
cloneArgs + = ( - - branch = " $ b r a n c h " )
fi
git clone " ' ' ${ cloneArgs [ @ ] } " " $ u r l " " $ t m p d i r "
getLatestVersion ( ) {
git describe - - tags - - abbrev = 0 - - match " ' ' ${ tag_format } " 2 > /dev/null || true
}
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 e t L a t e s t V e r s i o n ) "
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
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 e t L a t e s t V e r s i o n ) "
fi
else
last_tag = " $ ( g e t L a t e s t V e r s i o n ) "
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"
# 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-11-21 19:51:51 +00:00
2023-08-04 22:07:22 +00:00
in
[
2024-05-15 15:35:15 +00:00
( lib . getExe updateScript )
2022-02-20 05:27:41 +00:00
" - - u r l = ${ builtins . toString url } "
2024-05-15 15:35:15 +00:00
" - - t a g - f o r m a t = ${ tagFormat } "
2022-02-20 05:27:41 +00:00
] ++ lib . optionals ( branch != null ) [
" - - b r a n c h = ${ branch } "
2024-05-15 15:35:15 +00:00
] ++ lib . optionals ( tagPrefix != null ) [
2022-02-20 05:27:41 +00:00
" - - t a g - p r e f i x = ${ tagPrefix } "
2024-05-15 15:35:15 +00:00
] ++ lib . optionals ( tagConverter != null ) [
" - - t a g - c o n v e r t e r = ${ tagConverter } "
] ++ lib . optionals hardcodeZeroVersion [
" - - h a r d c o d e - z e r o - v e r s i o n "
2023-08-04 22:07:22 +00:00
] ++ lib . optionals shallowClone [
" - - s h a l l o w - c l o n e "
2022-02-20 05:27:41 +00:00
]