aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommitCG.java
blob: c7a03992b7660293e5a676e1af49c42c92d06299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2023, Tencent.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.revwalk;

import java.io.IOException;

import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.internal.storage.commitgraph.ChangedPathFilter;
import org.eclipse.jgit.internal.storage.commitgraph.CommitGraph;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;

/**
 * RevCommit parsed from
 * {@link org.eclipse.jgit.internal.storage.commitgraph.CommitGraph}.
 *
 * @since 6.5
 */
class RevCommitCG extends RevCommit {

	private final int graphPosition;

	private int generation = Constants.COMMIT_GENERATION_UNKNOWN;

	/**
	 * Create a new commit reference.
	 *
	 * @param id
	 *            object name for the commit.
	 * @param graphPosition
	 *            the position in the commit-graph of the object.
	 */
	protected RevCommitCG(AnyObjectId id, int graphPosition) {
		super(id);
		this.graphPosition = graphPosition;
	}

	@Override
	void parseCanonical(RevWalk walk, byte[] raw) throws IOException {
		if (walk.isRetainBody()) {
			buffer = raw;
		}
		parseInGraph(walk);
	}

	@Override
	void parseHeaders(RevWalk walk) throws MissingObjectException,
			IncorrectObjectTypeException, IOException {
		if (walk.isRetainBody()) {
			super.parseBody(walk); // This parses header and body
			return;
		}
		parseInGraph(walk);
	}

	private void parseInGraph(RevWalk walk) throws IOException {
		CommitGraph graph = walk.commitGraph();
		CommitGraph.CommitData data = graph.getCommitData(graphPosition);
		if (data == null) {
			// RevCommitCG was created because we got its graphPosition from
			// commit-graph. If now the commit-graph doesn't know about it,
			// something went wrong.
			throw new IllegalStateException();
		}
		if (!walk.shallowCommitsInitialized) {
			walk.initializeShallowCommits(this);
		}

		this.tree = walk.lookupTree(data.getTree());
		this.commitTime = (int) data.getCommitTime();
		this.generation = data.getGeneration();

		if (getParents() == null) {
			int[] pGraphList = data.getParents();
			if (pGraphList.length == 0) {
				this.parents = RevCommit.NO_PARENTS;
			} else {
				RevCommit[] pList = new RevCommit[pGraphList.length];
				for (int i = 0; i < pList.length; i++) {
					int graphPos = pGraphList[i];
					ObjectId objId = graph.getObjectId(graphPos);
					pList[i] = walk.lookupCommit(objId, graphPos);
				}
				this.parents = pList;
			}
		}
		flags |= PARSED;
	}

	@Override
	int getGeneration() {
		return generation;
	}

	/** {@inheritDoc} */
	@Override
	public ChangedPathFilter getChangedPathFilter(RevWalk rw) {
		return rw.commitGraph().getChangedPathFilter(graphPosition);
	}
}