@Test public void should_fail_with_repository_not_found_when_trying_to_clone_non_existing_repo() throws Exception { // given gitServer = EmbeddedHttpGitServer.fromBundle("launchpad", "saas-launchpad.bundle") .usingPort(6432) .create(); gitServer.start(); expectedException.expect(TransportException.class); expectedException.expectMessage("Git repository not found"); // when final GitCloner launchpadCloner = new GitCloner("http://localhost:6432/launchpadeeeee"); final Repository launchpad = launchpadCloner.cloneRepositoryToTempFolder(); // then // should fail }
@Test public void shouldFailOnException() throws Exception { // Given SyncableRepository pushRepository = config.getPushRepository(); Git git = mock(Git.class); doNothing().when(git).close(); CloneCommand cloneCommand = mock(CloneCommand.class); when(cloneCommand.setBare(true)).thenReturn(cloneCommand); when(cloneCommand.call()).thenThrow(new TransportException("Expected test exception")); when(pushRepository.clone(any())).thenReturn(cloneCommand); when(config.configure(cloneCommand)).thenReturn(cloneCommand); exception.expect(TransportException.class); exception.expectMessage("Expected test exception"); // When Status status = uut.call(); // Then assertThat(status, is(FAILED)); }
@Test(expected = MissingObjectException.class) public void testRemoteRepositoryHasNoCommitst() throws URISyntaxException, IOException, InvalidRemoteException, TransportException, GitAPIException, NoRepositorySelected { gitAccess.setRepository(LOCAL_TEST_REPOSITPRY); final StoredConfig config = gitAccess.getRepository().getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt")); gitAccess.commit("file test added"); // throws missingObjectException db2.resolve(gitAccess.getLastLocalCommit().getName() + "^{commit}"); }
@Test public void testPushOK() throws URISyntaxException, IOException, InvalidRemoteException, TransportException, GitAPIException, NoRepositorySelected { gitAccess.setRepository(LOCAL_TEST_REPOSITPRY); final StoredConfig config = gitAccess.getRepository().getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt")); gitAccess.commit("file test added"); gitAccess.push("", ""); assertEquals(db1.resolve(gitAccess.getLastLocalCommit().getName() + "^{commit}"), db2.resolve(gitAccess.getLastLocalCommit().getName() + "^{commit}")); }
public boolean cloneRepo( String directory, String uri ) { CloneCommand cmd = Git.cloneRepository(); cmd.setDirectory( new File( directory ) ); cmd.setURI( uri ); cmd.setCredentialsProvider( credentialsProvider ); try { Git git = cmd.call(); git.close(); return true; } catch ( Exception e ) { if ( ( e instanceof TransportException ) && ( ( e.getMessage().contains( "Authentication is required but no CredentialsProvider has been registered" ) || e.getMessage().contains( "not authorized" ) ) ) ) { if ( promptUsernamePassword() ) { return cloneRepo( directory, uri ); } } else { showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() ); } } return false; }
private void cloneRepository() throws IOException, InvalidRemoteException, TransportException, GitAPIException { File localPath = File.createTempFile("TestGitRepository", ""); localPath.delete(); System.out.println("Cloning from " + repository_url + " to " + localPath); Git result = Git.cloneRepository() .setURI(repository_url) .setDirectory(localPath) .call(); // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks! System.out.println("Having repository: " + result.getRepository().getDirectory()); repositoryPath = result.getRepository().getDirectory().getAbsolutePath(); @SuppressWarnings("deprecation") Preferences preferences = new InstanceScope().getNode(ModularityCheck.PLUGIN_ID); preferences.put(REPOSITORY_PATH, repositoryPath); }
public void openRepository() throws IOException, GitAPIException, InvalidRemoteException, TransportException { // Now open the created repository FileRepositoryBuilder builder = new FileRepositoryBuilder(); String directory = System.getProperty("user.home") + Properties.SEPARATOR + "TestGit\\";///.git if(new File(directory).mkdir() || new File(directory).exists()){ directory = directory + "/.git"; repository = builder .setGitDir( new File( directory)).readEnvironment() // scan // environment // GIT_* // variables .findGitDir() // scan up the file system tree .build(); }else System.out.println("Error during repository clonage. Permission denied for creating files at user home"); }
@After public void cleanUp() throws Exception { gApi.accounts().id(admin.getId().toString()).setDiffPreferences(DiffPreferencesInfo.defaults()); TestRepository<InMemoryRepository> allUsersRepo = cloneProject(allUsers); try { fetch(allUsersRepo, RefNames.REFS_USERS_DEFAULT + ":defaults"); } catch (TransportException e) { if (e.getMessage() .equals( "Remote does not have " + RefNames.REFS_USERS_DEFAULT + " available for fetch.")) { return; } throw e; } allUsersRepo.reset("defaults"); PushOneCommit push = pushFactory.create( db, admin.getIdent(), allUsersRepo, "Delete default preferences", VersionedAccountPreferences.PREFERENCES, ""); push.rm(RefNames.REFS_USERS_DEFAULT).assertOkStatus(); }
@Test public void fetchExternalIdsBranch() throws Exception { TestRepository<InMemoryRepository> allUsersRepo = cloneProject(allUsers, user); // refs/meta/external-ids is only visible to users with the 'Access Database' capability try { fetch(allUsersRepo, RefNames.REFS_EXTERNAL_IDS); fail("expected TransportException"); } catch (TransportException e) { assertThat(e.getMessage()) .isEqualTo( "Remote does not have " + RefNames.REFS_EXTERNAL_IDS + " available for fetch."); } allowGlobalCapabilities(REGISTERED_USERS, GlobalCapability.ACCESS_DATABASE); // re-clone to get new request context, otherwise the old global capabilities are still cached // in the IdentifiedUser object allUsersRepo = cloneProject(allUsers, user); fetch(allUsersRepo, RefNames.REFS_EXTERNAL_IDS); }
public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException { // prepare a new folder for the cloned repository File localPath = File.createTempFile("TestGitRepository", ""); localPath.delete(); // then clone System.out.println("Cloning from " + REMOTE_URL + " to " + localPath); Git.cloneRepository() .setURI(REMOTE_URL) .setDirectory(localPath) .call(); // now open the created repository FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(localPath) .readEnvironment() // scan environment GIT_* variables .findGitDir() // scan up the file system tree .build(); System.out.println("Having repository: " + repository.getDirectory()); repository.close(); }
@Test public void shouldDeleteBaseDirWhenCloneFails() throws Exception { Git mockGit = mock(Git.class); CloneCommand mockCloneCommand = mock(CloneCommand.class); when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand); when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand); when(mockCloneCommand.call()).thenThrow(new TransportException("failed to clone")); JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment); envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand)); envRepository.setUri("http://somegitserver/somegitrepo"); envRepository.setBasedir(this.basedir); try { envRepository.findOne("bar", "staging", "master"); } catch (Exception ex) { // expected - ignore } assertFalse("baseDir should be deleted when clone fails", this.basedir.listFiles().length>0); }
public Git call(final GitOperationsStep gitOperationsStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws InvalidRemoteException, TransportException, GitAPIException { CloneCommand cc = Git.cloneRepository().setURI(gitRepoUrl) .setDirectory(gitRepoFolder); if (!Const.isEmpty(this.branchName)) { cc = cc.setBranch(gitOperationsStep .environmentSubstitute(this.branchName)); } cc.setCloneAllBranches(this.cloneAllBranches).setCloneSubmodules( this.cloneSubModules); if (cp != null) { cc.setCredentialsProvider(cp); } return cc.setBare(false).call(); }
public StoreException.TestUpdateException wrapException(final StoreException.TestUpdateException exception) { final Throwable cause = exception.getCause(); if (gitUrl.endsWith(".git")) { if (cause instanceof TransportException) { if (cause.getMessage().contains("not authorized")) { return new GitNoAuthorizationException("Please check your user name and password", exception); } else if (cause.getMessage().contains("git-receive-pack not permitted")) { return new GitNoDevelperAccessLevelException( "Check if your access level is developer in [" + gitUrl.substring(0, gitUrl.length() - 4) + "/project_members]", exception); } } else if (cause instanceof IllegalStateException) { if ("pre-receive hook declined".equals(cause.getMessage())) { return new GitNoMasterAccessLevelException( "Check if your access level is master in [" + gitUrl.substring(0, gitUrl.length() - 4) + "/project_members]", exception); } } } return exception; }
private void execute() throws InvalidRemoteException, TransportException, GitAPIException, IOException { setProxy(); CloneCommand cmd = Git.cloneRepository() .setURI(config.getRemoteUrl()); if (config.getLocalPath() != "") { cmd.setDirectory(new File(config.getLocalPath())); } Git git = cmd.call(); // Set proxy setting to repository config. StoredConfig gitConfig = git.getRepository().getConfig(); gitConfig.setString("remote", "origin", "proxy", config.getProxyAddress()); gitConfig.save(); git.getRepository().close(); }
private static void gitAddCommitAndPush(Git git, String gitUrl, UserDetails userDetails, File basedir, String message, String branch, String origin, Logger logger) throws GitAPIException { PersonIdent personIdent = userDetails.createPersonIdent(); GitUtils.configureBranch(git, branch, origin, gitUrl); GitUtils.addDummyFileToEmptyFolders(basedir); logger.info("About to git commit and push to: " + gitUrl + " and remote name " + origin); int retryCount = 5; for (int i = retryCount; i > 0; i--) { if (i < retryCount) { try { Thread.sleep(500); } catch (InterruptedException e1) { e1.printStackTrace(); } } try { GitUtils.doAddCommitAndPushFiles(git, userDetails, personIdent, branch, origin, message, true); return; } catch (TransportException e) { if (i <= 1) { throw e; } else { logger.info("Caught a transport exception: " + e + " so retrying"); } } } }
/** * Currently svn Update * * * @param scmUserid * @param scmPass * @param scmBranch * @param requireXml * @param svcName * @param request * @param response * @return * @throws IOException */ private boolean reloadUsingSourceControl ( String scmUserid, String encryptedPass, String scmBranch, String svcName, BufferedWriter outputWriter ) throws IOException { try { ServiceInstance serviceInstance = new ServiceInstance(); serviceInstance.setScmLocation( csapApp.getSourceLocation() ); serviceInstance.setScm( csapApp.getSourceType() ); File definitionFolder = new File( csapApp.getRootModelBuildLocation() ); //back_up_to_csap_saved( clusterFileName, outputWriter ); StringBuilder output = new StringBuilder(); csapApp.move_to_csap_saved_folder( definitionFolder, output ); outputWriter.append( output.toString() ); sourceControlManager.checkOutFolder( scmUserid, encryptedPass, scmBranch, definitionFolder.getName(), serviceInstance, outputWriter ); } catch (TransportException gitException) { logger.error( "Definition reload failed: {}", CSAP.getCsapFilteredStackTrace( gitException ) ); outputWriter.write( "\n\n" + CSAP.CONFIG_PARSE_ERROR + "Git Access Error: Verify credentials and path is correct:\n" + gitException.getMessage() ); return false; } catch (Exception e) { logger.error( "Definition reload failed: {}", CSAP.getCsapFilteredStackTrace( e ) ); outputWriter.write( "\n\n" + CSAP.CONFIG_PARSE_ERROR + "SVN Failure: Verify password and target is correct:\n" + e ); if ( e.toString().indexOf( "is already a working copy for a different URL" ) != -1 ) { File svnCheckoutFolder = new File( Application.BUILD_DIR + svcName ); outputWriter.write( "Blowing away previous build folder, try again:" + svnCheckoutFolder ); FileUtils.deleteQuietly( svnCheckoutFolder ); } return false; } return true; }
public void cloneRepo(final String gitUri, final Path repoDir) throws InvalidRemoteException, GitAPIException, IOException { if (Files.exists(repoDir)) { LOGGER.debug("Deleting left-over repo dir" + repoDir.toAbsolutePath().toString()); com.sap.cloud.lm.sl.cf.core.util.FileUtils.deleteDirectory(repoDir); } configureGitSslValidation(); if (shoudlUseToken(gitServiceUrlString, gitUri)) { cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, token)); } if (refName != null && !refName.isEmpty()) { String fullRefName = refName.startsWith("refs/") ? refName : "refs/heads/" + refName; cloneCommand.setBranchesToClone(Arrays.asList(new String[] { fullRefName })); cloneCommand.setBranch(fullRefName); } cloneCommand.setTimeout(290); cloneCommand.setDirectory(repoDir.toAbsolutePath().toFile()); cloneCommand.setURI(gitUri); LOGGER.debug( MessageFormat.format("cloning repo with url {0} in repo dir {1} ref '{2}'", gitUri, repoDir.toAbsolutePath().toString())); try (Git callInstance = cloneCommand.call()) { Repository repo = callInstance.getRepository(); repo.close(); } catch (TransportException e) { Throwable cause1 = e.getCause(); if (cause1 != null && cause1.getCause() instanceof SSLHandshakeException) { throw new SLException(cause1.getCause(), "Failed to establish ssl connection"); // NOSONAR } throw e; } }
@Test public void testNoPushesAhead() throws RepositoryNotFoundException, IOException, URISyntaxException, NoRepositorySelected, InvalidRemoteException, TransportException, GitAPIException{ gitAccess.setRepository(LOCAL_TEST_REPOSITPRY); final StoredConfig config = gitAccess.getRepository().getConfig(); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.update(config); config.save(); int actual = gitAccess.getPushesAhead(); int expected = 0; assertEquals(expected, actual); }
protected void pushOneFileToRemote(String message) throws IOException, RepositoryNotFoundException, FileNotFoundException, InvalidRemoteException, TransportException, GitAPIException, InterruptedException { gitAccess.setRepository(LOCAL_TEST_REPOSITPRY); OptionsManager.getInstance().saveSelectedRepository(LOCAL_TEST_REPOSITPRY); PrintWriter out = new PrintWriter(LOCAL_TEST_REPOSITPRY + "/test.txt"); out.println(message); out.close(); gitAccess.addAll(gitAccess.getUnstagedFiles()); gitAccess.commit(message); gitAccess.push("", ""); }
public void cloneAll(String host) throws InvalidRemoteException, TransportException, GitAPIException{ Git git = Git.cloneRepository().setURI(host).setNoCheckout(true).setCloneAllBranches(true).call(); List<Ref> branches = git.branchList().setListMode( ListMode.REMOTE ).call(); for(Ref r:branches){ System.out.println(r.getName()); } }
private void pushAndLogResult(final PushCommand pushCommand) throws GitAPIException, InvalidRemoteException, TransportException { for (final PushResult result : pushCommand.call()) { for (final RemoteRefUpdate upd : result.getRemoteUpdates()) { log.info(upd.toString()); } } }
/** * Initializes a user's git repository. * * * @param workspacePath * @param userId * @param email * @return * @throws IOException * @throws InvalidRemoteException * @throws TransportException * @throws GitAPIException */ private static Path initializeUserRepository(Path workspacePath, String userId, URI email) throws IOException, InvalidRemoteException, TransportException, GitAPIException { Path gitPath = null; // // Initialize the User's Git repository // if (Files.notExists(workspacePath)) { logger.info("Creating user workspace: " + workspacePath.toAbsolutePath().toString()); // // Create our user's directory // Files.createDirectory(workspacePath); } gitPath = Paths.get(workspacePath.toString(), XacmlAdminUI.repositoryPath.getFileName().toString()); if (Files.notExists(gitPath)) { // // It doesn't exist yet, so Clone it and check it out // logger.info("Cloning user git directory: " + gitPath.toAbsolutePath().toString()); Git.cloneRepository().setURI(XacmlAdminUI.repositoryPath.toUri().toString()) .setDirectory(gitPath.toFile()) .setNoCheckout(false) .call(); // // Set userid // Git git = Git.open(gitPath.toFile()); StoredConfig config = git.getRepository().getConfig(); config.setString("user", null, "name", userId); if (email != null && email.getPath() != null) { config.setString("user", null, "email", email.toString()); } config.save(); } return gitPath; }
private void fetchFromUpstream() throws InvalidRemoteException, TransportException, GitAPIException { final boolean fetchAutomatically = Preferences.getStore().getBoolean(Preferences.PERMO__FETCH_AUTOMATICALLY); if (fetchAutomatically) { final Git git = new Git(repository); git.fetch().setRemote(JGitUtil.getDefaultRemote(repository)).call(); } }
private void cloneRemoteRepository() throws IOException, InvalidRemoteException, TransportException, GitAPIException{ loadPreferences(); //repositoryPath="C:\\Users\\Luciana\\AppData\\Local\\Temp\\TestGitRepository8482929131009567265"; if(repositoryPath.isEmpty()) cloneRepository(); else if(!last_repository_url.equals(repository_url) || new File(repositoryPath).listFiles().length == 0) cloneRepository(); if(!repositoryPath.endsWith("/.git")) repositoryPath = repositoryPath + Properties.SEPARATOR + "/.git"; openRepository(new File(repositoryPath)); }
public void testHttpUsernamePassword(String filename, String remoteURL) throws Exception { Path repoPath = directoryPath.resolve("testrepo"); File authData = new File(testFileLocation + filename); // If a developer does not have this file present, test should just pass. if (!authData.exists() && looseTesting) return; Scanner scanner = new Scanner(authData); String ignoreURL = scanner.next(); String username = scanner.next(); String password = scanner.next(); UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider(username, password); try { ClonedRepoHelper helper = new ClonedRepoHelper(repoPath, remoteURL, credentials); helper.obtainRepository(remoteURL); assertEquals(helper.getCompatibleAuthentication(), AuthMethod.HTTP); helper.fetch(false); Path fileLocation = repoPath.resolve("README.md"); System.out.println(fileLocation); FileWriter fw = new FileWriter(fileLocation.toString(), true); fw.write("1"); fw.close(); helper.addFilePathTest(fileLocation); helper.commit("Appended to file"); PushCommand command = helper.prepareToPushAll(); helper.pushAll(command); } catch (TransportException e) { e.printStackTrace(); fail("Test failed; it is likely that you have not name/password correctly in the file " + "or you do not have access to the Bitbucket repo. Note that httpUsernamePassword.txt " + "should have GitHub authentication info; httpUsernamePasswordPrivate.txt should have" + "Bitbucket authentication info."); } }
public void testHttpBadUsernamePassword(String filename, String remoteURL) throws Exception { Path repoPath = directoryPath.resolve("testrepo"); File authData = new File(testFileLocation + filename); // If a developer does not have this file present, test should just pass. if (!authData.exists() && looseTesting) return; Scanner scanner = new Scanner(authData); String ignoreURL = scanner.next(); String username = scanner.next(); String password = scanner.next(); UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider("", ""); try { ClonedRepoHelper helper = new ClonedRepoHelper(repoPath, remoteURL, credentials); helper.obtainRepository(remoteURL); assertEquals(helper.getCompatibleAuthentication(), AuthMethod.HTTP); helper.fetch(false); Path fileLocation = repoPath.resolve("README.md"); System.out.println(fileLocation); FileWriter fw = new FileWriter(fileLocation.toString(), true); fw.write("1"); fw.close(); helper.addFilePathTest(fileLocation); helper.commit("Appended to file"); credentials = new UsernamePasswordCredentialsProvider(username, password); helper.ownerAuth = credentials; PushCommand command = helper.prepareToPushAll(); helper.pushAll(command); helper.pushTags(); } catch (TransportException e) { e.printStackTrace(); fail("Test failed; it is likely that you have not name/password correctly in the file " + "or you do not have access to the Bitbucket repo. Note that httpUsernamePassword.txt " + "should have GitHub authentication info; httpUsernamePasswordPrivate.txt should have" + "Bitbucket authentication info."); } }
private void fetchGitHubPullRequest(Repository gitRepo, GHPullRequest pr) throws GitAPIException, InvalidRemoteException, TransportException { status.update(Code.SYNC, "Fetching", "Fetching PullRequests from GitHub"); try (Git git = Git.wrap(gitRepo)) { FetchCommand fetch = git.fetch(); fetch.setRemote(ghRepository.getCloneUrl()); fetch.setRefSpecs( new RefSpec( "+refs/pull/" + pr.getNumber() + "/head:refs/remotes/origin/pr/" + pr.getNumber())); fetch.setProgressMonitor(this); fetch.setCredentialsProvider(ghRepository.getCredentialsProvider()); fetch.call(); } }
public void clone(int from, int to) throws InvalidRemoteException, TransportException, IOException, GitAPIException { String urlHeader = "https://github.com/"; String urlFooter = ".git"; String outFilePath = ""; File dir = new File(inPath); File[] files = dir.listFiles(); for (int i = from; i < to; i++) { // System.out.println("Processing file number " + i ); if (!files[i].getName().endsWith(".json")) { continue; } String content = FileIO.readFileContents(files[i]); Gson parser = new Gson(); JsonArray repos = parser.fromJson(content, JsonElement.class).getAsJsonArray(); for (int j = 0; j < repos.size(); j++) { JsonObject repo = repos.get(j).getAsJsonObject(); String name = repo.get("full_name").getAsString(); boolean forked = repo.get("fork").getAsBoolean(); if (forked) continue; outFilePath = outPath + "/" + name; //File file = new File(outFilePath); if (names.contains(name)){ names.remove(name); continue; } String[] args = { urlHeader + name + urlFooter, outFilePath }; RepositoryCloner.clone(args); FileIO.writeFileContents(recovory, name + "/n" , true); } } }
public static void clone(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException { // prepare a new folder for the cloned repository String localpaths = args[1]; String url = args[0]; REMOTE_URL = url; File localPath = new File(localpaths); if (!localPath.exists()) localPath.mkdir(); else return; // then clone Git result = null; try { result = Git.cloneRepository().setURI(REMOTE_URL).setBare(true).setDirectory(localPath).call(); // Note: the call() returns an opened repository already which needs // to be closed to avoid file handle leaks! // workaround for // https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093 result.getRepository().close(); } catch (Exception e) { e.printStackTrace(); } finally { if (result != null && result.getRepository() != null) result.getRepository().close(); } }
public static void main(String args[]) throws InvalidRemoteException, TransportException, GitAPIException, IOException, ClassNotFoundException { new Settings(); //Write to inititate R. As no servlet here DataStatistics ds = new DataStatistics(); ds.init(); DownloadRepo dr = new DownloadRepo(); //dr.cloneRepo("libgdx", "libgdx"); File f = new File("E://githubrepos.txt"); List<String> ReposName = FileUtils.readLines(f); for(String reponame: ReposName) { String[] temp = reponame.split("/"); if(temp.length == 2) { dr.cloneRepo(temp[0], temp[1]); } } /*File f = new File("E://githubrepos.txt"); List<String> CommitsData = FileUtils.readLines(f); FileWriter f1 = new FileWriter("E://githubreposfin.txt"); Set<String> s = new HashSet<String>(); for(String str: CommitsData) { s.add(str.toLowerCase()); } for(String str1: s) { f1.append(str1 +"\n"); } f1.flush(); f1.close();*/ }
public Git call(final GitOperationsStep gitOperationsStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws IllegalArgumentException, IOException, InvalidRemoteException, TransportException, GitAPIException { PushCommand pc = git.push().setDryRun(dryRun).setForce(force) .setThin(thin); if (cp != null) { pc = pc.setCredentialsProvider(cp); } if (!Const.isEmpty(this.receivePack)) { pc = pc.setReceivePack(gitOperationsStep .environmentSubstitute(receivePack)); } if (!Const.isEmpty(this.referenceToPush)) { pc = pc.add(gitOperationsStep .environmentSubstitute(this.referenceToPush)); } if (!Const.isEmpty(this.remote)) { pc = pc.setRemote(gitOperationsStep .environmentSubstitute(this.remote)); } if (this.pushAllBranches) { pc = pc.setPushAll(); } if (this.pushAllTags) { pc = pc.setPushTags(); } pc.call(); return git; }
public Git call(final GitOperationsStep gitOperationsStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws IllegalArgumentException, IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, TransportException, GitAPIException { git.pull().setCredentialsProvider(cp).setRebase(rebase).call(); return git; }
/** * Displaying workflows */ @Test public void directWorkflowURL() throws Exception { Workflow mockWorkflow = Mockito.mock(Workflow.class); QueuedWorkflow mockQueuedWorkflow = Mockito.mock(QueuedWorkflow.class); when(mockQueuedWorkflow.getWorkflowList()).thenReturn(null); // Mock service WorkflowService mockWorkflowService = Mockito.mock(WorkflowService.class); when(mockWorkflowService.getWorkflow(Matchers.<GitDetails>anyObject())) .thenReturn(mockWorkflow) .thenReturn(null); when(mockWorkflowService.createQueuedWorkflow(anyObject())) .thenReturn(mockQueuedWorkflow) .thenThrow(new WorkflowNotFoundException()) .thenThrow(new WrongRepositoryStateException("Some Error")) .thenThrow(new TransportException("No SSH Key")) .thenThrow(new IOException()); List<WorkflowOverview> listOfTwoOverviews = new ArrayList<>(); listOfTwoOverviews.add(new WorkflowOverview("/workflow1.cwl", "label", "doc")); listOfTwoOverviews.add(new WorkflowOverview("/workflow2.cwl", "label2", "doc2")); when(mockWorkflowService.getWorkflowsFromDirectory(anyObject())) .thenReturn(listOfTwoOverviews) .thenReturn(Collections.singletonList(new WorkflowOverview("/workflow1.cwl", "label", "doc"))); // Mock controller/MVC WorkflowController workflowController = new WorkflowController( Mockito.mock(WorkflowFormValidator.class), mockWorkflowService, Mockito.mock(GraphVizService.class)); MockMvc mockMvc = MockMvcBuilders .standaloneSetup(workflowController) .build(); // Workflow already exists in the database mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within/workflow.cwl")) .andExpect(status().isOk()) .andExpect(view().name("workflow")) .andExpect(model().attribute("workflow", is(mockWorkflow))); // Workflow needs to be created, loading page mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within/workflow.cwl")) .andExpect(status().isOk()) .andExpect(view().name("loading")) .andExpect(model().attribute("queued", is(mockQueuedWorkflow))); // Directory URL, select between mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within")) .andExpect(status().isOk()) .andExpect(view().name("selectworkflow")) .andExpect(model().attributeExists("gitDetails")) .andExpect(model().attributeExists("workflowOverviews")); // Directory URL with only one workflow redirects mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within")) .andExpect(status().isFound()) .andExpect(redirectedUrl("/workflows/github.com/owner/reponame/blob/branch/path/within/workflow1.cwl")); // Workflow not found mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within/nonexistant.cwl")) .andExpect(status().isFound()) .andExpect(MockMvcResultMatchers.flash().attributeExists("errors")); // Git API error mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within/cantbecloned.cwl")) .andExpect(status().isFound()) .andExpect(MockMvcResultMatchers.flash().attributeExists("errors")); // Submodules with SSH Url mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within/submodulewithssh.cwl")) .andExpect(status().isFound()) .andExpect(MockMvcResultMatchers.flash().attributeExists("errors")); // Unexpected error mockMvc.perform(get("/workflows/github.com/owner/reponame/tree/branch/path/within/badworkflow.cwl")) .andExpect(status().isFound()) .andExpect(MockMvcResultMatchers.flash().attributeExists("errors")); }
@Test @Sandboxed public void fetchUserBranch() throws Exception { setApiUser(user); TestRepository<InMemoryRepository> allUsersRepo = cloneProject(allUsers, user); String userRefName = RefNames.refsUsers(user.id); // remove default READ permissions ProjectConfig cfg = projectCache.checkedGet(allUsers).getConfig(); cfg.getAccessSection(RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}", true) .remove(new Permission(Permission.READ)); saveProjectConfig(allUsers, cfg); // deny READ permission that is inherited from All-Projects deny(allUsers, RefNames.REFS + "*", Permission.READ, ANONYMOUS_USERS); // fetching user branch without READ permission fails try { fetch(allUsersRepo, userRefName + ":userRef"); fail("user branch is visible although no READ permission is granted"); } catch (TransportException e) { // expected because no READ granted on user branch } // allow each user to read its own user branch grant( allUsers, RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}", Permission.READ, false, REGISTERED_USERS); // fetch user branch using refs/users/YY/XXXXXXX fetch(allUsersRepo, userRefName + ":userRef"); Ref userRef = allUsersRepo.getRepository().exactRef("userRef"); assertThat(userRef).isNotNull(); // fetch user branch using refs/users/self fetch(allUsersRepo, RefNames.REFS_USERS_SELF + ":userSelfRef"); Ref userSelfRef = allUsersRepo.getRepository().getRefDatabase().exactRef("userSelfRef"); assertThat(userSelfRef).isNotNull(); assertThat(userSelfRef.getObjectId()).isEqualTo(userRef.getObjectId()); accountIndexedCounter.assertNoReindex(); // fetching user branch of another user fails String otherUserRefName = RefNames.refsUsers(admin.id); exception.expect(TransportException.class); exception.expectMessage("Remote does not have " + otherUserRefName + " available for fetch."); fetch(allUsersRepo, otherUserRefName + ":otherUserRef"); }
static RepoHelper cloneRepositoryWithChecks(String remoteURL, Path destinationPath, RepoHelperBuilder.AuthDialogResponse response, UserInfo userInfo) throws GitAPIException, IOException, CancelledAuthorizationException, NoRepoSelectedException { // Always use authentication. If authentication is unneeded (HTTP), it will still work even if the wrong // username password is used. This is what Eclipse does; in fact, it asks for username/password in the // same dialog box that the URL is entered. // // Set up both sets of credentials (username/password pair, as well // as just password. Only one of these will be used, but that is determined automatically via the JGit // callback mechanism. UsernamePasswordCredentialsProvider credentials = new UsernamePasswordCredentialsProvider(response.username, response.password); String sshPassword = response.password; // Try calling `git ls-remote ___` on the remote URL to see if it's valid TransportCommand command = Git.lsRemoteRepository().setRemote(remoteURL); ClonedRepoHelper repoHelper = new ClonedRepoHelper(destinationPath, remoteURL, sshPassword, userInfo); repoHelper.wrapAuthentication(command, credentials); try { command.call(); } catch (TransportException e) { // If the URL doesn't have a repo, a Transport Exception is thrown when this command is called. // We want the SessionController to report an InvalidRemoteException, though, because // that's the issue. System.out.println(e.getMessage()); System.out.println(e.getMessage().endsWith("not authorized") || e.getMessage().endsWith("not authorized.")); System.out.println(e.getMessage().endsWith("not found") || e.getMessage().endsWith("not found.")); /*Throwable exception = e; while (exception != null) { System.err.println("======================"); exception.printStackTrace(); exception = exception.getCause(); }*/ if (e.getMessage().endsWith("not found") || e.getMessage().endsWith("not found.")) { logger.error("Invalid remote exception thrown"); throw new InvalidRemoteException("Caught invalid repository when building a ClonedRepoHelper."); } else if (e.getMessage().endsWith("not authorized") || e.getMessage().endsWith("not authorized.")) { logger.error("Authentication error"); } } // Without the above try/catch block, the next line would run and throw the desired InvalidRemoteException, // but it would create a destination folder for the repo before stopping. By catching the error above, // we prevent unnecessary folder creation. By making it to this point, we've verified that the repo // is valid and that we can authenticate to it. if (response.protocol == AuthMethod.SSH) { repoHelper.obtainRepository(remoteURL); } else { repoHelper.setUsernamePasswordCredentials(credentials); repoHelper.obtainRepository(remoteURL); } return repoHelper; }
@Test public void pullTest() throws InvalidRemoteException, TransportException, GitAPIException, IOException { GitPull.create().quickPull(); }
public void cloneRepo(String username, String reponame) throws InvalidRemoteException, TransportException, GitAPIException, IOException, ClassNotFoundException{ CloneCommand clonecommand = Git.cloneRepository(); RepoSettings rs = new RepoSettings(username,reponame,true); File f = new File(rs.RepositoryPath); clonecommand.setDirectory(f); clonecommand.setBare(true); String URI = "https://github.com/" + username + "/" + reponame + ".git"; clonecommand.setURI(URI); File progress = new File(rs.Progress); FileWriter pro = new FileWriter(progress); pro.append("Cloning\n"); pro.flush(); pro.close(); //long startTime = System.nanoTime(); //Cloning the Bare Repo git = clonecommand.call(); //long endTime = System.nanoTime(); //System.out.println("Took "+(endTime - startTime) + " ns"); Iterable<RevCommit> logsreverse = git.log().all().call(); //Latest top oldest last List<RevCommit> logs = new ArrayList<RevCommit>(); for(RevCommit rev: logsreverse){ logs.add(rev); } Collections.reverse(logs); //Latest last, oldest top if(logs.size() == 0) { return; } pro = new FileWriter(progress); pro.append("Building " + logs.size() + "\n"); pro.flush(); pro.close(); Buildmodel bm = new Buildmodel(); int numofcommits = bm.build(git,logs, rs); //How many commits written, i.e not merge commits in this if(numofcommits > 200) { numofcommits = 200; } pro = new FileWriter(progress); pro.append("Detecting" + "\n"); pro.flush(); pro.close(); Detect detect = new Detect(); detect.detect(rs, numofcommits); /*Htmldata htmd = new Htmldata(); htmd.initiate(username,reponame); htmd.createhtml(new File(rs.Resultpath+"//result.tsv"));*/ pro = new FileWriter(progress); pro.append("Completed" + "\n"); pro.flush(); pro.close(); }