Skip to content

Initial fix for gh-repo-clone wiki#2493

Merged
mislav merged 2 commits intocli:trunkfrom
gunadhya:repo-clone-wiki
Dec 15, 2020
Merged

Initial fix for gh-repo-clone wiki#2493
mislav merged 2 commits intocli:trunkfrom
gunadhya:repo-clone-wiki

Conversation

@gunadhya
Copy link
Contributor

Fix for #2477

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thanks! This is a good start, but a lot about the current code is broken. Please address my line comments first.

}

// RepoHasWiki checks if a wiki exists in a repo
func (r Repository) RepoHasWiki() bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function would be better named as just HasWiki, since the "Repo" prefix is redundant on a Repository struct. (Other functions named RepoOwner/Name/Host have the "Repo" prefix only because they are part of an interface that is shared between possibly non-repo objects.)

However, I don't think this accessor provides any value, so you can just remove the function and have the caller inspect the HasWikiEnabled field directly.


var repo ghrepo.Interface
var protocol string
wikipref := false
Copy link
Contributor

Choose a reason for hiding this comment

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

This variable would be better named as wantsWiki or something like that. We prefer full names for variables

wikipref := false

// Check if repo opts has wiki in it and convert it into normal repo opts and get repo data
if strings.Contains(opts.Repository, ".wiki") {
Copy link
Contributor

Choose a reason for hiding this comment

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

This check works, but will detect .wiki appearing anywhere in the repository argument, not just the end, which you would want to avoid, as we only ever want to match the .wiki suffix.

Since opts.Repository at this point can be a URL too, it's best to not try to match its name until after it's parsed. So, I would suggest moving this block to after the if repositoryIsURL ... else blocks, and having it be something like:

wantsWiki := false
if strings.HasSuffix(repo.RepoName(), ".wiki") {
  repo = ghrepo.NewWithHost(repo.RepoOwner(), strings.TrimSuffix(repo.RepoName(), ".wiki"), repo.RepoHost())
  wantsWiki = true
}


// If RepoHasWiki is true and repo opts has wiki then create a new clone URL
if canonicalRepo.RepoHasWiki() && wikipref {
canonicalCloneURL = strings.Replace(canonicalCloneURL, ".git", ".wiki.git", 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

You do not want to accidentally replace a .git string that appears in the middle of the URL. It can be only at the end. For that reason, Replace is not suitable here. Instead:

canonicalCloneURL = strings.TrimSuffix(canonicalCloneURL, ".git") + ".wiki.git"

canonicalCloneURL := ghrepo.FormatRemoteURL(canonicalRepo, protocol)

// If RepoHasWiki is true and repo opts has wiki then create a new clone URL
if canonicalRepo.RepoHasWiki() && wikipref {
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition is wrong. If wikipref is true, the user has indicated that they want to clone a wiki, so we must always attempt to clone the wiki. However, if the current repository does not have a wiki, we need to throw an error. Something like:

if wikipref {
  if !canonicalRepo.HasWikiEnabled {
    return fmt.Errorf("the '%s' repository does not have a wiki", ghrepo.FullName(canonicalRepo))
  }
  // ...
}

{
name: "Wiki with HTTPS URL",
args: "https://github.com/OWNER/REPO.wiki.git",
want: "git clone https://github.com/OWNER/REPO.git",
Copy link
Contributor

Choose a reason for hiding this comment

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

This test expectation is wrong. The argument indicated that we want to clone the wiki. The URL that is used for clone does not have a .wiki suffix in it.

@gunadhya
Copy link
Contributor Author

gunadhya commented Dec 2, 2020

I've made the changes as requested. Please do let me know if anything else seems broken or need fixing.

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Thanks for the quick updates! Looks great so far.

As a final touch, maybe the added functionality deserves its own test cases in Test_RepoClone?

// If repo HasWikiEnabled and wantsWiki is true then create a new clone URL
if wantsWiki {
if !canonicalRepo.HasWikiEnabled {
return fmt.Errorf("The '%s' repository does not have a wiki or the wiki is not exported by the owner", ghrepo.FullName(canonicalRepo))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what the “or the wiki is not exported by the owner” part refers to. Is it necessary to say that? I always thought that a repo can either have a wiki turned off or on; nothing in-between.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

imageI got it from running this command on git. I'll update the error message to be The '%s' repository does not have a wiki.
And I'll add the Test case for Test_RepoClone.

@gunadhya gunadhya requested a review from mislav December 9, 2020 19:05
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Fantastic. Thank you very much!

@gunadhya
Copy link
Contributor Author

@mislav Thank you for guiding me through this issue.

@mislav mislav merged commit 65e5ba9 into cli:trunk Dec 15, 2020
@colematt
Copy link

colematt commented Jan 6, 2021

Thanks to @gunadhya for getting my request fixed. Great job, can confirm it works as I had hoped. 🙌🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants