git-setup-dirs.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #!/usr/bin/env bash
  2. SCRIPT_NAME=$(basename "$0")
  3. function usage()
  4. {
  5. echo "$SCRIPT_NAME [-h] [-n] [-u]"
  6. echo
  7. echo " arguments:"
  8. echo " -h: show this help text"
  9. echo " -n: dry run mode"
  10. echo " (default: run commands)"
  11. echo " -u: if a directory or worktree already exists, use it"
  12. echo " (default: fail and exit on existing directories)"
  13. echo
  14. echo " env vars:"
  15. echo " required:"
  16. echo " TOR_FULL_GIT_PATH: where the git repository directories reside."
  17. echo " You must set this env var, we recommend \$HOME/git/"
  18. echo " (default: fail if this env var is not set;"
  19. echo " current: $GIT_PATH)"
  20. echo
  21. echo " optional:"
  22. echo " TOR_MASTER: the name of the directory containing the tor.git clone"
  23. echo " The primary tor git directory is \$GIT_PATH/\$TOR_MASTER"
  24. echo " (default: tor; current: $TOR_MASTER_NAME)"
  25. echo " TOR_WKT_NAME: the name of the directory containing the tor"
  26. echo " worktrees. The tor worktrees are:"
  27. echo " \$GIT_PATH/\$TOR_WKT_NAME/{maint-*,release-*}"
  28. echo " (default: tor-wkt; current: $TOR_WKT_NAME)"
  29. echo " TOR_GIT_ORIGIN_PULL: the origin remote pull URL."
  30. echo " (current: $GIT_ORIGIN_PULL)"
  31. echo " TOR_GIT_ORIGIN_PUSH: the origin remote push URL"
  32. echo " (current: $GIT_ORIGIN_PUSH)"
  33. echo " TOR_UPSTREAM_REMOTE_NAME: the default upstream remote."
  34. echo " If \$TOR_UPSTREAM_REMOTE_NAME is not 'origin', we have a"
  35. echo " separate upstream remote, and we don't push to origin."
  36. echo " (default: $DEFAULT_UPSTREAM_REMOTE)"
  37. echo " TOR_GITHUB_PULL: the tor-github remote pull URL"
  38. echo " (current: $GITHUB_PULL)"
  39. echo " TOR_GITHUB_PUSH: the tor-github remote push URL"
  40. echo " (current: $GITHUB_PUSH)"
  41. echo " TOR_EXTRA_CLONE_ARGS: extra arguments to git clone"
  42. echo " (current: $TOR_EXTRA_CLONE_ARGS)"
  43. echo " TOR_EXTRA_REMOTE_NAME: the name of an extra remote"
  44. echo " This remote is not pulled by this script or git-pull-all.sh."
  45. echo " This remote is not pushed by git-push-all.sh."
  46. echo " (current: $TOR_EXTRA_REMOTE_NAME)"
  47. echo " TOR_EXTRA_REMOTE_PULL: the extra remote pull URL."
  48. echo " (current: $TOR_EXTRA_REMOTE_PULL)"
  49. echo " TOR_EXTRA_REMOTE_PUSH: the extra remote push URL"
  50. echo " (current: $TOR_EXTRA_REMOTE_PUSH)"
  51. echo " we recommend that you set these env vars in your ~/.profile"
  52. }
  53. #################
  54. # Configuration #
  55. #################
  56. # Don't change this configuration - set the env vars in your .profile
  57. # Where are all those git repositories?
  58. GIT_PATH=${TOR_FULL_GIT_PATH:-"FULL_PATH_TO_GIT_REPOSITORY_DIRECTORY"}
  59. # The primary tor git repository directory from which all the worktree have
  60. # been created.
  61. TOR_MASTER_NAME=${TOR_MASTER_NAME:-"tor"}
  62. # The worktrees location (directory).
  63. TOR_WKT_NAME=${TOR_WKT_NAME:-"tor-wkt"}
  64. # Origin repositories
  65. GIT_ORIGIN_PULL=${TOR_GIT_ORIGIN_PULL:-"https://gitlab.torproject.org/tpo/core/tor.git"}
  66. GIT_ORIGIN_PUSH=${TOR_GIT_ORIGIN_PUSH:-"git@git-rw.torproject.org:tor.git"}
  67. # The upstream remote which gitlab.torproject.org/tpo/core/tor.git points to.
  68. DEFAULT_UPSTREAM_REMOTE=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
  69. # Copy the URLs from origin
  70. GIT_UPSTREAM_PULL="$GIT_ORIGIN_PULL"
  71. GIT_UPSTREAM_PUSH="$GIT_ORIGIN_PUSH"
  72. # And avoid pushing to origin if we have an upstream
  73. if [ "$DEFAULT_UPSTREAM_REMOTE" != "origin" ]; then
  74. GIT_ORIGIN_PUSH="No pushes to origin, if there is an upstream"
  75. fi
  76. # GitHub repositories
  77. GITHUB_PULL=${TOR_GITHUB_PULL:-"https://github.com/torproject/tor.git"}
  78. GITHUB_PUSH=${TOR_GITHUB_PUSH:-"No_Pushing_To_GitHub"}
  79. ##########################
  80. # Git branches to manage #
  81. ##########################
  82. # The branches and worktrees need to be modified when there is a new branch,
  83. # and when an old branch is no longer supported.
  84. set -e
  85. eval "$(git-list-tor-branches.sh -b)"
  86. set +e
  87. # The main branch path has to be the main repository thus contains the
  88. # origin that will be used to fetch the updates. All the worktrees are created
  89. # from that repository.
  90. ORIGIN_PATH="$GIT_PATH/$TOR_MASTER_NAME"
  91. #######################
  92. # Argument processing #
  93. #######################
  94. # Controlled by the -n option. The dry run option will just output the command
  95. # that would have been executed for each worktree.
  96. DRY_RUN=0
  97. # Controlled by the -s option. The use existing option checks for existing
  98. # directories, and re-uses them, rather than creating a new directory.
  99. USE_EXISTING=0
  100. USE_EXISTING_HINT="Use existing: '$SCRIPT_NAME -u'."
  101. while getopts "hnu" opt; do
  102. case "$opt" in
  103. h) usage
  104. exit 0
  105. ;;
  106. n) DRY_RUN=1
  107. echo " *** DRY RUN MODE ***"
  108. ;;
  109. u) USE_EXISTING=1
  110. echo " *** USE EXISTING DIRECTORIES MODE ***"
  111. ;;
  112. *)
  113. echo
  114. usage
  115. exit 1
  116. ;;
  117. esac
  118. done
  119. ###########################
  120. # Git worktrees to manage #
  121. ###########################
  122. COUNT=${#WORKTREE[@]}
  123. #############
  124. # Constants #
  125. #############
  126. # Control characters
  127. CNRM=$'\x1b[0;0m' # Clear color
  128. # Bright color
  129. BGRN=$'\x1b[1;32m'
  130. BBLU=$'\x1b[1;34m'
  131. BRED=$'\x1b[1;31m'
  132. BYEL=$'\x1b[1;33m'
  133. IWTH=$'\x1b[3;37m'
  134. # Strings for the pretty print.
  135. MARKER="${BBLU}[${BGRN}+${BBLU}]${CNRM}"
  136. SUCCESS="${BGRN}success${CNRM}"
  137. SKIPPED="${BYEL}skipped${CNRM}"
  138. FAILED="${BRED}failed${CNRM}"
  139. ####################
  140. # Helper functions #
  141. ####################
  142. # Validate the given returned value (error code), print success or failed. The
  143. # second argument is the error output in case of failure, it is printed out.
  144. # On failure, this function exits.
  145. function validate_ret
  146. {
  147. if [ "$1" -eq 0 ]; then
  148. printf "%s\\n" "$SUCCESS"
  149. else
  150. printf "%s\\n" "$FAILED"
  151. printf " %s\\n" "$2"
  152. exit 1
  153. fi
  154. }
  155. # Validate the given returned value (error code), print success, skipped, or
  156. # failed. If $USE_EXISTING is 0, fail on error, otherwise, skip on error.
  157. # The second argument is the error output in case of failure, it is printed
  158. # out. On failure, this function exits.
  159. function validate_ret_skip
  160. {
  161. if [ "$1" -ne 0 ]; then
  162. if [ "$USE_EXISTING" -eq "0" ]; then
  163. # Fail and exit with error
  164. validate_ret "$1" "$2 $USE_EXISTING_HINT"
  165. else
  166. printf "%s\\n" "$SKIPPED"
  167. printf " %s\\n" "${IWTH}$2${CNRM}"
  168. # Tell the caller to skip the rest of the function
  169. return 0
  170. fi
  171. fi
  172. # Tell the caller to continue
  173. return 1
  174. }
  175. # Create a directory, and any missing enclosing directories.
  176. # If the directory already exists: fail if $USE_EXISTING is 0, otherwise skip.
  177. function make_directory
  178. {
  179. local cmd="mkdir -p '$1'"
  180. printf " %s Creating directory %s..." "$MARKER" "$1"
  181. local check_cmd="[ ! -d '$1' ]"
  182. msg=$( eval "$check_cmd" 2>&1 )
  183. if validate_ret_skip $? "Directory already exists."; then
  184. return
  185. fi
  186. if [ $DRY_RUN -eq 0 ]; then
  187. msg=$( eval "$cmd" 2>&1 )
  188. validate_ret $? "$msg"
  189. else
  190. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  191. fi
  192. }
  193. # Create a symlink from the first argument to the second argument
  194. # If the link already exists: fail if $USE_EXISTING is 0, otherwise skip.
  195. function make_symlink
  196. {
  197. local cmd="ln -s '$1' '$2'"
  198. printf " %s Creating symlink from %s to %s..." "$MARKER" "$1" "$2"
  199. local check_cmd="[ ! -e '$2' ]"
  200. msg=$( eval "$check_cmd" 2>&1 )
  201. if validate_ret_skip $? "File already exists."; then
  202. return
  203. fi
  204. if [ $DRY_RUN -eq 0 ]; then
  205. msg=$( eval "$cmd" 2>&1 )
  206. validate_ret $? "$msg"
  207. else
  208. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  209. fi
  210. }
  211. # Go into the directory or repository, even if $DRY_RUN is non-zero.
  212. # If the directory does not exist, fail and log an error.
  213. # Otherwise, silently succeed.
  214. function goto_dir
  215. {
  216. if ! cd "$1" 1>/dev/null 2>/dev/null ; then
  217. printf " %s Changing to directory %s..." "$MARKER" "$1"
  218. validate_ret 1 "$1: Not found. Stopping."
  219. fi
  220. }
  221. # Clone a repository into a directory.
  222. # If the directory already exists: fail if $USE_EXISTING is 0, otherwise skip.
  223. function clone_repo
  224. {
  225. local cmd="git clone $TOR_EXTRA_CLONE_ARGS '$1' '$2'"
  226. printf " %s Cloning %s into %s..." "$MARKER" "$1" "$2"
  227. local check_cmd="[ ! -d '$2' ]"
  228. msg=$( eval "$check_cmd" 2>&1 )
  229. if validate_ret_skip $? "Directory already exists."; then
  230. # If we skip the clone, we need to do a fetch
  231. goto_dir "$ORIGIN_PATH"
  232. fetch_remote "origin"
  233. return
  234. fi
  235. if [ $DRY_RUN -eq 0 ]; then
  236. msg=$( eval "$cmd" 2>&1 )
  237. validate_ret $? "$msg"
  238. else
  239. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  240. fi
  241. }
  242. # Add a remote by name and URL.
  243. # If the remote already exists: fail if $USE_EXISTING is 0, otherwise skip.
  244. function add_remote
  245. {
  246. local cmd="git remote add '$1' '$2'"
  247. printf " %s Adding remote %s at %s..." "$MARKER" "$1" "$2"
  248. local check_cmd="git remote get-url '$1'"
  249. msg=$( eval "$check_cmd" 2>&1 )
  250. ret=$?
  251. # We don't want a remote, so we invert the exit status
  252. if validate_ret_skip $(( ! ret )) \
  253. "Remote already exists for $1 at $msg."; then
  254. return
  255. fi
  256. if [ $DRY_RUN -eq 0 ]; then
  257. msg=$( eval "$cmd" 2>&1 )
  258. validate_ret $? "$msg"
  259. else
  260. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  261. fi
  262. }
  263. # Set a remote's push URL by name and URL.
  264. function set_remote_push
  265. {
  266. local cmd="git remote set-url --push '$1' '$2'"
  267. printf " %s Setting remote %s push URL to '%s'..." "$MARKER" "$1" "$2"
  268. if [ $DRY_RUN -eq 0 ]; then
  269. msg=$( eval "$cmd" 2>&1 )
  270. validate_ret $? "$msg"
  271. else
  272. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  273. fi
  274. }
  275. # Fetch a remote by name.
  276. function fetch_remote
  277. {
  278. local cmd="git fetch '$1'"
  279. printf " %s Fetching %s..." "$MARKER" "$1"
  280. if [ $DRY_RUN -eq 0 ]; then
  281. msg=$( eval "$cmd" 2>&1 )
  282. validate_ret $? "$msg"
  283. else
  284. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  285. fi
  286. }
  287. # Replace the fetch configs for a remote with config if they match a pattern.
  288. function replace_fetch_config
  289. {
  290. local cmd="git config --replace-all remote.'$1'.fetch '$2' '$3'"
  291. printf " %s Replacing %s fetch configs for '%s'..." \
  292. "$MARKER" "$1" "$3"
  293. if [ $DRY_RUN -eq 0 ]; then
  294. msg=$( eval "$cmd" 2>&1 )
  295. validate_ret $? "$msg"
  296. else
  297. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  298. fi
  299. }
  300. # Set up the tor-github PR config, so tor-github/pr/NNNN/head points to GitHub
  301. # PR NNNN. In some repositories, "/head" is optional.
  302. function set_tor_github_pr_fetch_config
  303. {
  304. # Standard branches
  305. replace_fetch_config tor-github \
  306. "+refs/heads/*:refs/remotes/tor-github/*" \
  307. "refs/heads"
  308. # PRs
  309. replace_fetch_config "tor-github" \
  310. "+refs/pull/*:refs/remotes/tor-github/pr/*" \
  311. "refs/pull.*pr"
  312. }
  313. # Set up the tor-github PR config, so tor-gitlab/mr/NNNN points to GitHub
  314. # MR NNNN. In some repositories, "/head" is optional.
  315. function set_tor_gitlab_mr_fetch_config
  316. {
  317. # standard branches
  318. replace_fetch_config tor-gitlab \
  319. "+refs/heads/*:refs/remotes/tor-gitlab/*" \
  320. "refs/heads"
  321. # MRs
  322. replace_fetch_config tor-gitlab \
  323. "+refs/merge-requests/*/head:refs/remotes/tor-gitlab/mr/*" \
  324. "refs/merge-requests.*mr"
  325. }
  326. # Add a new worktree for branch at path.
  327. # If the directory already exists: fail if $USE_EXISTING is 0, otherwise skip.
  328. function add_worktree
  329. {
  330. local cmd="git worktree add '$2' '$1'"
  331. printf " %s Adding worktree for %s at %s..." "$MARKER" "$1" "$2"
  332. local check_cmd="[ ! -d '$2' ]"
  333. msg=$( eval "$check_cmd" 2>&1 )
  334. if validate_ret_skip $? "Directory already exists."; then
  335. return
  336. fi
  337. if [ $DRY_RUN -eq 0 ]; then
  338. msg=$( eval "$cmd" 2>&1 )
  339. validate_ret $? "$msg"
  340. else
  341. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  342. fi
  343. }
  344. # Switch to the given branch name.
  345. # If the branch does not exist: fail.
  346. function switch_branch
  347. {
  348. local cmd="git checkout '$1'"
  349. printf " %s Switching branch to %s..." "$MARKER" "$1"
  350. if [ $DRY_RUN -eq 0 ]; then
  351. msg=$( eval "$cmd" 2>&1 )
  352. validate_ret $? "$msg"
  353. else
  354. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  355. fi
  356. }
  357. # Checkout a new branch with the given branch name.
  358. # If the branch already exists: fail if $USE_EXISTING is 0, otherwise skip.
  359. function new_branch
  360. {
  361. local cmd="git checkout -b '$1'"
  362. printf " %s Creating new branch %s..." "$MARKER" "$1"
  363. local check_cmd="git branch --list '$1'"
  364. msg=$( eval "$check_cmd" 2>&1 )
  365. if validate_ret_skip $? "Branch already exists."; then
  366. return
  367. fi
  368. if [ $DRY_RUN -eq 0 ]; then
  369. msg=$( eval "$cmd" 2>&1 )
  370. validate_ret $? "$msg"
  371. else
  372. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  373. fi
  374. }
  375. # Switch to an existing branch, or checkout a new branch with the given
  376. # branch name.
  377. function switch_or_new_branch
  378. {
  379. local cmd="git rev-parse --verify '$1'"
  380. if [ $DRY_RUN -eq 0 ]; then
  381. # Call switch_branch if there is a branch, or new_branch if there is not
  382. msg=$( eval "$cmd" 2>&1 )
  383. RET=$?
  384. if [ $RET -eq 0 ]; then
  385. # Branch: (commit id)
  386. switch_branch "$1"
  387. elif [ $RET -eq 128 ]; then
  388. # Not a branch: "fatal: Needed a single revision"
  389. new_branch "$1"
  390. else
  391. # Unexpected return value
  392. validate_ret $RET "$msg"
  393. fi
  394. else
  395. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}, then depending on the result:"
  396. switch_branch "$1"
  397. new_branch "$1"
  398. fi
  399. }
  400. # Set the upstream for branch to upstream.
  401. function set_upstream
  402. {
  403. # Note the argument order is swapped
  404. local cmd="git branch --set-upstream-to='$2' '$1'"
  405. printf " %s Setting upstream for %s to %s..." "$MARKER" "$1" "$2"
  406. if [ $DRY_RUN -eq 0 ]; then
  407. msg=$( eval "$cmd" 2>&1 )
  408. validate_ret $? "$msg"
  409. else
  410. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  411. fi
  412. }
  413. ###############
  414. # Entry point #
  415. ###############
  416. printf "%s Setting up the repository and remote %s\\n" "$MARKER" \
  417. "${BYEL}origin${CNRM}"
  418. # First, fetch the origin.
  419. ORIGIN_PARENT=$(dirname "$ORIGIN_PATH")
  420. make_directory "$ORIGIN_PARENT"
  421. # This is just cd with an error check
  422. goto_dir "$ORIGIN_PARENT"
  423. # clone repository / origin remote
  424. clone_repo "$GIT_ORIGIN_PULL" "$TOR_MASTER_NAME"
  425. goto_dir "$ORIGIN_PATH"
  426. set_remote_push "origin" "$GIT_ORIGIN_PUSH"
  427. # upstream remote, if different to origin
  428. if [ "$DEFAULT_UPSTREAM_REMOTE" != "origin" ]; then
  429. printf "%s Setting up remote %s\\n" "$MARKER" \
  430. "${BYEL}$DEFAULT_UPSTREAM_REMOTE${CNRM}"
  431. add_remote "$DEFAULT_UPSTREAM_REMOTE" "$GIT_UPSTREAM_PULL"
  432. set_remote_push "$DEFAULT_UPSTREAM_REMOTE" "$GIT_UPSTREAM_PUSH"
  433. fetch_remote "$DEFAULT_UPSTREAM_REMOTE"
  434. fi
  435. # GitHub remote
  436. printf "%s Setting up remote %s\\n" "$MARKER" "${BYEL}tor-github${CNRM}"
  437. # Add remote
  438. add_remote "tor-github" "$GITHUB_PULL"
  439. set_remote_push "tor-github" "$GITHUB_PUSH"
  440. # Add custom fetch for PRs
  441. set_tor_github_pr_fetch_config
  442. # Now fetch them all
  443. fetch_remote "tor-github"
  444. # Extra remote
  445. if [ "$TOR_EXTRA_REMOTE_NAME" ]; then
  446. printf "%s Setting up remote %s\\n" "$MARKER" \
  447. "${BYEL}$TOR_EXTRA_REMOTE_NAME${CNRM}"
  448. # Add remote
  449. add_remote "$TOR_EXTRA_REMOTE_NAME" "$TOR_EXTRA_REMOTE_PULL"
  450. set_remote_push "$TOR_EXTRA_REMOTE_NAME" "$TOR_EXTRA_REMOTE_PUSH"
  451. # But leave it to the user to decide if they want to fetch it
  452. #fetch_remote "$TOR_EXTRA_REMOTE_NAME"
  453. fi
  454. # Go over all configured worktree.
  455. for ((i=0; i<COUNT; i++)); do
  456. branch=${!WORKTREE[$i]:0:1}
  457. repo_path=${!WORKTREE[$i]:1:1}
  458. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$branch${CNRM}"
  459. # We cloned the repository, and main is the default branch
  460. if [ "$branch" = "main" ]; then
  461. if [ "$TOR_MASTER_NAME" != "main" ]; then
  462. # Set up a main branch link in the worktree directory
  463. make_symlink "$repo_path" "$GIT_PATH/$TOR_WKT_NAME/main"
  464. fi
  465. else
  466. # git makes worktree directories if they don't exist
  467. add_worktree "origin/$branch" "$repo_path"
  468. fi
  469. goto_dir "$repo_path"
  470. switch_or_new_branch "$branch"
  471. set_upstream "$branch" "origin/$branch"
  472. done
  473. echo
  474. echo "Remember to copy the git hooks from tor/scripts/git/*.git-hook to"
  475. echo "$ORIGIN_PATH/.git/hooks/*"