Search in sources :

Example 1 with CodeOwner

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;
}
Also used : CodeOwner(com.searchcode.app.dto.CodeOwner) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with CodeOwner

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;
}
Also used : CodeOwner(com.searchcode.app.dto.CodeOwner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with CodeOwner

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;
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) CodeOwner(com.searchcode.app.dto.CodeOwner) BlameResult(org.eclipse.jgit.blame.BlameResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlameCommand(org.eclipse.jgit.api.BlameCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

CodeOwner (com.searchcode.app.dto.CodeOwner)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 BlameCommand (org.eclipse.jgit.api.BlameCommand)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 BlameResult (org.eclipse.jgit.blame.BlameResult)1 FileRepository (org.eclipse.jgit.internal.storage.file.FileRepository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1