Skip to content

Handling git clonefork errors more gracefully #24

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions git-clonefork
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,52 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if [[ -z $1 ]]; then
echo "Please provide a fork url"
echo "usage: git clonefork <fork_url>"
exit 2
fi

GIT_ORIGIN=$1
# get the name of the directory that will be created when we clone
REPO_DIRECTORY=$(basename $GIT_ORIGIN .git)

# check if the directory already exists

if [ -d "$(pwd)/$REPO_DIRECTORY" ]; then
echo "A directory with name $REPO_DIRECTORY already exists in your current working directory."
echo "Please move, rename, or delete this directory before using git clonefork in your current working directory"
echo "usage: git clonefork <fork_url>"
exit 1
fi

# reformat the repo url into a github api request
GITHUB_API_URL=$(echo $GIT_ORIGIN | sed -r 's/.*(\@|\/\/)(.*)(\:|\/)([^:\/]*)\/([^\/\.]*)\.git/https:\/\/api.github.com\/repos\/\4\/\5/')

# make the api request and validate we get a good response
# -f makes curl fail silently, so REPO_DATA will be null on failure
REPO_DATA=$(curl -fs $GITHUB_API_URL)
if [[ -z $REPO_DATA ]]; then
echo "Please provide a valid fork url"
echo "usage: git clonefork <fork_url>"
exit 2
fi

if grep -Eiq "^https?://" <<<$GIT_ORIGIN; then
UPSTREAM_ORIGIN=$(curl -s $GITHUB_API_URL | jq -r .parent.clone_url)
# we aren't cloning with ssh, use the default parent clone_url
UPSTREAM_ORIGIN=$(echo $REPO_DATA | jq -r .parent.clone_url)
else
# we want to clone with ssh
UPSTREAM_ORIGIN=$(echo $REPO_DATA | jq -r .parent.ssh_url)
fi

if ! git clone $GIT_ORIGIN 2>/dev/null
then
echo "Failed to clone the repository. Please use a valid url pointing to your fork"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the repo exists in the current directory, the error message should be different.
CleanShot 2023-07-23 at 01 36 50@2x

Currently.
CleanShot 2023-07-23 at 01 33 18@2x

Copy link
Owner Author

@Cali0707 Cali0707 Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Leo6Leo just clarifying, in this scenario the repo exists in the current directory before running git clonefork <url>, and then you run git clonefork <url>?

echo "Usage: git clonefork <fork_url>"
exit 1
else
UPSTREAM_ORIGIN=$(curl -s $GITHUB_API_URL | jq -r .parent.ssh_url)
cd $REPO_DIRECTORY
git remote add upstream $UPSTREAM_ORIGIN
git remote set-url --push upstream no_push
fi
git clone $GIT_ORIGIN
cd $REPO_DIRECTORY
git remote add upstream $UPSTREAM_ORIGIN
git remote set-url --push upstream no_push