island/src/org/atriasoft/island/actions/StatusActions.java

207 lines
7.7 KiB
Java

package org.atriasoft.island.actions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.atriasoft.island.Commands;
import org.atriasoft.island.Env;
import org.atriasoft.island.internal.Log;
import org.atriasoft.island.model.ProjectConfig;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
public class StatusActions {
private static String default_behind_message = "[DEV] update dev tag version";
private static String default_update_message = "[VERSION] update dev tag version";
private static String base_name_of_a_tagged_branch = "branch_on_tag_";
public static boolean checkout_elem(final ProjectConfig elem, final String argument_remote_name, String branch_to_checkout, final String base_display) throws Exception {
Log.verbose("checkout : " + base_display);
Path git_repo_path = Env.get_island_root_path().resolve(elem.getPath());
if (!Files.exists(git_repo_path)){
Log.warning("checkout " + base_display + " ==> repository does not exist ...");
return false;
}
// check if the repository is modify
Git git = Git.open(git_repo_path.toFile());
boolean is_modify = git.status().call().hasUncommittedChanges();
if (is_modify == true){
Log.warning("checkout " + base_display + " ==> modify data can not checkout new branch");
return false;
}
List<String> list_branch_local = Commands.get_list_branch_local(git);
String select_branch = git.getRepository().getBranch();
boolean is_tag = false;
if (branch_to_checkout.equals("__TAG__")) {
branch_to_checkout = StatusActions.base_name_of_a_tagged_branch + elem.getTag();
is_tag = true;
if (elem.isVolatile()) {
Log.info("checkout " + base_display + " ==> Can not checkout for 'volatile' repository");
return true;
}
if (elem.getTag() == null) {
Log.info("checkout " + base_display + " ==> Can not checkout for 'null' Tag");
return true;
}
}
// check if we are on the good branch{
if (branch_to_checkout.equals(select_branch)) {
Log.info("checkout " + base_display + " ==> No change already on good branch");
return true;
}
// check if we have already checkout the branch before
Log.verbose(" check : " + branch_to_checkout + " in " + list_branch_local);
if (list_branch_local.contains(branch_to_checkout)) {
git.checkout().setCreateBranch(false).setName(branch_to_checkout).call();
Log.info("checkout " + base_display + " ==> switch branch");
return true;
}
List<String> list_tags = Commands.get_tags(git);
if (list_tags.contains(branch_to_checkout)) {
is_tag = true;
if (elem.getTag() == null) {
elem.setTag(branch_to_checkout);
branch_to_checkout = StatusActions.base_name_of_a_tagged_branch + elem.getTag();
}
}
// Check if the remote branch exist ...
if (is_tag) {
Log.info("checkout " + base_display + " ==> NO remote branch");
return true;
}
List<String> list_branch_remote = Commands.get_list_branch_remote(git);
String tryRemoteBranch = elem.getSelectRemotes().getName() + "/" + branch_to_checkout;
if (list_branch_remote.contains(tryRemoteBranch)) {
Log.info(" ==> find ...");
try {
git.checkout().setCreateBranch(true).setName(tryRemoteBranch).call();
} catch (Exception ex) {
ex.printStackTrace();
Log.error("checkout " + base_display + " ==> Can not checkout to the correct branch");
return false;
}
Log.info("checkout " + base_display + " ==> create new branch");
return true;
}
// Checkout a specific tags{
if (!list_tags.contains(elem.getTag())) {
Log.info("checkout " + base_display + " ==> NO remote tags");
return true;
}
Log.info(" ==> find ...");
Log.todo("checkout " + base_display + " ==> Can not checkout to the correct tags MUST be inplemented");
return false;
/*
// checkout the new branch{
cmd = "git checkout --quiet " + elem.tag + " -b " + branch_to_checkout;
// + " --track " + elem.select_remote["name"] + "/" + branch_to_checkout;
Log.verbose("execute : " + cmd);
ret = multiprocess.run_command(cmd, cwd=git_repo_path);
if ret[1] != "" \
and ret != false{
Log.info("'" + str(ret) + "'");
Log.error("checkout " + base_display + " ==> Can not checkout to the correct tags");
return false;
}
Log.info("checkout " + base_display + " ==> create new branch: " + branch_to_checkout);
return true;
*/
}
public static int displayStatus(final ProjectConfig elem, final String argument_remote_name, final boolean argument_display_tag, final int id_element, final String base_display) throws IOException, GitAPIException {
String volatileString = "";
if (elem.isVolatile()) {
volatileString = " (volatile)";
}
Log.verbose("status : " + base_display);
//Log.debug("elem : " + str(elem))
Path git_repo_path = Env.get_island_root_path().resolve(elem.getPath());
if (!Files.exists(git_repo_path)) {
Log.print(base_display + volatileString + "\r\t\t\t\t\t\t\t\t\t" + " (not download)");
return 0;
}
Git git = Git.open(git_repo_path.toFile());
boolean is_modify = git.status().call().hasUncommittedChanges();
List<String> list_branch = Commands.get_list_branch_all(git);
String select_branch = git.getRepository().getBranch();
Log.verbose("List all branch: " + list_branch);
String tracking_remote_branch = null;
if (!select_branch.startsWith(StatusActions.base_name_of_a_tagged_branch)) {
// get tracking branch
tracking_remote_branch = Commands.get_tracking_branch(git, argument_remote_name, select_branch);
if (tracking_remote_branch == null) {
Log.print(base_display + volatileString + "\r\t\t\t\t\t\t\t (NO BRANCH)");
return 0;
}
} else {
tracking_remote_branch = select_branch.substring(StatusActions.base_name_of_a_tagged_branch.length());
}
String modify_status = " ";
if (is_modify == true) {
modify_status = " *** ";
}
Log.verbose("select branch = '" + select_branch + "' is modify : " + is_modify + " track: '" + tracking_remote_branch + "'");
List<ObjectId> ret_current_branch_sha1 = Commands.get_revision_list_to_branch(git, select_branch);
List<ObjectId> ret_track_branch_sha1 = Commands.get_revision_list_to_branch(git, tracking_remote_branch);
// remove all identical sha1 ==> not needed for this
int in_forward = 0;
for (ObjectId elem_sha1 : ret_current_branch_sha1) {
if (!ret_track_branch_sha1.contains(elem_sha1)) {
in_forward++;
}
}
int in_behind = 0;
for (ObjectId elem_sha1 : ret_track_branch_sha1) {
if (!ret_current_branch_sha1.contains(elem_sha1)) {
in_behind++;
}
}
String behind_forward_comment = "";
if (in_forward != 0) {
behind_forward_comment += "forward=" + in_forward;
}
if (in_behind != 0) {
if (in_forward != 0) {
behind_forward_comment += " ";
}
behind_forward_comment += "behind=" + in_behind;
}
if (behind_forward_comment != "") {
behind_forward_comment = "\r\t\t\t\t\t\t\t\t\t\t\t\t[" + behind_forward_comment + "]";
}
String tags_comment = "";
// check the current tags of the repository
if (argument_display_tag) {
List<String> ret_current_tags = Commands.get_tags_current(git);
Log.verbose("tags found: " + ret_current_tags);
for (String elem_tag : ret_current_tags) {
if (!tags_comment.isEmpty()) {
tags_comment += ",";
}
tags_comment += elem_tag;
}
if (!tags_comment.isEmpty()) {
tags_comment = "\r\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t[" + tags_comment + "]";
} else {
tags_comment = "\r\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t- - - - -";
}
}
Log.print(base_display + volatileString + "\r\t\t\t\t\t\t\t" + modify_status + "(" + select_branch + " -> " + tracking_remote_branch + ")" + behind_forward_comment + tags_comment);
if (is_modify) {
Commands.get_diff(git);
}
return in_behind;
}
}