Skip to content

Commit 92ff8a0

Browse files
committed
Add converter for ObjectId class.
This fixes legacy support for serializing an Objectid as a byte array.
1 parent 5bb65d5 commit 92ff8a0

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ public static final class DescriptorImpl extends SCMDescriptor<GitSCM> {
12841284

12851285
public DescriptorImpl() {
12861286
super(GitSCM.class, GitRepositoryBrowser.class);
1287+
Run.XSTREAM.registerConverter(new ObjectIdConverter());
12871288
load();
12881289
}
12891290

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package hudson.plugins.git;
2+
3+
import com.thoughtworks.xstream.converters.Converter;
4+
import com.thoughtworks.xstream.converters.MarshallingContext;
5+
import com.thoughtworks.xstream.converters.UnmarshallingContext;
6+
import com.thoughtworks.xstream.core.util.Base64Encoder;
7+
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
8+
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9+
10+
import org.eclipse.jgit.lib.ObjectId;
11+
12+
/**
13+
* Object id converter. This is required for legacy support for originally
14+
* serializing an ObjectId as a byte[]. The new format supports marshalling as a
15+
* standard lower-case SHA1 hexadecimal string but unmarshalling both byte[] and
16+
* String types.
17+
*/
18+
public class ObjectIdConverter implements Converter {
19+
20+
private Base64Encoder base64;
21+
22+
/**
23+
* Create ObjectId converter
24+
*/
25+
public ObjectIdConverter() {
26+
base64 = new Base64Encoder();
27+
}
28+
29+
public boolean canConvert(Class type) {
30+
return ObjectId.class == type;
31+
}
32+
33+
public void marshal(Object source, HierarchicalStreamWriter writer,
34+
MarshallingContext context) {
35+
writer.setValue(((ObjectId) source).name());
36+
}
37+
38+
public Object unmarshal(HierarchicalStreamReader reader,
39+
final UnmarshallingContext context) {
40+
if (reader.hasMoreChildren()
41+
&& "byte-array".equals(reader.peekNextChild())) {
42+
reader.moveDown();
43+
ObjectId sha1 = ObjectId.fromRaw(base64.decode(reader.getValue()));
44+
reader.moveUp();
45+
return sha1;
46+
} else
47+
return ObjectId.fromString(reader.getValue());
48+
}
49+
}

0 commit comments

Comments
 (0)