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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*
* This file is part of gitg
*
* Copyright (C) 2014 - Jesse van den Kieboom
*
* gitg is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* gitg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gitg. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Gitg
{
public enum RemoteState
{
DISCONNECTED,
CONNECTING,
CONNECTED,
TRANSFERRING
}
public errordomain RemoteError
{
ALREADY_CONNECTED,
ALREADY_CONNECTING,
ALREADY_DISCONNECTED,
STILL_CONNECTING
}
public interface CredentialsProvider : Object
{
public abstract Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error;
}
public class Remote : Ggit.Remote
{
private class Callbacks : Ggit.RemoteCallbacks
{
private Remote d_remote;
private Ggit.RemoteCallbacks? d_proxy;
public delegate void TransferProgress(Ggit.TransferProgress stats);
private TransferProgress? d_transfer_progress;
public Callbacks(Remote remote, Ggit.RemoteCallbacks? proxy, owned TransferProgress? transfer_progress)
{
d_remote = remote;
d_proxy = proxy;
d_transfer_progress = (owned)transfer_progress;
}
protected override void progress(string message)
{
if (d_proxy != null)
{
d_proxy.progress(message);
}
}
protected override void transfer_progress(Ggit.TransferProgress stats)
{
if (d_transfer_progress != null)
{
d_transfer_progress(stats);
}
if (d_proxy != null)
{
d_proxy.transfer_progress(stats);
}
}
protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
{
d_remote.tip_updated(refname, a, b);
if (d_proxy != null)
{
d_proxy.update_tips(refname, a, b);
}
}
protected override void completion(Ggit.RemoteCompletionType type)
{
if (d_proxy != null)
{
d_proxy.completion(type);
}
}
protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
{
Ggit.Cred? ret = null;
var provider = d_remote.credentials_provider;
if (provider != null)
{
ret = provider.credentials(url, username_from_url, allowed_types);
}
if (ret == null && d_proxy != null)
{
ret = d_proxy.credentials(url, username_from_url, allowed_types);
}
return ret;
}
}
private RemoteState d_state;
private string[]? d_fetch_specs;
private string[]? d_push_specs;
private uint d_reset_transfer_progress_timeout;
private double d_transfer_progress;
private Callbacks? d_callbacks;
public signal void tip_updated(string refname, Ggit.OId a, Ggit.OId b);
public override void dispose()
{
if (d_reset_transfer_progress_timeout != 0)
{
Source.remove(d_reset_transfer_progress_timeout);
d_reset_transfer_progress_timeout = 0;
}
base.dispose();
}
public double transfer_progress
{
get { return d_transfer_progress; }
}
public RemoteState state
{
get { return d_state; }
private set
{
if (d_state != value)
{
d_state = value;
notify_property("state");
}
}
}
private void do_reset_transfer_progress()
{
d_reset_transfer_progress_timeout = 0;
d_transfer_progress = 0.0;
notify_property("transfer-progress");
}
private void reset_transfer_progress(bool with_delay)
{
if (d_transfer_progress == 0)
{
return;
}
if (with_delay)
{
d_reset_transfer_progress_timeout = Timeout.add(500, () => {
do_reset_transfer_progress();
return false;
});
}
else if (d_reset_transfer_progress_timeout == 0)
{
do_reset_transfer_progress();
}
}
private void update_transfer_progress(Ggit.TransferProgress stats)
{
var total = stats.get_total_objects();
var received = stats.get_received_objects();
var indexed = stats.get_indexed_objects();
d_transfer_progress = (double)(received + indexed) / (double)(total + total);
notify_property("transfer-progress");
if (received == total && indexed == total)
{
reset_transfer_progress(true);
}
}
private void update_state(bool force_disconnect = false)
{
if (get_connected())
{
if (force_disconnect)
{
disconnect.begin((obj, res) => {
try
{
disconnect.end(res);
} catch {}
});
}
else
{
state = RemoteState.CONNECTED;
}
}
else
{
state = RemoteState.DISCONNECTED;
}
}
public new async void connect(Ggit.Direction direction, Ggit.RemoteCallbacks? callbacks = null) throws Error
{
if (get_connected())
{
if (state != RemoteState.CONNECTED)
{
state = RemoteState.CONNECTED;
}
throw new RemoteError.ALREADY_CONNECTED("already connected");
}
else if (state == RemoteState.CONNECTING)
{
throw new RemoteError.ALREADY_CONNECTING("already connecting");
}
else
{
reset_transfer_progress(false);
}
state = RemoteState.CONNECTING;
while (true)
{
try
{
d_callbacks = new Callbacks(this, callbacks, update_transfer_progress);
yield Async.thread(() => {
base.connect(direction, d_callbacks, null, null);
});
}
catch (Error e)
{
d_callbacks = null;
// NOTE: need to check the message for now in case of failed
// http or ssh auth. This is fragile and will likely break
// in future libgit2 releases. Please fix!
if (e.message == "Unexpected HTTP status code: 401" ||
e.message == "error authenticating: Username/PublicKey combination invalid")
{
continue;
}
else
{
update_state();
throw e;
}
}
break;
}
update_state();
}
public new async void disconnect() throws Error
{
if (!get_connected())
{
if (state != RemoteState.DISCONNECTED)
{
state = RemoteState.DISCONNECTED;
}
throw new RemoteError.ALREADY_DISCONNECTED("already disconnected");
}
try
{
yield Async.thread(() => {
base.disconnect();
});
}
catch (Error e)
{
update_state();
reset_transfer_progress(true);
throw e;
}
update_state();
reset_transfer_progress(true);
}
private async void push_intern(string branch, Ggit.RemoteCallbacks? callbacks) throws Error
{
state = RemoteState.TRANSFERRING;
reset_transfer_progress(false);
try
{
yield Async.thread(() => {
var options = new Ggit.PushOptions();
if (d_callbacks == null) {
d_callbacks = new Callbacks(this, callbacks, update_transfer_progress);
}
options.set_remote_callbacks(d_callbacks);
string [] push_refs = { "refs/heads/%s:refs/heads/%s".printf(branch, branch) };
if (!base.push(push_refs, options))
throw new Error(0,0,"push");
});
}
catch (Error e)
{
reset_transfer_progress(true);
throw e;
}
reset_transfer_progress(true);
}
private async void download_intern(string? message, Ggit.RemoteCallbacks? callbacks) throws Error
{
bool dis = false;
if (!get_connected())
{
dis = true;
yield connect(Ggit.Direction.FETCH, callbacks);
}
state = RemoteState.TRANSFERRING;
reset_transfer_progress(false);
try
{
yield Async.thread(() => {
var options = new Ggit.FetchOptions();
options.set_remote_callbacks(d_callbacks);
base.download(null, options);
if (message != null)
{
base.update_tips(d_callbacks, true, options.get_download_tags(), message);
}
});
}
catch (Error e)
{
update_state(dis);
reset_transfer_progress(true);
throw e;
}
update_state(dis);
reset_transfer_progress(true);
}
public new async void download(Ggit.RemoteCallbacks? callbacks = null) throws Error
{
yield download_intern(null, callbacks);
}
public new async void push(string branch, Ggit.RemoteCallbacks? callbacks = null) throws Error
{
yield push_intern(branch, callbacks);
}
public new async void fetch(string? message, Ggit.RemoteCallbacks? callbacks = null) throws Error
{
var msg = message;
if (msg == null)
{
var name = get_name();
if (name == null)
{
name = get_url();
}
if (name != null)
{
msg = "fetch: " + name;
}
else
{
msg = "";
}
}
yield download_intern(msg, callbacks);
}
public string[]? fetch_specs
{
owned get
{
if (d_fetch_specs != null)
{
return d_fetch_specs;
}
try
{
return get_fetch_specs();
}
catch (Error e)
{
return null;
}
}
set
{
d_fetch_specs = value;
}
}
public string[]? push_specs
{
owned get
{
if (d_push_specs != null)
{
return d_push_specs;
}
try
{
return get_push_specs();
}
catch (Error e)
{
return null;
}
}
set
{
d_push_specs = value;
}
}
public CredentialsProvider? credentials_provider
{
get; set;
}
}
}
|