gitg-ref-action-checkout.vala 3.11 KB
Newer Older
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
/*
 * This file is part of gitg
 *
 * Copyright (C) 2015 - 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
{

class RefActionCheckout : GitgExt.UIElement, GitgExt.Action, GitgExt.RefAction, Object
{
	// Do this to pull in config.h before glib.h (for gettext...)
	private const string version = Gitg.Config.VERSION;

	public GitgExt.Application? application { owned get; construct set; }
	public GitgExt.RefActionInterface action_interface { get; construct set; }
	public Gitg.Ref reference { get; construct set; }

	public RefActionCheckout(GitgExt.Application        application,
	                         GitgExt.RefActionInterface action_interface,
	                         Gitg.Ref                   reference)
	{
		Object(application:      application,
		       action_interface: action_interface,
		       reference:        reference);
	}

	public string id
	{
		owned get { return "/org/gnome/gitg/ref-actions/checkout"; }
	}

	public string display_name
	{
		owned get { return _("Checkout"); }
	}

	public string description
	{
		owned get { return _("Checkout the selected reference"); }
	}

	public bool enabled
	{
		get
		{
			try
			{
62 63
				return reference.is_branch() && !((Ggit.Branch)reference).is_head();
			} catch {}
64

65
			return false;
66 67 68
		}
	}

69
	public async bool checkout()
70 71
	{
		var repo = application.repository;
72
		var notification = new SimpleNotification(_("Checkout %s").printf(@"'$(reference.parsed_name.shortname)'"));
73
		bool retval = false;
74

75
		application.notifications.add(notification);
76 77 78

		try
		{
79 80
			yield Async.thread(() => {
				Commit commit;
81

82 83 84 85 86 87 88 89 90
				try
				{
					commit = reference.resolve().lookup() as Gitg.Commit;
				}
				catch (Error e)
				{
					notification.error(_("Failed to lookup commit: %s").printf(e.message));
					return;
				}
91

92 93 94 95
				try
				{
					var opts = new Ggit.CheckoutOptions();
					opts.set_strategy(Ggit.CheckoutStrategy.SAFE);
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
					repo.checkout_tree(commit.get_tree(), opts);
				}
				catch (Error e)
				{
					notification.error(_("Failed to checkout branch: %s").printf(e.message));
					return;
				}

				try
				{
					repo.set_head(reference.get_name());
				}
				catch (Error e)
				{
					notification.error(_("Failed to update HEAD: %s").printf(e.message));
					return;
				}

				retval = true;
			});
117 118
		} catch {}

119 120 121 122 123 124 125 126 127 128 129 130 131 132
		if (retval)
		{
			notification.success(_("Successfully checked out branch to working directory"));
			action_interface.refresh();
		}

		return retval;
	}

	public void activate()
	{
		checkout.begin((obj, res) => {
			checkout.end(res);
		});
133 134 135 136 137 138
	}
}

}

// ex:set ts=4 noet