-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathremote.js
125 lines (100 loc) · 3.02 KB
/
remote.js
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
function Remote (utils, args) {
return function (repository, nativeRemote) {
this.repository = repository;
this.utils = utils;
this.args = args;
var _this = this
, _priv = utils._createPrivate(_this)
, opts = {};
_priv.native = nativeRemote;
_priv.connected = false;
if (!(nativeRemote instanceof Gitteh.bindings.NativeRemote)) {
throw new Error('Don\'t construct me, see Repository.remote()');
}
opts.connected = _priv.connected;
opts.enumerable = true;
opts.get = function () {
return opts.connected;
};
utils._defineObject(_this, 'connected', opts);
utils._immutable(_this, nativeRemote)
.set('name')
.set('url');
var fetchSpec = new RefSpec(nativeRemote.fetchSpec.src, nativeRemote.fetchSpec.dst)
, pushSpec = new RefSpec(nativeRemote.pushSpec.src, nativeRemote.pushSpec.dst);
utils._immutable(_this, {fetchSpec: fetchSpec, pushSpec: pushSpec})
.set('fetchSpec')
.set('pushSpec');
};
};
Remote.prototype.connect = function () {
var _this = this
, _priv = this.utils._getPrivate(_this)
, _ref = this.args({
dir: {
type: "remoteDir"
},
cb: {
type: "function"
}
})
, dir = _ref[0]
, cb = _ref[1];
dir = dir === "push" ? Gitteh.bindings.GIT_DIRECTION_PUSH : Gitteh.bindings.GIT_DIRECTION_FETCH;
return _priv.native.connect(dir, this.utils._wrapCallback(cb, function (refs) {
var refNames = Object.keys(refs)
, headOid = refs["HEAD"];
for (ref in refs) {
var oid = refs[ref];
if (ref === "HEAD") continue;
if (oid === headOid) {
var headRef = this.fetchSpec.transformTo(ref);
_this.utils._immutable(_this, {headRef: headRef})
.set("headRef", "HEAD");
break;
}
};
_this.utils._immutable(_this, {refNames: refNames})
.set("refNames", "refs");
_priv.connected = true;
cb();
}));
};
Remote.prototype.fetch = function () {
var _this = this
, _priv = this.utils._getPrivate(_this)
, updateTimer = null;
if(!this.connected) {
throw new Error('Remote isn\'t connected.');
}
var _ref = this.args({
progressCb: {
type: "function"
},
cb: {
type: "function"
}
})
, progressCb = _ref[0]
, cb = _ref[1];
var update = function () {
var progress = _priv.native.stats;
progressCb(progress.bytes, progress.total, progress.done);
updateTimer = setTimeout(update, 500);
};
setTimeout(update, 500);
_priv.native.download = function () {
clearTimeout(updateTimer);
if (err) {
return cb(err);
}
_priv.native.updateTips(_this.utils._wrapCallback(cb, function () {
cb();
}));
};
return _priv.native.download;
};
module.exports.all = function (Gitteh, utils, args) {
Gitteh.Remote = Remote(utils, args);
Gitteh.Remote.prototype = Remote.prototype;
};