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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
* Copyright (c) 2021 Qualcomm Innovation Center, Inc.
* and other copyright owners as documented in the project's IP log.
*
* 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.file;
import java.io.File;
import java.text.MessageFormat;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.lib.ObjectId;
/**
* A pack file (or pack related) File.
*
* Example: "pack-0123456789012345678901234567890123456789.idx"
*/
public class PackFile extends File {
private static final long serialVersionUID = 1L;
private static final String PREFIX = "pack-"; //$NON-NLS-1$
private static final String TMP_GC_PREFIX = ".tmp-"; //$NON-NLS-1$
private final String base; // PREFIX + id i.e.
// pack-0123456789012345678901234567890123456789
private final String id; // i.e. 0123456789012345678901234567890123456789
private final boolean hasOldPrefix;
private final PackExt packExt;
private static String createName(String id, PackExt extension) {
return PREFIX + id + '.' + extension.getExtension();
}
/**
* Create a PackFile for a pack or related file.
*
* @param file
* File pointing to the location of the file.
*/
public PackFile(File file) {
this(file.getParentFile(), file.getName());
}
/**
* Create a PackFile for a pack or related file.
*
* @param directory
* Directory to create the PackFile in.
* @param id
* the {@link ObjectId} for this pack
* @param ext
* the <code>packExt</code> of the name.
*/
public PackFile(File directory, ObjectId id, PackExt ext) {
this(directory, id.name(), ext);
}
/**
* Create a PackFile for a pack or related file.
*
* @param directory
* Directory to create the PackFile in.
* @param id
* the <code>id</code> (40 Hex char) section of the pack name.
* @param ext
* the <code>packExt</code> of the name.
*/
public PackFile(File directory, String id, PackExt ext) {
this(directory, createName(id, ext));
}
/**
* Create a PackFile for a pack or related file.
*
* @param directory
* Directory to create the PackFile in.
* @param name
* Filename (last path section) of the PackFile
*/
public PackFile(File directory, String name) {
super(directory, name);
int dot = name.lastIndexOf('.');
if (dot < 0) {
base = name;
hasOldPrefix = false;
packExt = null;
} else {
base = name.substring(0, dot);
String tail = name.substring(dot + 1); // ["old-"] + extension
packExt = getPackExt(tail);
String old = tail.substring(0,
tail.length() - getExtension().length());
hasOldPrefix = old.equals(getExtPrefix(true));
}
id = base.startsWith(PREFIX) ? base.substring(PREFIX.length()) : base;
}
/**
* Getter for the field <code>id</code>.
*
* @return the <code>id</code> (40 Hex char) section of the name.
*/
public String getId() {
return id;
}
/**
* Getter for the field <code>packExt</code>.
*
* @return the <code>packExt</code> of the name.
*/
public PackExt getPackExt() {
return packExt;
}
/**
* @return whether the file is a temporary GC file
*/
public boolean isTmpGCFile() {
return id.startsWith(TMP_GC_PREFIX);
}
/**
* Create a new similar PackFile with the given extension instead.
*
* @param ext
* PackExt the extension to use.
* @return a PackFile instance with specified extension
*/
public PackFile create(PackExt ext) {
return new PackFile(getParentFile(), getName(ext));
}
/**
* Create a new similar PackFile in the given directory.
*
* @param directory
* Directory to create the new PackFile in.
* @return a PackFile in the given directory
*/
public PackFile createForDirectory(File directory) {
return new PackFile(directory, getName(false));
}
/**
* Create a new similar preserved PackFile in the given directory.
*
* @param directory
* Directory to create the new PackFile in.
* @return a PackFile in the given directory with "old-" prefixing the
* extension
*/
public PackFile createPreservedForDirectory(File directory) {
return new PackFile(directory, getName(true));
}
private String getName(PackExt ext) {
return base + '.' + getExtPrefix(hasOldPrefix) + ext.getExtension();
}
private String getName(boolean isPreserved) {
return base + '.' + getExtPrefix(isPreserved) + getExtension();
}
private String getExtension() {
return packExt == null ? "" : packExt.getExtension(); //$NON-NLS-1$
}
private static String getExtPrefix(boolean isPreserved) {
return isPreserved ? "old-" : ""; //$NON-NLS-1$ //$NON-NLS-2$
}
private static PackExt getPackExt(String endsWithExtension) {
for (PackExt ext : PackExt.values()) {
if (endsWithExtension.equals(ext.getExtension())) {
return ext;
}
if (endsWithExtension.equals("old-" + ext.getExtension())) {
return ext;
}
}
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().unrecognizedPackExtension, endsWithExtension));
}
}
|