aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/GraphCommitData.java
blob: 6ae40ff552c007db5796f6db2385a09ac452a0e0 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2022, 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.internal.storage.commitgraph;

import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.COMMIT_DATA_WIDTH;
import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EDGE_LAST_MASK;
import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_EXTRA_EDGES_NEEDED;
import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_LAST_EDGE;
import static org.eclipse.jgit.internal.storage.commitgraph.CommitGraphConstants.GRAPH_NO_PARENT;

import java.text.MessageFormat;
import java.util.Arrays;

import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.commitgraph.CommitGraph.CommitData;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.util.NB;

/**
 * Represent the collection of {@link CommitData}.
 */
class GraphCommitData {

	private static final int[] NO_PARENTS = {};

	private final byte[] data;

	private final byte[] extraList;

	private final int hashLength;

	private final int commitDataLength;

	/**
	 * Initialize the GraphCommitData.
	 *
	 * @param hashLength
	 *            length of object hash.
	 * @param commitData
	 *            content of CommitData Chunk.
	 * @param extraList
	 *            content of Extra Edge List Chunk.
	 */
	GraphCommitData(int hashLength, @NonNull byte[] commitData,
			byte[] extraList) {
		this.data = commitData;
		this.extraList = extraList;
		this.hashLength = hashLength;
		this.commitDataLength = hashLength + COMMIT_DATA_WIDTH;
	}

	/**
	 * Get the metadata of a commit。
	 *
	 * @param graphPos
	 *            the position in the commit-graph of the object.
	 * @return the metadata of a commit or null if not found.
	 */
	CommitData getCommitData(int graphPos) {
		int dataIdx = commitDataLength * graphPos;

		// parse tree
		ObjectId tree = ObjectId.fromRaw(data, dataIdx);

		// parse date
		long dateHigh = NB.decodeUInt32(data, dataIdx + hashLength + 8) & 0x3;
		long dateLow = NB.decodeUInt32(data, dataIdx + hashLength + 12);
		long commitTime = dateHigh << 32 | dateLow;

		// parse generation
		int generation = NB.decodeInt32(data, dataIdx + hashLength + 8) >> 2;

		// parse first parent
		int parent1 = NB.decodeInt32(data, dataIdx + hashLength);
		if (parent1 == GRAPH_NO_PARENT) {
			return new CommitDataImpl(tree, NO_PARENTS, commitTime, generation);
		}

		// parse second parent
		int parent2 = NB.decodeInt32(data, dataIdx + hashLength + 4);
		if (parent2 == GRAPH_NO_PARENT) {
			return new CommitDataImpl(tree, new int[] { parent1 }, commitTime,
					generation);
		}

		if ((parent2 & GRAPH_EXTRA_EDGES_NEEDED) == 0) {
			return new CommitDataImpl(tree, new int[] { parent1, parent2 },
					commitTime, generation);
		}

		// parse parents for octopus merge
		return new CommitDataImpl(tree,
				findParentsForOctopusMerge(parent1,
						parent2 & GRAPH_EDGE_LAST_MASK),
				commitTime, generation);
	}

	private int[] findParentsForOctopusMerge(int parent1, int extraEdgePos) {
		int maxOffset = extraList.length - 4;
		int offset = extraEdgePos * 4;
		if (offset < 0 || offset > maxOffset) {
			throw new IllegalArgumentException(MessageFormat.format(
					JGitText.get().invalidExtraEdgeListPosition,
					Integer.valueOf(extraEdgePos)));
		}
		int[] pList = new int[32];
		pList[0] = parent1;
		int count = 1;
		int parentPosition;
		for (; offset <= maxOffset; offset += 4) {
			if (count >= pList.length) {
				// expand the pList
				pList = Arrays.copyOf(pList, pList.length + 32);
			}
			parentPosition = NB.decodeInt32(extraList, offset);
			if ((parentPosition & GRAPH_LAST_EDGE) != 0) {
				pList[count++] = parentPosition & GRAPH_EDGE_LAST_MASK;
				break;
			}
			pList[count++] = parentPosition;
		}
		return Arrays.copyOf(pList, count);
	}

	private static class CommitDataImpl implements CommitData {

		private final ObjectId tree;

		private final int[] parents;

		private final long commitTime;

		private final int generation;

		public CommitDataImpl(ObjectId tree, int[] parents, long commitTime,
				int generation) {
			this.tree = tree;
			this.parents = parents;
			this.commitTime = commitTime;
			this.generation = generation;
		}

		@Override
		public ObjectId getTree() {
			return tree;
		}

		@Override
		public int[] getParents() {
			return parents;
		}

		@Override
		public long getCommitTime() {
			return commitTime;
		}

		@Override
		public int getGeneration() {
			return generation;
		}
	}
}