gitrepo.bash 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env bash
  2. set -e
  3. set -x
  4. readonly tmpdir="$(mktemp -d)"
  5. trap "rm -rf '$tmpdir'" EXIT
  6. readonly outdir="${BASH_SOURCE%/*}"
  7. readonly defaultBranch='master'
  8. export GIT_AUTHOR_NAME='testauthor'
  9. export GIT_AUTHOR_EMAIL='[email protected]'
  10. export GIT_COMMITTER_NAME='testauthor'
  11. export GIT_COMMITTER_EMAIL='[email protected]'
  12. export GIT_CONFIG_NOSYSTEM=1
  13. export GIT_CONFIG_GLOBAL="$tmpdir/.gitconfig"
  14. git config --global protocol.file.allow always
  15. (
  16. cd "$tmpdir"
  17. git --bare init -b "$defaultBranch" gitrepo.git
  18. rm -f gitrepo.git/hooks/*.sample
  19. mkdir gitrepo
  20. cd gitrepo
  21. git init -b "$defaultBranch"
  22. echo 'cmake_minimum_required(VERSION 3.31)
  23. project(Example NONE)
  24. add_custom_target(example ALL
  25. COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/example.cmake.in example.cmake
  26. )' >CMakeLists.txt
  27. echo 'message(STATUS example)' >example.cmake.in
  28. git add CMakeLists.txt example.cmake.in
  29. git commit -m "Initial import into repo."
  30. git push ../gitrepo.git "$defaultBranch"
  31. cd ../gitrepo.git
  32. git gc --prune
  33. cd ..
  34. ln -s gitrepo.git GIT
  35. git --bare init -b "$defaultBranch" gitrepo-sub.git
  36. rm -f gitrepo-sub.git/hooks/*.sample
  37. mkdir gitrepo-sub
  38. cd gitrepo-sub
  39. git init -b "$defaultBranch"
  40. echo 'cmake_minimum_required(VERSION 3.31)
  41. project(ExampleWithSub NONE)
  42. file(STRINGS "${CMAKE_SOURCE_DIR}/.git/config" git_config_strings
  43. REGEX "^\\[submodule")
  44. foreach(submodule m1 m2)
  45. option(WITH_${submodule} "Enable ${submodule}" OFF)
  46. set(${submodule}_INITED FALSE)
  47. set(${submodule}_UPDATED FALSE)
  48. foreach(s ${git_config_strings})
  49. if("${s}" MATCHES "${submodule}")
  50. set(${submodule}_INITED TRUE)
  51. endif()
  52. endforeach()
  53. if(EXISTS "${CMAKE_SOURCE_DIR}/m/${submodule}/CMakeLists.txt")
  54. set(${submodule}_UPDATED TRUE)
  55. endif()
  56. if(WITH_${submodule})
  57. if(NOT ${submodule}_INITED)
  58. message(FATAL_ERROR "${submodule} not inited")
  59. elseif(NOT ${submodule}_UPDATED)
  60. message(FATAL_ERROR "${submodule} not updated")
  61. endif()
  62. else()
  63. if(${submodule}_INITED)
  64. message(FATAL_ERROR "${submodule} inited")
  65. elseif(${submodule}_UPDATED)
  66. message(FATAL_ERROR "${submodule} updated")
  67. endif()
  68. endif()
  69. endforeach()' >CMakeLists.txt
  70. git add CMakeLists.txt
  71. git submodule add -- ../GIT m/m1
  72. git submodule add -- ../GIT m/m2
  73. git submodule add -- ../GIT m/m3
  74. git commit -m "Initial import into repo."
  75. git push ../gitrepo-sub.git "$defaultBranch"
  76. cd ../gitrepo-sub.git
  77. git gc --prune
  78. cd ..
  79. ln -s gitrepo-sub.git GIT-with-submodules
  80. git --bare init -b "$defaultBranch" gitrepo-sub-rec.git
  81. rm -f gitrepo-sub-rec.git/hooks/*.sample
  82. mkdir gitrepo-sub-rec
  83. cd gitrepo-sub-rec
  84. git init -b "$defaultBranch"
  85. echo 'cmake_minimum_required(VERSION 3.19)
  86. project(ExampleWithRecSub NONE)
  87. set(top_submodule_dir "${CMAKE_SOURCE_DIR}/submodule")
  88. if(NOT EXISTS "${top_submodule_dir}/CMakeLists.txt")
  89. message(FATAL_ERROR "Top submodule not updated")
  90. endif()
  91. option(WITH_RECURSIVE "Submodules are updated recursively" ON)
  92. foreach(submodule m1 m2 m3)
  93. set(${submodule}_UPDATED FALSE)
  94. if(EXISTS "${top_submodule_dir}/m/${submodule}/CMakeLists.txt")
  95. set(${submodule}_UPDATED TRUE)
  96. endif()
  97. if(WITH_RECURSIVE)
  98. if(NOT ${submodule}_UPDATED)
  99. message(FATAL_ERROR "${submodule} not updated")
  100. endif()
  101. else()
  102. if(${submodule}_UPDATED)
  103. message(FATAL_ERROR "${submodule} updated")
  104. endif()
  105. endif()
  106. endforeach()' >CMakeLists.txt
  107. git add CMakeLists.txt
  108. git submodule add -- ../GIT-with-submodules submodule
  109. git commit -m "Initial import into repo."
  110. git push ../gitrepo-sub-rec.git "$defaultBranch"
  111. cd ../gitrepo-sub-rec.git
  112. git gc --prune
  113. cd ..
  114. )
  115. tar cvzf "$outdir/gitrepo.tgz" -C "$tmpdir" gitrepo.git
  116. tar cvzf "$outdir/gitrepo-sub.tgz" -C "$tmpdir" gitrepo-sub.git
  117. tar cvzf "$outdir/gitrepo-sub-rec.tgz" -C "$tmpdir" gitrepo-sub-rec.git
  118. git_tag=$(cd "$tmpdir/gitrepo.git" ; git rev-parse HEAD)
  119. sed -i "/generated by gitrepo.bash/ s/ [0-9a-f]\\+ / $git_tag /" "$outdir/CMakeLists.txt"