git 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 blobs_path=$(project_blobs_path "$project" "$@")
  274. local blob
  275. if ! [[ -f "$blobs_path" ]]
  276. then
  277. return
  278. fi
  279. while read blob
  280. do
  281. git_remove "$repository_path" "$blob"
  282. done < "$blobs_path"
  283. if ! git_diff_staged_check "$repository_path"
  284. then
  285. git_commit "$repository_path" "Removed blobs"
  286. fi
  287. }
  288. git_project_prepare_patch() {
  289. local project=$1
  290. shift
  291. local repository=$1
  292. shift
  293. local branch=$project
  294. local argument
  295. local path
  296. for argument in "$@"
  297. do
  298. if [[ -z "$path" ]]
  299. then
  300. path="$argument"
  301. else
  302. path="$path/$argument"
  303. fi
  304. branch="$branch-$argument"
  305. done
  306. if [[ -n $branch ]]
  307. then
  308. local prepare_branch=$BRANCH_PREFIX$branch
  309. local prepare_path=$path
  310. git_project_patch_recursive "$project" "$repository" "$prepare_branch" "$prepare_path"
  311. fi
  312. }
  313. git_project_prepare_revision() {
  314. local project=$1
  315. shift
  316. local repository=$1
  317. shift
  318. local repository_path=$(git_project_repository_path "$repository")
  319. local project_path=$(project_path "$project")
  320. local configs_path="$project_path/$CONFIGS"
  321. local branch=$project
  322. local prepare_revision
  323. local argument
  324. local path
  325. for argument in "" "$@"
  326. do
  327. if [[ -n $argument ]]
  328. then
  329. if [[ -z $path ]]
  330. then
  331. path="$argument"
  332. else
  333. path="$path/$argument"
  334. fi
  335. branch="$branch-$argument"
  336. fi
  337. local revision_path="$configs_path/$path/$REVISION"
  338. if [[ -f $revision_path ]]; then
  339. prepare_revision=$(< "$revision_path")
  340. fi
  341. done
  342. if [[ -n $branch ]]
  343. then
  344. local prepare_branch=$BRANCH_PREFIX$branch
  345. git_branch_create "$repository_path" "$prepare_branch" "$prepare_revision"
  346. fi
  347. }
  348. git_project_prepare_check() {
  349. local project=$1
  350. shift
  351. local repository=$1
  352. shift
  353. local repository_path=$(git_project_repository_path "$repository")
  354. local branch=$project
  355. local argument
  356. for argument in "$@"
  357. do
  358. branch="$branch-$argument"
  359. done
  360. if [[ -n $branch ]]
  361. then
  362. local prepare_branch=$BRANCH_PREFIX$branch
  363. git_branch_check "$repository_path" "$prepare_branch"
  364. fi
  365. }
  366. git_project_prepare_clean() {
  367. local project=$1
  368. shift
  369. local repository=$1
  370. shift
  371. local repository_path=$(git_project_repository_path "$repository")
  372. local branch=$project
  373. local argument
  374. for argument in "$@"
  375. do
  376. branch="$branch-$argument"
  377. done
  378. if [[ -n $branch ]]
  379. then
  380. local prepare_branch=$BRANCH_PREFIX$branch
  381. if git_branch_check "$repository_path" "$prepare_branch"
  382. then
  383. git_branch_delete "$repository_path" "$prepare_branch"
  384. fi
  385. fi
  386. }
  387. git_project_checkout() {
  388. local project=$1
  389. shift
  390. local repository=$1
  391. shift
  392. local repository_path=$(git_project_repository_path "$repository")
  393. local branch=$project
  394. local argument
  395. for argument in "$@"
  396. do
  397. branch="$branch-$argument"
  398. done
  399. if [[ -n $branch ]]
  400. then
  401. local checkout_branch=$BRANCH_PREFIX$branch
  402. if git_branch_check "$repository_path" "$checkout_branch"
  403. then
  404. git_branch_checkout "$repository_path" "$checkout_branch"
  405. git_submodule_update "$repository_path"
  406. fi
  407. fi
  408. }
  409. git_project_update() {
  410. local project=$1
  411. shift
  412. local repository=$1
  413. shift
  414. local repository_path=$(git_project_repository_path "$repository")
  415. git_fetch "$repository_path"
  416. git_branch_checkout "$repository_path" "$ORIGIN_HEAD"
  417. git_project_prepare_clean "$project" "$repository" "$@"
  418. git_project_prepare "$project" "$repository" "$@"
  419. }
  420. git_project_update_check() {
  421. local project=$1
  422. shift
  423. local repository=$1
  424. shift
  425. git_project_prepare_check "$project" "$repository" "$@"
  426. git_fetch_check "$repository_path"
  427. }
  428. git_project_release() {
  429. local project=$1
  430. shift
  431. local repository=$1
  432. shift
  433. local repository_path=$(git_project_repository_path "$repository")
  434. local branch=$project
  435. local argument
  436. for argument in "$@"
  437. do
  438. branch="$branch-$argument"
  439. done
  440. if [[ -n $branch ]]
  441. then
  442. local release_branch=$BRANCH_PREFIX$branch
  443. if git_branch_check "$repository_path" "$release_branch"
  444. then
  445. local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
  446. local sources_path="$root/$SOURCES/$repository"
  447. printf '%s\n' "Releasing sources archive for $project (with ${arguments:-no argument}) from "$repository" git"
  448. git_branch_checkout "$repository_path" "$release_branch"
  449. git_submodule_update "$repository_path"
  450. git_clean "$repository_path"
  451. archive_create "$archive_path" "$sources_path" "$release_branch"
  452. file_verification_create "$archive_path"
  453. fi
  454. fi
  455. }
  456. git_project_release_check() {
  457. local project=$1
  458. shift
  459. local repository=$1
  460. shift
  461. local repository_path=$(git_project_repository_path "$repository")
  462. local branch=$project
  463. local argument
  464. for argument in "$@"
  465. do
  466. branch="$branch-$argument"
  467. done
  468. if [[ -n $branch ]]
  469. then
  470. local release_branch=$BRANCH_PREFIX$branch
  471. if git_branch_check "$repository_path" "$release_branch"
  472. then
  473. local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
  474. file_exists_check "$archive_path"
  475. fi
  476. fi
  477. }