Skip to content

Commit 5455169

Browse files
committed
Merge pull request #2 from mapaiva/v0.0.2
V0.0.2
2 parents 8221ce2 + 461aff0 commit 5455169

File tree

12 files changed

+875
-177
lines changed

12 files changed

+875
-177
lines changed

Gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ module.exports = function(grunt) {
2323

2424
'./libraries/**',
2525

26+
'./language/**',
27+
2628
'./node_modules/**',
2729

2830
'!./node_modules/grunt*/**',

app/app.js

Lines changed: 126 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ var
99
GIT_REPO_NAME = require('./node_modules/git-repo-name'),
1010

1111
// Git class that perfoms git commands
12-
GIT = require('./app/core/git');
12+
GIT = require('./app/core/git'),
13+
14+
// Locale language
15+
LANG = window.navigator.userLanguage || window.navigator.language,
16+
17+
// Messages and labels of the application
18+
MSGS;
1319

1420
WIN.focus();
1521

@@ -20,12 +26,21 @@ process.on('uncaughtException', function(err) {
2026
alert(err);
2127
});
2228

29+
/* Get the locale language */
30+
try {
31+
MSGS = require('./language/'.concat(LANG).concat('.json'));
32+
} catch (err){
33+
MSGS = require('./language/en.json');
34+
}
35+
2336
/* AngularJS app init */
2437
(function () {
2538
var app = angular.module('gitpie', ['components', 'attributes', 'header', 'content']);
2639

27-
app.factory('CommomService', function () {
28-
var storagedRepos = JSON.parse(localStorage.getItem('repos')) || [],
40+
app.factory('CommomService', function ($rootScope) {
41+
var repositoriesStr = localStorage.getItem('repos'),
42+
43+
repositories = JSON.parse(repositoriesStr) || {},
2944

3045
findWhere = function (array, object) {
3146

@@ -39,47 +54,134 @@ process.on('uncaughtException', function(err) {
3954
return null;
4055
},
4156

42-
repositories = [];
57+
saveRepository = function (repository) {
58+
var storagedRepositories = JSON.parse(repositoriesStr) || {};
59+
60+
storagedRepositories.github = storagedRepositories.github || [];
61+
storagedRepositories.bitbucket = storagedRepositories.bitbucket || [];
62+
storagedRepositories.outhers = storagedRepositories.outhers || [];
63+
64+
switch (repository.type) {
65+
case 'GITHUB':
66+
storagedRepositories.github.push(repository);
67+
break;
68+
69+
case 'BITBUCKET':
70+
storagedRepositories.bitbucket.push(repository);
71+
break;
72+
73+
default:
74+
storagedRepositories.outhers.push(repository);
75+
break;
76+
}
4377

44-
storagedRepos.forEach(function (item) {
45-
delete item.$$hashKey;
46-
delete item.selected;
78+
localStorage.setItem('repos', JSON.stringify(storagedRepositories));
79+
repositoriesStr = JSON.stringify(storagedRepositories);
80+
};
4781

48-
repositories.push(item);
49-
});
82+
repositories.github = repositories.github || [];
83+
repositories.bitbucket = repositories.bitbucket || [];
84+
repositories.outhers = repositories.outhers || [];
85+
86+
if (repositories.github.length > 0 || repositories.bitbucket.length > 0 || repositories.outhers.length > 0) {
87+
repositories.isEmpty = false;
88+
} else {
89+
repositories.isEmpty = true;
90+
}
91+
92+
// Set the application messages globally
93+
$rootScope.MSGS = MSGS;
5094

5195
return {
5296

53-
addRepository: function (repositoryPath) {
97+
addRepository: function (repositoryPath, callback) {
5498

5599
if (repositoryPath) {
56-
var repositoryExists = findWhere(repositories, { path: repositoryPath});
57100

58101
// Easter egg :D
59-
if (repositoryPath.toLowerCase() === 'i have no idea') {
60-
alert('It happends with me all the time too. But let\'s try find your project again!');
102+
if (repositoryPath.toLowerCase() === $rootScope.MSGS['i have no idea']) {
103+
alert($rootScope.MSGS['It happends with me all the time too. But lets\'s try find your project again!']);
61104

62-
} else if (!repositoryExists) {
63-
var name = GIT_REPO_NAME(repositoryPath);
105+
} else {
106+
var name = GIT_REPO_NAME(repositoryPath),
107+
type,
108+
index,
109+
repositoryExists,
110+
repository;
64111

65112
if (name) {
66-
var index = repositories.push({
67-
name: name,
68-
path: repositoryPath
69-
});
70113

71-
localStorage.setItem('repos', JSON.stringify(repositories));
114+
GIT.listRemotes(repositoryPath, function (err, stdout) {
115+
116+
if (stdout.toLowerCase().indexOf('github.com') != -1) {
117+
repositoryExists = findWhere(repositories.github, { path: repositoryPath});
118+
type = 'github';
119+
120+
} else if (stdout.toLowerCase().indexOf('bitbucket.org') != -1) {
121+
repositoryExists = findWhere(repositories.bitbucket, { path: repositoryPath});
122+
type = 'bitbucket';
123+
124+
} else {
125+
repositoryExists = findWhere(repositories.outhers, { path: repositoryPath});
126+
type = 'outhers';
127+
}
128+
129+
if (!repositoryExists) {
130+
131+
index = repositories[type].push({
132+
name: name,
133+
path: repositoryPath,
134+
type : type.toUpperCase()
135+
});
136+
137+
repository = repositories[type][index - 1];
138+
139+
saveRepository(repository);
140+
} else {
141+
repository = repositoryExists;
142+
}
143+
144+
repositories.isEmpty = false;
145+
146+
if (callback && typeof callback == 'function') {
147+
callback.call(this, repository);
148+
}
149+
});
72150

73-
return repositories[index-1];
74151
} else {
75-
alert('Nothing for me here.\n The folder ' + repositoryPath + ' is not a git project');
152+
alert($rootScope.MSGS['Nothing for me here.\n The folder {folder} is not a git project'].replace('{folder}', repositoryPath));
76153
}
77-
} else {
78-
return repositoryExists;
79154
}
80155
}
81156
},
82157

158+
// Return true if the repository was selected and false case not
159+
removeRepository: function (repositoryType, index) {
160+
var storagedRepositories = JSON.parse(repositoriesStr) || {},
161+
type = repositoryType.toLowerCase(),
162+
removedRepository;
163+
164+
storagedRepositories.github = storagedRepositories.github || [];
165+
storagedRepositories.bitbucket = storagedRepositories.bitbucket || [];
166+
storagedRepositories.outhers = storagedRepositories.outhers || [];
167+
168+
storagedRepositories[type].splice(index, 1);
169+
removedRepository = repositories[type].splice(index, 1);
170+
171+
localStorage.setItem('repos', JSON.stringify(storagedRepositories));
172+
repositoriesStr = JSON.stringify(storagedRepositories);
173+
174+
return removedRepository[0].selected;
175+
},
176+
177+
closeAnyContextMenu: function () {
178+
var contextMenus = document.querySelectorAll('.context-menu');
179+
180+
angular.forEach(contextMenus, function (item) {
181+
document.body.removeChild(item);
182+
});
183+
},
184+
83185
repositories: repositories
84186
};
85187
});

app/core/git.js

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ Git.prototype.getCurrentBranch = function (path, callback) {
7373
Git.prototype.getCommitHistory = function (opts, callback) {
7474

7575
exec(
76-
"git --no-pager log -n 50 --pretty=format:%an-gtseparator-%cr-gtseparator-%h-gtseparator-%s" + (opts.skip ? ' --skip '.concat(opts.skip) : '' ),
76+
"git --no-pager log -n 50 --pretty=format:%an-gtseparator-%cr-gtseparator-%h-gtseparator-%s-gtseparator-%b-pieLineBreak-" + (opts.skip ? ' --skip '.concat(opts.skip) : '' ),
7777

7878
{ cwd: opts.path, env: ENV },
7979

8080
function (error, stdout, stderr) {
81-
var lines = stdout.split('\n'),
81+
var lines = stdout.split('-pieLineBreak-'),
8282
historyList = [],
8383
err = null;
8484

@@ -87,14 +87,18 @@ Git.prototype.getCommitHistory = function (opts, callback) {
8787
} else {
8888

8989
for (var i = 0; i < lines.length; i++) {
90-
var historyItem = lines[i].split('-gtseparator-');
9190

92-
historyList.push({
93-
user: historyItem[0],
94-
date: historyItem[1],
95-
hash: historyItem[2],
96-
message: historyItem[3]
97-
});
91+
if (lines[i] !== '') {
92+
var historyItem = lines[i].split('-gtseparator-');
93+
94+
historyList.push({
95+
user: historyItem[0],
96+
date: historyItem[1],
97+
hash: historyItem[2],
98+
message: historyItem[3],
99+
body: historyItem[4]
100+
});
101+
}
98102
}
99103
}
100104

@@ -235,7 +239,7 @@ Git.prototype.getUnsyncFileDiff = function (opts, callback) {
235239

236240
Git.prototype.getFileDiff = function (opts, callback) {
237241

238-
exec('git log -p -1 ' + opts.hash + ' -- ' + opts.file, { cwd: opts.path, env: ENV}, function (error, stdout, stderr) {
242+
exec('git log --format=\'%N\' -p -1 ' + opts.hash + ' -- ' + opts.file, { cwd: opts.path, env: ENV}, function (error, stdout, stderr) {
239243
var err = null;
240244

241245
if (error !== null) {
@@ -248,13 +252,20 @@ Git.prototype.getFileDiff = function (opts, callback) {
248252
});
249253
};
250254

251-
Git.prototype.sync = function (path, callback) {
255+
Git.prototype.sync = function (opts, callback) {
252256

253-
exec('git pull', { cwd: path, env: ENV}, function (error, stdout, stderr) {
257+
exec('git pull', { cwd: opts.path, env: ENV}, function (error, stdout, stderr) {
254258
var err = null;
255259

256260
if (error !== null) {
257261
err = error;
262+
} else {
263+
264+
try {
265+
execSync('git push origin ' + opts.branch, { cwd: opts.path, env: ENV});
266+
} catch (pushError) {
267+
err = pushError.message;
268+
}
258269
}
259270

260271
if (callback && typeof callback == 'function') {
@@ -344,4 +355,48 @@ Git.prototype.getCommitCount = function (path, callback) {
344355
});
345356
};
346357

358+
Git.prototype.listRemotes = function (path, callback) {
359+
360+
exec('git remote -v', { cwd: path, env: ENV}, function (error, stdout, stderr) {
361+
var err = null;
362+
363+
if (error !== null) {
364+
err = error.message;
365+
}
366+
367+
if (callback && typeof callback == 'function') {
368+
callback.call(this, err, stdout);
369+
}
370+
});
371+
};
372+
373+
Git.prototype.discartChangesInFile = function (path, opts) {
374+
var command;
375+
376+
opts = opts || {};
377+
378+
if (opts.isUnknow) {
379+
command = 'git clean -df '.concat(opts.file);
380+
} else {
381+
command = 'git checkout -- '.concat(opts.file);
382+
}
383+
384+
if (opts.forceSync) {
385+
return execSync(command, { cwd: path, env: ENV });
386+
} else {
387+
388+
exec(command, { cwd: path, env: ENV}, function (error, stdout, stderr) {
389+
var err = null;
390+
391+
if (error !== null) {
392+
err = error.message;
393+
}
394+
395+
if (opts.callback && typeof opts.callback == 'function') {
396+
opts.callback.call(this, err, stdout);
397+
}
398+
});
399+
}
400+
};
401+
347402
module.exports = new Git();

app/frontend/modules/components.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,39 @@
3838
templateUrl: 'app/frontend/view/components/pane.html',
3939
replace: true
4040
};
41+
})
42+
43+
.directive('pieDialog', function() {
44+
var template = [
45+
'<div class="modal" ng-show="show">',
46+
'<div class="modal-overlay" ng-click="hideModal()"></div>',
47+
'<div class="modal-dialog" ng-style="dialogStyle">',
48+
'<div class="modal-close" ng-click="hideModal()"><span class="octicon octicon-x"></span></div>',
49+
'<div class="modal-dialog-content" ng-transclude></div>',
50+
'</div>',
51+
'</div>'
52+
].join('');
53+
54+
return {
55+
restrict: 'E',
56+
scope: {
57+
show: '='
58+
},
59+
replace: true, // Replace with the template below
60+
transclude: true, // we want to insert custom content inside the directive
61+
link: function($scope, $element, $attrs) {
62+
63+
if ($attrs.width)
64+
$scope.dialogStyle.width = $attrs.width;
65+
if ($attrs.height)
66+
$scope.dialogStyle.height = $attrs.height;
67+
68+
$scope.hideModal = function() {
69+
$scope.show = false;
70+
};
71+
},
72+
template: template
73+
};
4174
});
4275

4376
})();

0 commit comments

Comments
 (0)