-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Git commit hash version comparing
- Loading branch information
Showing
6 changed files
with
224 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/net/fabricmc/loader/impl/util/version/CommitHashVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package net.fabricmc.loader.impl.util.version; | ||
|
||
import net.fabricmc.loader.api.Version; | ||
import net.fabricmc.loader.api.VersionParsingException; | ||
import net.fabricmc.loader.impl.lib.gson.JsonReader; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.time.Instant; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CommitHashVersion implements Version { | ||
private static final Pattern GIT_COMMIT_HASH_PATTERN = Pattern.compile("^[0-9a-fA-F]{40}$"); | ||
private static final Pattern SOURCES_PATTERN = Pattern.compile("^(?:https?://)?github\\.com/([\\w-]+)/([\\w-]+)/?$"); | ||
|
||
private final String commitHash; | ||
private final Instant commitDate; | ||
|
||
public CommitHashVersion(String commitHash, String sources) throws VersionParsingException { | ||
if (!GIT_COMMIT_HASH_PATTERN.matcher(commitHash).matches()) { | ||
throw new VersionParsingException("Invalid Git commit hash"); | ||
} | ||
|
||
this.commitHash = commitHash; | ||
|
||
Matcher matcher = SOURCES_PATTERN.matcher(sources); | ||
if (!matcher.matches()) { | ||
throw new VersionParsingException("Unsupported or invalid sources link"); | ||
} | ||
|
||
String apiUrlString = String.format("https://api.github.com/repos/%s/%s/git/commits/%s", matcher.group(1), matcher.group(2), this.commitHash); | ||
URL apiUrl; | ||
try { | ||
apiUrl = new URL(apiUrlString); | ||
} catch (MalformedURLException e) { | ||
throw new VersionParsingException("Sources URL is malformed"); | ||
} | ||
|
||
String dateString; | ||
try (JsonReader reader = new JsonReader(new InputStreamReader(apiUrl.openStream()))) { | ||
reader.beginObject(); | ||
|
||
// skip sha, node id, url, HTML url, author | ||
for (int i = 0; i < 5; i++) { | ||
reader.nextName(); | ||
reader.skipValue(); | ||
} | ||
|
||
reader.nextName(); | ||
reader.beginObject(); | ||
// skip name, email | ||
for (int i = 0; i < 2; i++) { | ||
reader.nextName(); | ||
reader.skipValue(); | ||
} | ||
reader.nextName(); | ||
dateString = reader.nextString(); | ||
} catch (IOException e) { | ||
throw new VersionParsingException("Could not connect to GitHub's API"); | ||
} | ||
|
||
Instant date; | ||
try { | ||
date = Instant.parse(dateString); | ||
} catch (DateTimeParseException e) { | ||
throw new VersionParsingException("Date could not be parsed"); | ||
} | ||
|
||
this.commitDate = date; | ||
} | ||
|
||
@Override | ||
public String getFriendlyString() { | ||
return String.format("%s (%s)", this.commitHash.substring(0, 7), this.commitDate.toString()); | ||
} | ||
|
||
@Override | ||
public int compareTo(@NotNull Version other) { | ||
if (!(other instanceof CommitHashVersion)) { | ||
return this.getFriendlyString().compareTo(other.getFriendlyString()); | ||
} | ||
|
||
return this.commitDate.compareTo(((CommitHashVersion) other).commitDate); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof CommitHashVersion) { | ||
return this.commitHash.equals(((CommitHashVersion) obj).commitHash); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.getFriendlyString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters