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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
/* foundry-git-delta.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-delta-private.h"
#include "foundry-git-diff-hunk-private.h"
#include "foundry-git-diff-private.h"
#include "foundry-git-error.h"
#include "foundry-git-patch-private.h"
#include "foundry-util.h"
struct _FoundryGitDelta
{
GObject parent_instance;
FoundryGitDiff *diff;
gsize delta_idx;
char *old_path;
char *new_path;
git_oid old_oid;
git_oid new_oid;
guint old_mode;
guint new_mode;
FoundryVcsDeltaStatus status;
guint context_lines;
};
G_DEFINE_FINAL_TYPE (FoundryGitDelta, foundry_git_delta, FOUNDRY_TYPE_VCS_DELTA)
static char *
foundry_git_delta_dup_old_path (FoundryVcsDelta *delta)
{
return g_strdup (FOUNDRY_GIT_DELTA (delta)->old_path);
}
static char *
foundry_git_delta_dup_new_path (FoundryVcsDelta *delta)
{
return g_strdup (FOUNDRY_GIT_DELTA (delta)->new_path);
}
static char *
foundry_git_delta_dup_old_id (FoundryVcsDelta *delta)
{
FoundryGitDelta *self = FOUNDRY_GIT_DELTA (delta);
char str[GIT_OID_HEXSZ + 1];
git_oid_tostr (str, sizeof str, &self->old_oid);
str[GIT_OID_HEXSZ] = 0;
return g_strdup (str);
}
static char *
foundry_git_delta_dup_new_id (FoundryVcsDelta *delta)
{
FoundryGitDelta *self = FOUNDRY_GIT_DELTA (delta);
char str[GIT_OID_HEXSZ + 1];
git_oid_tostr (str, sizeof str, &self->new_oid);
str[GIT_OID_HEXSZ] = 0;
return g_strdup (str);
}
static guint
foundry_git_delta_get_old_mode (FoundryVcsDelta *delta)
{
return FOUNDRY_GIT_DELTA (delta)->old_mode;
}
static guint
foundry_git_delta_get_new_mode (FoundryVcsDelta *delta)
{
return FOUNDRY_GIT_DELTA (delta)->new_mode;
}
static FoundryVcsDeltaStatus
foundry_git_delta_get_status (FoundryVcsDelta *delta)
{
return FOUNDRY_GIT_DELTA (delta)->status;
}
static void
foundry_git_delta_finalize (GObject *object)
{
FoundryGitDelta *self = (FoundryGitDelta *)object;
g_clear_object (&self->diff);
g_clear_pointer (&self->old_path, g_free);
g_clear_pointer (&self->new_path, g_free);
G_OBJECT_CLASS (foundry_git_delta_parent_class)->finalize (object);
}
static DexFuture *
foundry_git_delta_list_hunks_thread (gpointer data)
{
FoundryGitDelta *self = data;
g_autoptr(FoundryGitRepositoryPaths) paths = NULL;
g_autoptr(FoundryGitPatch) git_patch = NULL;
g_autoptr(GListStore) store = NULL;
g_autoptr(git_patch) patch = NULL;
g_autoptr(git_repository) repository = NULL;
g_autoptr(git_blob) old_blob = NULL;
g_autoptr(git_blob) new_blob = NULL;
g_autoptr(GBytes) contents = NULL;
g_autoptr(GError) error = NULL;
const git_diff_delta *delta = NULL;
gsize num_hunks;
const char *old_path = NULL;
const char *new_path = NULL;
int ret;
g_assert (FOUNDRY_IS_GIT_DELTA (self));
g_assert (FOUNDRY_IS_GIT_DIFF (self->diff));
store = g_list_store_new (FOUNDRY_TYPE_GIT_DIFF_HUNK);
delta = _foundry_git_diff_get_delta (self->diff, self->delta_idx);
if (delta == NULL)
return foundry_git_reject_last_error ();
old_path = delta->old_file.path;
new_path = delta->new_file.path;
paths = _foundry_git_diff_dup_paths (self->diff);
if (!foundry_git_repository_paths_open (paths, &repository, &error))
return dex_future_new_for_error (g_steal_pointer (&error));
{
g_autofree char *file_path = NULL;
const char *workdir;
const char *buf = NULL;
gsize buf_len = 0;
/* Try to create patch from blobs directly first, as git_patch_from_diff
* can fail with OID type mismatches when the diff was created with
* a NULL tree or when OIDs are zero */
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
if (!git_oid_is_zero (&delta->old_file.id))
git_blob_lookup (&old_blob, repository, &delta->old_file.id);
if (!git_oid_is_zero (&delta->new_file.id))
git_blob_lookup (&new_blob, repository, &delta->new_file.id);
diff_opts.context_lines = self->context_lines;
/* Read from working directory as fallback for unstaged changes */
/* Prefer blobs from index/tree for staged changes */
workdir = git_repository_workdir (repository);
if (workdir != NULL && new_path != NULL)
{
g_autoptr(GFile) file = NULL;
file_path = g_build_filename (workdir, new_path, NULL);
file = g_file_new_for_path (file_path);
if ((contents = g_file_load_bytes (file, NULL, NULL, NULL)))
buf = g_bytes_get_data (contents, &buf_len);
}
if (old_blob != NULL && new_blob != NULL)
{
/* Both blobs available - compare them */
ret = git_patch_from_blobs (&patch, old_blob, old_path, new_blob, new_path, &diff_opts);
}
else if (old_blob != NULL && buf != NULL)
{
/* Compare old blob to working directory file */
ret = git_patch_from_blob_and_buffer (&patch, old_blob, old_path, buf, buf_len, new_path, &diff_opts);
}
else if (old_blob != NULL)
{
/* Old blob but no working directory file - file was deleted */
ret = git_patch_from_blob_and_buffer (&patch, old_blob, old_path, NULL, 0, new_path, &diff_opts);
}
else if (new_blob != NULL)
{
/* New blob available - prefer blob over workdir for staged changes */
/* Copy blob contents into GBytes to keep them alive after repository/blob are released */
if (contents == NULL)
{
const char *blob_content = git_blob_rawcontent (new_blob);
gsize blob_size = git_blob_rawsize (new_blob);
contents = g_bytes_new (blob_content, blob_size);
}
ret = git_patch_from_blob_and_buffer (&patch, NULL, old_path, g_bytes_get_data (contents, NULL), g_bytes_get_size (contents), new_path, &diff_opts);
}
else if (buf != NULL)
{
/* New file - compare NULL to working directory file */
ret = git_patch_from_blob_and_buffer (&patch, NULL, old_path, buf, buf_len, new_path, &diff_opts);
}
else
{
/* Both are zero and no working directory file - empty patch */
ret = git_patch_from_blob_and_buffer (&patch, NULL, old_path, NULL, 0, new_path, &diff_opts);
}
if (ret != 0)
{
/* Fallback to git_patch_from_diff if blob-based creation fails */
ret = _foundry_git_diff_patch_from_diff (self->diff, &patch, self->delta_idx);
}
}
if (ret != 0)
return foundry_git_reject_last_error ();
git_patch = _foundry_git_patch_new_with_bytes (g_steal_pointer (&patch), g_steal_pointer (&contents));
num_hunks = _foundry_git_patch_get_num_hunks (git_patch);
if (num_hunks >= G_MAXUINT)
return dex_future_new_reject (G_IO_ERROR,
G_IO_ERROR_FAILED,
"Too many hunks in patch");
for (gsize i = 0; i < num_hunks; i++)
{
g_autoptr(FoundryGitDiffHunk) hunk = _foundry_git_diff_hunk_new (git_patch, i);
g_list_store_append (store, hunk);
}
return dex_future_new_take_object (g_steal_pointer (&store));
}
static DexFuture *
foundry_git_delta_list_hunks (FoundryVcsDelta *delta)
{
FoundryGitDelta *self = FOUNDRY_GIT_DELTA (delta);
g_assert (FOUNDRY_IS_GIT_DELTA (self));
return dex_thread_spawn ("[git-delta-list-hunks]",
foundry_git_delta_list_hunks_thread,
g_object_ref (self),
g_object_unref);
}
typedef struct
{
FoundryGitDelta *delta;
guint context_lines;
} SerializeData;
static void
serialize_data_free (gpointer data)
{
SerializeData *serialize_data = data;
g_object_unref (serialize_data->delta);
g_free (serialize_data);
}
static DexFuture *
foundry_git_delta_serialize_thread (gpointer data)
{
SerializeData *serialize_data = data;
FoundryGitDelta *self = serialize_data->delta;
guint context_lines = serialize_data->context_lines;
g_autoptr(GString) diff_text = NULL;
g_autoptr(FoundryGitRepositoryPaths) paths = NULL;
g_autoptr(FoundryGitPatch) git_patch = NULL;
g_autoptr(git_patch) patch = NULL;
g_autoptr(git_repository) repository = NULL;
g_autoptr(git_blob) old_blob = NULL;
g_autoptr(git_blob) new_blob = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GBytes) contents = NULL;
const git_diff_delta *delta = NULL;
gsize num_hunks;
const char *old_path = NULL;
const char *new_path = NULL;
g_autofree char *old_path_dup = NULL;
g_autofree char *new_path_dup = NULL;
FoundryVcsDeltaStatus status;
g_autofree char *git_dir = NULL;
int ret;
g_assert (FOUNDRY_IS_GIT_DELTA (self));
g_assert (FOUNDRY_IS_GIT_DIFF (self->diff));
diff_text = g_string_new (NULL);
old_path_dup = foundry_vcs_delta_dup_old_path (FOUNDRY_VCS_DELTA (self));
new_path_dup = foundry_vcs_delta_dup_new_path (FOUNDRY_VCS_DELTA (self));
status = foundry_vcs_delta_get_status (FOUNDRY_VCS_DELTA (self));
/* Print diff header */
if (status == FOUNDRY_VCS_DELTA_STATUS_RENAMED)
g_string_append_printf (diff_text, "diff --git a/%s b/%s\n",
old_path_dup ? old_path_dup : "/dev/null",
new_path_dup ? new_path_dup : "/dev/null");
else if (status == FOUNDRY_VCS_DELTA_STATUS_DELETED)
g_string_append_printf (diff_text, "diff --git a/%s b/%s\n",
old_path_dup ? old_path_dup : "/dev/null",
"/dev/null");
else if (status == FOUNDRY_VCS_DELTA_STATUS_ADDED)
g_string_append_printf (diff_text, "diff --git a/%s b/%s\n",
"/dev/null",
new_path_dup ? new_path_dup : "/dev/null");
else
g_string_append_printf (diff_text, "diff --git a/%s b/%s\n",
old_path_dup ? old_path_dup : "/dev/null",
new_path_dup ? new_path_dup : "/dev/null");
if (old_path_dup && new_path_dup && g_strcmp0 (old_path_dup, new_path_dup) != 0)
g_string_append_printf (diff_text, "rename from %s\nrename to %s\n", old_path_dup, new_path_dup);
/* Create patch with specified context_lines */
delta = _foundry_git_diff_get_delta (self->diff, self->delta_idx);
if (delta == NULL)
return foundry_git_reject_last_error ();
old_path = delta->old_file.path;
new_path = delta->new_file.path;
paths = _foundry_git_diff_dup_paths (self->diff);
if (!foundry_git_repository_paths_open (paths, &repository, &error))
return dex_future_new_for_error (g_steal_pointer (&error));
{
g_autofree char *file_path = NULL;
const char *workdir;
const char *buf = NULL;
gsize buf_len = 0;
/* Try to create patch from blobs directly first, as git_patch_from_diff
* can fail with OID type mismatches when the diff was created with
* a NULL tree or when OIDs are zero */
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
if (!git_oid_is_zero (&delta->old_file.id))
git_blob_lookup (&old_blob, repository, &delta->old_file.id);
if (!git_oid_is_zero (&delta->new_file.id))
git_blob_lookup (&new_blob, repository, &delta->new_file.id);
diff_opts.context_lines = context_lines;
/* Read from working directory as fallback for unstaged changes */
/* Prefer blobs from index/tree for staged changes */
workdir = git_repository_workdir (repository);
if (workdir != NULL && new_path != NULL)
{
g_autoptr(GFile) file = NULL;
file_path = g_build_filename (workdir, new_path, NULL);
file = g_file_new_for_path (file_path);
if ((contents = g_file_load_bytes (file, NULL, NULL, NULL)))
buf = g_bytes_get_data (contents, &buf_len);
}
if (old_blob != NULL && new_blob != NULL)
{
/* Both blobs available - compare them */
ret = git_patch_from_blobs (&patch, old_blob, old_path, new_blob, new_path, &diff_opts);
}
else if (old_blob != NULL && buf != NULL)
{
/* Compare old blob to working directory file */
ret = git_patch_from_blob_and_buffer (&patch, old_blob, old_path, buf, buf_len, new_path, &diff_opts);
}
else if (old_blob != NULL)
{
/* Old blob but no working directory file - file was deleted */
ret = git_patch_from_blob_and_buffer (&patch, old_blob, old_path, NULL, 0, new_path, &diff_opts);
}
else if (new_blob != NULL)
{
/* New blob available - prefer blob over workdir for staged changes */
/* Copy blob contents into GBytes to keep them alive after repository/blob are released */
if (contents == NULL)
{
const char *blob_content = git_blob_rawcontent (new_blob);
gsize blob_size = git_blob_rawsize (new_blob);
contents = g_bytes_new (blob_content, blob_size);
}
ret = git_patch_from_blob_and_buffer (&patch, NULL, old_path, g_bytes_get_data (contents, NULL), g_bytes_get_size (contents), new_path, &diff_opts);
}
else if (buf != NULL)
{
/* New file - compare NULL to working directory file */
ret = git_patch_from_blob_and_buffer (&patch, NULL, old_path, buf, buf_len, new_path, &diff_opts);
}
else
{
/* Both are zero and no working directory file - empty patch */
ret = git_patch_from_blob_and_buffer (&patch, NULL, old_path, NULL, 0, new_path, &diff_opts);
}
if (ret != 0)
{
/* Fallback to git_patch_from_diff if blob-based creation fails */
ret = _foundry_git_diff_patch_from_diff (self->diff, &patch, self->delta_idx);
}
}
if (ret != 0)
return foundry_git_reject_last_error ();
git_patch = _foundry_git_patch_new_with_bytes (g_steal_pointer (&patch), g_steal_pointer (&contents));
num_hunks = _foundry_git_patch_get_num_hunks (git_patch);
/* Print each hunk */
for (gsize i = 0; i < num_hunks; i++)
{
const git_diff_hunk *ghunk;
gsize num_lines;
ghunk = _foundry_git_patch_get_hunk (git_patch, i);
if (ghunk != NULL)
g_string_append (diff_text, ghunk->header);
num_lines = _foundry_git_patch_get_num_lines_in_hunk (git_patch, i);
for (gsize j = 0; j < num_lines; j++)
{
const git_diff_line *gline;
gline = _foundry_git_patch_get_line (git_patch, i, j);
if (gline != NULL)
{
if (g_ascii_isprint (gline->origin))
g_string_append_c (diff_text, (char)gline->origin);
if (gline->content != NULL && gline->content_len > 0)
g_string_append_len (diff_text, gline->content, gline->content_len);
if (gline->content_len == 0 || gline->content[gline->content_len - 1] != '\n')
g_string_append_c (diff_text, '\n');
}
}
}
return dex_future_new_take_string (g_string_free (g_steal_pointer (&diff_text), FALSE));
}
static DexFuture *
foundry_git_delta_serialize (FoundryVcsDelta *delta,
guint context_lines)
{
FoundryGitDelta *self = FOUNDRY_GIT_DELTA (delta);
SerializeData *serialize_data;
g_assert (FOUNDRY_IS_GIT_DELTA (self));
serialize_data = g_new0 (SerializeData, 1);
serialize_data->delta = g_object_ref (self);
serialize_data->context_lines = context_lines;
return dex_thread_spawn ("[git-delta-serialize]",
foundry_git_delta_serialize_thread,
serialize_data,
serialize_data_free);
}
static void
foundry_git_delta_class_init (FoundryGitDeltaClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FoundryVcsDeltaClass *vcs_delta_class = FOUNDRY_VCS_DELTA_CLASS (klass);
object_class->finalize = foundry_git_delta_finalize;
vcs_delta_class->dup_old_path = foundry_git_delta_dup_old_path;
vcs_delta_class->dup_new_path = foundry_git_delta_dup_new_path;
vcs_delta_class->dup_old_id = foundry_git_delta_dup_old_id;
vcs_delta_class->dup_new_id = foundry_git_delta_dup_new_id;
vcs_delta_class->get_old_mode = foundry_git_delta_get_old_mode;
vcs_delta_class->get_new_mode = foundry_git_delta_get_new_mode;
vcs_delta_class->get_status = foundry_git_delta_get_status;
vcs_delta_class->list_hunks = foundry_git_delta_list_hunks;
vcs_delta_class->serialize = foundry_git_delta_serialize;
}
static void
foundry_git_delta_init (FoundryGitDelta *self)
{
self->context_lines = 3;
}
static FoundryVcsDeltaStatus
map_git_delta_status (git_delta_t git_status)
{
switch (git_status)
{
case GIT_DELTA_UNMODIFIED:
return FOUNDRY_VCS_DELTA_STATUS_UNMODIFIED;
case GIT_DELTA_ADDED:
return FOUNDRY_VCS_DELTA_STATUS_ADDED;
case GIT_DELTA_DELETED:
return FOUNDRY_VCS_DELTA_STATUS_DELETED;
case GIT_DELTA_MODIFIED:
return FOUNDRY_VCS_DELTA_STATUS_MODIFIED;
case GIT_DELTA_RENAMED:
return FOUNDRY_VCS_DELTA_STATUS_RENAMED;
case GIT_DELTA_COPIED:
return FOUNDRY_VCS_DELTA_STATUS_COPIED;
case GIT_DELTA_IGNORED:
return FOUNDRY_VCS_DELTA_STATUS_IGNORED;
case GIT_DELTA_UNTRACKED:
return FOUNDRY_VCS_DELTA_STATUS_UNTRACKED;
case GIT_DELTA_TYPECHANGE:
return FOUNDRY_VCS_DELTA_STATUS_TYPECHANGE;
case GIT_DELTA_UNREADABLE:
return FOUNDRY_VCS_DELTA_STATUS_UNREADABLE;
case GIT_DELTA_CONFLICTED:
return FOUNDRY_VCS_DELTA_STATUS_CONFLICTED;
default:
return FOUNDRY_VCS_DELTA_STATUS_UNMODIFIED;
}
}
FoundryGitDelta *
_foundry_git_delta_new (FoundryGitDiff *diff,
gsize delta_idx)
{
FoundryGitDelta *self;
const git_diff_delta *delta;
g_return_val_if_fail (FOUNDRY_IS_GIT_DIFF (diff), NULL);
delta = _foundry_git_diff_get_delta (diff, delta_idx);
g_return_val_if_fail (delta != NULL, NULL);
self = g_object_new (FOUNDRY_TYPE_GIT_DELTA, NULL);
self->diff = g_object_ref (diff);
self->delta_idx = delta_idx;
self->old_path = g_strdup (delta->old_file.path);
self->new_path = g_strdup (delta->new_file.path);
self->old_oid = delta->old_file.id;
self->new_oid = delta->new_file.id;
self->old_mode = delta->old_file.mode;
self->new_mode = delta->new_file.mode;
self->status = map_git_delta_status (delta->status);
return self;
}
void
_foundry_git_delta_set_context_lines (FoundryGitDelta *self,
guint context_lines)
{
g_return_if_fail (FOUNDRY_IS_GIT_DELTA (self));
self->context_lines = context_lines;
}
|