Skip to content

Commit

Permalink
Merge pull request #1 from sm-Fifteen/ng-service
Browse files Browse the repository at this point in the history
Not master
  • Loading branch information
sm-Fifteen authored Jan 17, 2017
2 parents 7cdbdaa + 8468f33 commit 7fb3e06
Show file tree
Hide file tree
Showing 15 changed files with 511 additions and 173 deletions.
127 changes: 31 additions & 96 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ const Promise = require('promise')
let mainWindow

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))

// Open the DevTools.
mainWindow.webContents.openDevTools()
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'renderer/index.html'),
protocol: 'file:',
slashes: true
}))

// Open the DevTools.
mainWindow.webContents.openDevTools()

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
require('./main/gitface.js')(electron, app, mainWindow);
}

// This method will be called when Electron has finished
Expand All @@ -42,84 +44,17 @@ app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.


const ipc = require('ipc-promise');
const dialog = require('electron').dialog;
const fs = require('fs');
const NodeGit = require("nodegit");

var currentDirectory = ".";
var currentRepo;

ipc.on('open-directory-picker', function(ev) {
return new Promise(function (fulfill, reject){
var filePaths = dialog.showOpenDialog(mainWindow, {
defaultPath: currentDirectory,
properties: ["openDirectory", "createDirectory"],
})

if(!filePaths) {
reject();
} else {
fulfill(filePaths[0]);
}
});
});

ipc.on('change-directory', function(pathToRepo) {
return safelyOpenRepo(pathToRepo).then(function(repoObject){
currentRepo = repoObject;
currentDirectory = pathToRepo;

return currentDirectory;
}, function(errorMessage) {
dialog.showErrorBox('Error while opening repository', errorMessage);
// Nothing else
})
})

ipc.on('get-repo-path', function() {
return Promise.resolve({
cwd: currentDirectory,
gitPath: (currentRepo) ? currentRepo.path() : undefined,
})
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})

function safelyOpenRepo(pathToRepo) {
// Libgit2 doesn't provide error objects on its public API.
// We need our own logic to safely open and clone repos

var fsStat = Promise.denodeify(require('fs').stat);

return fsStat(pathToRepo).then(function(stats){
if (stats.isDirectory()) {
return pathToRepo;
} else {
throw "Path is not a valid directory";
}
}).then(function(pathToRepo) {
return NodeGit.Repository.open(pathToRepo);
}).then(function (repoObject) {
return repoObject;
}, function(error){
return; // No parameter, not a repo
});
}
80 changes: 80 additions & 0 deletions main/gitface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module.exports = function(electron, app, mainWindow) {
const Promise = require('promise')
const ipc = require('electron').ipcMain
const dialog = electron.dialog;
const fs = require('fs');
const NodeGit = require("nodegit");
const Repository = require("./repository.js")

var repo = undefined;

ipc.on('open-directory-picker', function(ev) {
var filePaths = dialog.showOpenDialog(mainWindow, {
defaultPath: (repo || {}).path || "",
properties: ["openDirectory", "createDirectory"],
})

if(!filePaths) {
ev.returnValue = undefined;
} else {
ev.returnValue = filePaths[0];
}
});

ipc.on('change-directory', function(ev, path) {
repo = new Repository(path);

repo.testRepo().then(function(repoData) {
ev.sender.send('changed-directory', repoData);
});
})

ipc.on('get-files', function(ev) {

})

ipc.on('get-ref-data', function(ev) {
// refData should mirror the git dir hierarchy
var refData = {}

Promise.all([
repo.getHead().then(function(head) {
refData['HEAD'] = head;
}),
repo.getReferences().then(function(refs) {
refData['refs'] = refs;
})
]).then(function(){
ev.sender.send('reply-ref-data', refData)
})
})

// First commit should be the last commit we know, so we get the current head at least once,
// then the renderer should query for the last commit we sent it.
ipc.on('get-commit-chain', function(ev, firstCommit, rangeLimit, includeFirst) {
console.log("Got message")
repo.getCommitChain(firstCommit, rangeLimit, includeFirst).then(function(serializedCommitList){
ev.sender.send('reply-commit-chain', serializedCommitList)
}).catch(function(e){
console.error(e);
});
})

ipc.on('git-status', function(ev) {
return repo.getDirectory().then(NodeGit.Repository.open).then(function(repoObject) {
return repoObject.getStatus();
}).then(serializeStatus);
})

function serializeStatus(statusList) {
var statusMap = [];
statusList.forEach(function(statusFile) {
statusMap.push({
path: statusFile.path(),
name: path.basename(statusFile.path()),
status: statusFile.status(),
});
});
return statusMap;
}
}
Loading

0 comments on commit 7fb3e06

Please sign in to comment.