Java Code Examples for org.eclipse.jgit.lib.Constants#OBJ_BLOB

The following examples show how to use org.eclipse.jgit.lib.Constants#OBJ_BLOB . 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: RevWalk.java    From onedev with MIT License 6 votes vote down vote up
/**
 * Locate a reference to any object without loading it.
 * <p>
 * The object may or may not exist in the repository. It is impossible to
 * tell from this method's return value.
 *
 * @param id
 *            name of the object.
 * @param type
 *            type of the object. Must be a valid Git object type.
 * @return reference to the object. Never null.
 */
@NonNull
public RevObject lookupAny(AnyObjectId id, int type) {
	RevObject r = objects.get(id);
	if (r == null) {
		switch (type) {
		case Constants.OBJ_COMMIT:
			r = createCommit(id);
			break;
		case Constants.OBJ_TREE:
			r = new RevTree(id);
			break;
		case Constants.OBJ_BLOB:
			r = new RevBlob(id);
			break;
		case Constants.OBJ_TAG:
			r = new RevTag(id);
			break;
		default:
			throw new IllegalArgumentException(MessageFormat.format(
					JGitText.get().invalidGitType, Integer.valueOf(type)));
		}
		objects.add(r);
	}
	return r;
}
 
Example 2
Source File: GitTag.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private GitObjectType getType (RevObject object) {
    GitObjectType objType = GitObjectType.UNKNOWN;
    if (object != null) {
        switch (object.getType()) {
            case Constants.OBJ_COMMIT:
                objType = GitObjectType.COMMIT;
                break;
            case Constants.OBJ_BLOB:
                objType = GitObjectType.BLOB;
                break;
            case Constants.OBJ_TAG:
                objType = GitObjectType.TAG;
                break;
            case Constants.OBJ_TREE:
                objType = GitObjectType.TREE;
                break;
        }
    }
    return objType;
}
 
Example 3
Source File: GitIgnore.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@Nullable
@Override
public GitProperty createForChild(@NotNull String name, @NotNull FileMode fileMode) {
  if (matchers.isEmpty() || (fileMode.getObjectType() == Constants.OBJ_BLOB)) {
    return null;
  }
  final List<String> localList = new ArrayList<>();
  final List<String> globalList = new ArrayList<>();
  final List<PathMatcher> childMatchers = new ArrayList<>();
  for (PathMatcher matcher : matchers) {
    processMatcher(localList, globalList, childMatchers, matcher.createChild(name, true));
  }
  if (localList.isEmpty() && globalList.isEmpty() && childMatchers.isEmpty()) {
    return null;
  }
  return new GitIgnore(localList, globalList, childMatchers);
}
 
Example 4
Source File: RevWalk.java    From onedev with MIT License 5 votes vote down vote up
private RevObject parseNew(AnyObjectId id, ObjectLoader ldr)
		throws LargeObjectException, CorruptObjectException,
		MissingObjectException, IOException {
	RevObject r;
	int type = ldr.getType();
	switch (type) {
	case Constants.OBJ_COMMIT: {
		final RevCommit c = createCommit(id);
		c.parseCanonical(this, getCachedBytes(c, ldr));
		r = c;
		break;
	}
	case Constants.OBJ_TREE: {
		r = new RevTree(id);
		r.flags |= PARSED;
		break;
	}
	case Constants.OBJ_BLOB: {
		r = new RevBlob(id);
		r.flags |= PARSED;
		break;
	}
	case Constants.OBJ_TAG: {
		final RevTag t = new RevTag(id);
		t.parseCanonical(this, getCachedBytes(t, ldr));
		r = t;
		break;
	}
	default:
		throw new IllegalArgumentException(MessageFormat.format(
				JGitText.get().badObjectType, Integer.valueOf(type)));
	}
	objects.add(r);
	return r;
}
 
Example 5
Source File: GitFileTreeEntry.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@NotNull
@Override
public Map<String, String> getProperties() throws IOException {
  final Map<String, String> props = getUpstreamProperties();
  final FileMode fileMode = getFileMode();
  if (fileMode.equals(FileMode.SYMLINK)) {
    props.remove(SVNProperty.EOL_STYLE);
    props.remove(SVNProperty.MIME_TYPE);
    props.put(SVNProperty.SPECIAL, "*");
  } else {
    if (fileMode.equals(FileMode.EXECUTABLE_FILE))
      props.put(SVNProperty.EXECUTABLE, "*");

    if (props.containsKey(SVNProperty.MIME_TYPE)) {
      props.remove(SVNProperty.EOL_STYLE);
    } else if (props.containsKey(SVNProperty.EOL_STYLE)) {
      props.remove(SVNProperty.MIME_TYPE);
    } else if (fileMode.getObjectType() == Constants.OBJ_BLOB) {
      if (branch.getRepository().isObjectBinary(filter, getObjectId())) {
        props.put(SVNProperty.MIME_TYPE, MIME_BINARY);
      } else {
        props.put(SVNProperty.EOL_STYLE, SVNProperty.EOL_STYLE_NATIVE);
      }
    }
  }
  return props;
}
 
Example 6
Source File: GitFile.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@NotNull
default SVNNodeKind getKind() {
  final int objType = getFileMode().getObjectType();
  switch (objType) {
    case Constants.OBJ_TREE:
    case Constants.OBJ_COMMIT:
      return SVNNodeKind.DIR;
    case Constants.OBJ_BLOB:
      return SVNNodeKind.FILE;
    default:
      throw new IllegalStateException("Unknown obj type: " + objType);
  }
}
 
Example 7
Source File: GitFilterProperty.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
@Override
public GitProperty createForChild(@NotNull String name, @NotNull FileMode fileMode) {
  final boolean isDir = fileMode.getObjectType() != Constants.OBJ_BLOB;
  final PathMatcher matcherChild = matcher.createChild(name, isDir);
  if ((matcherChild != null) && (isDir || matcherChild.isMatch())) {
    return new GitFilterProperty(matcherChild, filterName);
  }
  return null;
}
 
Example 8
Source File: GitAutoProperty.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@Nullable
@Override
public GitProperty createForChild(@NotNull String name, @NotNull FileMode fileMode) {
  if (fileMode.getObjectType() == Constants.OBJ_BLOB) {
    return null;
  }
  if (matcher.getSvnMaskGlobal() != null) {
    return null;
  }
  final PathMatcher matcherChild = matcher.createChild(name, true);
  if (matcherChild == null) {
    return null;
  }
  return new GitAutoProperty(matcherChild, property, value);
}
 
Example 9
Source File: RevBlob.java    From onedev with MIT License 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public final int getType() {
	return Constants.OBJ_BLOB;
}
 
Example 10
Source File: GitCommit.java    From compiler with Apache License 2.0 4 votes vote down vote up
private void updateChangedFiles(final RevCommit parent, final int parentIndex, final RevCommit child) {
	final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE);
	df.setRepository(repository);
	df.setDiffComparator(RawTextComparator.DEFAULT);
	df.setDetectRenames(true);

	try {
		final AbstractTreeIterator parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree());
		final AbstractTreeIterator childIter = new CanonicalTreeParser(null, repository.newObjectReader(), child.getTree());
		List<DiffEntry> diffs = df.scan(parentIter, childIter);
		for (final DiffEntry diff : diffs) {
			if (diff.getChangeType() == ChangeType.MODIFY) {
				if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
					updateChangedFiles(parent, child, diff, ChangeKind.MODIFIED);
				}
			// RENAMED file may have the same/different object id(s) for old and new
			} else if (diff.getChangeType() == ChangeType.RENAME) {
				if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
					updateChangedFiles(parent, child, diff, ChangeKind.RENAMED);
				}
			} else if (diff.getChangeType() == ChangeType.COPY) {
				if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
					updateChangedFiles(parent, child, diff, ChangeKind.COPIED);
				}
			// ADDED file should not have old path and its old object id is 0's
			} else if (diff.getChangeType() == ChangeType.ADD) {
				if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) {
					updateChangedFiles(parent, child, diff, ChangeKind.ADDED);
				}
			// DELETED file's new object id is 0's and doesn't have new path
			} else if (diff.getChangeType() == ChangeType.DELETE) {
				if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) {
					String oldPath = diff.getOldPath();
					String oldObjectId = diff.getOldId().toObjectId().getName();
					ChangedFile.Builder cfb = getChangedFile(oldPath, ChangeKind.DELETED);
					filePathGitObjectIds.put(oldPath, diff.getNewId().toObjectId());
				}
			}
		}
	} catch (final IOException e) {
		if (debug)
			System.err.println("Git Error getting commit diffs: " + e.getMessage());
	}
	df.close();
}
 
Example 11
Source File: GitProctorCore.java    From proctor with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public <C> C getFileContents(
        final Class<C> c,
        final String[] path,
        @Nullable final C defaultValue,
        final String revision
) throws StoreException.ReadException, JsonProcessingException {
    try {
        if (!ObjectId.isId(revision)) {
            throw new StoreException.ReadException("Malformed id " + revision);
        }
        final ObjectId blobOrCommitId = ObjectId.fromString(revision);

        final ObjectLoader loader = git.getRepository().open(blobOrCommitId);

        if (loader.getType() == Constants.OBJ_COMMIT) {
            // look up the file at this revision
            final RevCommit commit = RevCommit.parse(loader.getCachedBytes());

            final TreeWalk treeWalk2 = new TreeWalk(git.getRepository());
            treeWalk2.addTree(commit.getTree());
            treeWalk2.setRecursive(true);
            final String joinedPath = String.join("/", path);
            treeWalk2.setFilter(PathFilter.create(joinedPath));

            if (!treeWalk2.next()) {
                // it did not find expected file `joinPath` so return default value
                return defaultValue;
            }
            final ObjectId blobId = treeWalk2.getObjectId(0);
            return getFileContents(c, blobId);
        } else if (loader.getType() == Constants.OBJ_BLOB) {
            return getFileContents(c, blobOrCommitId);
        } else {
            throw new StoreException.ReadException("Invalid Object Type " + loader.getType() + " for id " + revision);
        }
    } catch (final IOException e) {
        throw new StoreException.ReadException(e);
    }
}