git 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. # Copyright (C) 2019 Andrew Robbins <contact@andrewrobbins.info>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. BRANCH_PREFIX="libreboot-"
  18. DOTGIT=".git"
  19. HEAD="HEAD"
  20. ORIGIN_HEAD="origin/HEAD"
  21. WILDDOTPATCH="*.patch"
  22. GIT_NAME="Libreboot"
  23. GIT_EMAIL="libreboot@libreboot.org"
  24. git_check() {
  25. local repository_path=$1
  26. directory_filled_check "$repository_path/$DOTGIT"
  27. }
  28. git_clone() {
  29. local repository_path=$1
  30. local url=$2
  31. git clone "$url" "$repository_path"
  32. }
  33. git_submodule_update() {
  34. local repository_path=$1
  35. (
  36. cd "$repository_path" 2>/dev/null || exit 1
  37. git submodule update --init
  38. )
  39. }
  40. git_merge() {
  41. local repository_path=$1
  42. local revision=$2
  43. (
  44. cd "$repository_path" 2>/dev/null || exit 1
  45. git merge "$revision"
  46. )
  47. }
  48. git_branch_create() {
  49. local repository_path=$1
  50. local branch=$2
  51. local revision=$3
  52. (
  53. cd "$repository_path" 2>/dev/null || exit 1
  54. git checkout -B "$branch"
  55. if [[ -n "$revision" ]]
  56. then
  57. git reset --hard "$revision"
  58. fi
  59. )
  60. }
  61. git_branch_delete() {
  62. local repository_path=$1
  63. local branch=$2
  64. (
  65. cd "$repository_path" 2>/dev/null || exit 1
  66. git branch -D "$branch"
  67. )
  68. }
  69. git_branch_checkout() {
  70. local repository_path=$1
  71. local branch=$2
  72. (
  73. cd "$repository_path" 2>/dev/null || exit 1
  74. git checkout "$branch" > /dev/null
  75. )
  76. }
  77. git_branch_check() {
  78. local repository_path=$1
  79. local branch=$2
  80. (
  81. cd "$repository_path" 2>/dev/null || exit 1
  82. git rev-parse --verify "$branch" >/dev/null 2>&1
  83. )
  84. }
  85. git_fetch() {
  86. local repository_path=$1
  87. (
  88. cd "$repository_path" 2>/dev/null || exit 1
  89. git fetch origin
  90. )
  91. }
  92. git_fetch_check() {
  93. local repository_path=$1
  94. (
  95. cd "$repository_path" 2>/dev/null || exit 1
  96. git fetch --dry-run origin >/dev/null 2>&1
  97. )
  98. }
  99. git_clean() {
  100. local repository_path=$1
  101. (
  102. cd "$repository_path" 2>/dev/null || exit 1
  103. git clean -df
  104. )
  105. }
  106. git_remove() {
  107. local repository_path=$1
  108. local path=$2
  109. (
  110. cd "$repository_path" 2>/dev/null || exit 1
  111. git rm -rf "$path"
  112. )
  113. }
  114. git_diff_staged_check() {
  115. local repository_path=$1
  116. (
  117. cd "$repository_path" 2>/dev/null || exit 1
  118. git diff --staged --quiet
  119. )
  120. }
  121. git_diff_check() {
  122. local repository_path=$1
  123. (
  124. cd "$repository_path" 2>/dev/null || exit 1
  125. git diff --quiet
  126. )
  127. }
  128. git_commit() {
  129. local repository_path=$1
  130. local message=$2
  131. (
  132. export GIT_COMMITTER_NAME=$GIT_NAME
  133. export GIT_COMMITTER_EMAIL=$GIT_EMAIL
  134. cd "$repository_path" 2>/dev/null || exit 1
  135. git commit --author="$GIT_NAME <$GIT_EMAIL>" -m "$message"
  136. )
  137. }
  138. git_am() {
  139. local repository_path=$1
  140. local branch=$2
  141. local patch=$3
  142. (
  143. export GIT_COMMITTER_NAME=$GIT_NAME
  144. export GIT_COMMITTER_EMAIL=$GIT_EMAIL
  145. cd "$repository_path" 2>/dev/null || exit 1
  146. git checkout "$branch" >/dev/null 2>&1
  147. if ! git am "$patch"; then
  148. git am --abort
  149. exit 1
  150. fi
  151. )
  152. }
  153. git_apply() {
  154. local repository_path=$1
  155. local branch=$2
  156. local patch=$3
  157. (
  158. cd "$repository_path" 2>/dev/null || exit 1
  159. git checkout "$branch" >/dev/null 2>&1
  160. git apply --index "$patch"
  161. )
  162. }
  163. git_apply_check() {
  164. local repository_path=$1
  165. local branch=$2
  166. local patch=$3
  167. (
  168. cd "$repository_path" 2>/dev/null || exit 1
  169. git checkout "$branch" >/dev/null 2>&1
  170. git apply --check "$patch"
  171. )
  172. }
  173. git_patch() {
  174. local repository_path=$1
  175. local branch=$2
  176. local patch=$3
  177. git_apply_check "$repository_path" "$branch" "$patch" || return 1
  178. case $patch in
  179. *.patch)
  180. git_am "$repository_path" "$branch" "$patch"
  181. ;;
  182. *.diff)
  183. git_apply "$repository_path" "$branch" "$patch"
  184. git_commit "$repository_path" "Applied ${patch##*/}"
  185. ;;
  186. *)
  187. ;;
  188. esac
  189. }
  190. git_revision() {
  191. local repository_path=$1
  192. (
  193. cd "$repository_path" 2>/dev/null || exit 1
  194. git rev-parse "$HEAD"
  195. )
  196. }
  197. git_describe() {
  198. local repository_path=$1
  199. (
  200. cd "$repository_path" 2>/dev/null || exit 1
  201. git describe --tags
  202. )
  203. }
  204. git_files() {
  205. local repository_path="$1"
  206. (
  207. cd "$repository_path" 2>/dev/null || exit 1
  208. git ls-files -z | sort -z
  209. )
  210. }
  211. git_project_repository_path() {
  212. local repository=$1
  213. printf '%s\n' "$root/$SOURCES/$repository"
  214. }
  215. git_project_check() {
  216. local repository=$1
  217. local repository_path=$(git_project_repository_path "$repository")
  218. git_check "$repository_path"
  219. }
  220. git_project_patch_recursive() {
  221. local project=$1
  222. local repository=$2
  223. local branch=$3
  224. local path=$4
  225. local repository_path=$(git_project_repository_path "$repository")
  226. local project_path=$(project_path "$project")
  227. local patches_path=$project_path/$PATCHES/$path
  228. if ! [[ -d $project_path/$PATCHES ]]; then
  229. return 0
  230. fi
  231. for patch in "$patches_path"/[!.]*.@(patch|diff); do
  232. git_patch "$repository_path" "$branch" "$patch" || return 1
  233. done
  234. if [[ $path != . ]]; then
  235. git_project_patch_recursive "$project" "$repository" "$branch" "$(dirname "$path")"
  236. fi
  237. }
  238. git_project_clone() {
  239. local repository=$1
  240. shift
  241. local urls=$@
  242. local repository_path=$(git_project_repository_path "$repository")
  243. local directory_path=$(dirname "$repository_path")
  244. local url
  245. mkdir -p "$directory_path"
  246. (
  247. set +e
  248. for url in $urls
  249. do
  250. if git_clone "$repository_path" "$url"
  251. then
  252. return 0
  253. fi
  254. done
  255. return 1
  256. )
  257. }
  258. git_project_prepare() {
  259. local project=$1
  260. shift
  261. local repository=$1
  262. shift
  263. git_project_prepare_revision "$project" "$repository" "$@"
  264. git_project_prepare_blobs "$project" "$repository" "$@"
  265. git_project_prepare_patch "$project" "$repository" "$@"
  266. }
  267. git_project_prepare_blobs() {
  268. local project=$1
  269. shift
  270. local repository=$1
  271. shift
  272. local repository_path=$(git_project_repository_path "$repository")
  273. local blob
  274. while read -r blob
  275. do
  276. git_remove "$repository_path" "$blob"
  277. done < <(project_blobs "$project" "$@")
  278. if ! git_diff_staged_check "$repository_path"
  279. then
  280. git_commit "$repository_path" "Removed blobs"
  281. fi
  282. }
  283. git_project_prepare_patch() {
  284. local project=$1
  285. shift
  286. local repository=$1
  287. shift
  288. local branch=$project
  289. local argument
  290. local path
  291. for argument in "$@"
  292. do
  293. if [[ -z "$path" ]]
  294. then
  295. path="$argument"
  296. else
  297. path="$path/$argument"
  298. fi
  299. branch="$branch-$argument"
  300. done
  301. if [[ -n $branch ]]
  302. then
  303. local prepare_branch=$BRANCH_PREFIX$branch
  304. local prepare_path=$path
  305. git_project_patch_recursive "$project" "$repository" "$prepare_branch" "$prepare_path"
  306. fi
  307. }
  308. git_project_prepare_revision() {
  309. local project=$1
  310. shift
  311. local repository=$1
  312. shift
  313. local repository_path=$(git_project_repository_path "$repository")
  314. local project_path=$(project_path "$project")
  315. local configs_path="$project_path/$CONFIGS"
  316. local branch=$project
  317. local prepare_revision
  318. local argument
  319. local path
  320. for argument in "" "$@"
  321. do
  322. if [[ -n $argument ]]
  323. then
  324. if [[ -z $path ]]
  325. then
  326. path="$argument"
  327. else
  328. path="$path/$argument"
  329. fi
  330. branch="$branch-$argument"
  331. fi
  332. local revision_path="$configs_path/$path/$REVISION"
  333. if [[ -f $revision_path ]]; then
  334. prepare_revision=$(< "$revision_path")
  335. fi
  336. done
  337. if [[ -n $branch ]]
  338. then
  339. local prepare_branch=$BRANCH_PREFIX$branch
  340. git_branch_create "$repository_path" "$prepare_branch" "$prepare_revision"
  341. fi
  342. }
  343. git_project_prepare_check() {
  344. local project=$1
  345. shift
  346. local repository=$1
  347. shift
  348. local repository_path=$(git_project_repository_path "$repository")
  349. local branch=$project
  350. local argument
  351. for argument in "$@"
  352. do
  353. branch="$branch-$argument"
  354. done
  355. if [[ -n $branch ]]
  356. then
  357. local prepare_branch=$BRANCH_PREFIX$branch
  358. git_branch_check "$repository_path" "$prepare_branch"
  359. fi
  360. }
  361. git_project_prepare_clean() {
  362. local project=$1
  363. shift
  364. local repository=$1
  365. shift
  366. local repository_path=$(git_project_repository_path "$repository")
  367. local branch=$project
  368. local argument
  369. for argument in "$@"
  370. do
  371. branch="$branch-$argument"
  372. done
  373. if [[ -n $branch ]]
  374. then
  375. local prepare_branch=$BRANCH_PREFIX$branch
  376. if git_branch_check "$repository_path" "$prepare_branch"
  377. then
  378. git_branch_delete "$repository_path" "$prepare_branch"
  379. fi
  380. fi
  381. }
  382. git_project_checkout() {
  383. local project=$1
  384. shift
  385. local repository=$1
  386. shift
  387. local repository_path=$(git_project_repository_path "$repository")
  388. local branch=$project
  389. local argument
  390. for argument in "$@"
  391. do
  392. branch="$branch-$argument"
  393. done
  394. if [[ -n $branch ]]
  395. then
  396. local checkout_branch=$BRANCH_PREFIX$branch
  397. if git_branch_check "$repository_path" "$checkout_branch"
  398. then
  399. git_branch_checkout "$repository_path" "$checkout_branch"
  400. git_submodule_update "$repository_path"
  401. fi
  402. fi
  403. }
  404. git_project_update() {
  405. local project=$1
  406. shift
  407. local repository=$1
  408. shift
  409. local repository_path=$(git_project_repository_path "$repository")
  410. git_fetch "$repository_path"
  411. git_branch_checkout "$repository_path" "$ORIGIN_HEAD"
  412. git_project_prepare_clean "$project" "$repository" "$@"
  413. git_project_prepare "$project" "$repository" "$@"
  414. }
  415. git_project_update_check() {
  416. local project=$1
  417. shift
  418. local repository=$1
  419. shift
  420. git_project_prepare_check "$project" "$repository" "$@"
  421. git_fetch_check "$repository_path"
  422. }
  423. git_project_release() {
  424. local project=$1
  425. shift
  426. local repository=$1
  427. shift
  428. local repository_path=$(git_project_repository_path "$repository")
  429. local branch=$project
  430. local argument
  431. for argument in "$@"
  432. do
  433. branch="$branch-$argument"
  434. done
  435. if [[ -n $branch ]]
  436. then
  437. local release_branch=$BRANCH_PREFIX$branch
  438. if git_branch_check "$repository_path" "$release_branch"
  439. then
  440. local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
  441. local sources_path="$root/$SOURCES/$repository"
  442. printf '%s\n' "Releasing sources archive for $project (with ${arguments:-no argument}) from "$repository" git"
  443. git_branch_checkout "$repository_path" "$release_branch"
  444. git_submodule_update "$repository_path"
  445. git_clean "$repository_path"
  446. archive_create "$archive_path" "$sources_path" "$release_branch"
  447. file_verification_create "$archive_path"
  448. fi
  449. fi
  450. }
  451. git_project_release_check() {
  452. local project=$1
  453. shift
  454. local repository=$1
  455. shift
  456. local repository_path=$(git_project_repository_path "$repository")
  457. local branch=$project
  458. local argument
  459. for argument in "$@"
  460. do
  461. branch="$branch-$argument"
  462. done
  463. if [[ -n $branch ]]
  464. then
  465. local release_branch=$BRANCH_PREFIX$branch
  466. if git_branch_check "$repository_path" "$release_branch"
  467. then
  468. local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
  469. file_exists_check "$archive_path"
  470. fi
  471. fi
  472. }