List of usage examples for org.eclipse.jgit.lib Repository getConfig
@NonNull public abstract StoredConfig getConfig();
From source file:org.omegat.convert.ConvertProject26to37team.java
License:Open Source License
/** * Get repository URL for SVN.// ww w . ja v a2s. c o m */ private static String getGITUrl(File wc) throws Exception { Repository repository = Git.open(wc).getRepository(); StoredConfig config = repository.getConfig(); return config.getString("remote", "origin", "url"); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void shouldPullForcepullNotClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(false); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); repo.setForcePull(true);//w w w . j av a 2s . co m boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was false", shouldPull, is(true)); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void shouldPullNotClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(false); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was true", shouldPull, is(false)); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void shouldPullClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was false", shouldPull, is(true)); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void testFetchException() throws Exception { Git git = mock(Git.class); CloneCommand cloneCommand = mock(CloneCommand.class); MockGitFactory factory = new MockGitFactory(git, cloneCommand); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); this.repository.setGitFactory(factory); //refresh()->shouldPull StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); when(git.status()).thenReturn(statusCommand); Repository repository = mock(Repository.class); when(git.getRepository()).thenReturn(repository); StoredConfig storedConfig = mock(StoredConfig.class); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); //refresh()->fetch FetchCommand fetchCommand = mock(FetchCommand.class); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenThrow(new InvalidRemoteException("invalid mock remote")); //here is our exception we are testing //refresh()->checkout CheckoutCommand checkoutCommand = mock(CheckoutCommand.class); //refresh()->checkout->containsBranch ListBranchCommand listBranchCommand = mock(ListBranchCommand.class); when(git.checkout()).thenReturn(checkoutCommand); when(git.branchList()).thenReturn(listBranchCommand); List<Ref> refs = new ArrayList<>(); Ref ref = mock(Ref.class); refs.add(ref);/*from w w w .j a v a 2 s. co m*/ when(ref.getName()).thenReturn("/master"); when(listBranchCommand.call()).thenReturn(refs); //refresh()->merge MergeCommand mergeCommand = mock(MergeCommand.class); when(git.merge()).thenReturn(mergeCommand); when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName(); Ref headRef = mock(Ref.class); when(repository.getRef(anyString())).thenReturn(headRef); ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 }); when(headRef.getObjectId()).thenReturn(newObjectId); SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", null); assertEquals(locations.getVersion(), newObjectId.getName()); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void testMergeException() throws Exception { Git git = mock(Git.class); CloneCommand cloneCommand = mock(CloneCommand.class); MockGitFactory factory = new MockGitFactory(git, cloneCommand); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); this.repository.setGitFactory(factory); //refresh()->shouldPull StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); when(git.status()).thenReturn(statusCommand); Repository repository = mock(Repository.class); when(git.getRepository()).thenReturn(repository); StoredConfig storedConfig = mock(StoredConfig.class); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); //refresh()->fetch FetchCommand fetchCommand = mock(FetchCommand.class); FetchResult fetchResult = mock(FetchResult.class); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenReturn(fetchResult); when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST); //refresh()->checkout CheckoutCommand checkoutCommand = mock(CheckoutCommand.class); //refresh()->checkout->containsBranch ListBranchCommand listBranchCommand = mock(ListBranchCommand.class); when(git.checkout()).thenReturn(checkoutCommand); when(git.branchList()).thenReturn(listBranchCommand); List<Ref> refs = new ArrayList<>(); Ref ref = mock(Ref.class); refs.add(ref);/*w w w .j a v a 2s . c om*/ when(ref.getName()).thenReturn("/master"); when(listBranchCommand.call()).thenReturn(refs); //refresh()->merge MergeCommand mergeCommand = mock(MergeCommand.class); when(git.merge()).thenReturn(mergeCommand); when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName(); Ref headRef = mock(Ref.class); when(repository.getRef(anyString())).thenReturn(headRef); ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 }); when(headRef.getObjectId()).thenReturn(newObjectId); SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master"); assertEquals(locations.getVersion(), newObjectId.getName()); }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepositoryTests.java
License:Apache License
@Test public void testResetHardException() throws Exception { Git git = mock(Git.class); CloneCommand cloneCommand = mock(CloneCommand.class); MockGitFactory factory = new MockGitFactory(git, cloneCommand); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); this.repository.setGitFactory(factory); //refresh()->shouldPull StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); when(git.status()).thenReturn(statusCommand); Repository repository = mock(Repository.class); when(git.getRepository()).thenReturn(repository); StoredConfig storedConfig = mock(StoredConfig.class); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true).thenReturn(false); //refresh()->fetch FetchCommand fetchCommand = mock(FetchCommand.class); FetchResult fetchResult = mock(FetchResult.class); when(git.fetch()).thenReturn(fetchCommand); when(fetchCommand.setRemote(anyString())).thenReturn(fetchCommand); when(fetchCommand.call()).thenReturn(fetchResult); when(fetchResult.getTrackingRefUpdates()).thenReturn(Collections.EMPTY_LIST); //refresh()->checkout CheckoutCommand checkoutCommand = mock(CheckoutCommand.class); //refresh()->checkout->containsBranch ListBranchCommand listBranchCommand = mock(ListBranchCommand.class); when(git.checkout()).thenReturn(checkoutCommand); when(git.branchList()).thenReturn(listBranchCommand); List<Ref> refs = new ArrayList<>(); Ref ref = mock(Ref.class); refs.add(ref);//from w w w . j a v a 2 s . c om when(ref.getName()).thenReturn("/master"); when(listBranchCommand.call()).thenReturn(refs); //refresh()->merge MergeCommand mergeCommand = mock(MergeCommand.class); when(git.merge()).thenReturn(mergeCommand); when(mergeCommand.call()).thenThrow(new NotMergedException()); //here is our exception we are testing //refresh()->hardReset ResetCommand resetCommand = mock(ResetCommand.class); when(git.reset()).thenReturn(resetCommand); when(resetCommand.call()).thenReturn(ref); //refresh()->return git.getRepository().getRef("HEAD").getObjectId().getName(); Ref headRef = mock(Ref.class); when(repository.getRef(anyString())).thenReturn(headRef); ObjectId newObjectId = ObjectId.fromRaw(new int[] { 1, 2, 3, 4, 5 }); when(headRef.getObjectId()).thenReturn(newObjectId); SearchPathLocator.Locations locations = this.repository.getLocations("bar", "staging", "master"); assertEquals(locations.getVersion(), newObjectId.getName()); }
From source file:org.wandora.application.tools.git.Status.java
License:Open Source License
@Override public void execute(Wandora wandora, Context context) { try {/* www . j a v a 2s . c o m*/ Git git = getGit(); if (git != null) { setDefaultLogger(); setLogTitle("Git status"); Repository repository = git.getRepository(); StoredConfig config = repository.getConfig(); log("Git conf:"); log(config.toText()); log("Git status:"); org.eclipse.jgit.api.Status status = git.status().call(); log("Added: " + status.getAdded()); log("Changed: " + status.getChanged()); log("Conflicting: " + status.getConflicting()); log("ConflictingStageState: " + status.getConflictingStageState()); log("IgnoredNotInIndex: " + status.getIgnoredNotInIndex()); log("Missing: " + status.getMissing()); log("Modified: " + status.getModified()); log("Removed: " + status.getRemoved()); log("Untracked: " + status.getUntracked()); log("UntrackedFolders: " + status.getUntrackedFolders()); log("Ready."); } else { logAboutMissingGitRepository(); } } catch (Exception e) { log(e); } setState(WAIT); }
From source file:org.webcat.core.git.GitCloner.java
License:Open Source License
private Repository createWorkingCopyRepositoryIfNecessary(File location, File remoteDir) throws IOException { Repository wcRepository; try {/*from w w w. ja v a 2 s .co m*/ wcRepository = RepositoryCache.open(FileKey.lenient(location, FS.DETECTED)); } catch (RepositoryNotFoundException e) { // Create the repository from scratch. if (!location.exists()) { location.mkdirs(); } InitCommand init = Git.init(); init.setDirectory(location); init.setBare(false); wcRepository = init.call().getRepository(); StoredConfig config = wcRepository.getConfig(); config.setBoolean("core", null, "bare", false); try { RefSpec refSpec = new RefSpec().setForceUpdate(true).setSourceDestination(Constants.R_HEADS + "*", Constants.R_REMOTES + "origin" + "/*"); RemoteConfig remoteConfig = new RemoteConfig(config, "origin"); remoteConfig.addURI(new URIish(remoteDir.toString())); remoteConfig.addFetchRefSpec(refSpec); remoteConfig.update(config); } catch (URISyntaxException e2) { // Do nothing. } config.save(); } return wcRepository; }
From source file:org.wso2.carbon.deployment.synchronizer.git.util.GitUtilities.java
License:Open Source License
/** * Adds the remote repository at remoteUrl to the given local repository * * @param repository Repository instance representing local repo * @param remoteUrl remote repository url * @return true if remote successfully added, else false *//*from w w w . j ava2s.c o m*/ public static boolean addRemote(Repository repository, String remoteUrl) { boolean remoteAdded = false; StoredConfig config = repository.getConfig(); config.setString(GitDeploymentSynchronizerConstants.REMOTE, GitDeploymentSynchronizerConstants.ORIGIN, GitDeploymentSynchronizerConstants.URL, remoteUrl); config.setString(GitDeploymentSynchronizerConstants.REMOTE, GitDeploymentSynchronizerConstants.ORIGIN, GitDeploymentSynchronizerConstants.FETCH, GitDeploymentSynchronizerConstants.FETCH_LOCATION); config.setString(GitDeploymentSynchronizerConstants.BRANCH, GitDeploymentSynchronizerConstants.MASTER, GitDeploymentSynchronizerConstants.REMOTE, GitDeploymentSynchronizerConstants.ORIGIN); config.setString(GitDeploymentSynchronizerConstants.BRANCH, GitDeploymentSynchronizerConstants.MASTER, GitDeploymentSynchronizerConstants.MERGE, GitDeploymentSynchronizerConstants.GIT_REFS_HEADS_MASTER); try { config.save(); remoteAdded = true; } catch (IOException e) { log.error( "Error in adding remote origin " + remoteUrl + " for local repository " + repository.toString(), e); } return remoteAdded; }