Skip to content
Snippets Groups Projects
Commit 9e736619 authored by Florian Heck's avatar Florian Heck
Browse files

added status bean and method for parsing status

parent 1e96e56f
No related branches found
No related tags found
No related merge requests found
...@@ -452,6 +452,20 @@ public class Repository { ...@@ -452,6 +452,20 @@ public class Repository {
return Optional.of(conflicts); return Optional.of(conflicts);
} }
public Optional<Status> getStatus() {
Optional<ExecRes> output = git.exec(dir, "status", "-z");
Function<ExecRes, Status> toStatus = execRes -> {
if (git.failed(execRes)) {
LOG.warning(() -> String.format("Failed to get status information for repo %s", this));
return null;
}
return Status.parseStatus(this, execRes.stdOut+"\0");
};
return output.map(toStatus);
}
/** /**
* Returns the {@link GitWrapper} used by this {@link Repository}. * Returns the {@link GitWrapper} used by this {@link Repository}.
* *
......
package de.uni_passau.fim.seibt.gitwrapper.repo;
import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Status {
private static final Pattern STATUS_ENTRY = Pattern.compile("(.?.) (?:.*? )?(.*?)\0");
public final String head;
public final Map<File, String> changed;
public final Map<File, String> unmerged;
private Status(String head, Map<File, String> changed, Map<File, String> unmerged) {
this.head = head;
this.changed = Collections.unmodifiableMap(changed);
this.unmerged = Collections.unmodifiableMap(unmerged);
}
public boolean isClean() {
return changed.isEmpty() && unmerged.isEmpty();
}
public boolean hasConflicts() {
return !unmerged.isEmpty();
}
@Override
public String toString() {
String out = "";
if (isClean()) {
out = "Clean working directory on commit " + head + "\n";
} else {
if (!unmerged.isEmpty()) {
out = "Unmerged files:";
out += unmerged.keySet().stream().map(File::getPath).reduce("", (list, file) -> String.join("\n", list, file));
out += "\n";
}
if (!changed.isEmpty()) {
out += "Changed files:";
out += changed.keySet().stream().map(File::getPath).reduce("", (list, file) -> String.join("\n", list, file));
out += "\n";
}
}
return out;
}
static Status parseStatus(Repository repo, String gitOutput) {
Map<File, String> changed = new HashMap<>();
Map<File, String> unmerged = new HashMap<>();
Matcher matcher = STATUS_ENTRY.matcher(gitOutput);
while (matcher.find()) {
// used new path, if file was moved
File file = new File(matcher.group(2));
String code = matcher.group(1).toUpperCase();
if (code.contains("U")) {
unmerged.put(file, code);
} else {
changed.put(file, code);
}
}
return new Status(repo.getCurrentHEAD().get().getId(), changed, unmerged);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment