use of com.searchcode.app.dto.CodeOwner in project searchcode-server by boyter.
the class IndexSvnRepoJob method getInfoExternal.
private CodeOwner getInfoExternal(int codeLinesSize, String repoName, String repoLocations, String fileName) {
CodeOwner owner = new CodeOwner("Unknown", codeLinesSize, (int) (System.currentTimeMillis() / 1000));
ProcessBuilder processBuilder = new ProcessBuilder(this.SVN_BINARY_PATH, "info", "--xml", fileName);
processBuilder.directory(new File(repoLocations + repoName));
Process process = null;
BufferedReader bufferedReader = null;
try {
process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, Values.CHARSET_UTF8);
bufferedReader = new BufferedReader(isr);
String line;
StringBuilder bf = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
bf.append(Singleton.getHelpers().removeUTF8BOM(line));
}
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new ByteArrayInputStream(bf.toString().getBytes()));
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("entry");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
Node node = eElement.getElementsByTagName("commit").item(0);
Element e = (Element) node;
owner.setName(e.getElementsByTagName("author").item(0).getTextContent());
}
}
} catch (IOException | ParserConfigurationException | SAXException ex) {
Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " getInfoExternal for " + repoName + " " + fileName + "\n with message: " + ex.getMessage());
} finally {
Singleton.getHelpers().closeQuietly(process);
Singleton.getHelpers().closeQuietly(bufferedReader);
}
return owner;
}
use of com.searchcode.app.dto.CodeOwner in project searchcode-server by boyter.
the class IndexGitRepoJob method getBlameInfoExternal.
/**
* Only works if we have path to GIT
*/
public List<CodeOwner> getBlameInfoExternal(int codeLinesSize, String repoName, String repoLocations, String fileName) {
List<CodeOwner> codeOwners = new ArrayList<>(codeLinesSize);
// -w is to ignore whitespace bug
ProcessBuilder processBuilder = new ProcessBuilder(this.GIT_BINARY_PATH, "blame", "-c", "-w", fileName);
// The / part is required due to centos bug for version 1.1.1
processBuilder.directory(new File(repoLocations + "/" + repoName));
Process process = null;
BufferedReader bufferedReader = null;
try {
process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, Values.CHARSET_UTF8);
bufferedReader = new BufferedReader(isr);
String line;
DateFormat df = new SimpleDateFormat("yyyy-mm-dd kk:mm:ss");
HashMap<String, CodeOwner> owners = new HashMap<>();
boolean foundSomething = false;
while ((line = bufferedReader.readLine()) != null) {
Singleton.getLogger().info("Blame line " + repoName + fileName + ": " + line);
String[] split = line.split("\t");
if (split.length > 2 && split[1].length() != 0) {
foundSomething = true;
String author = split[1].substring(1);
int commitTime = (int) (System.currentTimeMillis() / 1000);
try {
commitTime = (int) (df.parse(split[2]).getTime() / 1000);
} catch (ParseException ex) {
Singleton.getLogger().info("time parse expection for " + repoName + fileName);
}
if (owners.containsKey(author)) {
CodeOwner codeOwner = owners.get(author);
codeOwner.incrementLines();
int timestamp = codeOwner.getMostRecentUnixCommitTimestamp();
if (commitTime > timestamp) {
codeOwner.setMostRecentUnixCommitTimestamp(commitTime);
}
owners.put(author, codeOwner);
} else {
owners.put(author, new CodeOwner(author, 1, commitTime));
}
}
}
if (foundSomething == false) {
// External call for CentOS issue
String[] split = fileName.split("/");
if (split.length != 1) {
codeOwners = getBlameInfoExternal(codeLinesSize, repoName, repoLocations, String.join("/", Arrays.asList(split).subList(1, split.length)));
}
} else {
codeOwners = new ArrayList<>(owners.values());
}
} catch (IOException | StringIndexOutOfBoundsException ex) {
Singleton.getLogger().info("getBlameInfoExternal repoloc: " + repoLocations + "/" + repoName);
Singleton.getLogger().info("getBlameInfoExternal fileName: " + fileName);
Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " getBlameInfoExternal for " + repoName + " " + fileName + "\n with message: " + ex.getMessage());
} finally {
Singleton.getHelpers().closeQuietly(process);
Singleton.getHelpers().closeQuietly(bufferedReader);
}
return codeOwners;
}
use of com.searchcode.app.dto.CodeOwner in project searchcode-server by boyter.
the class IndexGitRepoJob method getBlameInfo.
/**
* Uses the inbuilt git
* TODO this method appears to leak memory like crazy... need to investigate
* TODO lots of hairy bits in here need tests to capture issues
*/
public List<CodeOwner> getBlameInfo(int codeLinesSize, String repoName, String repoLocations, String fileName) {
List<CodeOwner> codeOwners = new ArrayList<>(codeLinesSize);
try {
// The / part is required due to centos bug for version 1.1.1
// This appears to be correct
String repoLoc = repoLocations + "/" + repoName + "/.git";
Repository localRepository = new FileRepository(new File(repoLoc));
BlameCommand blamer = new BlameCommand(localRepository);
ObjectId commitID = localRepository.resolve("HEAD");
if (commitID == null) {
Singleton.getLogger().info("getBlameInfo commitID is null for " + repoLoc + " " + fileName);
return codeOwners;
}
BlameResult blame;
// Somewhere in here appears to be wrong...
blamer.setStartCommit(commitID);
blamer.setFilePath(fileName);
blame = blamer.call();
// Hail mary attempt to solve issue on CentOS Attempt to set at all costs
if (blame == null) {
// This one appears to solve the issue so don't remove it
String[] split = fileName.split("/");
blamer.setStartCommit(commitID);
if (split.length != 1) {
blamer.setFilePath(String.join("/", Arrays.asList(split).subList(1, split.length)));
}
blame = blamer.call();
}
if (blame == null) {
String[] split = fileName.split("/");
blamer.setStartCommit(commitID);
if (split.length != 1) {
blamer.setFilePath("/" + String.join("/", Arrays.asList(split).subList(1, split.length)));
}
blame = blamer.call();
}
if (blame == null) {
Singleton.getLogger().info("getBlameInfo blame is null for " + repoLoc + " " + fileName);
}
if (blame != null) {
// Get all the owners their number of commits and most recent commit
HashMap<String, CodeOwner> owners = new HashMap<>();
RevCommit commit;
PersonIdent authorIdent;
try {
for (int i = 0; i < codeLinesSize; i++) {
commit = blame.getSourceCommit(i);
authorIdent = commit.getAuthorIdent();
if (owners.containsKey(authorIdent.getName())) {
CodeOwner codeOwner = owners.get(authorIdent.getName());
codeOwner.incrementLines();
int timestamp = codeOwner.getMostRecentUnixCommitTimestamp();
if (commit.getCommitTime() > timestamp) {
codeOwner.setMostRecentUnixCommitTimestamp(commit.getCommitTime());
}
owners.put(authorIdent.getName(), codeOwner);
} else {
owners.put(authorIdent.getName(), new CodeOwner(authorIdent.getName(), 1, commit.getCommitTime()));
}
}
} catch (IndexOutOfBoundsException ex) {
// Ignore this as its not really a problem or is it?
Singleton.getLogger().info("IndexOutOfBoundsException when trying to get blame for " + repoName + " " + fileName);
}
codeOwners = new ArrayList<>(owners.values());
}
} catch (IOException ex) {
Singleton.getLogger().info("IOException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString());
} catch (GitAPIException ex) {
Singleton.getLogger().info("GitAPIException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString());
} catch (IllegalArgumentException ex) {
Singleton.getLogger().info("IllegalArgumentException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString());
}
// Try to clean up
System.gc();
return codeOwners;
}
Aggregations