Deal correctly with stage/unstage of selection on new files

This commit is contained in:
Alberto Fanjul 2022-09-01 21:56:29 +02:00
parent 6785cd73f8
commit efe545bc75
7 changed files with 67 additions and 12 deletions

View file

@ -1275,6 +1275,8 @@ namespace GitgCommit
foreach (var pset in selection)
{
if (pset.patches.length == 0)
continue;
if (staging)
{
yield stage.stage_patch(pset);
@ -1284,6 +1286,7 @@ namespace GitgCommit
yield stage.unstage_patch(pset);
}
}
d_main.diff_view.clear_selection();
}
private async void discard_selection() throws Error

View file

@ -23,6 +23,7 @@ interface Gitg.DiffSelectable : Object
public abstract bool can_select { get; construct set; }
public abstract PatchSet selection { owned get; }
public abstract void clear_selection ();
}
// ex:ts=4 noet

View file

@ -128,6 +128,10 @@ class Gitg.DiffViewFileRendererTextSplit : Gtk.Box, DiffSelectable, DiffViewFile
}
}
public void clear_selection()
{
}
public bool can_select { get; construct set; }
public PatchSet selection

View file

@ -68,6 +68,8 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
private Settings? d_stylesettings;
private FontManager d_font_manager;
private bool d_has_selection;
public Style d_style { get; construct set; }
public bool new_is_workdir { get; construct set; }
@ -122,7 +124,10 @@ class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFi
}
}
private bool d_has_selection;
public void clear_selection()
{
d_has_selection = false;
}
public bool has_selection
{

View file

@ -96,6 +96,15 @@ class Gitg.DiffViewFile : Gtk.Grid
return has_selection;
}
public void clear_selection()
{
foreach (var renderer in renderer_list)
{
var sel = renderer as DiffSelectable;
sel.clear_selection();
}
}
public PatchSet get_selection()
{
var ret = new PatchSet();

View file

@ -853,6 +853,7 @@ public class Gitg.DiffView : Gtk.Grid
renderer_text.notify["has-selection"].connect(on_selection_changed);
}
}
on_selection_changed();
}
if (current_is_binary)
{
@ -1061,6 +1062,14 @@ public class Gitg.DiffView : Gtk.Grid
return ret;
}
public void clear_selection()
{
foreach (var file in d_grid_files.get_children())
{
((Gitg.DiffViewFile)file).clear_selection();
}
}
private void update_hide_show_options(Gdk.Window window, int ex, int ey)
{
void *data;

View file

@ -874,15 +874,17 @@ public class Stage : Object
yield thread_index((index) => {
var entries = index.get_entries();
var entry = entries.get_by_path(newf, 0);
uchar[] old_content = new uchar[0];
if (entry == null)
{
throw new StageError.INDEX_ENTRY_NOT_FOUND(patch.filename);
print("\npath not in index %s\n", newf.get_path());
index.add_file(newf);
index.write();
} else {
var old_blob = d_repository.lookup<Ggit.Blob>(entry.get_id());
old_content = old_blob.get_raw_content();
}
var old_blob = d_repository.lookup<Ggit.Blob>(entry.get_id());
unowned uchar[] old_content = old_blob.get_raw_content();
var old_stream = new MemoryInputStream.from_bytes(new Bytes(old_content));
apply_patch(index, old_stream, new_stream, patch);
@ -957,14 +959,22 @@ public class Stage : Object
if (entry == null)
{
throw new StageError.INDEX_ENTRY_NOT_FOUND(patch.filename);
index.add_file(file);
index.write();
entries = index.get_entries();
entry = entries.get_by_path(file, 0);
}
var head_entry = tree.get_by_path(patch.filename);
var head_blob = d_repository.lookup<Ggit.Blob>(head_entry.get_id());
var index_blob = d_repository.lookup<Ggit.Blob>(entry.get_id());
unowned uchar[] head_content = head_blob.get_raw_content();
uchar[] head_content = new uchar[0];
try {
var head_entry = tree.get_by_path(patch.filename);
var head_blob = d_repository.lookup<Ggit.Blob>(head_entry.get_id());
head_content = head_blob.get_raw_content();
} catch (Error e) {}
var index_blob = d_repository.lookup<Ggit.Blob>(entry.get_id());
unowned uchar[] index_content = index_blob.get_raw_content();
var head_stream = new MemoryInputStream.from_bytes(new Bytes(head_content));
@ -972,7 +982,21 @@ public class Stage : Object
var reversed = patch.reversed();
apply_patch(index, index_stream, head_stream, reversed);
try {
apply_patch(index, index_stream, head_stream, reversed);
} catch(Error e) {
var stage = d_repository.stage;
stage.delete_path.begin(file.get_path(), (obj, res) => {
try
{
stage.delete_path.end(res);
}
catch (Error e)
{
warning("%s\n", e.message);
}
});
}
head_stream.close();
index_stream.close();