| 31 | import org.eclipse.jgit.lib.Repository; |
| 32 | |
| 33 | @AutoValue |
| 34 | public abstract class RefState { |
| 35 | public static ImmutableSetMultimap<Project.NameKey, RefState> parseStates( |
| 36 | Iterable<byte[]> states) { |
| 37 | RefState.check(states != null, null); |
| 38 | ImmutableSetMultimap.Builder<Project.NameKey, RefState> result = ImmutableSetMultimap.builder(); |
| 39 | for (byte[] b : states) { |
| 40 | RefState.check(b != null, null); |
| 41 | String s = new String(b, UTF_8); |
| 42 | List<String> parts = Splitter.on(':').splitToList(s); |
| 43 | RefState.check(parts.size() == 3 && !parts.get(0).isEmpty() && !parts.get(1).isEmpty(), s); |
| 44 | result.put(Project.NameKey.parse(parts.get(0)), RefState.create(parts.get(1), parts.get(2))); |
| 45 | } |
| 46 | return result.build(); |
| 47 | } |
| 48 | |
| 49 | public static RefState create(String ref, String sha) { |
| 50 | return new AutoValue_RefState(ref, ObjectId.fromString(sha)); |
| 51 | } |
| 52 | |
| 53 | public static RefState create(String ref, @Nullable ObjectId id) { |
| 54 | return new AutoValue_RefState(ref, firstNonNull(id, ObjectId.zeroId())); |
| 55 | } |
| 56 | |
| 57 | public static RefState of(Ref ref) { |
| 58 | return new AutoValue_RefState(ref.getName(), ref.getObjectId()); |
| 59 | } |
| 60 | |
| 61 | public byte[] toByteArray(Project.NameKey project) { |
| 62 | byte[] a = (project.toString() + ':' + ref() + ':').getBytes(UTF_8); |
| 63 | byte[] b = new byte[a.length + ObjectIds.STR_LEN]; |
| 64 | System.arraycopy(a, 0, b, 0, a.length); |
| 65 | id().copyTo(b, a.length); |
| 66 | return b; |
| 67 | } |
| 68 | |
| 69 | public static void check(boolean condition, String str) { |
| 70 | checkArgument(condition, "invalid RefState: %s", str); |
| 71 | } |
| 72 | |
| 73 | public abstract String ref(); |
| 74 | |
| 75 | public abstract ObjectId id(); |
| 76 | |
| 77 | public boolean match(Repository repo) throws IOException { |
| 78 | Ref ref = repo.exactRef(ref()); |
| 79 | ObjectId expected = ref != null ? ref.getObjectId() : ObjectId.zeroId(); |
| 80 | return id().equals(expected); |
| 81 | } |
| 82 | } |
nothing calls this directly
no outgoing calls
no test coverage detected