org.eclipse.jgit.attributes.Attributes Java Examples

The following examples show how to use org.eclipse.jgit.attributes.Attributes. 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 File: ResolveMerger.java    From onedev with MIT License 6 votes vote down vote up
/**
 * Does the content merge. The three texts base, ours and theirs are
 * specified with {@link CanonicalTreeParser}. If any of the parsers is
 * specified as <code>null</code> then an empty text will be used instead.
 *
 * @param base
 * @param ours
 * @param theirs
 * @param attributes
 *
 * @return the result of the content merge
 * @throws IOException
 */
private MergeResult<RawText> contentMerge(CanonicalTreeParser base,
		CanonicalTreeParser ours, CanonicalTreeParser theirs,
		Attributes attributes)
		throws IOException {
	RawText baseText;
	RawText ourText;
	RawText theirsText;

	try {
		baseText = base == null ? RawText.EMPTY_TEXT : getRawText(
						base.getEntryObjectId(), attributes);
		ourText = ours == null ? RawText.EMPTY_TEXT : getRawText(
						ours.getEntryObjectId(), attributes);
		theirsText = theirs == null ? RawText.EMPTY_TEXT : getRawText(
						theirs.getEntryObjectId(), attributes);
	} catch (BinaryBlobException e) {
		MergeResult<RawText> r = new MergeResult<>(Collections.<RawText>emptyList());
		r.setContainsConflicts(true);
		return r;
	}
	return (mergeAlgorithm.merge(RawTextComparator.DEFAULT, baseText,
			ourText, theirsText));
}
 
Example #2
Source File: ResolveMerger.java    From onedev with MIT License 6 votes vote down vote up
/**
 * Writes merged file content to the working tree.
 *
 * @param rawMerged
 *            the raw merged content
 * @param attributes
 *            the files .gitattributes entries
 * @return the working tree file to which the merged content was written.
 * @throws FileNotFoundException
 * @throws IOException
 */
private File writeMergedFile(TemporaryBuffer rawMerged,
		Attributes attributes)
		throws FileNotFoundException, IOException {
	File workTree = nonNullRepo().getWorkTree();
	FS fs = nonNullRepo().getFS();
	File of = new File(workTree, tw.getPathString());
	File parentFolder = of.getParentFile();
	if (!fs.exists(parentFolder)) {
		parentFolder.mkdirs();
	}
	EolStreamType streamType = EolStreamTypeUtil.detectStreamType(
			OperationType.CHECKOUT_OP, workingTreeOptions,
			attributes);
	try (OutputStream os = EolStreamTypeUtil.wrapOutputStream(
			new BufferedOutputStream(new FileOutputStream(of)),
			streamType)) {
		rawMerged.writeTo(os, null);
	}
	return of;
}
 
Example #3
Source File: GitAttributesFactory.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @see EolStreamTypeUtil
 */
@Nullable
private GitAttributesFactory.EolType getEolType(@NotNull Attributes attrs) {
  if (attrs.isSet("binary") || attrs.isUnset("text"))
    return EolType.Binary;

  final String eol = attrs.getValue("eol");
  if (eol != null) {
    switch (eol) {
      case "lf":
        return EolType.LF;
      case "crlf":
        return EolType.CRLF;
    }
  }

  if (attrs.isUnspecified("text"))
    return null;

  return EolType.Native;
}
 
Example #4
Source File: ResolveMerger.java    From onedev with MIT License 5 votes vote down vote up
private ObjectId insertMergeResult(TemporaryBuffer buf,
		Attributes attributes) throws IOException {
	InputStream in = buf.openInputStream();
	try (LfsInputStream is = LfsFactory.getInstance().applyCleanFilter(
			getRepository(), in,
			buf.length(), attributes.get(Constants.ATTR_MERGE))) {
		return getObjectInserter().insert(OBJ_BLOB, is.getLength(), is);
	}
}
 
Example #5
Source File: ResolveMerger.java    From onedev with MIT License 5 votes vote down vote up
private RawText getRawText(ObjectId id,
		Attributes attributes)
		throws IOException, BinaryBlobException {
	if (id.equals(ObjectId.zeroId()))
		return new RawText(new byte[] {});

	ObjectLoader loader = LfsFactory.getInstance().applySmudgeFilter(
			getRepository(), reader.open(id, OBJ_BLOB),
			attributes.get(Constants.ATTR_MERGE));
	int threshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD;
	return RawText.load(loader, threshold);
}
 
Example #6
Source File: GitAttributesFactory.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@NotNull
@Override
public GitProperty[] create(@NotNull InputStream stream) throws IOException {
  AttributesNode r = new AttributesNode();
  r.parse(stream);
  final List<GitProperty> properties = new ArrayList<>();
  for (AttributesRule rule : r.getRules()) {
    final Wildcard wildcard;
    try {
      wildcard = new Wildcard(rule.getPattern());
    } catch (InvalidPatternException | PatternSyntaxException e) {
      log.warn("Found invalid git pattern: {}", rule.getPattern());
      continue;
    }

    final Attributes attrs = new Attributes(rule.getAttributes().toArray(emptyAttributes));

    final EolType eolType = getEolType(attrs);
    if (eolType != null) {
      processProperty(properties, wildcard, SVNProperty.MIME_TYPE, eolType.mimeType);
      processProperty(properties, wildcard, SVNProperty.EOL_STYLE, eolType.eolStyle);
    }
    processProperty(properties, wildcard, SVNProperty.NEEDS_LOCK, getNeedsLock(attrs));

    final String filter = getFilter(attrs);
    if (filter != null)
      properties.add(new GitFilterProperty(wildcard.getMatcher(), filter));
  }

  return properties.toArray(GitProperty.emptyArray);
}
 
Example #7
Source File: GitAttributesFactory.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
private String getNeedsLock(@NotNull Attributes attrs) {
  if (attrs.isSet("lockable"))
    return "*";

  return null;
}
 
Example #8
Source File: ResolveMerger.java    From onedev with MIT License 4 votes vote down vote up
/**
 * Updates the index after a content merge has happened. If no conflict has
 * occurred this includes persisting the merged content to the object
 * database. In case of conflicts this method takes care to write the
 * correct stages to the index.
 *
 * @param base
 * @param ours
 * @param theirs
 * @param result
 * @param attributes
 * @throws FileNotFoundException
 * @throws IOException
 */
private void updateIndex(CanonicalTreeParser base,
		CanonicalTreeParser ours, CanonicalTreeParser theirs,
		MergeResult<RawText> result, Attributes attributes)
		throws FileNotFoundException,
		IOException {
	TemporaryBuffer rawMerged = null;
	try {
		rawMerged = doMerge(result);
		File mergedFile = inCore ? null
				: writeMergedFile(rawMerged, attributes);
		if (result.containsConflicts()) {
			// A conflict occurred, the file will contain conflict markers
			// the index will be populated with the three stages and the
			// workdir (if used) contains the halfway merged content.
			add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, EPOCH, 0);
			add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, EPOCH, 0);
			add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, EPOCH, 0);
			mergeResults.put(tw.getPathString(), result);
			return;
		}

		// No conflict occurred, the file will contain fully merged content.
		// The index will be populated with the new merged version.
		DirCacheEntry dce = new DirCacheEntry(tw.getPathString());

		// Set the mode for the new content. Fall back to REGULAR_FILE if
		// we can't merge modes of OURS and THEIRS.
		int newMode = mergeFileModes(tw.getRawMode(0), tw.getRawMode(1),
				tw.getRawMode(2));
		dce.setFileMode(newMode == FileMode.MISSING.getBits()
				? FileMode.REGULAR_FILE : FileMode.fromBits(newMode));
		if (mergedFile != null) {
			dce.setLastModified(
					nonNullRepo().getFS().lastModifiedInstant(mergedFile));
			dce.setLength((int) mergedFile.length());
		}
		dce.setObjectId(insertMergeResult(rawMerged, attributes));
		builder.add(dce);
	} finally {
		if (rawMerged != null) {
			rawMerged.destroy();
		}
	}
}
 
Example #9
Source File: GitAttributesFactory.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@Nullable
private static String getFilter(@NotNull Attributes attrs) {
  return attrs.getValue("filter");
}
 
Example #10
Source File: ResolveMerger.java    From onedev with MIT License 3 votes vote down vote up
/**
 * Remembers the {@link CheckoutMetadata} for the given path; it may be
 * needed in {@link #checkout()} or in {@link #cleanUp()}.
 *
 * @param path
 *            of the current node
 * @param attributes
 *            for the current node
 * @throws IOException
 *             if the smudge filter cannot be determined
 * @since 5.1
 */
protected void addCheckoutMetadata(String path, Attributes attributes)
		throws IOException {
	if (checkoutMetadata != null) {
		EolStreamType eol = EolStreamTypeUtil.detectStreamType(
				OperationType.CHECKOUT_OP, workingTreeOptions, attributes);
		CheckoutMetadata data = new CheckoutMetadata(eol,
				tw.getFilterCommand(Constants.ATTR_FILTER_TYPE_SMUDGE));
		checkoutMetadata.put(path, data);
	}
}
 
Example #11
Source File: ResolveMerger.java    From onedev with MIT License 3 votes vote down vote up
/**
 * Remember a path for deletion, and remember its {@link CheckoutMetadata}
 * in case it has to be restored in {@link #cleanUp()}.
 *
 * @param path
 *            of the entry
 * @param isFile
 *            whether it is a file
 * @param attributes
 *            for the entry
 * @throws IOException
 *             if the {@link CheckoutMetadata} cannot be determined
 * @since 5.1
 */
protected void addDeletion(String path, boolean isFile,
		Attributes attributes) throws IOException {
	toBeDeleted.add(path);
	if (isFile) {
		addCheckoutMetadata(path, attributes);
	}
}
 
Example #12
Source File: ResolveMerger.java    From onedev with MIT License 2 votes vote down vote up
/**
 * Adds a {@link DirCacheEntry} for direct checkout and remembers its
 * {@link CheckoutMetadata}.
 *
 * @param path
 *            of the entry
 * @param entry
 *            to add
 * @param attributes
 *            for the current entry
 * @throws IOException
 *             if the {@link CheckoutMetadata} cannot be determined
 * @since 5.1
 */
protected void addToCheckout(String path, DirCacheEntry entry,
		Attributes attributes) throws IOException {
	toBeCheckedOut.put(path, entry);
	addCheckoutMetadata(path, attributes);
}