git-setup-dirs.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 tor master 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 tor master 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://git.torproject.org/tor.git"}
  66. GIT_ORIGIN_PUSH=${TOR_GIT_ORIGIN_PUSH:-"git@git-rw.torproject.org:tor.git"}
  67. # The upstream remote which git.torproject.org/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 master 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. # Add a new worktree for branch at path.
  314. # If the directory already exists: fail if $USE_EXISTING is 0, otherwise skip.
  315. function add_worktree
  316. {
  317. local cmd="git worktree add '$2' '$1'"
  318. printf " %s Adding worktree for %s at %s..." "$MARKER" "$1" "$2"
  319. local check_cmd="[ ! -d '$2' ]"
  320. msg=$( eval "$check_cmd" 2>&1 )
  321. if validate_ret_skip $? "Directory already exists."; then
  322. return
  323. fi
  324. if [ $DRY_RUN -eq 0 ]; then
  325. msg=$( eval "$cmd" 2>&1 )
  326. validate_ret $? "$msg"
  327. else
  328. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  329. fi
  330. }
  331. # Switch to the given branch name.
  332. # If the branch does not exist: fail.
  333. function switch_branch
  334. {
  335. local cmd="git checkout '$1'"
  336. printf " %s Switching branch to %s..." "$MARKER" "$1"
  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. # Checkout a new branch with the given branch name.
  345. # If the branch already exists: fail if $USE_EXISTING is 0, otherwise skip.
  346. function new_branch
  347. {
  348. local cmd="git checkout -b '$1'"
  349. printf " %s Creating new branch %s..." "$MARKER" "$1"
  350. local check_cmd="git branch --list '$1'"
  351. msg=$( eval "$check_cmd" 2>&1 )
  352. if validate_ret_skip $? "Branch already exists."; then
  353. return
  354. fi
  355. if [ $DRY_RUN -eq 0 ]; then
  356. msg=$( eval "$cmd" 2>&1 )
  357. validate_ret $? "$msg"
  358. else
  359. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  360. fi
  361. }
  362. # Switch to an existing branch, or checkout a new branch with the given
  363. # branch name.
  364. function switch_or_new_branch
  365. {
  366. local cmd="git rev-parse --verify '$1'"
  367. if [ $DRY_RUN -eq 0 ]; then
  368. # Call switch_branch if there is a branch, or new_branch if there is not
  369. msg=$( eval "$cmd" 2>&1 )
  370. RET=$?
  371. if [ $RET -eq 0 ]; then
  372. # Branch: (commit id)
  373. switch_branch "$1"
  374. elif [ $RET -eq 128 ]; then
  375. # Not a branch: "fatal: Needed a single revision"
  376. new_branch "$1"
  377. else
  378. # Unexpected return value
  379. validate_ret $RET "$msg"
  380. fi
  381. else
  382. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}, then depending on the result:"
  383. switch_branch "$1"
  384. new_branch "$1"
  385. fi
  386. }
  387. # Set the upstream for branch to upstream.
  388. function set_upstream
  389. {
  390. # Note the argument order is swapped
  391. local cmd="git branch --set-upstream-to='$2' '$1'"
  392. printf " %s Setting upstream for %s to %s..." "$MARKER" "$1" "$2"
  393. if [ $DRY_RUN -eq 0 ]; then
  394. msg=$( eval "$cmd" 2>&1 )
  395. validate_ret $? "$msg"
  396. else
  397. printf "\\n %s\\n" "${IWTH}$cmd${CNRM}"
  398. fi
  399. }
  400. ###############
  401. # Entry point #
  402. ###############
  403. printf "%s Setting up the repository and remote %s\\n" "$MARKER" \
  404. "${BYEL}origin${CNRM}"
  405. # First, fetch the origin.
  406. ORIGIN_PARENT=$(dirname "$ORIGIN_PATH")
  407. make_directory "$ORIGIN_PARENT"
  408. # This is just cd with an error check
  409. goto_dir "$ORIGIN_PARENT"
  410. # clone repository / origin remote
  411. clone_repo "$GIT_ORIGIN_PULL" "$TOR_MASTER_NAME"
  412. goto_dir "$ORIGIN_PATH"
  413. set_remote_push "origin" "$GIT_ORIGIN_PUSH"
  414. # upstream remote, if different to origin
  415. if [ "$DEFAULT_UPSTREAM_REMOTE" != "origin" ]; then
  416. printf "%s Setting up remote %s\\n" "$MARKER" \
  417. "${BYEL}$DEFAULT_UPSTREAM_REMOTE${CNRM}"
  418. add_remote "$DEFAULT_UPSTREAM_REMOTE" "$GIT_UPSTREAM_PULL"
  419. set_remote_push "$DEFAULT_UPSTREAM_REMOTE" "$GIT_UPSTREAM_PUSH"
  420. fetch_remote "$DEFAULT_UPSTREAM_REMOTE"
  421. fi
  422. # GitHub remote
  423. printf "%s Setting up remote %s\\n" "$MARKER" "${BYEL}tor-github${CNRM}"
  424. # Add remote
  425. add_remote "tor-github" "$GITHUB_PULL"
  426. set_remote_push "tor-github" "$GITHUB_PUSH"
  427. # Add custom fetch for PRs
  428. set_tor_github_pr_fetch_config
  429. # Now fetch them all
  430. fetch_remote "tor-github"
  431. # Extra remote
  432. if [ "$TOR_EXTRA_REMOTE_NAME" ]; then
  433. printf "%s Setting up remote %s\\n" "$MARKER" \
  434. "${BYEL}$TOR_EXTRA_REMOTE_NAME${CNRM}"
  435. # Add remote
  436. add_remote "$TOR_EXTRA_REMOTE_NAME" "$TOR_EXTRA_REMOTE_PULL"
  437. set_remote_push "$TOR_EXTRA_REMOTE_NAME" "$TOR_EXTRA_REMOTE_PUSH"
  438. # But leave it to the user to decide if they want to fetch it
  439. #fetch_remote "$TOR_EXTRA_REMOTE_NAME"
  440. fi
  441. # Go over all configured worktree.
  442. for ((i=0; i<COUNT; i++)); do
  443. branch=${!WORKTREE[$i]:0:1}
  444. repo_path=${!WORKTREE[$i]:1:1}
  445. printf "%s Handling branch %s\\n" "$MARKER" "${BYEL}$branch${CNRM}"
  446. # We cloned the repository, and master is the default branch
  447. if [ "$branch" = "master" ]; then
  448. if [ "$TOR_MASTER_NAME" != "master" ]; then
  449. # Set up a master link in the worktree directory
  450. make_symlink "$repo_path" "$GIT_PATH/$TOR_WKT_NAME/master"
  451. fi
  452. else
  453. # git makes worktree directories if they don't exist
  454. add_worktree "origin/$branch" "$repo_path"
  455. fi
  456. goto_dir "$repo_path"
  457. switch_or_new_branch "$branch"
  458. set_upstream "$branch" "origin/$branch"
  459. done
  460. echo
  461. echo "Remember to copy the git hooks from tor/scripts/git/*.git-hook to"
  462. echo "$ORIGIN_PATH/.git/hooks/*"