Java Code Examples for org.eclipse.jgit.util.RawParseUtils

The following examples show how to use org.eclipse.jgit.util.RawParseUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: onedev   Source File: RevTag.java    License: MIT License 6 votes vote down vote up
void parseCanonical(RevWalk walk, byte[] rawTag)
		throws CorruptObjectException {
	final MutableInteger pos = new MutableInteger();
	final int oType;

	pos.value = 53; // "object $sha1\ntype "
	oType = Constants.decodeTypeString(this, rawTag, (byte) '\n', pos);
	walk.idBuffer.fromString(rawTag, 7);
	object = walk.lookupAny(walk.idBuffer, oType);

	int p = pos.value += 4; // "tag "
	final int nameEnd = RawParseUtils.nextLF(rawTag, p) - 1;
	tagName = RawParseUtils.decode(UTF_8, rawTag, p, nameEnd);

	if (walk.isRetainBody())
		buffer = rawTag;
	flags |= PARSED;
}
 
Example 2
Source Project: netbeans   Source File: AddTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testAddMissingSymlink () throws Exception {
    if (isWindows()) {
        return;
    }
    String path = "folder/file";
    File f = new File(workDir, path);
    
    // try with commandline client
    File link = new File(workDir, "link");
    Files.createSymbolicLink(Paths.get(link.getAbsolutePath()), Paths.get(path));
    getClient(workDir).add(new File[] { link }, NULL_PROGRESS_MONITOR);
    DirCacheEntry e = repository.readDirCache().getEntry(link.getName());
    assertEquals(FileMode.SYMLINK, e.getFileMode());
    assertEquals(0, e.getLength());
    try (ObjectReader reader = repository.getObjectDatabase().newReader()) {
        assertTrue(reader.has(e.getObjectId()));
        byte[] bytes = reader.open(e.getObjectId()).getBytes();
        assertEquals(path, RawParseUtils.decode(bytes));
    }
}
 
Example 3
Source Project: orion.server   Source File: GitUtils.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static File getSymRef(File workTree, File dotGit, FS fs)
		throws IOException {
	byte[] content = IO.readFully(dotGit);
	if (!isSymRef(content))
		throw new IOException(MessageFormat.format(
				JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath()));

	int pathStart = 8;
	int lineEnd = RawParseUtils.nextLF(content, pathStart);
	if (content[lineEnd - 1] == '\n')
		lineEnd--;
	if (lineEnd == pathStart)
		throw new IOException(MessageFormat.format(
				JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath()));

	String gitdirPath = RawParseUtils.decode(content, pathStart, lineEnd);
	File gitdirFile = fs.resolve(workTree, gitdirPath);
	if (gitdirFile.isAbsolute())
		return gitdirFile;
	else
		return new File(workTree, gitdirPath).getCanonicalFile();
}
 
Example 4
Source Project: hop   Source File: UIGit.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean commit( String authorName, String message ) {
  PersonIdent author = RawParseUtils.parsePersonIdent( authorName );
  // Set the local time
  PersonIdent author2 = new PersonIdent( author.getName(), author.getEmailAddress(),
      SystemReader.getInstance().getCurrentTime(),
      SystemReader.getInstance().getTimezone( SystemReader.getInstance().getCurrentTime() ) );
  try {
    git.commit().setAuthor( author2 ).setMessage( message ).call();
    return true;
  } catch ( Exception e ) {
    showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() );
    return false;
  }
}
 
Example 5
Source Project: onedev   Source File: RevTag.java    License: MIT License 5 votes vote down vote up
private Charset guessEncoding() {
	try {
		return RawParseUtils.parseEncoding(buffer);
	} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
		return UTF_8;
	}
}
 
Example 6
Source Project: onedev   Source File: FooterLine.java    License: MIT License 5 votes vote down vote up
/**
 * Extract the email address (if present) from the footer.
 * <p>
 * If there is an email address looking string inside of angle brackets
 * (e.g. "&lt;[email protected]&gt;"), the return value is the part extracted from inside the
 * brackets. If no brackets are found, then {@link #getValue()} is returned
 * if the value contains an '@' sign. Otherwise, null.
 *
 * @return email address appearing in the value of this footer, or null.
 */
public String getEmailAddress() {
	final int lt = RawParseUtils.nextLF(buffer, valStart, '<');
	if (valEnd <= lt) {
		final int at = RawParseUtils.nextLF(buffer, valStart, '@');
		if (valStart < at && at < valEnd)
			return getValue();
		return null;
	}
	final int gt = RawParseUtils.nextLF(buffer, lt, '>');
	if (valEnd < gt)
		return null;
	return RawParseUtils.decode(enc, buffer, lt, gt - 1);
}
 
Example 7
Source Project: onedev   Source File: RevCommit.java    License: MIT License 5 votes vote down vote up
/**
 * Parse the footer lines (e.g. "Signed-off-by") for machine processing.
 * <p>
 * This method splits all of the footer lines out of the last paragraph of
 * the commit message, providing each line as a key-value pair, ordered by
 * the order of the line's appearance in the commit message itself.
 * <p>
 * A footer line's key must match the pattern {@code ^[A-Za-z0-9-]+:}, while
 * the value is free-form, but must not contain an LF. Very common keys seen
 * in the wild are:
 * <ul>
 * <li>{@code Signed-off-by} (agrees to Developer Certificate of Origin)
 * <li>{@code Acked-by} (thinks change looks sane in context)
 * <li>{@code Reported-by} (originally found the issue this change fixes)
 * <li>{@code Tested-by} (validated change fixes the issue for them)
 * <li>{@code CC}, {@code Cc} (copy on all email related to this change)
 * <li>{@code Bug} (link to project's bug tracking system)
 * </ul>
 *
 * @return ordered list of footer lines; empty list if no footers found.
 */
public final List<FooterLine> getFooterLines() {
	final byte[] raw = buffer;
	int ptr = raw.length - 1;
	while (raw[ptr] == '\n') // trim any trailing LFs, not interesting
		ptr--;

	final int msgB = RawParseUtils.commitMessage(raw, 0);
	final ArrayList<FooterLine> r = new ArrayList<>(4);
	final Charset enc = guessEncoding();
	for (;;) {
		ptr = RawParseUtils.prevLF(raw, ptr);
		if (ptr <= msgB)
			break; // Don't parse commit headers as footer lines.

		final int keyStart = ptr + 2;
		if (raw[keyStart] == '\n')
			break; // Stop at first paragraph break, no footers above it.

		final int keyEnd = RawParseUtils.endOfFooterLineKey(raw, keyStart);
		if (keyEnd < 0)
			continue; // Not a well formed footer line, skip it.

		// Skip over the ': *' at the end of the key before the value.
		//
		int valStart = keyEnd + 1;
		while (valStart < raw.length && raw[valStart] == ' ')
			valStart++;

		// Value ends at the LF, and does not include it.
		//
		int valEnd = RawParseUtils.nextLF(raw, valStart);
		if (raw[valEnd - 1] == '\n')
			valEnd--;

		r.add(new FooterLine(raw, enc, keyStart, keyEnd, valStart, valEnd));
	}
	Collections.reverse(r);
	return r;
}
 
Example 8
Source Project: onedev   Source File: MessageRevFilter.java    License: MIT License 5 votes vote down vote up
static RawCharSequence textFor(RevCommit cmit) {
	final byte[] raw = cmit.getRawBuffer();
	final int b = RawParseUtils.commitMessage(raw, 0);
	if (b < 0)
		return RawCharSequence.EMPTY;
	return new RawCharSequence(raw, b, raw.length);
}
 
Example 9
Source Project: onedev   Source File: CommitterRevFilter.java    License: MIT License 5 votes vote down vote up
static RawCharSequence textFor(RevCommit cmit) {
	final byte[] raw = cmit.getRawBuffer();
	final int b = RawParseUtils.committer(raw, 0);
	if (b < 0)
		return RawCharSequence.EMPTY;
	final int e = RawParseUtils.nextLF(raw, b, '>');
	return new RawCharSequence(raw, b, e);
}
 
Example 10
Source Project: onedev   Source File: AuthorRevFilter.java    License: MIT License 5 votes vote down vote up
static RawCharSequence textFor(RevCommit cmit) {
	final byte[] raw = cmit.getRawBuffer();
	final int b = RawParseUtils.author(raw, 0);
	if (b < 0)
		return RawCharSequence.EMPTY;
	final int e = RawParseUtils.nextLF(raw, b, '>');
	return new RawCharSequence(raw, b, e);
}
 
Example 11
public static String readFile(File directory, String fileName)
        throws IOException {
    byte[] content = IO.readFully(new File(directory, fileName));
    // strip off the last LF
    int end = RawParseUtils.prevLF(content, content.length);
    return RawParseUtils.decode(content, 0, end + 1);
}
 
Example 12
Source Project: git-merge-repos   Source File: SubtreeMerger.java    License: Apache License 2.0 5 votes vote down vote up
private void addTrees(Map<SubtreeConfig, RevCommit> parentCommits, TreeWalk treeWalk)
		throws IOException {
	for (Map.Entry<SubtreeConfig, RevCommit> entry : parentCommits.entrySet()) {
		String directory = entry.getKey().getSubtreeDirectory();
		RevCommit parentCommit = entry.getValue();
		if (".".equals(directory)) {
			treeWalk.addTree(parentCommit.getTree());
		} else {
			byte[] prefix = directory.getBytes(RawParseUtils.UTF8_CHARSET);
			CanonicalTreeParser treeParser = new CanonicalTreeParser(prefix,
					treeWalk.getObjectReader(), parentCommit.getTree());
			treeWalk.addTree(treeParser);
		}
	}
}
 
Example 13
Source Project: netbeans   Source File: AddTest.java    License: Apache License 2.0 4 votes vote down vote up
public void testAddSymlink () throws Exception {
    if (isWindows()) {
        return;
    }
    String path = "folder/file";
    File f = new File(workDir, path);
    f.getParentFile().mkdir();
    write(f, "file");
    add(f);
    commit(f);
    
    Thread.sleep(1100);
    
    // try with commandline client
    File link = new File(workDir, "link");
    runExternally(workDir, Arrays.asList("ln", "-s", path, link.getName()));
    long ts = Files.readAttributes(Paths.get(link.getAbsolutePath()), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS).lastModifiedTime().toMillis();
    runExternally(workDir, Arrays.asList("git", "add", link.getName()));
    DirCacheEntry e = repository.readDirCache().getEntry(link.getName());
    assertEquals(FileMode.SYMLINK, e.getFileMode());
    ObjectId id = e.getObjectId();
    assertTrue((ts - 1000) < e.getLastModified() && (ts + 1000) > e.getLastModified());
    try(ObjectReader reader = repository.getObjectDatabase().newReader()) {
        assertTrue(reader.has(e.getObjectId()));
        byte[] bytes = reader.open(e.getObjectId()).getBytes();
        assertEquals(path, RawParseUtils.decode(bytes));

        // now with internal
        File link2 = new File(workDir, "link2");
        Files.createSymbolicLink(Paths.get(link2.getAbsolutePath()), Paths.get(path));
        ts = Files.readAttributes(Paths.get(link2.getAbsolutePath()), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS).lastModifiedTime().toMillis();
        getClient(workDir).add(new File[]{link2}, NULL_PROGRESS_MONITOR);

        DirCacheEntry e2 = repository.readDirCache().getEntry(link2.getName());
        assertEquals(FileMode.SYMLINK, e2.getFileMode());
        assertEquals(id, e2.getObjectId());
        assertEquals(0, e2.getLength());
        assertTrue((ts - 1000) < e2.getLastModified() && (ts + 1000) > e2.getLastModified());
        assertTrue(reader.has(e2.getObjectId()));
        bytes = reader.open(e2.getObjectId()).getBytes();
        assertEquals(path, RawParseUtils.decode(bytes));
    }
}
 
Example 14
Source Project: onedev   Source File: RevTag.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the tagger identity from the raw buffer.
 * <p>
 * This method parses and returns the content of the tagger line, after
 * taking the tag's character set into account and decoding the tagger
 * name and email address. This method is fairly expensive and produces a
 * new PersonIdent instance on each invocation. Callers should invoke this
 * method only if they are certain they will be outputting the result, and
 * should cache the return value for as long as necessary to use all
 * information from it.
 *
 * @return identity of the tagger (name, email) and the time the tag
 *         was made by the tagger; null if no tagger line was found.
 */
public final PersonIdent getTaggerIdent() {
	final byte[] raw = buffer;
	final int nameB = RawParseUtils.tagger(raw, 0);
	if (nameB < 0)
		return null;
	return RawParseUtils.parsePersonIdent(raw, nameB);
}
 
Example 15
Source Project: onedev   Source File: RevTag.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the complete tag message and decode it to a string.
 * <p>
 * This method parses and returns the message portion of the tag buffer,
 * after taking the tag's character set into account and decoding the buffer
 * using that character set. This method is a fairly expensive operation and
 * produces a new string on each invocation.
 *
 * @return decoded tag message as a string. Never null.
 */
public final String getFullMessage() {
	byte[] raw = buffer;
	int msgB = RawParseUtils.tagMessage(raw, 0);
	if (msgB < 0) {
		return ""; //$NON-NLS-1$
	}
	return RawParseUtils.decode(guessEncoding(), raw, msgB, raw.length);
}
 
Example 16
Source Project: onedev   Source File: RevTag.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the tag message and return the first "line" of it.
 * <p>
 * The first line is everything up to the first pair of LFs. This is the
 * "oneline" format, suitable for output in a single line display.
 * <p>
 * This method parses and returns the message portion of the tag buffer,
 * after taking the tag's character set into account and decoding the buffer
 * using that character set. This method is a fairly expensive operation and
 * produces a new string on each invocation.
 *
 * @return decoded tag message as a string. Never null. The returned string
 *         does not contain any LFs, even if the first paragraph spanned
 *         multiple lines. Embedded LFs are converted to spaces.
 */
public final String getShortMessage() {
	byte[] raw = buffer;
	int msgB = RawParseUtils.tagMessage(raw, 0);
	if (msgB < 0) {
		return ""; //$NON-NLS-1$
	}

	int msgE = RawParseUtils.endOfParagraph(raw, msgB);
	String str = RawParseUtils.decode(guessEncoding(), raw, msgB, msgE);
	if (RevCommit.hasLF(raw, msgB, msgE)) {
		str = StringUtils.replaceLineBreaksWithSpace(str);
	}
	return str;
}
 
Example 17
Source Project: onedev   Source File: RevCommit.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the gpg signature from the raw buffer.
 * <p>
 * This method parses and returns the raw content of the gpgsig lines. This
 * method is fairly expensive and produces a new byte[] instance on each
 * invocation. Callers should invoke this method only if they are certain
 * they will need, and should cache the return value for as long as
 * necessary to use all information from it.
 * <p>
 * RevFilter implementations should try to use
 * {@link org.eclipse.jgit.util.RawParseUtils} to scan the
 * {@link #getRawBuffer()} instead, as this will allow faster evaluation of
 * commits.
 *
 * @return contents of the gpg signature; null if the commit was not signed.
 * @since 5.1
 */
public final byte[] getRawGpgSignature() {
	final byte[] raw = buffer;
	final byte[] header = {'g', 'p', 'g', 's', 'i', 'g'};
	final int start = RawParseUtils.headerStart(header, raw, 0);
	if (start < 0) {
		return null;
	}
	final int end = RawParseUtils.headerEnd(raw, start);
	return Arrays.copyOfRange(raw, start, end);
}
 
Example 18
Source Project: onedev   Source File: RevCommit.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the author identity from the raw buffer.
 * <p>
 * This method parses and returns the content of the author line, after
 * taking the commit's character set into account and decoding the author
 * name and email address. This method is fairly expensive and produces a
 * new PersonIdent instance on each invocation. Callers should invoke this
 * method only if they are certain they will be outputting the result, and
 * should cache the return value for as long as necessary to use all
 * information from it.
 * <p>
 * RevFilter implementations should try to use
 * {@link org.eclipse.jgit.util.RawParseUtils} to scan the
 * {@link #getRawBuffer()} instead, as this will allow faster evaluation of
 * commits.
 *
 * @return identity of the author (name, email) and the time the commit was
 *         made by the author; null if no author line was found.
 */
public final PersonIdent getAuthorIdent() {
	final byte[] raw = buffer;
	final int nameB = RawParseUtils.author(raw, 0);
	if (nameB < 0)
		return null;
	return RawParseUtils.parsePersonIdent(raw, nameB);
}
 
Example 19
Source Project: onedev   Source File: RevCommit.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the committer identity from the raw buffer.
 * <p>
 * This method parses and returns the content of the committer line, after
 * taking the commit's character set into account and decoding the committer
 * name and email address. This method is fairly expensive and produces a
 * new PersonIdent instance on each invocation. Callers should invoke this
 * method only if they are certain they will be outputting the result, and
 * should cache the return value for as long as necessary to use all
 * information from it.
 * <p>
 * RevFilter implementations should try to use
 * {@link org.eclipse.jgit.util.RawParseUtils} to scan the
 * {@link #getRawBuffer()} instead, as this will allow faster evaluation of
 * commits.
 *
 * @return identity of the committer (name, email) and the time the commit
 *         was made by the committer; null if no committer line was found.
 */
public final PersonIdent getCommitterIdent() {
	final byte[] raw = buffer;
	final int nameB = RawParseUtils.committer(raw, 0);
	if (nameB < 0)
		return null;
	return RawParseUtils.parsePersonIdent(raw, nameB);
}
 
Example 20
Source Project: onedev   Source File: RevCommit.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the complete commit message and decode it to a string.
 * <p>
 * This method parses and returns the message portion of the commit buffer,
 * after taking the commit's character set into account and decoding the
 * buffer using that character set. This method is a fairly expensive
 * operation and produces a new string on each invocation.
 *
 * @return decoded commit message as a string. Never null.
 */
public final String getFullMessage() {
	byte[] raw = buffer;
	int msgB = RawParseUtils.commitMessage(raw, 0);
	if (msgB < 0) {
		return ""; //$NON-NLS-1$
	}
	return RawParseUtils.decode(guessEncoding(), raw, msgB, raw.length);
}
 
Example 21
Source Project: onedev   Source File: RevCommit.java    License: MIT License 3 votes vote down vote up
/**
 * Parse the commit message and return the first "line" of it.
 * <p>
 * The first line is everything up to the first pair of LFs. This is the
 * "oneline" format, suitable for output in a single line display.
 * <p>
 * This method parses and returns the message portion of the commit buffer,
 * after taking the commit's character set into account and decoding the
 * buffer using that character set. This method is a fairly expensive
 * operation and produces a new string on each invocation.
 *
 * @return decoded commit message as a string. Never null. The returned
 *         string does not contain any LFs, even if the first paragraph
 *         spanned multiple lines. Embedded LFs are converted to spaces.
 */
public final String getShortMessage() {
	byte[] raw = buffer;
	int msgB = RawParseUtils.commitMessage(raw, 0);
	if (msgB < 0) {
		return ""; //$NON-NLS-1$
	}

	int msgE = RawParseUtils.endOfParagraph(raw, msgB);
	String str = RawParseUtils.decode(guessEncoding(), raw, msgB, msgE);
	if (hasLF(raw, msgB, msgE)) {
		str = StringUtils.replaceLineBreaksWithSpace(str);
	}
	return str;
}
 
Example 22
Source Project: onedev   Source File: ObjectWalk.java    License: MIT License 3 votes vote down vote up
/**
 * Get the current object's complete path.
 * <p>
 * This method is not very efficient and is primarily meant for debugging
 * and final output generation. Applications should try to avoid calling it,
 * and if invoked do so only once per interesting entry, where the name is
 * absolutely required for correct function.
 *
 * @return complete path of the current entry, from the root of the
 *         repository. If the current entry is in a subtree there will be at
 *         least one '/' in the returned string. Null if the current entry
 *         has no path, such as for annotated tags or root level trees.
 */
public String getPathString() {
	if (pathLen == 0) {
		pathLen = updatePathBuf(currVisit);
		if (pathLen == 0)
			return null;
	}
	return RawParseUtils.decode(pathBuf, 0, pathLen);
}
 
Example 23
Source Project: onedev   Source File: FooterLine.java    License: MIT License 2 votes vote down vote up
/**
 * Get key name of this footer.
 *
 * @return key name of this footer; that is the text before the ":" on the
 *         line footer's line. The text is decoded according to the commit's
 *         specified (or assumed) character encoding.
 */
public String getKey() {
	return RawParseUtils.decode(enc, buffer, keyStart, keyEnd);
}
 
Example 24
Source Project: onedev   Source File: FooterLine.java    License: MIT License 2 votes vote down vote up
/**
 * Get value of this footer.
 *
 * @return value of this footer; that is the text after the ":" and any
 *         leading whitespace has been skipped. May be the empty string if
 *         the footer has no value (line ended with ":"). The text is
 *         decoded according to the commit's specified (or assumed)
 *         character encoding.
 */
public String getValue() {
	return RawParseUtils.decode(enc, buffer, valStart, valEnd);
}
 
Example 25
Source Project: onedev   Source File: RevCommit.java    License: MIT License 2 votes vote down vote up
/**
 * Determine the encoding of the commit message buffer.
 * <p>
 * Locates the "encoding" header (if present) and returns its value. Due to
 * corruption in the wild this may be an invalid encoding name that is not
 * recognized by any character encoding library.
 * <p>
 * If no encoding header is present, null.
 *
 * @return the preferred encoding of {@link #getRawBuffer()}; or null.
 * @since 4.2
 */
@Nullable
public final String getEncodingName() {
	return RawParseUtils.parseEncodingName(buffer);
}
 
Example 26
Source Project: onedev   Source File: RevCommit.java    License: MIT License 2 votes vote down vote up
/**
 * Determine the encoding of the commit message buffer.
 * <p>
 * Locates the "encoding" header (if present) and then returns the proper
 * character set to apply to this buffer to evaluate its contents as
 * character data.
 * <p>
 * If no encoding header is present {@code UTF-8} is assumed.
 *
 * @return the preferred encoding of {@link #getRawBuffer()}.
 * @throws IllegalCharsetNameException
 *             if the character set requested by the encoding header is
 *             malformed and unsupportable.
 * @throws UnsupportedCharsetException
 *             if the JRE does not support the character set requested by
 *             the encoding header.
 */
public final Charset getEncoding() {
	return RawParseUtils.parseEncoding(buffer);
}