Git git = new Git(localRepository); StoredConfig config = git.getRepository().getConfig(); config.setString("remote", "origin", "url", "http://github.com/user/repo"); config.save();
void cleanAndResetToMaster() throws IOException { try { git.reset().setMode(ResetCommand.ResetType.HARD).call(); checkout("master"); deleteBranch(BRANCH_AT_REVISION); deleteBranch(BRANCH_AT_HEAD); } catch (Exception e) { String currentBranch = git.getRepository().getBranch(); LOGGER.error("Error while trying to clean up config repository, CurrentBranch: {} \n : \n Message: {} \n StackTrace: {}", currentBranch, e.getMessage(), e.getStackTrace(), e); throw new RuntimeException(e); } }
@Signature public ArrayMemory getTags() { Map<String, Ref> tags = getWrappedObject().getRepository().getTags(); ArrayMemory memory = new ArrayMemory(); for (Map.Entry<String, Ref> entry : tags.entrySet()) { memory.put(entry.getKey(), GitUtils.valueOf(entry.getValue())); } return memory; }
@Signature public void init(boolean bare) throws IOException { getWrappedObject().getRepository().create(bare); }
@Signature public String getBranch() throws IOException { return getWrappedObject().getRepository().getBranch(); }
@Signature public RepositoryState getState() { return getWrappedObject().getRepository().getRepositoryState(); }
@Signature public String getFullBranch() throws IOException { return getWrappedObject().getRepository().getFullBranch(); }
@Signature public File getDirectory() { return getWrappedObject().getRepository().getDirectory(); }
@Signature public String getRemoteName(String name) { return getWrappedObject().getRepository().getRemoteName(name); }
@Signature public boolean isBare() { return getWrappedObject().getRepository().isBare(); }
@Signature public File getWorkTree() throws IOException { return getWrappedObject().getRepository().getWorkTree(); }
private boolean isClean(Git git, String label) { StatusCommand status = git.status(); try { BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(git.getRepository(), label); boolean isBranchAhead = trackingStatus != null && trackingStatus.getAheadCount() > 0; return status.call().isClean() && !isBranchAhead; } catch (Exception e) { String message = "Could not execute status command on local repository. Cause: (" + e.getClass().getSimpleName() + ") " + e.getMessage(); warn(message, e); return false; } }
@Signature public Memory findRef(String name) throws IOException { Ref ref = getWrappedObject().getRepository().findRef(name); return ref == null ? Memory.NULL : GitUtils.valueOf(ref); }
@Signature public Memory exactRef(String name) throws IOException { Ref ref = getWrappedObject().getRepository().exactRef(name); return ref == null ? Memory.NULL : GitUtils.valueOf(ref); }
@Signature public boolean isExists() throws IOException { return getWrappedObject().getRepository().getObjectDatabase().exists(); }
@Signature public Memory resolve(String revstr) throws IOException { return GitUtils.valueOf(getWrappedObject().getRepository().resolve(revstr)); }
@Test public void shouldSwitchToMasterAndDeleteTempBranches() throws Exception, GitAPIException { configRepo.checkin(goConfigRevision("v1", "md5-1")); configRepo.createBranch(ConfigRepository.BRANCH_AT_HEAD, configRepo.getCurrentRevCommit()); configRepo.createBranch(ConfigRepository.BRANCH_AT_REVISION, configRepo.getCurrentRevCommit()); configRepo.git().checkout().setName(ConfigRepository.BRANCH_AT_REVISION).call(); assertThat(configRepo.git().getRepository().getBranch(), is(ConfigRepository.BRANCH_AT_REVISION)); assertThat(configRepo.git().branchList().call().size(), is(3)); configRepo.cleanAndResetToMaster(); assertThat(configRepo.git().getRepository().getBranch(), is("master")); assertThat(configRepo.git().branchList().call().size(), is(1)); }
@Test public void shouldCleanAndResetToMasterDuringInitialization() throws Exception { configRepo.checkin(goConfigRevision("v1", "md5-1")); configRepo.createBranch(ConfigRepository.BRANCH_AT_REVISION, configRepo.getCurrentRevCommit()); configRepo.git().checkout().setName(ConfigRepository.BRANCH_AT_REVISION).call(); assertThat(configRepo.git().getRepository().getBranch(), is(ConfigRepository.BRANCH_AT_REVISION)); new ConfigRepository(systemEnvironment).initialize(); assertThat(configRepo.git().getRepository().getBranch(), is("master")); assertThat(configRepo.git().branchList().call().size(), is(1)); }
@Signature public Memory resolveCommit(String revstr) throws IOException, GitAPIException { ObjectId objectId = getWrappedObject().getRepository().resolve(revstr); if (objectId == null) { return Memory.NULL; } LogCommand command = getWrappedObject() .log() .add(objectId) .setMaxCount(1); Iterable<RevCommit> call = command.call(); for (RevCommit revCommit : call) { return GitUtils.valueOf(revCommit); } return Memory.NULL; }
@Test public void shouldCleanAndResetToMasterOnceMergeFlowIsComplete() throws Exception { String original = "first\nsecond\n"; String changeOnBranch = "first\nsecond\nthird\n"; String changeOnMaster = "1st\nsecond\n"; String oldMd5 = "md5-1"; configRepo.checkin(goConfigRevision(original, oldMd5)); configRepo.checkin(goConfigRevision(changeOnMaster, "md5-2")); configRepo.getConfigMergedWithLatestRevision(goConfigRevision(changeOnBranch, "md5-3"), oldMd5); assertThat(configRepo.git().getRepository().getBranch(), is("master")); assertThat(configRepo.git().branchList().call().size(), is(1)); }