@NotNull private static Collection<String> getRemoteNames(@NotNull GitRepository repository) { Collection<String> names = new ArrayList<String>(repository.getRemotes().size()); for (GitRemote remote : repository.getRemotes()) { names.add(remote.getName()); } return names; }
@NotNull private GitFetchResult fetchAll( @NotNull GitRepository repository, @NotNull GitFetchResult fetchResult) { for (GitRemote remote : repository.getRemotes()) { String url = remote.getFirstUrl(); if (url == null) { LOG.error("URL is null for remote " + remote.getName()); continue; } if (GitHttpAdapter.shouldUseJGit(url)) { GitFetchResult res = GitHttpAdapter.fetch(repository, remote, url, null); res.addPruneInfo(fetchResult.getPrunedRefs()); fetchResult = res; myErrors.addAll(fetchResult.getErrors()); if (!fetchResult.isSuccess()) { break; } } else { GitFetchResult res = fetchNatively(repository.getRoot(), remote, null); res.addPruneInfo(fetchResult.getPrunedRefs()); fetchResult = res; if (!fetchResult.isSuccess()) { break; } } } return fetchResult; }
@Nullable private static GitRemote getRemoteByName( @NotNull GitRepository repository, @NotNull String remoteName) { for (GitRemote remote : repository.getRemotes()) { if (remote.getName().equals(remoteName)) { return remote; } } return null; }
@Nullable static String findGithubRemoteUrl(@NotNull GitRepository repository) { for (GitRemote remote : repository.getRemotes()) { for (String url : remote.getUrls()) { if (isGithubUrl(url)) { return url; } } } return null; }
@Nullable public static GitRemote findGitHubRemoteBranch(@NotNull GitRepository repository) { // i.e. find origin which points on my github repo // Check that given repository is properly configured git repository for (GitRemote gitRemote : repository.getRemotes()) { if (getGithubUrl(gitRemote) != null) { return gitRemote; } } return null; }
private void doLoadForksFromGit(@NotNull ProgressIndicator indicator) { for (GitRemote remote : myGitRepository.getRemotes()) { for (String url : remote.getUrls()) { if (GithubUrlUtil.isGithubUrl(url)) { GithubFullPath path = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(url); if (path != null) { doAddFork(path, remote.getName(), indicator); break; } } } } }
@Nullable public static GitRemote findRemoteByName( @NotNull GitRepository repository, @NotNull final String name) { return findRemoteByName(repository.getRemotes(), name); }
public static void shareProjectOnGithub( @NotNull final Project project, @Nullable final VirtualFile file) { BasicAction.saveAll(); // get gitRepository final GitRepository gitRepository = GithubUtil.getGitRepository(project, file); final boolean gitDetected = gitRepository != null; final VirtualFile root = gitDetected ? gitRepository.getRoot() : project.getBaseDir(); // check for existing git repo boolean externalRemoteDetected = false; if (gitDetected) { final String githubRemote = GithubUtil.findGithubRemoteUrl(gitRepository); if (githubRemote != null) { GithubNotifications.showInfoURL( project, "Project is already on GitHub", "GitHub", githubRemote); return; } externalRemoteDetected = !gitRepository.getRemotes().isEmpty(); } final GithubAuthDataHolder authHolder = GithubAuthDataHolder.createFromSettings(); // get available GitHub repos with modal progress final GithubInfo githubInfo = loadGithubInfoWithModal(authHolder, project); if (githubInfo == null) { return; } // Show dialog (window) final GithubShareDialog shareDialog = new GithubShareDialog( project, githubInfo.getRepositoryNames(), githubInfo.getUser().canCreatePrivateRepo()); DialogManager.show(shareDialog); if (!shareDialog.isOK()) { return; } final boolean isPrivate = shareDialog.isPrivate(); final String name = shareDialog.getRepositoryName(); final String description = shareDialog.getDescription(); // finish the job in background final boolean finalExternalRemoteDetected = externalRemoteDetected; new Task.Backgroundable(project, "Sharing project on GitHub...") { @Override public void run(@NotNull ProgressIndicator indicator) { // create GitHub repo (network) LOG.info("Creating GitHub repository"); indicator.setText("Creating GitHub repository..."); final String url = createGithubRepository(project, authHolder, indicator, name, description, isPrivate); if (url == null) { return; } LOG.info("Successfully created GitHub repository"); // creating empty git repo if git is not initialized LOG.info("Binding local project with GitHub"); if (!gitDetected) { LOG.info("No git detected, creating empty git repo"); indicator.setText("Creating empty git repo..."); if (!createEmptyGitRepository(project, root, indicator)) { return; } } GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project); final GitRepository repository = repositoryManager.getRepositoryForRoot(root); LOG.assertTrue(repository != null, "GitRepository is null for root " + root); if (repository == null) { GithubNotifications.showError( project, "Failed to create GitHub Repository", "Can't find Git repository"); return; } final String remoteUrl = GithubUrlUtil.getCloneUrl(githubInfo.getUser().getLogin(), name); final String remoteName = finalExternalRemoteDetected ? "github" : "origin"; // git remote add origin [email protected]:login/name.git LOG.info("Adding GitHub as a remote host"); indicator.setText("Adding GitHub as a remote host..."); if (!GithubUtil.addGithubRemote(project, repository, remoteName, remoteUrl)) { return; } // create sample commit for binding project if (!performFirstCommitIfRequired(project, root, repository, indicator, name, url)) { return; } // git push origin master LOG.info("Pushing to github master"); indicator.setText("Pushing to github master..."); if (!pushCurrentBranch(project, repository, remoteName, remoteUrl, name, url)) { return; } GithubNotifications.showInfoURL( project, "Successfully shared project on GitHub", name, url); } }.queue(); }