executable_guix-custom-refresh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env bash
  2. trap "git restore ." EXIT
  3. GIT_AUTHOR="${AUTHOR_EMAIL:-go.wigust@gmail.com}"
  4. patch_guix_upstream()
  5. {
  6. if [[ $(git diff guix/upstream.scm) == "" ]]
  7. then
  8. patch --strip=1 <<'EOF'
  9. diff --git a/guix/upstream.scm b/guix/upstream.scm
  10. index 6584d5e4c4..ea94184961 100644
  11. --- a/guix/upstream.scm
  12. +++ b/guix/upstream.scm
  13. @@ -24,6 +24,7 @@
  14. #:use-module (guix discovery)
  15. #:use-module ((guix download)
  16. #:select (download-to-store url-fetch))
  17. + #:use-module (guix git-download)
  18. #:use-module (guix gnupg)
  19. #:use-module (guix packages)
  20. #:use-module (guix diagnostics)
  21. @@ -400,7 +401,8 @@ SOURCE, an <upstream-source>."
  22. (define %method-updates
  23. ;; Mapping of origin methods to source update procedures.
  24. - `((,url-fetch . ,package-update/url-fetch)))
  25. + `((,url-fetch . ,package-update/url-fetch)
  26. + (,git-fetch . ,package-update/url-fetch)))
  27. (define* (package-update store package updaters
  28. #:key (key-download 'interactive))
  29. EOF
  30. fi
  31. }
  32. maintainer()
  33. {
  34. mapfile -t output < <(git log --grep="gnu: Add" --format=%s --author="$GIT_AUTHOR")
  35. for line in "${output[@]}"
  36. do
  37. package="${line/gnu: Add /}"
  38. package="${package::-1}"
  39. case "$package" in
  40. *Revert*|*service*|premake4|perl-strictures-2|perl-role-tiny-2|emacs-cl-generic|emacs-seq|emacs-emms-player-simple-mpv|php-with-bcmath|shlomif-cmake-modules|go-github-com-muesli-reflow|go-github-com-emirpasic-gods-trees-binaryheap|nginx-socket-cloexec|emacs-kotlin-mode|emacs-dired-hacks|emacs-which-key)
  41. printf ""
  42. ;;
  43. *go-github-com*)
  44. printf ""
  45. ;;
  46. *zabbix-agentd*)
  47. printf "zabbix-agentd\nzabbix-server\n"
  48. ;;
  49. *)
  50. echo "$package"
  51. ;;
  52. esac
  53. done
  54. }
  55. log_packages_for_upgrade()
  56. {
  57. # shellcheck disable=SC2046
  58. if ! [ -e refresh.log ]
  59. then
  60. printf "No refresh.log exists, creating...\n"
  61. ./pre-inst-env guix refresh $(maintainer) |& tee refresh.log
  62. fi
  63. }
  64. upgrade_package()
  65. {
  66. printf 'Updating "%s" package and remember version.\n' "$package"
  67. version="$(./pre-inst-env guix refresh --update "$package" |& awk '/updating from version/ { print $NF }' | sed 's/\.\.\.//')"
  68. if [ -z "$version" ]
  69. then
  70. printf "No version found.\n"
  71. exit 1
  72. fi
  73. file="$(git diff --stat | awk '/packages/ { print $1 }')"
  74. printf 'Trying to build "%s" package.\n' "$package"
  75. mapfile -t hashes < <(./pre-inst-env guix build "$package" |& awk '/hash:/ { print $NF }')
  76. if [ "${#hashes[@]}" -ne 0 ]
  77. then
  78. if grep --quiet "${hashes[0]}" "$file"
  79. then
  80. if git diff "$file" | grep --quiet '(version'
  81. then
  82. printf 'Substitute "%s" with "%s" in "%s".\n' "${hashes[0]}" "${hashes[1]}" "$file"
  83. if sed --in-place "s/${hashes[0]}/${hashes[1]}/" "$file"
  84. then
  85. printf "Building package again..."
  86. if ./pre-inst-env guix build "$package"
  87. then
  88. sed --in-place "/$package/d" refresh.log
  89. fi
  90. fi
  91. else
  92. git restore "$file"
  93. printf '\033[31merror: Failed to update "%s" package. Restoring "%s" file.\033[0m\n' "$package" "$file"
  94. exit 1
  95. fi
  96. fi
  97. fi
  98. if [[ $(git diff "$file") == "" ]]
  99. then
  100. printf '\033[31merror: Nothing to commit.\033[0m\n'
  101. exit 1
  102. else
  103. if ./pre-inst-env guix build "$package"
  104. then
  105. git commit --message="$(printf "gnu: %s: Update to $version.\n\n* %s (%s): Update to $version." "$package" "$file" "$package")" "$file"
  106. printf '\033[32mCommit the change in "%s" file.\033[0m\n' "$file"
  107. git show
  108. sed --in-place "/$package/d" refresh.log
  109. else
  110. git restore "$file"
  111. printf '\033[31merror: Failed to build "%s" package. Restoring "%s" file.\033[0m\n' "$package" "$file"
  112. fi
  113. fi
  114. }
  115. main()
  116. {
  117. case "$1" in
  118. debug)
  119. maintainer
  120. ;;
  121. *)
  122. patch_guix_upstream
  123. # XXX: Don't call .direnv/aliases/environment -- .direnv/aliases/build
  124. .direnv/aliases/environment -- .direnv/aliases/build
  125. log_packages_for_upgrade
  126. case "$2" in
  127. -i|--interactive)
  128. select package in $(awk '/would be upgraded/ { print $2 }' refresh.log)
  129. do
  130. upgrade_package "$package"
  131. done
  132. ;;
  133. *)
  134. while IFS= read -r package
  135. do
  136. upgrade_package "$package"
  137. done < <(awk '/would be upgraded/ { print $2 }' refresh.log)
  138. ;;
  139. esac
  140. ;;
  141. esac
  142. }
  143. main "$@"