-
Notifications
You must be signed in to change notification settings - Fork 637
/
remotes.js
158 lines (143 loc) · 4.83 KB
/
remotes.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const ko = require('knockout');
const _ = require('lodash');
const octicons = require('octicons');
const components = require('ungit-components');
const programEvents = require('ungit-program-events');
components.register('remotes', (args) => new RemotesViewModel(args.server, args.repoPath));
class RemotesViewModel {
constructor(server, repoPath) {
this.repoPath = repoPath;
this.server = server;
this.remotes = ko.observable([]);
this.currentRemote = ko.observable(null);
this.currentRemote.subscribe((value) => {
programEvents.dispatch({ event: 'current-remote-changed', newRemote: value });
});
this.fetchLabel = ko.computed(() => {
if (this.currentRemote()) return `Fetch from ${this.currentRemote()}`;
else return 'No remotes specified';
});
this.remotesIcon = octicons.download.toSVG({ height: 18 });
this.closeIcon = octicons.x.toSVG({ height: 18 });
this.fetchEnabled = ko.computed(() => this.remotes().length > 0);
this.shouldAutoFetch = ungit.config.autoFetch;
this.updateRemotes();
}
updateNode(parentElement) {
ko.renderTemplate('remotes', this, {}, parentElement);
}
clickFetch() {
this.fetch({ nodes: true, tags: true });
}
async onProgramEvent(event) {
if (event.event === 'request-app-content-refresh' || event.event === 'request-fetch-tags') {
await this.fetch({ tags: true });
} else if (event.event === 'git-directory-changed' && this.shouldAutoFetch) {
await this.fetch({ tags: true });
} else if (event.event === 'update-remote') {
await this.updateRemotes();
}
}
async fetch(options) {
if (!this.currentRemote() || !options.tags) return;
ungit.logger.debug('remotes.fetch() triggered');
try {
const tagPromise = this.server.getPromise('/remote/tags', {
path: this.repoPath(),
remote: this.currentRemote(),
});
programEvents.dispatch({ event: 'remote-tags-update', tags: await tagPromise });
if (!this.server.isInternetConnected) {
this.server.isInternetConnected = true;
}
} catch (err) {
let errorMessage;
let stdout;
let stderr;
try {
errorMessage = `Ungit has failed to fetch a remote. ${err.res.body.error}`;
stdout = err.res.body.stdout;
stderr = err.res.body.stderr;
} catch (e) {
errorMessage = '';
}
if (errorMessage.includes('Could not resolve host')) {
if (this.server.isInternetConnected) {
this.server.isInternetConnected = false;
errorMessage =
'Could not resolve host. This usually means you are disconnected from internet and no longer push or fetch from remote. However, Ungit will be functional for local git operations.';
stdout = '';
stderr = '';
} else {
// Message is already seen, just return
return;
}
}
programEvents.dispatch({
event: 'git-error',
data: {
isWarning: true,
command: err.res.body.command,
error: err.res.body.error,
stdout,
stderr,
repoPath: err.res.body.workingDirectory,
},
});
} finally {
ungit.logger.debug('remotes.fetch() finished');
}
}
updateRemotes() {
return this.server
.getPromise('/remotes', { path: this.repoPath() })
.then((remotes) => {
remotes = remotes.map((remote) => ({
name: remote,
changeRemote: () => {
this.currentRemote(remote);
},
}));
this.remotes(remotes);
if (!this.currentRemote() && remotes.length > 0) {
if (_.find(remotes, { name: 'origin' })) {
// default to origin if it exists
this.currentRemote('origin');
} else {
// otherwise take the first one
this.currentRemote(remotes[0].name);
}
if (this.shouldAutoFetch) {
this.shouldAutoFetch = false;
return this.fetch({ nodes: true, tags: true });
}
}
})
.catch((err) => {
if (err.errorCode != 'not-a-repository') {
this.server.unhandledRejection(err);
} else {
ungit.logger.warn('updateRemotes failed', err);
}
});
}
showAddRemoteDialog() {
components.showModal('addremotemodal', { path: this.repoPath() });
}
remoteRemove(remote) {
components.showModal('yesnomodal', {
title: 'Are you sure?',
details: `Deleting ${remote.name} remote cannot be undone with ungit.`,
closeFunc: (isYes) => {
if (isYes) {
this.server
.delPromise(`/remotes/${remote.name}`, { path: this.repoPath() })
.then(() => {
this.updateRemotes();
})
.catch((e) => this.server.unhandledRejection(e));
}
},
});
}
}