Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions htmlContent/git-panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
<div class="btn-group git-available dropup">
<button type="button" class="git-remotes btn small dropdown-toggle" data-toggle="dropdown" title="{{TOOLTIP_PICK_REMOTE}}">
<span class="caret"></span>
<span class="ftp-prefix">FTP: </span>
<span class="git-selected-remote">&mdash;</span>
<span class="ftp-prefix" data-is-gitftp="false">FTP: </span>
<span class="git-selected-remote">&mdash; first</span>
</button>
<ul class="dropdown-menu git-remotes-dropdown"></ul>
<button title="{{TOOLTIP_PULL}}" class="btn small git-pull"><i class="octicon octicon-repo-pull"></i></button>
Expand Down
4 changes: 2 additions & 2 deletions htmlContent/git-remotes-picker.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{#hasRemotes}}
{{#remotes}}
<li class="remote">
<a href="#" data-remote-name="{{name}}" data-is-gitftp="false">
<a href="#" data-remote-name="{{name}}" data-is-gitftp="false" class="remote-name">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets use type="normal" and type="ftp" so we can add other types in the future

{{#deletable}}<span class="trash-icon remove-remote">&times;</span>{{/deletable}}
<span class="change-remote">{{name}}</span>
</a>
Expand All @@ -19,7 +19,7 @@
{{#hasFtpRemotes}}
{{#ftpRemotes}}
<li class="gitftp-remote">
<a href="#" data-remote-name="{{name}}" data-is-gitftp="true">
<a href="#" data-remote-name="{{name}}" data-is-gitftp="true" class="remote-name">
<span class="trash-icon gitftp-remove-remote">&times;</span>
<span class="change-remote">{{name}}</span>
</a>
Expand Down
1 change: 1 addition & 0 deletions src/Ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ define(function (require) {
if (response) {
return GitFtp.gitFtpRemoveScope(remoteName).then(function () {
$selectedElement.parent().remove();
console.log("find", $gitPanel.find(".git-remotes-dropdown .remote").first().find("a"));
var newRemote = $gitPanel.find(".git-remotes-dropdown .remote").first().find("a").data("remote-name");
$currentRemote.data("remote-name", newRemote).html(newRemote);
}).fail(function (err) {
Expand Down
1 change: 1 addition & 0 deletions src/Git/GitFtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ define(function (require, exports) {
usernameArgs = ["config", "--add", "git-ftp." + scope + ".user", username],
passwordArgs = ["config", "--add", "git-ftp." + scope + ".password", password];

// FIXME: `Possibly unhandled TypeError: Object [object Object] has no method 'fail'`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bluebird which we will be using soon exclusively instead of q has method .catch instead .fail

return Promise.all([
git(scopeArgs),
git(usernameArgs),
Expand Down
177 changes: 102 additions & 75 deletions src/Remotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@ define(function (require) {
Events = require("src/Events"),
EventEmitter = require("src/EventEmitter"),
Git = require("src/Git/Git"),
GitFtp = require("src/Git/GitFtp"),
Preferences = require("src/Preferences"),
Promise = require("bluebird"),
Strings = require("strings"),
Utils = require("src/Utils");
Utils = require("src/Utils"),
q = require("../thirdparty/q");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use new Promise = require("bluebird"), instead of q.
q will be removed soon completely

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem replacing q with bluebird...
With q when I use spread I get as results an object with:

result.value
result.state

So I can use result.state to check if everyting went good.
With bluebird I get only the result.value. How can I do?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the use-case with bluebird - you need to return an array:
https://github.com/zaggino/brackets-git/blob/master/src/Remotes.js#L110-L116

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already get the two arrays I need, I just don't get the state of the tasks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, use Q for now and I'll look at it when I'll be merging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to abort and throw error only if "Git.getRemotes" fails.
If instead "GitFtp.getFtpRemotes" fails I don't care and I should just show the git remotes alone.


// Templates
var gitRemotesPickerTemplate = require("text!htmlContent/git-remotes-picker.html");

// Module variables
var $selectedRemote = null,
$remotesDropdown = null;
$remotesDropdown = null,
$gitPanel = null,
gitFtpEnabled = false;

function initVariables() {
var $gitPanel = $("#git-panel");
$gitPanel = $("#git-panel");
$selectedRemote = $gitPanel.find(".git-selected-remote");
$remotesDropdown = $gitPanel.find(".git-remotes-dropdown");
gitFtpEnabled = Preferences.get("useGitFtp");
}

// Implementation
Expand All @@ -43,100 +51,119 @@ define(function (require) {

function clearRemotePicker() {
$selectedRemote
.html("&mdash;")
.data("remote", null);
.html("&mdash;")
.data("remote", null);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you format it like this? please use formatting recomenned by jquery with indentation: http://api.jquery.com/end/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it's the auto indentator, I hate it :|

}

function selectRemote(remoteName) {
if (!remoteName) {
return clearRemotePicker();
}
setDefaultRemote(remoteName);
$selectedRemote
.text(remoteName)
.data("remote", remoteName);
}

function refreshRemotesPicker() {
Git.getRemotes().then(function (remotes) {
var defaultRemoteName = getDefaultRemote(),
defaultRemote;
// Check if the selected remote is a FTP remote
var isGitFtp = ($remotesDropdown.find(".remote-name[data-is-gitftp=true][data-remote-name=\"" + remoteName + "\"]").length) ? true : false;

// empty the list first
$remotesDropdown.empty();
// Set as default remote only if is not an FTP remote
if (!isGitFtp) { setDefaultRemote(remoteName); }

// Add option to define new remote
$remotesDropdown.append("<li><a class=\"git-remote-new\"><span>" + Strings.CREATE_NEW_REMOTE + "</span></a></li>");
$remotesDropdown.append("<li class=\"divider\"></li>");
// If is an FTP remote, disable the "pull" button, if not, enable it
$gitPanel.find("git-pull").prop("disabled", isGitFtp);

if (remotes.length > 0) {
EventEmitter.emit(Events.GIT_REMOTE_AVAILABLE);
} else {
EventEmitter.emit(Events.GIT_REMOTE_NOT_AVAILABLE);
clearRemotePicker();
return;
}
// Enable the Git-FTP prefix if needed (or disable it)
$selectedRemote.prev(".ftp-prefix").attr("data-is-gitftp", isGitFtp);

// Add options to change remote
remotes.forEach(function (remoteInfo) {
var canDelete = remoteInfo.name !== "origin";
// Update remote name of $selectedRemote
$selectedRemote.text(remoteName).data("remote", remoteName);
}

var $a = $("<a/>")
.attr("href", "#")
.addClass("remote-name")
.data("remote-name", remoteInfo.name);
function refreshRemotesPicker() {
// Run both getRemotes and getFtpRemotes and render with Mustache the template
q.allSettled([Git.getRemotes(), GitFtp.getFtpRemotes()]).spread(function (remotes, ftpRemotes) {

// If both promises are fullfilled then render the template
if (remotes.state === "fulfilled" && (ftpRemotes.state === "fulfilled" || gitFtpEnabled === false)) {

// Set default remote name and cache the remotes dropdown menu
var defaultRemoteName = getDefaultRemote(),
$remotesDropdown = $gitPanel.find(".git-remotes-dropdown"),
$remotesDropdownList = "";

// Disable Git-push and Git-pull if there are not remotes defined
$gitPanel.find(".git-pull, .git-push").prop("disabled", (remotes.value.length === 0 && ftpRemotes.value.length === 0));

// Add options to change remote
remotes.value = $.map(remotes.value, function (remote) {
return {
"name": remote.name,
"deletable": (remote.name !== "origin") ? true : false
};
});

if (canDelete) {
$a.append("<span class='trash-icon remove-remote'>&times;</span>");
}
// Pass to Mustache the needed data
$remotesDropdownList = Mustache.render(gitRemotesPickerTemplate, {
Strings: Strings,
remotes: remotes.value,
ftpRemotes: ftpRemotes.value,
hasRemotes: remotes.value.length ? true : false,
hasFtpRemotes: ftpRemotes.value.length ? true : false,
gitFtpEnabled: gitFtpEnabled
});

$a.append("<span class='change-remote'>" + remoteInfo.name + "</span>");
$a.appendTo($("<li class=\"remote\"/>").appendTo($remotesDropdown));
// Inject the rendered template inside the $remotesDropdown
$remotesDropdown.html($remotesDropdownList);

if (remoteInfo.name === defaultRemoteName) {
defaultRemote = remoteInfo.name;
if (remotes.value.length !== 0) {
selectRemote(defaultRemoteName);
} else {
clearRemotePicker();
}

return $a;
});
if (ftpRemotes.state === "rejected" && gitFtpEnabled === true) {
throw ErrorHandler.showError(
ftpRemotes.reason,
"Failed to get a list of Git-FTP remotes, Git remotes was successfully initalized and are usable."
);
}

selectRemote(defaultRemote || _.first(remotes).name);
}).catch(function (err) {
ErrorHandler.showError(err, "Preparing remotes picker failed");
} else {
if (remotes.state === "rejected") {
throw ErrorHandler.showError(remotes.reason, "Failed to get a list of remotes.");
}
if (ftpRemotes.state === "rejected" && gitFtpEnabled === true) {
throw ErrorHandler.showError(ftpRemotes.reason, "Failed to get a list of Git-FTP remotes.");
}
}
});
}

function handleRemoteCreation() {
return Utils.askQuestion(Strings.CREATE_NEW_REMOTE, Strings.ENTER_REMOTE_NAME)
.then(function (name) {
return Utils.askQuestion(Strings.CREATE_NEW_REMOTE, Strings.ENTER_REMOTE_URL).then(function (url) {
return [name, url];
});
})
.spread(function (name, url) {
return Git.createRemote(name, url).then(function () {
return refreshRemotesPicker();
});
})
.catch(function (err) {
ErrorHandler.showError(err, "Remote creation failed");
.then(function (name) {
return Utils.askQuestion(Strings.CREATE_NEW_REMOTE, Strings.ENTER_REMOTE_URL).then(function (url) {
return [name, url];
});
})
.spread(function (name, url) {
return Git.createRemote(name, url).then(function () {
return refreshRemotesPicker();
});
})
.catch(function (err) {
ErrorHandler.showError(err, "Remote creation failed");
});
}

function deleteRemote(remoteName) {
return Utils.askQuestion(Strings.DELETE_REMOTE,
StringUtils.format(Strings.DELETE_REMOTE_NAME, remoteName),
{ booleanResponse: true })
.then(function (response) {
if (response === true) {
return Git.deleteRemote(remoteName).then(function () {
return refreshRemotesPicker();
});
}
})
.catch(function (err) {
ErrorHandler.logError(err);
return Utils.askQuestion(Strings.DELETE_REMOTE, StringUtils.format(Strings.DELETE_REMOTE_NAME, remoteName), { booleanResponse: true })
.then(function (response) {
if (response === true) {
return Git.deleteRemote(remoteName).then(function () {
return refreshRemotesPicker();
});
}
})
.catch(function (err) {
ErrorHandler.logError(err);
});
}

function pullFromRemote(remoteName) {
Expand Down Expand Up @@ -205,11 +232,11 @@ define(function (require) {
return new Promise(function (resolve, reject) {
Utils.askQuestion(Strings.SET_ORIGIN_URL, _.escape(Strings.URL)).then(function (url) {
Git.createRemote("origin", url)
.then(function () {
return Git.push("origin");
})
.then(resolve)
.catch(reject);
.then(function () {
return Git.push("origin");
})
.then(resolve)
.catch(reject);
});
});

Expand Down