-
Notifications
You must be signed in to change notification settings - Fork 5
Add git-clone-or-pull script
#25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
98dbe44
Add `git-clone-or-pull` script
mernst 8af23d4
Add debugging diagnostics
mernst 62b1080
Don't show path to script
mernst 3bd3ea6
Merge ../git-scripts into git-clone-or-pull
mernst a308bf6
Use variable for script name
mernst c22661c
Merge ../git-scripts into script-name
mernst 9df9dfd
Merge ../git-scripts into script-name
mernst 784a10e
Merge ../git-scripts-branch-script-name into git-clone-or-pull
mernst 2f449e1
Use in more places
mernst 16d8d58
Merge ../git-scripts-branch-script-name into git-clone-or-pull
mernst 02dfb69
Merge ../git-scripts into git-clone-or-pull
mernst cb83d04
Merge ../git-scripts into git-clone-or-pull
mernst 7083f15
Merge ../git-scripts into git-clone-or-pull
mernst 04d70c8
Fixes from CodeRabbit
mernst d2b37f3
Better test and cleanup
mernst 55e5b85
Fixes
mernst 1a9161c
Merge ../git-scripts into git-clone-or-pull
mernst 2189306
Merge ../git-scripts into git-clone-or-pull
mernst 3c5cad5
Fixes from CodeRabbit
mernst e0eb2ef
More consistent output
mernst 3c8efb8
Use `beginswith` for clarity
mernst 0c3cd37
Improve usage message
mernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/bin/sh | ||
|
|
||
| # git-clone-or-pull: clone a GitHub repository, or pull it if already cloned. | ||
| # | ||
| # Usage: git-clone-or-pull ORG REPO_NAME [DESTINATION_PARENT] [GIT_CLONE_ARGS...] | ||
| # ORG is the GitHub organization | ||
| # REPO_NAME is the repository name (without the organization) | ||
| # DESTINATION_PARENT is the directory that contains the clone directory. It | ||
| # defaults to the current directory. | ||
| # GIT_CLONE_ARGS is extra arguments to git clone. It defaults to | ||
| # "-q --single-branch --depth 1" (without the quotes). | ||
| # It is not used if the destination already exists. | ||
|
|
||
| SCRIPT_NAME="$(basename "$0")" | ||
|
|
||
| if [ "$#" -lt 2 ]; then | ||
| echo "Usage: ${SCRIPT_NAME} ORG REPO_NAME [DESTINATION_PARENT] [GIT_CLONE_ARGS...]" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| beginswith() { case $2 in "$1"*) true ;; *) false ;; esac } | ||
|
|
||
| ORG=$1 | ||
| shift | ||
| REPO_NAME=$1 | ||
| shift | ||
| DESTINATION_PARENT=. | ||
| if [ "$#" -ne 0 ]; then | ||
| if ! beginswith "-" "$1"; then | ||
| # Does not start with "-" and therefore isn't a command-line argument. | ||
| DESTINATION_PARENT=$1 | ||
| shift | ||
| if [ ! -d "${DESTINATION_PARENT}" ]; then | ||
| mkdir -p "${DESTINATION_PARENT}" | ||
| fi | ||
| fi | ||
| fi | ||
| if [ "$#" -eq 0 ]; then | ||
| # Default command-line arguments. | ||
| set -- -q --single-branch --depth 1 | ||
| fi | ||
|
|
||
| REPO_URL="https://github.com/${ORG}/${REPO_NAME}.git" | ||
| DESTINATION="${DESTINATION_PARENT:?}/${REPO_NAME}" | ||
|
|
||
| # Try twice in case of network lossage. 60 seconds is not enough to clone a branch of the JDK. | ||
| if test -d "${DESTINATION}"; then | ||
| (cd "${DESTINATION}" && (timeout 180 git pull -q || (sleep 1m && (timeout 180 git pull || true)))) | ||
mernst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else | ||
| if ! timeout 180 git clone "$@" "${REPO_URL}" "${DESTINATION}"; then | ||
| rm -rf "${DESTINATION}" | ||
| sleep 180 | ||
| if ! timeout 180 git clone "$@" "${REPO_URL}" "${DESTINATION}"; then | ||
| echo "Failed to clone ${REPO_URL}" | ||
| exit 1 | ||
| fi | ||
| fi | ||
| fi | ||
|
|
||
| echo "${SCRIPT_NAME}: ${REPO_NAME} is at $(cd "${DESTINATION}" && git rev-parse HEAD)" | ||
mernst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.