{"thread":{"id":"57210","subject":"[PATCH] git-gui: insert menu entries for added remotes before the \"All\" entry","startedAt":"2022-01-07T12:58:06Z","lastAt":"2022-01-07T12:58:06Z","messageCount":1,"participants":["Tom Levy"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"445715","messageId":"20220107124408.1969-1-tomlevy93@gmail.com","threadId":"57210","inReplyTo":null,"subject":"[PATCH] git-gui: insert menu entries for added remotes before the \"All\" entry","fromName":"Tom Levy","fromEmail":"tomlevy93@gmail.com","sentAt":"2022-01-07T12:44:08Z","receivedAt":"2022-01-07T12:58:06Z","isPatch":true,"sender":{"key":"tomlevy93@gmail.com","avatar":null},"body":"Hi,\n\ngit-gui has a bug in the way it adds menu entries for new remotes.\n\nWhen a remote is added (using Remote > Add...), menu entries for the\nnew remote are currently added to the end of the \"Fetch from\" and\n\"Prune from\" submenus.\n\nHowever, those submenus normally have a special entry \"All\" at the\nend. Adding the new entries at the end messes up the order, and also\ncauses the \"All\" entry to be added multiple times because\n'update_all_remotes_menu_entry' re-adds an \"All\" entry if the last\nentry is not \"All\".\n\nThe patch below inserts the new entries before the \"All\" entry.\n\nI moved the logic for checking if the \"All\" entry exists into a new\nprocedure 'has_all_remotes_menu_entry'. Note that this changes the\nfirst condition in 'update_all_remotes_menu_entry'; previously the\nnegation was subtly broken.\n\nRegards,\nTom Levy\n\n-- >8 --\n\nSubject: [PATCH] git-gui: insert menu entries for added remotes before the\n \"All\" entry\n\nWhen a remote is added, the new fetch/prune menu entries need to be\ninserted before the \"All\" entry (and its separator) rather than at the\nend, so that \"All\" will remain the last option.\n\nSigned-off-by: Tom Levy <tomlevy93@gmail.com>\n---\n lib/remote.tcl | 28 +++++++++++++++++++++-------\n 1 file changed, 21 insertions(+), 7 deletions(-)\n\ndiff --git a/lib/remote.tcl b/lib/remote.tcl\nindex ef77ed7..5e94bdb 100644\n--- a/lib/remote.tcl\n+++ b/lib/remote.tcl\n@@ -132,6 +132,14 @@ proc load_all_remotes {} {\n \tset all_remotes [lsort -unique $all_remotes]\n }\n \n+proc remote_submenu_insert_pos {sub_m} {\n+\tif {[has_all_remotes_menu_entry $sub_m]} {\n+\t\treturn [expr {[$sub_m index end] - 1}] ; # index of separator\n+\t} else {\n+\t\treturn end\n+\t}\n+}\n+\n proc add_fetch_entry {r} {\n \tglobal repo_config\n \tset remote_m .mbar.remote\n@@ -159,13 +167,16 @@ proc add_fetch_entry {r} {\n \tif {$enable} {\n \t\tmake_sure_remote_submenues_exist $remote_m\n \n-\t\t$fetch_m add command \\\n+\t\tset fetch_pos [remote_submenu_insert_pos $fetch_m]\n+\t\t$fetch_m insert $fetch_pos command \\\n \t\t\t-label $r \\\n \t\t\t-command [list fetch_from $r]\n-\t\t$prune_m add command \\\n+\t\tset prune_pos [remote_submenu_insert_pos $prune_m]\n+\t\t$prune_m insert $prune_pos command \\\n \t\t\t-label $r \\\n \t\t\t-command [list prune_from $r]\n-\t\t$remove_m add command \\\n+\t\tset remove_pos end ; # no \"All\" entry, so always insert at end\n+\t\t$remove_m insert $remove_pos command \\\n \t\t\t-label $r \\\n \t\t\t-command [list remove_remote $r]\n \t}\n@@ -230,6 +241,11 @@ proc make_sure_remote_submenues_exist {remote_m} {\n \t}\n }\n \n+proc has_all_remotes_menu_entry {sub_m} {\n+\texpr {[$sub_m type end] eq \"command\" \\\n+\t\t\t&& [$sub_m entrycget end -label] eq [mc \"All\"]}\n+}\n+\n proc update_all_remotes_menu_entry {} {\n \tglobal all_remotes\n \n@@ -245,8 +261,7 @@ proc update_all_remotes_menu_entry {} {\n \tset prune_m $remote_m.prune\n \tif {$have_remote > 1} {\n \t\tmake_sure_remote_submenues_exist $remote_m\n-\t\tif {[$fetch_m type end] eq \"command\" \\\n-\t\t\t\t&& [$fetch_m entrycget end -label] ne [mc \"All\"]} {\n+\t\tif {![has_all_remotes_menu_entry $fetch_m]} {\n \n \t\t\t$fetch_m insert end separator\n \t\t\t$fetch_m insert end command \\\n@@ -260,8 +275,7 @@ proc update_all_remotes_menu_entry {} {\n \t\t}\n \t} else {\n \t\tif {[winfo exists $fetch_m]} {\n-\t\t\tif {[$fetch_m type end] eq \"command\" \\\n-\t\t\t\t\t&& [$fetch_m entrycget end -label] eq [mc \"All\"]} {\n+\t\t\tif {[has_all_remotes_menu_entry $fetch_m]} {\n \n \t\t\t\tdelete_from_menu $fetch_m end\n \t\t\t\tdelete_from_menu $fetch_m end\n-- \n2.30.2\n\n"}]}