From f093cfa7e4cbb27280576d39d63e5000df9f0d7c Mon Sep 17 00:00:00 2001 From: Ansuz Inspirati Date: Mon, 25 Aug 2025 08:35:41 +1000 Subject: New feature - show git branch with icon view text (#1838) Display the git branch along with directory name in icon view; when the directory is a git repository. Obviously further work will be required to enable/disable as a displayed option, and potentially some level of customisation. This would require open discussion of possibilities. Solutions for co-existence with other displayable parameters also to be community determined. Consideration and feedback appreciated. * Add UI support to enable/disable option Added user interface support for option to enable and disable the display of the git branch name in icon view. * Add internationsation support for the new feature With translations for most of the currently supported languages. * Update caja.pot Build system regenerated .pot file with additional strings from new feature (already in previous commit), new strings already introduced to code-base, along with updated source file line numbers of existing strings. Required for the automated pull mechanism in Transifex - which should complete once merged with master branch. --- src/file-manager/fm-desktop-icon-view.c | 8 +++ src/file-manager/fm-icon-container.c | 88 +++++++++++++++++++++++++++++++++ src/file-manager/fm-icon-view.c | 66 ++++++++++++++++++++++++- src/file-manager/fm-icon-view.h | 12 ++--- 4 files changed, 167 insertions(+), 7 deletions(-) (limited to 'src/file-manager') diff --git a/src/file-manager/fm-desktop-icon-view.c b/src/file-manager/fm-desktop-icon-view.c index 954c26de..1e08b7cf 100644 --- a/src/file-manager/fm-desktop-icon-view.c +++ b/src/file-manager/fm-desktop-icon-view.c @@ -89,6 +89,7 @@ static gboolean real_supports_auto_layout (FMIconView static gboolean real_supports_scaling (FMIconView *view); static gboolean real_supports_keep_aligned (FMIconView *view); static gboolean real_supports_labels_beside_icons (FMIconView *view); +static gboolean real_supports_display_git_branch (FMIconView *view); static void real_merge_menus (FMDirectoryView *view); static void real_update_menus (FMDirectoryView *view); static gboolean real_supports_zooming (FMDirectoryView *view); @@ -336,6 +337,7 @@ fm_desktop_icon_view_class_init (FMDesktopIconViewClass *class) FM_ICON_VIEW_CLASS (class)->supports_scaling = real_supports_scaling; FM_ICON_VIEW_CLASS (class)->supports_keep_aligned = real_supports_keep_aligned; FM_ICON_VIEW_CLASS (class)->supports_labels_beside_icons = real_supports_labels_beside_icons; + FM_ICON_VIEW_CLASS (class)->supports_display_git_branch = real_supports_display_git_branch; } /*This code is only reached when running on x11*/ @@ -919,6 +921,12 @@ real_supports_labels_beside_icons (FMIconView *view) return FALSE; } +static gboolean +real_supports_display_git_branch (FMIconView *view) +{ + return FALSE; +} + static gboolean real_supports_zooming (FMDirectoryView *view) { diff --git a/src/file-manager/fm-icon-container.c b/src/file-manager/fm-icon-container.c index c83cf783..8054c2da 100644 --- a/src/file-manager/fm-icon-container.c +++ b/src/file-manager/fm-icon-container.c @@ -34,6 +34,7 @@ #include #include +#include #include "fm-icon-container.h" G_DEFINE_TYPE (FMIconContainer, fm_icon_container, CAJA_TYPE_ICON_CONTAINER); @@ -295,6 +296,67 @@ fm_icon_container_get_icon_text_attribute_names (CajaIconContainer *container, return attributes; } +/** + * Parse a .git directory/file to determine repository branch name + **/ +static char * +get_git_branch (const char *git_path) { + gchar *head_content = NULL; + gchar *branch_name = NULL; + gchar *head_path = NULL; + + if (g_file_test (git_path, G_FILE_TEST_IS_REGULAR)) + { + /* It's a file, thus we are probably dealing with a submodule, so read it to resolve the real git dir */ + gchar *contents = NULL; + if (g_file_get_contents (git_path, &contents, NULL, NULL) && contents) + { + const char *git_dir_prefix = "gitdir: "; + if (g_str_has_prefix (contents, git_dir_prefix)) + { + size_t git_dir_prefix_len = strlen (git_dir_prefix); + gchar *relative = g_strstrip (contents + git_dir_prefix_len); + gchar *base = g_path_get_dirname (git_path); + g_free (git_path); + git_path = g_build_filename (base, relative, NULL); + g_free (base); + } + g_free (contents); + } + } + + /* Extract the current git branch name of the repository from file HEAD within .git directory */ + if (g_file_test (git_path, G_FILE_TEST_IS_DIR)) + { + head_path = g_build_filename (git_path, "HEAD", NULL); + if (g_file_get_contents (head_path, &head_content, NULL, NULL)) + { + g_strstrip (head_content); + if (g_str_has_prefix (head_content, "ref: ")) + { + gchar **parts = g_strsplit (head_content, "/", -1); + if (parts != NULL) + { + branch_name = g_strdup (parts[g_strv_length(parts) - 1]); + g_strchomp (branch_name); + g_strfreev (parts); + } + } + else + { + /* Repository is in a detached HEAD state */ + branch_name = g_strdup_printf (_("detached: %.7s"), head_content); + } + } + } + + g_free (head_content); + g_free (head_path); + + /* branch_name is a newly allocated string, so the caller is responsible for freeing it */ + return branch_name; +} + /* This callback returns the text, both the editable part, and the * part below that is not editable. */ @@ -398,6 +460,32 @@ fm_icon_container_get_icon_text (CajaIconContainer *container, if (j == 0) { *additional_text = NULL; + if (container->details->display_git_branch == CAJA_DISPLAY_GIT_BRANCH_ENABLED) + { + /* If we have a directory, check if it's a git repository or submodule */ + GFileType file_type = caja_file_get_file_type (file); + if (file_type == G_FILE_TYPE_DIRECTORY) + { + GFile *location = caja_file_get_location (file); + char *path = g_file_get_path (location); + if (path != NULL) + { + char *git_path = g_build_path (G_DIR_SEPARATOR_S, path, ".git", NULL); + if (git_path != NULL && G_UNLIKELY(g_file_test (git_path, G_FILE_TEST_EXISTS))) + { + char *branch = get_git_branch (git_path); + if (branch != NULL) + { + *additional_text = g_strdup_printf ("[%s]", branch); + g_free (branch); + } + } + g_free (git_path); + g_free (path); + } + g_object_unref (location); + } + } } else if (j == 1) { diff --git a/src/file-manager/fm-icon-view.c b/src/file-manager/fm-icon-view.c index 83517273..25708184 100644 --- a/src/file-manager/fm-icon-view.c +++ b/src/file-manager/fm-icon-view.c @@ -201,6 +201,7 @@ static void default_sort_in_reverse_order_changed_callback (gpointer callback_da static void default_use_tighter_layout_changed_callback (gpointer callback_data); static void default_zoom_level_changed_callback (gpointer callback_data); static void labels_beside_icons_changed_callback (gpointer callback_data); +static void display_git_branch_changed_callback (gpointer callback_data); static void all_columns_same_width_changed_callback (gpointer callback_data); static void fm_icon_view_iface_init (CajaViewIface *iface); @@ -265,6 +266,9 @@ fm_icon_view_finalize (GObject *object) g_signal_handlers_disconnect_by_func (caja_icon_view_preferences, labels_beside_icons_changed_callback, icon_view); + g_signal_handlers_disconnect_by_func (caja_icon_view_preferences, + display_git_branch_changed_callback, + icon_view); g_signal_handlers_disconnect_by_func (caja_compact_view_preferences, default_zoom_level_changed_callback, icon_view); @@ -705,6 +709,16 @@ fm_icon_view_supports_labels_beside_icons (FMIconView *view) supports_labels_beside_icons, (view)); } +static gboolean +fm_icon_view_supports_display_git_branch (FMIconView *view) +{ + g_return_val_if_fail (FM_IS_ICON_VIEW (view), FALSE); + + return EEL_CALL_METHOD_WITH_RETURN_VALUE + (FM_ICON_VIEW_CLASS, view, + supports_display_git_branch, (view)); +} + static gboolean fm_icon_view_supports_tighter_layout (FMIconView *view) { @@ -1183,6 +1197,14 @@ real_supports_labels_beside_icons (FMIconView *view) return TRUE; } +static gboolean +real_supports_display_git_branch (FMIconView *view) +{ + g_return_val_if_fail (FM_IS_ICON_VIEW (view), TRUE); + + return TRUE; +} + static gboolean set_sort_reversed (FMIconView *icon_view, gboolean new_value) { @@ -1282,6 +1304,31 @@ set_labels_beside_icons (FMIconView *icon_view) } } +static void +set_display_git_branch (FMIconView *icon_view) +{ + gboolean git_branch; + + if (fm_icon_view_supports_display_git_branch (icon_view)) + { + git_branch = fm_icon_view_is_compact (icon_view) || + g_settings_get_boolean (caja_icon_view_preferences, CAJA_PREFERENCES_ICON_VIEW_DISPLAY_GIT_BRANCH); + + if (git_branch) + { + caja_display_git_branch_enable + (get_icon_container (icon_view), + CAJA_DISPLAY_GIT_BRANCH_ENABLED); + } + else + { + caja_display_git_branch_enable + (get_icon_container (icon_view), + CAJA_DISPLAY_GIT_BRANCH_DISABLED); + } + } +} + static void set_columns_same_width (FMIconView *icon_view) { @@ -1383,6 +1430,7 @@ fm_icon_view_begin_loading (FMDirectoryView *view) fm_icon_view_get_directory_tighter_layout (icon_view, file)); set_labels_beside_icons (icon_view); + set_display_git_branch (icon_view); set_columns_same_width (icon_view); /* We must set auto-layout last, because it invokes the layout_changed @@ -2902,6 +2950,18 @@ labels_beside_icons_changed_callback (gpointer callback_data) set_labels_beside_icons (icon_view); } +static void +display_git_branch_changed_callback (gpointer callback_data) +{ + FMIconView *icon_view; + + g_return_if_fail (FM_IS_ICON_VIEW (callback_data)); + + icon_view = FM_ICON_VIEW (callback_data); + + set_display_git_branch (icon_view); +} + static void all_columns_same_width_changed_callback (gpointer callback_data) { @@ -3293,6 +3353,7 @@ fm_icon_view_class_init (FMIconViewClass *klass) klass->supports_manual_layout = real_supports_manual_layout; klass->supports_keep_aligned = real_supports_keep_aligned; klass->supports_labels_beside_icons = real_supports_labels_beside_icons; + klass->supports_display_git_branch = real_supports_display_git_branch; klass->get_directory_auto_layout = fm_icon_view_real_get_directory_auto_layout; klass->get_directory_sort_by = fm_icon_view_real_get_directory_sort_by; klass->get_directory_sort_reversed = fm_icon_view_real_get_directory_sort_reversed; @@ -3389,7 +3450,10 @@ fm_icon_view_init (FMIconView *icon_view) "changed::" CAJA_PREFERENCES_ICON_VIEW_LABELS_BESIDE_ICONS, G_CALLBACK (labels_beside_icons_changed_callback), icon_view); - + g_signal_connect_swapped (caja_icon_view_preferences, + "changed::" CAJA_PREFERENCES_ICON_VIEW_DISPLAY_GIT_BRANCH, + G_CALLBACK (display_git_branch_changed_callback), + icon_view); g_signal_connect_swapped (caja_compact_view_preferences, "changed::" CAJA_PREFERENCES_COMPACT_VIEW_DEFAULT_ZOOM_LEVEL, G_CALLBACK (default_zoom_level_changed_callback), diff --git a/src/file-manager/fm-icon-view.h b/src/file-manager/fm-icon-view.h index dfd2a171..c8c8c2f7 100644 --- a/src/file-manager/fm-icon-view.h +++ b/src/file-manager/fm-icon-view.h @@ -108,17 +108,17 @@ struct FMIconViewClass */ gboolean (* supports_scaling) (FMIconView *view); - /* supports_auto_layout is a function pointer that subclasses may - * override to control whether snap-to-grid mode - * should be enabled. The default implementation returns FALSE. + /* */ gboolean (* supports_keep_aligned) (FMIconView *view); - /* supports_auto_layout is a function pointer that subclasses may - * override to control whether snap-to-grid mode - * should be enabled. The default implementation returns FALSE. + /* */ gboolean (* supports_labels_beside_icons) (FMIconView *view); + + /* + */ + gboolean (* supports_display_git_branch) (FMIconView *view); }; /* GObject support */ -- cgit v1.2.1