git 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. if ! git_apply_check "$repository_path" "$branch" "$patch"; then
  178. printf '%s\n' "Patching failed with: ${patch##*$root/$PROJECTS/}"
  179. return 1
  180. fi
  181. case $patch in
  182. *.patch)
  183. git_am "$repository_path" "$branch" "$patch"
  184. ;;
  185. *.diff)
  186. git_apply "$repository_path" "$branch" "$patch"
  187. git_commit "$repository_path" "Applied ${patch##*/}"
  188. ;;
  189. *)
  190. ;;
  191. esac
  192. }
  193. git_revision() {
  194. local repository_path=$1
  195. (
  196. cd "$repository_path" 2>/dev/null || exit 1
  197. git rev-parse "$HEAD"
  198. )
  199. }
  200. git_describe() {
  201. local repository_path=$1
  202. (
  203. cd "$repository_path" 2>/dev/null || exit 1
  204. git describe --tags
  205. )
  206. }
  207. git_files() {
  208. local repository_path="$1"
  209. (
  210. cd "$repository_path" 2>/dev/null || exit 1
  211. git ls-files -z | sort -z
  212. )
  213. }
  214. git_project_repository_path() {
  215. local repository=$1
  216. printf '%s\n' "$root/$SOURCES/$repository"
  217. }
  218. git_project_check() {
  219. local repository=$1
  220. local repository_path=$(git_project_repository_path "$repository")
  221. git_check "$repository_path"
  222. }
  223. git_project_patch_recursive() {
  224. local project=$1
  225. local repository=$2
  226. local branch=$3
  227. local path=$4
  228. local repository_path=$(git_project_repository_path "$repository")
  229. local project_path=$(project_path "$project")
  230. local patches_path=$project_path/$PATCHES/$path
  231. if ! [[ -d $project_path/$PATCHES ]]; then
  232. return 0
  233. fi
  234. for patch in "$patches_path"/[!.]*.@(patch|diff); do
  235. git_patch "$repository_path" "$branch" "$patch" || return 1
  236. done
  237. if [[ -n $path && $path != . ]]; then
  238. git_project_patch_recursive "$project" "$repository" "$branch" "$(dirname "$path")"
  239. fi
  240. }
  241. git_project_clone() {
  242. local repository=$1
  243. shift
  244. local urls=$@
  245. local repository_path=$(git_project_repository_path "$repository")
  246. local directory_path=$(dirname "$repository_path")
  247. local url
  248. mkdir -p "$directory_path"
  249. (
  250. set +e
  251. for url in $urls
  252. do
  253. if git_clone "$repository_path" "$url"
  254. then
  255. return 0
  256. fi
  257. done
  258. return 1
  259. )
  260. }
  261. git_project_prepare() {
  262. local project=$1
  263. shift
  264. local repository=$1
  265. shift
  266. git_project_prepare_revision "$project" "$repository" "$@"
  267. git_project_prepare_blobs "$project" "$repository" "$@"
  268. git_project_prepare_patch "$project" "$repository" "$@"
  269. }
  270. git_project_prepare_blobs() {
  271. local project=$1
  272. shift
  273. local repository=$1
  274. shift
  275. local repository_path=$(git_project_repository_path "$repository")
  276. local blob
  277. while read -r blob
  278. do
  279. git_remove "$repository_path" "$blob"
  280. done < <(project_blobs "$project" "$@")
  281. if ! git_diff_staged_check "$repository_path"
  282. then
  283. git_commit "$repository_path" "Removed blobs"
  284. fi
  285. }
  286. git_project_prepare_patch() {
  287. local project=$1
  288. shift
  289. local repository=$1
  290. shift
  291. local branch=$project
  292. local argument
  293. local path
  294. for argument in "$@"
  295. do
  296. if [[ -z "$path" ]]
  297. then
  298. path="$argument"
  299. else
  300. path="$path/$argument"
  301. fi
  302. branch="$branch-$argument"
  303. done
  304. if [[ -n $branch ]]
  305. then
  306. local prepare_branch=$BRANCH_PREFIX$branch
  307. local prepare_path=$path
  308. git_project_patch_recursive "$project" "$repository" "$prepare_branch" "$prepare_path"
  309. fi
  310. }
  311. git_project_prepare_revision() {
  312. local project=$1
  313. shift
  314. local repository=$1
  315. shift
  316. local repository_path=$(git_project_repository_path "$repository")
  317. local project_path=$(project_path "$project")
  318. local configs_path="$project_path/$CONFIGS"
  319. local branch=$project
  320. local prepare_revision
  321. local argument
  322. local path
  323. for argument in "" "$@"
  324. do
  325. if [[ -n $argument ]]
  326. then
  327. if [[ -z $path ]]
  328. then
  329. path="$argument"
  330. else
  331. path="$path/$argument"
  332. fi
  333. branch="$branch-$argument"
  334. fi
  335. local revision_path="$configs_path/$path/$REVISION"
  336. if [[ -f $revision_path ]]; then
  337. prepare_revision=$(< "$revision_path")
  338. fi
  339. done
  340. if [[ -n $branch ]]
  341. then
  342. local prepare_branch=$BRANCH_PREFIX$branch
  343. git_branch_create "$repository_path" "$prepare_branch" "$prepare_revision"
  344. fi
  345. }
  346. git_project_prepare_check() {
  347. local project=$1
  348. shift
  349. local repository=$1
  350. shift
  351. local repository_path=$(git_project_repository_path "$repository")
  352. local branch=$project
  353. local argument
  354. for argument in "$@"
  355. do
  356. branch="$branch-$argument"
  357. done
  358. if [[ -n $branch ]]
  359. then
  360. local prepare_branch=$BRANCH_PREFIX$branch
  361. git_branch_check "$repository_path" "$prepare_branch"
  362. fi
  363. }
  364. git_project_prepare_clean() {
  365. local project=$1
  366. shift
  367. local repository=$1
  368. shift
  369. local repository_path=$(git_project_repository_path "$repository")
  370. local branch=$project
  371. local argument
  372. for argument in "$@"
  373. do
  374. branch="$branch-$argument"
  375. done
  376. if [[ -n $branch ]]
  377. then
  378. local prepare_branch=$BRANCH_PREFIX$branch
  379. if git_branch_check "$repository_path" "$prepare_branch"
  380. then
  381. git_branch_delete "$repository_path" "$prepare_branch"
  382. fi
  383. fi
  384. }
  385. git_project_checkout() {
  386. local project=$1
  387. shift
  388. local repository=$1
  389. shift
  390. local repository_path=$(git_project_repository_path "$repository")
  391. local branch=$project
  392. local argument
  393. for argument in "$@"
  394. do
  395. branch="$branch-$argument"
  396. done
  397. if [[ -n $branch ]]
  398. then
  399. local checkout_branch=$BRANCH_PREFIX$branch
  400. if git_branch_check "$repository_path" "$checkout_branch"
  401. then
  402. git_branch_checkout "$repository_path" "$checkout_branch"
  403. git_submodule_update "$repository_path"
  404. fi
  405. fi
  406. }
  407. git_project_update() {
  408. local project=$1
  409. shift
  410. local repository=$1
  411. shift
  412. local repository_path=$(git_project_repository_path "$repository")
  413. git_fetch "$repository_path"
  414. git_branch_checkout "$repository_path" "$ORIGIN_HEAD"
  415. git_project_prepare_clean "$project" "$repository" "$@"
  416. git_project_prepare "$project" "$repository" "$@"
  417. }
  418. git_project_update_check() {
  419. local project=$1
  420. shift
  421. local repository=$1
  422. shift
  423. git_project_prepare_check "$project" "$repository" "$@"
  424. git_fetch_check "$repository_path"
  425. }
  426. git_project_release() {
  427. local project=$1
  428. shift
  429. local repository=$1
  430. shift
  431. local repository_path=$(git_project_repository_path "$repository")
  432. local branch=$project
  433. local argument
  434. for argument in "$@"
  435. do
  436. branch="$branch-$argument"
  437. done
  438. if [[ -n $branch ]]
  439. then
  440. local release_branch=$BRANCH_PREFIX$branch
  441. if git_branch_check "$repository_path" "$release_branch"
  442. then
  443. local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
  444. local sources_path="$root/$SOURCES/$repository"
  445. printf '%s\n' "Releasing sources archive for $project (with ${arguments:-no argument}) from "$repository" git"
  446. git_branch_checkout "$repository_path" "$release_branch"
  447. git_submodule_update "$repository_path"
  448. git_clean "$repository_path"
  449. archive_create "$archive_path" "$sources_path" "$release_branch"
  450. file_verification_create "$archive_path"
  451. fi
  452. fi
  453. }
  454. git_project_release_check() {
  455. local project=$1
  456. shift
  457. local repository=$1
  458. shift
  459. local repository_path=$(git_project_repository_path "$repository")
  460. local branch=$project
  461. local argument
  462. for argument in "$@"
  463. do
  464. branch="$branch-$argument"
  465. done
  466. if [[ -n $branch ]]
  467. then
  468. local release_branch=$BRANCH_PREFIX$branch
  469. if git_branch_check "$repository_path" "$release_branch"
  470. then
  471. local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
  472. file_exists_check "$archive_path"
  473. fi
  474. fi
  475. }