diff --git a/pom.xml b/pom.xml index e23b7f1..d49fb29 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ info.istlab.IoTP IoTP jar - 0.40 + 0.41 IoTP http://maven.apache.org @@ -42,6 +42,12 @@ org.eclipse.paho.client.mqttv3 1.2.5 + + + org.eclipse.jgit + org.eclipse.jgit + 6.6.0.202305301015-r + diff --git a/src/main/java/info/istlab/IoTP/GitManager.java b/src/main/java/info/istlab/IoTP/GitManager.java new file mode 100644 index 0000000..58ba7a2 --- /dev/null +++ b/src/main/java/info/istlab/IoTP/GitManager.java @@ -0,0 +1,101 @@ + +package info.istlab.IoTP; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Set; + +import javax.swing.JOptionPane; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.StashCreateCommand; +import org.eclipse.jgit.api.Status; +import org.eclipse.jgit.api.StatusCommand; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.errors.NoWorkTreeException; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; + +public class GitManager { + + Git git = null; + Repository repository; + + public GitManager(File root) throws IOException { + repository = Git.open(new File(root.getParentFile().getAbsolutePath() + File.separator + ".git")) + .getRepository(); + } + + public void checkClean() throws NoWorkTreeException, GitAPIException { + + git = new Git(repository); + + StatusCommand statusCommand = git.status(); + Status status = statusCommand.call(); + + if (!status.isClean()) { + System.out.println("Gitのローカルリポジトリに変更点があります。"); + Set changedFiles = status.getModified(); + Set untrackedFiles = status.getUntracked(); + + System.out.println("変更されたファイル:"); + for (String filePath : changedFiles) { + System.out.println(filePath); + } + System.out.println("まだGit管理下にないファイル:"); + for (String filePath : untrackedFiles) { + System.out.println(filePath); + } + int res = JOptionPane.showConfirmDialog(Launcher.theapp, "教員提供のSampleSrcが修正されています。\n\n" + // + "初期状態に戻しますか?\n\n" + // + "「はい」を押すと,修正を削除して初期状態に戻します。\n" + // + "「いいえ」「取消」を押すと,なにもしません。\n\n" + // + "あなたがまだ何も作業しておらず,これから実験を\n" + // + "始めるときだけ「はい」を押してください。\n作業の続きを行うときは「いいえ」を押してください。", "教員が提供するサンプルソースコードが修正されています", + JOptionPane.YES_NO_CANCEL_OPTION); + if (res == JOptionPane.YES_OPTION) { + stash(); + } else { + + } + } else { + System.out.println("No uncommitted changes in the local repository."); + } + if (git != null) + git.close(); + showNotice(); + } + + public void stash() throws GitAPIException { + git = new Git(repository); + + StashCreateCommand stashCreateCommand = git.stashCreate(); + stashCreateCommand.setIncludeUntracked(true); + + SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss"); + + stashCreateCommand.setWorkingDirectoryMessage(sdf.format(new Date()) + " に退避した作業"); + RevCommit stashCommit = stashCreateCommand.call(); + + if (stashCommit != null) { + System.out.println("Stash created. Stash commit ID: " + stashCommit.getName()); + } else { + System.out.println("No local changes to stash."); + } + } + + public void showNotice() { + JOptionPane.showMessageDialog(Launcher.theapp, "(1) デバイスを認識すると,タイトルバーに COM3 のように表示します。\n"+// + " 複数みえるときはWindowsのデバイスマネージャ>ポートでM5Stickのポート番号を調べたうえで,\n"+// + " このプログラムの Serialメニュー から,書き込み先ポートを選んでください。\n\n"+// + "(2) デバイスを認識しないときは,このプログラムを再起動してみてください。\n"+// + " (Appメニュー>このプログラムを再起動する)\n\n"+// + "(3) デバイスを返却するときは,FactoryTest を書き込んでおいてください。\n"+// + " (Fileメニュー>FactoryTestを書き込む)\n\n"+ // + " デバイスは充電した状態で返却してください。"// + , "IoTP実験における大事な注意点です。たくさんありますがしっかり読んでください。", JOptionPane.WARNING_MESSAGE); + + } +} diff --git a/src/main/java/info/istlab/IoTP/Launcher.java b/src/main/java/info/istlab/IoTP/Launcher.java index aee5eb9..ed47df2 100644 --- a/src/main/java/info/istlab/IoTP/Launcher.java +++ b/src/main/java/info/istlab/IoTP/Launcher.java @@ -25,13 +25,17 @@ import javax.swing.event.MouseInputListener; import javax.swing.tree.TreePath; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.errors.NoWorkTreeException; + public class Launcher extends JFrame implements MouseInputListener, KeyListener, Runnable { public static Launcher theapp; - public static String version = "0.40"; + public static String version = "0.41"; static int reboot_msec = 2000; // JPanel mainP; File root; + GitManager gitrepom; JTree tree; Hashtable file2editor; SerialChecker serialChecker; @@ -47,7 +51,12 @@ theapp = this; root = _root; - + try { + gitrepom = new GitManager(root); + gitrepom.checkClean(); + } catch (IOException | NoWorkTreeException | GitAPIException e) { + e.printStackTrace(); + } // Create a TreeModel object to represent our tree of files FileTreeModel model = new FileTreeModel(root, false); @@ -144,7 +153,7 @@ menu.add(mi); mi = new JMenuItem(isEnglish ? "MQTT Client" : "MQTTクライアント"); - mi.addActionListener(ae -> MqttWindow.create().setVisible(true) ); + mi.addActionListener(ae -> MqttWindow.create().setVisible(true)); menu.add(mi); setJMenuBar(menuBar); @@ -157,13 +166,13 @@ } - void updateSerialMenu(ArrayList validSerialNames){ + void updateSerialMenu(ArrayList validSerialNames) { serialMenu.removeAll(); JMenuItem mi = new JMenuItem("シリアルモニタを開く"); mi.addActionListener(ae -> SerialWindow.invoke(true)); serialMenu.add(mi); - if (validSerialNames.size()>0){ + if (validSerialNames.size() > 0) { serialMenu.addSeparator(); JMenuItem label = new JMenuItem("使用するシリアルポートを選択"); label.setEnabled(false); @@ -171,22 +180,24 @@ } // App.serialName があるかチェック。なければ nullにする。 boolean hasCurrent = false; // 以前の設定があるので、変更しない - if (validSerialNames.size()==0){ + if (validSerialNames.size() == 0) { App.serialName = null; - } else if (validSerialNames.size()==1){ + } else if (validSerialNames.size() == 1) { App.serialName = validSerialNames.get(0); } else { String currentName = App.serialName; - for(String s: validSerialNames){ - if (s.equals(currentName)) hasCurrent = true; + for (String s : validSerialNames) { + if (s.equals(currentName)) + hasCurrent = true; } - if (!hasCurrent) App.serialName = validSerialNames.get(0);//以前のSerialがなくなってたら、最初(0)に再設定する + if (!hasCurrent) + App.serialName = validSerialNames.get(0);// 以前のSerialがなくなってたら、最初(0)に再設定する } - for(String s: validSerialNames){ + for (String s : validSerialNames) { JCheckBoxMenuItem jcbmi = new JCheckBoxMenuItem(s); serialMenu.add(jcbmi); - if (s.equals(App.serialName)){ + if (s.equals(App.serialName)) { jcbmi.setSelected(true); jcbmi.addActionListener(e -> dummychooseSerial(e)); // jcbmi.setEnabled(false); @@ -203,11 +214,13 @@ serialMenu.add(mi); } - void dummychooseSerial(ActionEvent e){ + + void dummychooseSerial(ActionEvent e) { JCheckBoxMenuItem mi = (JCheckBoxMenuItem) e.getSource(); mi.setSelected(true); } - void chooseSerial(ActionEvent e){ + + void chooseSerial(ActionEvent e) { App.serialName = e.getActionCommand(); updateSerialMenu(serialChecker.validSerialNames); } @@ -278,12 +291,13 @@ openFolder(new File(App.workingDir)); // openFolder(App.execPath.getParent().toFile()); } + public void openExecPath() { // openFolder(new File(App.workingDir)); openFolder(App.execPath.getParent().toFile()); } - public void restart(){ + public void restart() { reboot_msec = 0; App.reboot(App.execPath.toString()); } @@ -412,8 +426,7 @@ } public void updateTitle(String string) { - setTitle("IoTP v" + version + " "+string); + setTitle("IoTP v" + version + " " + string); } - }