Skip to content

Add Git to IDE #4816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@
<classpathentry kind="lib" path="test-lib/fest-swing-1.2.jar"/>
<classpathentry kind="lib" path="test-lib/fest-util-1.1.2.jar"/>
<classpathentry kind="lib" path="test-lib/jcip-annotations-1.0.jar"/>
<classpathentry kind="lib" path="../app/lib/slf4j-simple-1.7.20.jar"/>
<classpathentry kind="lib" path="../app/lib/slf4j-api-1.7.20.jar"/>
<classpathentry kind="lib" path="../app/lib/org.eclipse.jgit-4.2.0.201601211800-r.jar"/>
</classpath>
Binary file added app/lib/org.eclipse.jgit-4.2.0.201601211800-r.jar
Binary file not shown.
Binary file added app/lib/slf4j-api-1.7.20.jar
Binary file not shown.
Binary file added app/lib/slf4j-simple-1.7.20.jar
Binary file not shown.
176 changes: 138 additions & 38 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@
import java.util.function.Predicate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jgit.revwalk.*;


import static processing.app.I18n.tr;
import static processing.app.Theme.scale;


/**
* Main editor panel for the Processing Development Environment.
*/
Expand Down Expand Up @@ -199,11 +205,16 @@ public boolean test(Sketch sketch) {
Runnable exportHandler;
private Runnable exportAppHandler;

private File dirToGit = null;
private GitManager git = new GitManager();



public Editor(Base ibase, File file, int[] storedLocation, int[] defaultLocation, Platform platform) throws Exception {
super("Arduino");
this.base = ibase;
this.platform = platform;
dirToGit = file.getParentFile();

Base.setIcon(this);

Expand Down Expand Up @@ -356,7 +367,14 @@ public void windowDeactivated(WindowEvent e) {

// Open the document that was passed in
boolean loaded = handleOpenInternal(file);
if (!loaded) sketch = null;
if (!loaded) {
sketch = null;
}
else
{
git.gitInit(dirToGit);
}

}


Expand Down Expand Up @@ -425,6 +443,7 @@ public boolean importData(JComponent src, Transferable transferable) {
}

private void setPlacement(int[] storedLocation, int[] defaultLocation) {

if (storedLocation.length > 5 && storedLocation[5] != 0) {
setExtendedState(storedLocation[5]);
setPlacement(defaultLocation);
Expand Down Expand Up @@ -467,7 +486,6 @@ protected int[] getPlacement() {
* with things in the Preferences window.
*/
public void applyPreferences() {

// apply the setting for 'use external editor'
boolean external = PreferencesData.getBoolean("editor.external");

Expand Down Expand Up @@ -570,9 +588,89 @@ public void menuSelected(MenuEvent e) {
menubar.add(toolsMenu);

menubar.add(buildHelpMenu());

menubar.add(buildGitMenu());

setJMenuBar(menubar);
}

private JMenu buildGitMenu()
{
JMenu gitMenu = new JMenu("Git");
gitMenu.setMnemonic(KeyEvent.VK_G);

JMenuItem commitItem = newJMenuItem("Commit", 'i');
commitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!validationUntitled()){

String commitTitle = JOptionPane.showInputDialog(new JFrame(),"Commit Label","Commit",1);
String result = "Commit aborted";
String whites = "^\\s*$";

if(commitTitle != null && !matchRegex(whites,commitTitle)){
result = git.gitCommit(commitTitle, dirToGit.listFiles());
}
System.out.println(result);
}
}
});

JMenuItem logItem = newJMenuItem("Show Log", 'g');
logItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!validationUntitled()){
printGitLog(git.gitLog());
}


}
});


gitMenu.add(commitItem);
gitMenu.add(logItem);


return gitMenu;
}

private boolean validationUntitled(){
if(untitled){
System.out.println("the Files must be saved to use Git");
}
return untitled;
}

private void printGitLog(Iterable<RevCommit> logs){
System.out.println("\nLog:");

try{
for (RevCommit log : logs)
{
System.out.println(log.toString());
System.out.println(log.getAuthorIdent());
System.out.println(log.getFullMessage());
System.out.println("\n");
}
}catch (NullPointerException ex)
{
System.out.println("There is no log to show");
}


}

private boolean matchRegex(String pattern, String line)
{
Pattern patternObj = Pattern.compile(pattern);
Matcher matchObj = patternObj.matcher(line);


return matchObj.find();
}

private JMenu buildFileMenu() {
JMenuItem item;
Expand Down Expand Up @@ -693,7 +791,7 @@ public void actionPerformed(ActionEvent e) {
}
return fileMenu;
}

///limite
public void rebuildRecentSketchesMenu() {
recentSketchesMenu.removeAll();
for (JMenuItem recentSketchMenuItem : base.getRecentSketchesMenuItems()) {
Expand Down Expand Up @@ -741,13 +839,13 @@ public void actionPerformed(ActionEvent e) {
});
sketchMenu.add(item);

// item = new JMenuItem("Stop");
// item.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// handleStop();
// }
// });
// sketchMenu.add(item);
// item = new JMenuItem("Stop");
// item.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// handleStop();
// }
// });
// sketchMenu.add(item);

sketchMenu.addSeparator();

Expand Down Expand Up @@ -1397,23 +1495,23 @@ public void actionPerformed(ActionEvent e) {
JMenuItem copyForumItem = newJMenuItemShift(tr("Copy for Forum"), 'C');
copyForumItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
new DiscourseFormat(Editor.this, false).show();
// }
// });
// }
// });
}
});
menu.add(copyForumItem);

JMenuItem copyHTMLItem = newJMenuItemAlt(tr("Copy as HTML"), 'C');
copyHTMLItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
new DiscourseFormat(Editor.this, true).show();
// }
// });
// }
// });
}
});
menu.add(copyHTMLItem);
Expand Down Expand Up @@ -1695,18 +1793,18 @@ public void setText(String what) {
* Called to update the text but not switch to a different set of code
* (which would affect the undo manager).
*/
// public void setText2(String what, int start, int stop) {
// beginCompoundEdit();
// textarea.setText(what);
// endCompoundEdit();
//
// // make sure that a tool isn't asking for a bad location
// start = Math.max(0, Math.min(start, textarea.getDocumentLength()));
// stop = Math.max(0, Math.min(start, textarea.getDocumentLength()));
// textarea.select(start, stop);
//
// textarea.requestFocus(); // get the caret blinking
// }
// public void setText2(String what, int start, int stop) {
// beginCompoundEdit();
// textarea.setText(what);
// endCompoundEdit();
//
// // make sure that a tool isn't asking for a bad location
// start = Math.max(0, Math.min(start, textarea.getDocumentLength()));
// stop = Math.max(0, Math.min(start, textarea.getDocumentLength()));
// textarea.select(start, stop);
//
// textarea.requestFocus(); // get the caret blinking
// }


public String getSelectedText() {
Expand Down Expand Up @@ -1777,7 +1875,7 @@ protected void setCode(final SketchCodeDocument codeDoc) {
bl.printStackTrace();
}
// set up this guy's own undo manager
// code.undo = new UndoManager();
// code.undo = new UndoManager();

codeDoc.setDocument(document);
}
Expand Down Expand Up @@ -2003,12 +2101,12 @@ public void run() {
* Implements Sketch &rarr; Stop, or pressing Stop on the toolbar.
*/
private void handleStop() { // called by menu or buttons
// toolbar.activate(EditorToolbar.STOP);
// toolbar.activate(EditorToolbar.STOP);

internalCloseRunner();

toolbar.deactivateRun();
// toolbar.deactivate(EditorToolbar.STOP);
// toolbar.deactivate(EditorToolbar.STOP);

// focus the PDE again after quitting presentation mode [toxi 030903]
toFront();
Expand Down Expand Up @@ -2299,6 +2397,8 @@ public boolean handleSaveAs() {
// the Save As method of the Sketch object, since that's the
// only one who knows whether something was renamed.
//sketchbook.rebuildMenusAsync();
dirToGit = sketch.getData().getDataFolder().getParentFile();
git.gitInit(dirToGit);
} else {
statusNotice(tr("Save Canceled."));
return false;
Expand Down Expand Up @@ -2794,10 +2894,10 @@ public void statusError(String what) {
*/
public void statusError(Exception e) {
e.printStackTrace();
// if (e == null) {
// System.err.println("Editor.statusError() was passed a null exception.");
// return;
// }
// if (e == null) {
// System.err.println("Editor.statusError() was passed a null exception.");
// return;
// }

if (e instanceof RunnerException) {
RunnerException re = (RunnerException) e;
Expand Down Expand Up @@ -2843,7 +2943,7 @@ public void statusError(Exception e) {
}
statusError(mess);
}
// e.printStackTrace();
// e.printStackTrace();
}


Expand Down
78 changes: 78 additions & 0 deletions app/src/processing/app/GitManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package processing.app;

/**
* Created by carlos on 1/04/16.
*/

import java.io.IOException;
import java.io.File;
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.api.errors.*;
import org.eclipse.jgit.revwalk.*;


public class GitManager {
private Git git;

public String gitInit(File dirUrl)
{

try {
System.out.println(dirUrl.getAbsolutePath());
git = Git.init().setDirectory( dirUrl ).call();

} catch (Exception e) {
return "Something went wrong, check if you have git already installed ";
}

return "Git initialized successfully";

}

private boolean gitStage(File[] FilesToStage)
{
for(File f : FilesToStage)
{
try {
git.add().addFilepattern(f.getName()).call();
}catch(Exception e){
return false;
}

}
return true;
}

public String gitCommit(String coment, File[] fileToCommit)
{

if(gitStage(fileToCommit))
{
try{
git.commit().setMessage( coment ).call();
}catch(Exception e)
{
return "There was a problem while committing the files";
}
return "File Committed successfully";
}
else
{
return "There was a problem while staging the files";
}
}

public Iterable<RevCommit> gitLog()
{
Iterable<RevCommit> iterable = null;
try {

iterable = git.log().call();

}catch(GitAPIException ex)
{ }

return iterable;
}

}
Loading