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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/* foundry-git-blame.c
*
* Copyright 2025 Christian Hergert <chergert@redhat.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "foundry-git-autocleanups.h"
#include "foundry-git-blame-private.h"
#include "foundry-git-signature-private.h"
#include "foundry-vcs-file.h"
/**
* FoundryGitBlame:
*
* Git implementation of blame information for version control files.
*
* FoundryGitBlame provides Git-specific functionality for retrieving and
* managing blame information for files in version control. It handles
* blame data retrieval, caching, and provides efficient access to
* authorship and modification information for code analysis and display.
*/
struct _FoundryGitBlame
{
FoundryVcsBlame parent_instance;
GMutex mutex;
git_blame *base_blame;
git_blame *bytes_blame;
};
G_DEFINE_FINAL_TYPE (FoundryGitBlame, foundry_git_blame, FOUNDRY_TYPE_VCS_BLAME)
static git_blame *
get_blame_locked (FoundryGitBlame *self)
{
return self->bytes_blame ? self->bytes_blame : self->base_blame;
}
static DexFuture *
foundry_git_blame_update (FoundryVcsBlame *vcs_blame,
GBytes *contents)
{
FoundryGitBlame *self = (FoundryGitBlame *)vcs_blame;
g_autoptr(GMutexLocker) locker = NULL;
g_autoptr(git_blame) blame = NULL;
gconstpointer data = NULL;
gsize size = 0;
dex_return_error_if_fail (FOUNDRY_IS_GIT_BLAME (self));
dex_return_error_if_fail (contents != NULL);
locker = g_mutex_locker_new (&self->mutex);
if (contents != NULL)
data = g_bytes_get_data (contents, &size);
g_clear_pointer (&self->bytes_blame, git_blame_free);
if (git_blame_buffer (&blame, self->base_blame, data, size) == 0)
self->bytes_blame = g_steal_pointer (&blame);
return dex_future_new_true ();
}
static FoundryVcsSignature *
foundry_git_blame_query_line (FoundryVcsBlame *blame,
guint line)
{
FoundryGitBlame *self = (FoundryGitBlame *)blame;
g_autoptr(GMutexLocker) locker = NULL;
const git_blame_hunk *hunk;
git_blame *gblame;
g_assert (FOUNDRY_IS_GIT_BLAME (self));
g_assert (self->base_blame != NULL);
locker = g_mutex_locker_new (&self->mutex);
gblame = get_blame_locked (self);
if ((hunk = git_blame_get_hunk_byline (gblame, line + 1)))
{
g_autoptr(git_signature) copy = NULL;
/* TODO: This is often accessed sequentially so it might make
* sense to keep the most-recently-used one and then
* reuse it instead of copy/construct on each line.
*/
if (hunk->final_signature == NULL)
return NULL;
if (git_signature_dup (©, hunk->final_signature) != 0)
return NULL;
return _foundry_git_signature_new (g_steal_pointer (©));
}
return NULL;
}
/**
* foundry_git_blame_dup_commit_id:
* @self: a #FoundryGitBlame
* @line: the line number (0-indexed)
*
* Gets the commit ID string for the given line number from the blame
* information.
*
* The commit ID is the hash of the commit that last modified the line.
*
* Returns: (transfer full): a newly allocated string containing the commit ID,
* or %NULL if the line is not found or not tracked
*
* Since: 1.1
*/
char *
foundry_git_blame_dup_commit_id (FoundryGitBlame *self,
guint line)
{
g_autoptr(GMutexLocker) locker = NULL;
const git_blame_hunk *hunk;
git_blame *gblame;
char str[GIT_OID_HEXSZ + 1];
g_return_val_if_fail (FOUNDRY_IS_GIT_BLAME (self), NULL);
g_return_val_if_fail (self->base_blame != NULL, NULL);
locker = g_mutex_locker_new (&self->mutex);
gblame = get_blame_locked (self);
if ((hunk = git_blame_get_hunk_byline (gblame, line + 1)))
{
git_oid_tostr (str, sizeof str, &hunk->final_commit_id);
str[GIT_OID_HEXSZ] = 0;
return g_strdup (str);
}
return NULL;
}
static guint
foundry_git_blame_get_n_lines (FoundryVcsBlame *blame)
{
FoundryGitBlame *self = (FoundryGitBlame *)blame;
g_autoptr(GMutexLocker) locker = NULL;
git_blame *gblame;
gsize hunk_count;
guint n_lines = 0;
g_assert (FOUNDRY_IS_GIT_BLAME (self));
g_assert (self->base_blame != NULL);
locker = g_mutex_locker_new (&self->mutex);
gblame = get_blame_locked (self);
hunk_count = git_blame_get_hunk_count (gblame);
for (gsize i = 0; i < hunk_count; i++)
{
const git_blame_hunk *hunk = git_blame_get_hunk_byindex (gblame, i);
if (hunk != NULL)
n_lines += hunk->lines_in_hunk;
}
return n_lines;
}
static void
foundry_git_blame_finalize (GObject *object)
{
FoundryGitBlame *self = (FoundryGitBlame *)object;
g_clear_pointer (&self->bytes_blame, git_blame_free);
g_clear_pointer (&self->base_blame, git_blame_free);
g_mutex_clear (&self->mutex);
G_OBJECT_CLASS (foundry_git_blame_parent_class)->finalize (object);
}
static void
foundry_git_blame_class_init (FoundryGitBlameClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FoundryVcsBlameClass *vcs_blame_class = FOUNDRY_VCS_BLAME_CLASS (klass);
object_class->finalize = foundry_git_blame_finalize;
vcs_blame_class->update = foundry_git_blame_update;
vcs_blame_class->query_line = foundry_git_blame_query_line;
vcs_blame_class->get_n_lines = foundry_git_blame_get_n_lines;
}
static void
foundry_git_blame_init (FoundryGitBlame *self)
{
g_mutex_init (&self->mutex);
}
FoundryGitBlame *
_foundry_git_blame_new (git_blame *base_blame,
git_blame *bytes_blame)
{
FoundryGitBlame *self;
g_return_val_if_fail (base_blame != NULL, NULL);
self = g_object_new (FOUNDRY_TYPE_GIT_BLAME, NULL);
self->base_blame = g_steal_pointer (&base_blame);
self->bytes_blame = g_steal_pointer (&bytes_blame);
return self;
}
|