Skip to content

Commit

Permalink
Change git branches cleanly (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Wootten authored Jul 15, 2021
1 parent b0c4050 commit 8e02deb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ namespace Scratch.FolderManager {
menu.append (create_submenu_for_new ());

if (monitored_repo != null) {
menu.append (new ChangeBranchMenu (this));
var branch_menu = new ChangeBranchMenu (this) {
sensitive = !monitored_repo.has_uncommitted
};
menu.append (branch_menu);
}

menu.append (new Gtk.SeparatorMenuItem ());
Expand Down
13 changes: 13 additions & 0 deletions src/Services/MonitoredRepository.vala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ namespace Scratch.Services {
}
}

public bool has_uncommitted {
get {
return file_status_map.size > 0;
}
}

construct {
file_status_map = new Gee.HashMap<string, Ggit.StatusFlags?> ();
status_options = new Ggit.StatusOptions (
Expand Down Expand Up @@ -172,6 +178,13 @@ namespace Scratch.Services {
public void change_branch (string new_branch_name) throws Error {
var branch = git_repo.lookup_branch (new_branch_name, Ggit.BranchType.LOCAL);
git_repo.set_head (((Ggit.Ref)branch).get_name ());
var options = new Ggit.CheckoutOptions () {
//Ensure documents match checked out branch (deal with potential conflicts/losses beforehand)
strategy = Ggit.CheckoutStrategy.FORCE
};

git_repo.checkout_head (options);

branch_name = new_branch_name;
}

Expand Down

0 comments on commit 8e02deb

Please sign in to comment.