Example usage for org.eclipse.jgit.diff RawText getLineDelimiter

List of usage examples for org.eclipse.jgit.diff RawText getLineDelimiter

Introduction

In this page you can find the example usage for org.eclipse.jgit.diff RawText getLineDelimiter.

Prototype

public String getLineDelimiter() 

Source Link

Document

Get the line delimiter for the first line.

Usage

From source file:model.Buildmodel.java

public int build(Git git, List<RevCommit> logs, RepoSettings rs)
        throws NoHeadException, GitAPIException, IOException {

    //ObjectId lastCommitId = repository.resolve(Constants.MASTER);
    ObjectId parentid = null;//from   w w w  .j  a v a2s  . co m
    ObjectId currid = null;

    Repository repository = git.getRepository();

    //a new reader to read objects from getObjectDatabase()
    ObjectReader reader = repository.newObjectReader();
    CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
    CanonicalTreeParser newTreeIter = new CanonicalTreeParser();

    List<DiffEntry> diffs = null;

    WriteData wd = new WriteData(); //Class to write data to an external file
    wd.initiate(rs);

    int cnt = 0; //Commits count that is written in data
    for (RevCommit rev : logs) {

        if (rev.getParentCount() == 1) { //Not taking Merge commits, for taking them make it >= .

            try {

                parentid = repository.resolve(rev.getParent(0).getName() + "^{tree}");
                currid = repository.resolve(rev.getName() + "^{tree}");

                //Reset this parser to walk through the given tree
                oldTreeIter.reset(reader, parentid);
                newTreeIter.reset(reader, currid);
                diffs = git.diff() //Returns a command object to execute a diff command
                        .setNewTree(newTreeIter).setOldTree(oldTreeIter).call(); //returns a DiffEntry for each path which is different

            } catch (RevisionSyntaxException | IOException | GitAPIException e) {
                e.printStackTrace();
            }

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            //Create a new formatter with a default level of context.
            DiffFormatter df = new DiffFormatter(out);
            //Set the repository the formatter can load object contents from.
            df.setRepository(git.getRepository());

            ArrayList<String> diffText = new ArrayList<String>();
            ArrayList<String> filetypes = new ArrayList<String>();

            //A DiffEntry is 'A value class representing a change to a file' therefore for each file you have a diff entry.
            int ovllineadd = 0;
            int ovllinerem = 0;
            int totallinechanged = 0;
            int totalfilrem = 0;
            int totalfiladd = 0;

            for (DiffEntry diff : diffs) {

                try {

                    df.format(diff); //Format a patch script for one file entry.
                    RawText r = new RawText(out.toByteArray());
                    r.getLineDelimiter();
                    diffText.add(out.toString());

                    String st = out.toString();
                    String filetype = null;
                    int filerem = 0, fileadd = 0, filtyp = 0;
                    ;
                    int lineadd = 0, linerem = 0;

                    String[] temp = st.split("\n");

                    for (String tem : temp) {

                        if (tem.startsWith("---") || tem.startsWith("+++")) {

                            if (tem.startsWith("---")) {
                                if (tem.endsWith("null")) {
                                    fileadd++; //File added
                                } else {
                                    int p = tem.lastIndexOf("/");
                                    if (p >= 0) {
                                        tem = tem.substring(p + 1, tem.length());
                                    }
                                    int h = tem.lastIndexOf(".");
                                    if (h >= 0) {
                                        filetype = tem.substring(h, tem.length());
                                        filetype = filetype.replace(" ", "");
                                        filetype = filetype.replace("\"", "");
                                        if (filtyp != 1) {
                                            filetypes.add(filetype);
                                            filtyp = 1;
                                        }
                                    } else {
                                        //README, LICENSE type wanna do?
                                    }
                                }
                            }
                            if (tem.startsWith("+++")) {
                                if (tem.endsWith("null")) {
                                    filerem++; //Fil removed
                                } else {
                                    int p = tem.lastIndexOf("/");
                                    if (p >= 0) {
                                        tem = tem.substring(p + 1, tem.length());
                                    }
                                    int h = tem.lastIndexOf(".");
                                    if (h >= 0) {
                                        filetype = tem.substring(h, tem.length());
                                        filetype = filetype.replace(" ", "");
                                        filetype = filetype.replace("\"", "");
                                        if (filtyp != 1) {
                                            filetypes.add(filetype);
                                            filtyp = 1;
                                        }
                                    } else {
                                        //README, LICENSE type wanna do?
                                    }
                                }
                            }

                        }

                        if (tem.startsWith("+") && !tem.startsWith("+++")) {
                            lineadd++;
                        } else if (tem.startsWith("-") && !tem.startsWith("---")) {
                            linerem++;
                        }

                    }
                    ovllineadd += lineadd;
                    ovllinerem += linerem;
                    totallinechanged += (lineadd + linerem);
                    totalfiladd += fileadd;
                    totalfilrem += filerem;

                    out.reset();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            PersonIdent p = rev.getAuthorIdent();
            //DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            DateFormat formatter = new SimpleDateFormat("HH");
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

            String email = p.getEmailAddress();
            //So that email folder is created properly
            if (email.contains("://") || email.contains("//")) {
                email = email.replace(":", "");
                email = email.replace("//", "");
            }

            String[] commsgwords = rev.getFullMessage().split(" ");

            wd.write(rev.getName(), email, totallinechanged, ovllineadd, ovllinerem, filetypes.size(),
                    totalfiladd, totalfilrem, Integer.parseInt(formatter.format(p.getWhen())), filetypes,
                    commsgwords.length);
            cnt++;
        }
    }

    repository.close();
    return cnt;
}

From source file:org.eclipse.egit.core.op.CreatePatchOperation.java

License:Open Source License

/**
 * Updates prefixes to workspace paths//from   w w w .j  ava  2  s .  co  m
 *
 * @param sb
 * @param diffFmt
 */
public void updateWorkspacePatchPrefixes(StringBuilder sb, DiffFormatter diffFmt) {
    RawText rt = new RawText(sb.toString().getBytes());

    final String oldPrefix = diffFmt.getOldPrefix();
    final String newPrefix = diffFmt.getNewPrefix();

    StringBuilder newSb = new StringBuilder();
    final Pattern diffPattern = Pattern.compile("^diff --git (" + oldPrefix + "(.+)) (" + newPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    final Pattern oldPattern = Pattern.compile("^--- (" + oldPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$
    final Pattern newPattern = Pattern.compile("^\\+\\+\\+ (" + newPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$

    int i = 0;
    while (i < rt.size()) {
        String line = rt.getString(i);

        Matcher diffMatcher = diffPattern.matcher(line);
        Matcher oldMatcher = oldPattern.matcher(line);
        Matcher newMatcher = newPattern.matcher(line);
        if (diffMatcher.find()) {
            String group = diffMatcher.group(2); // old path
            IProject project = getProject(group);
            IPath newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(diffMatcher.group(1), newPath.toString());
            group = diffMatcher.group(4); // new path
            newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(diffMatcher.group(3), newPath.toString());
        } else if (oldMatcher.find()) {
            String group = oldMatcher.group(2);
            IProject project = getProject(group);
            IPath newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(oldMatcher.group(1), newPath.toString());
        } else if (newMatcher.find()) {
            String group = newMatcher.group(2);
            IProject project = getProject(group);
            IPath newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(newMatcher.group(1), newPath.toString());
        }
        newSb.append(line);

        i++;
        if (i < rt.size() || !rt.isMissingNewlineAtEnd())
            newSb.append(rt.getLineDelimiter());
    }
    // reset sb to newSb
    sb.setLength(0);
    sb.append(newSb);
}