-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
143 lines (114 loc) · 3.77 KB
/
git.js
File metadata and controls
143 lines (114 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use strict";
var util = require("util");
var Plugin = require("../cloud9.core/plugin");
var c9util = require("../cloud9.core/util");
var name = "git";
var ProcessManager;
var EventBus;
module.exports = function setup(options, imports, register) {
ProcessManager = imports["process-manager"];
EventBus = imports.eventbus;
imports.ide.register(name, GitPlugin, register);
};
var GitPlugin = function(ide, workspace) {
Plugin.call(this, ide, workspace);
this.pm = ProcessManager;
this.eventbus = EventBus;
this.workspaceId = workspace.workspaceId;
this.channel = this.workspaceId + "::git";
this.hooks = ["command"];
this.name = "git";
this.gitEnv = {
GIT_ASKPASS: "/bin/echo",
EDITOR: "",
GIT_EDITOR: ""
};
this.processCount = 0;
};
util.inherits(GitPlugin, Plugin);
(function() {
this.init = function() {
var self = this;
this.eventbus.on(this.channel, function(msg) {
if (msg.type == "shell-start")
self.processCount += 1;
if (msg.type == "shell-exit")
self.processCount -= 1;
self.ide.broadcast(JSON.stringify(msg), self.name);
});
};
this.command = function (user, message, client) {
var self = this;
var cmd = message.command ? message.command.toLowerCase() : "";
if (cmd !== "git")
return false;
if (typeof message.protocol == "undefined")
message.protocol = "client";
// git encourages newlines in commit messages; see also #678
// so if a \n is detected, treat them properly as newlines
if (message.argv[1] == "commit" && message.argv[2] == "-m") {
if (message.argv[3].indexOf("\\n") > -1) {
message.argv[3] = message.argv[3].replace(/\\n/g,"\n");
}
}
this.pm.spawn("shell", {
command: "git",
args: message.argv.slice(1),
cwd: message.cwd,
env: this.gitEnv,
extra: message.extra,
encoding: "ascii"
}, this.channel, function(err, pid) {
if (err)
self.error(err, 1, message, client);
});
return true;
};
var githelp = null;
var commandsMap = {
"default": {
"commands": {
"[PATH]": {"hint": "path pointing to a folder or file. Autocomplete with [TAB]"}
}
}
};
this.$commandHints = function(commands, message, callback) {
var self = this;
if (!githelp) {
githelp = {};
this.pm.exec("shell", {
command: "git",
args: [],
cwd: message.cwd,
env: this.gitEnv
}, function(code, out, err) {
if (!out && err)
out = err;
if (!out)
return callback();
githelp = {"git": {
"hint": "the stupid content tracker",
"commands": {}
}};
out.replace(/[\s]{3,4}([\w]+)[\s]+(.*)\n/gi, function(m, sub, hint) {
githelp.git.commands[sub] = self.augmentCommand(sub, {"hint": hint});
});
onfinish();
}, null, null);
}
else {
onfinish();
}
function onfinish() {
c9util.extend(commands, githelp);
callback();
}
};
this.augmentCommand = function(cmd, struct) {
var map = commandsMap[cmd] || commandsMap["default"];
return c9util.extend(struct, map || {});
};
this.canShutdown = function() {
return this.processCount === 0;
};
}).call(GitPlugin.prototype);