public static Repository prepareBareRemote() throws IOException { // Create a folder in the temp folder that will act as the remote repository File remoteDir = File.createTempFile("remote", ""); remoteDir.delete(); remoteDir.mkdirs(); // Create a bare repository FileKey fileKey = FileKey.exact(remoteDir, FS.DETECTED); Repository remoteRepo = fileKey.open(false); remoteRepo.create(true); return remoteRepo; }
/** * @param gitConfigFolder e.g. /your/project/root/.git * * @return Returns git config remote.origin.url field of the repository located at gitConfigFolder */ public static String getGitRemoteUrl( String gitConfigFolder ) throws MojoExecutionException { try { Repository repo = new RepositoryBuilder().setGitDir( new File( gitConfigFolder ) ).readEnvironment().findGitDir() .build(); Config config = repo.getConfig(); return config.getString( "remote", "origin", "url" ); } catch ( Exception e ) { throw new MojoExecutionException( "Error trying to get remote origin url of git repository", e ); } }
public void initialize() throws IOException { if (!gitRepo.getDirectory().exists()) { gitRepo.create(); } else { cleanAndResetToMaster(); } }
private Ref resetHard(Git git, String label, String ref) { ResetCommand reset = git.reset(); reset.setRef(ref); reset.setMode(ResetType.HARD); try { Ref resetRef = reset.call(); if (resetRef != null) { this.logger.info( "Reset label " + label + " to version " + resetRef.getObjectId()); } return resetRef; } catch (Exception ex) { String message = "Could not reset to remote for " + label + " (current ref=" + ref + "), remote: " + git.getRepository().getConfig() .getString("remote", "origin", "url"); warn(message, ex); return null; } }
public static ArrayMemory valueOf(Ref ref) { ArrayMemory memory = new ArrayMemory(); memory.refOfIndex("name").assign(ref.getName()); memory.refOfIndex("peeled").assign(ref.isPeeled()); memory.refOfIndex("symbolic").assign(ref.isSymbolic()); memory.refOfIndex("objectId").assign(valueOf(ref.getObjectId())); memory.refOfIndex("storage").assign(valueOf(ref.getStorage())); return memory; }
private byte[] contentFromTree(RevTree tree) { try { final ObjectReader reader = gitRepo.newObjectReader(); CanonicalTreeParser parser = new CanonicalTreeParser(); parser.reset(reader, tree); String lastPath = null; while (true) { final String path = parser.getEntryPathString(); parser = parser.next(); if (path.equals(lastPath)) { break; } lastPath = path; if (path.equals(CRUISE_CONFIG_XML)) { final ObjectId id = parser.getEntryObjectId(); final ObjectLoader loader = reader.open(id); return loader.getBytes(); } } return null; } catch (IOException e) { LOGGER.error("Could not fetch content from the config repository found at path '{}'", workingDir.getAbsolutePath(), e); throw new RuntimeException("Error while fetching content from the config repository.", e); } }
Ref getBranch(String branchName) throws GitAPIException { List<Ref> branches = getAllBranches(); for (Ref branch : branches) { if (branch.getName().endsWith(branchName)) { return branch; } } return null; }
@Autowired public ConfigRepository(SystemEnvironment systemEnvironment) throws IOException { this.systemEnvironment = systemEnvironment; workingDir = this.systemEnvironment.getConfigRepoDir(); File configRepoDir = new File(workingDir, ".git"); gitRepo = new FileRepositoryBuilder().setGitDir(configRepoDir).build(); gitRepo.getConfig().setInt("gc", null, "auto", 0); git = new Git(gitRepo); }
@Signature public File getDirectory() { return getWrappedObject().getRepository().getDirectory(); }
@Override public void close() throws IOException { if (!isClosed) { try { if (repository != null) { repository.close(); } closeGits(); if (isTempDirectory) { deleteTempDirectory(); } } finally { isClosed = true; } } }
private static List<RevCommit> filterValidCommits(List<RevCommit> revCommits) { final List<RevCommit> filteredCommits = new LinkedList<>(); for (RevCommit commit : revCommits) { final String commitAuthor = commit.getAuthorIdent().getName(); if (!USERS_EXCLUDED_FROM_VALIDATION.contains(commitAuthor)) { filteredCommits.add(commit); } } return filteredCommits; }
@Test public void testCommitMessageHasProperStructure() { for (RevCommit commit : filterValidCommits(lastCommits)) { final String commitMessage = commit.getFullMessage(); final int error = validateCommitMessage(commitMessage); if (error != 0) { final String commitId = commit.getId().getName(); fail(getInvalidCommitMessageFormattingError(commitId, commitMessage) + error); } } }
@Signature public boolean isExists() throws IOException { return getWrappedObject().getRepository().getObjectDatabase().exists(); }
public static ArrayMemory valueOf(ReflogEntry value) { ArrayMemory memory = new ArrayMemory(); memory.refOfIndex("newId").assign(valueOf(value.getNewId())); memory.refOfIndex("oldId").assign(valueOf(value.getOldId())); memory.refOfIndex("who").assign(value.getWho() == null ? Memory.NULL : valueOf(value.getWho())); memory.refOfIndex("comment").assign(value.getComment()); return memory; }
@Signature public String getFullBranch() throws IOException { return getWrappedObject().getRepository().getFullBranch(); }
public static ArrayMemory valueOf(DiffEntry value) { ArrayMemory memory = new ArrayMemory(); memory.refOfIndex("oldId").assign(value.getOldId() == null ? Memory.NULL : valueOf(value.getOldId().toObjectId())); memory.refOfIndex("oldPath").assign(value.getOldPath()); memory.refOfIndex("oldMode").assign(value.getOldMode() == null ? Memory.NULL : StringMemory.valueOf(value.getOldMode().toString())); memory.refOfIndex("newId").assign(value.getNewId() == null ? Memory.NULL : valueOf(value.getNewId().toObjectId())); memory.refOfIndex("newPath").assign(value.getNewPath()); memory.refOfIndex("newMode").assign(value.getNewMode() == null ? Memory.NULL : StringMemory.valueOf(value.getNewMode().toString())); memory.refOfIndex("score").assign(value.getScore()); memory.refOfIndex("changeType").assign(value.getChangeType().name()); return memory; }
public static Memory valueOf(AnyObjectId value) { return value == null ? Memory.NULL : StringMemory.valueOf(value.getName()); }
@After public void tearDown() throws Exception { configRepo.git().close(); configRepo.getGitRepo().close(); }