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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
|
/* foundry-git-vcs.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 <glib/gi18n-lib.h>
#include "ssh-agent-sign.h"
#include "foundry-auth-provider.h"
#include "foundry-git-autocleanups.h"
#include "foundry-git-file.h"
#include "foundry-git-file-list-private.h"
#include "foundry-git-error.h"
#include "foundry-git-blame-private.h"
#include "foundry-git-branch-private.h"
#include "foundry-git-file-private.h"
#include "foundry-git-reference-private.h"
#include "foundry-git-repository-private.h"
#include "foundry-git-repository-paths-private.h"
#include "foundry-git-remote-private.h"
#include "foundry-git-tag-private.h"
#include "foundry-git-tree.h"
#include "foundry-git-vcs-private.h"
#include "foundry-operation.h"
#include "foundry-util.h"
/**
* FoundryGitVcs:
*
* Git implementation of the version control system interface.
*
* FoundryGitVcs provides Git-specific functionality for version control operations
* including repository management, branch operations, and remote synchronization.
* It integrates with the Git library to provide a unified interface for Git
* operations within the Foundry development environment.
*/
struct _FoundryGitVcs
{
FoundryVcs parent_instance;
FoundryGitMonitor *monitor;
FoundryGitRepository *repository;
GFile *workdir;
};
G_DEFINE_FINAL_TYPE (FoundryGitVcs, foundry_git_vcs, FOUNDRY_TYPE_VCS)
static char *
foundry_git_vcs_dup_id (FoundryVcs *vcs)
{
return g_strdup ("git");
}
static char *
foundry_git_vcs_dup_name (FoundryVcs *vcs)
{
return g_strdup (_("Git"));
}
static char *
foundry_git_vcs_dup_branch_name (FoundryVcs *vcs)
{
char *branch_name;
branch_name = _foundry_git_repository_dup_branch_name (FOUNDRY_GIT_VCS (vcs)->repository);
if (branch_name == NULL)
return g_strdup ("main");
return branch_name;
}
static guint
foundry_git_vcs_get_priority (FoundryVcs *vcs)
{
return 100;
}
static gboolean
foundry_git_vcs_is_ignored (FoundryVcs *vcs,
const char *relative_path)
{
FoundryGitVcs *self = FOUNDRY_GIT_VCS (vcs);
return _foundry_git_repository_is_ignored (self->repository, relative_path);
}
static gboolean
foundry_git_vcs_is_file_ignored (FoundryVcs *vcs,
GFile *file)
{
FoundryGitVcs *self = FOUNDRY_GIT_VCS (vcs);
g_autofree char *relative_path = NULL;
if (!g_file_has_prefix (file, self->workdir))
return FALSE;
relative_path = g_file_get_relative_path (self->workdir, file);
return _foundry_git_repository_is_ignored (self->repository, relative_path);
}
static DexFuture *
foundry_git_vcs_list_files (FoundryVcs *vcs)
{
return _foundry_git_repository_list_files (FOUNDRY_GIT_VCS (vcs)->repository);
}
static DexFuture *
foundry_git_vcs_list_branches (FoundryVcs *vcs)
{
return _foundry_git_repository_list_branches (FOUNDRY_GIT_VCS (vcs)->repository);
}
static DexFuture *
foundry_git_vcs_list_tags (FoundryVcs *vcs)
{
return _foundry_git_repository_list_tags (FOUNDRY_GIT_VCS (vcs)->repository);
}
static DexFuture *
foundry_git_vcs_list_remotes (FoundryVcs *vcs)
{
return _foundry_git_repository_list_remotes (FOUNDRY_GIT_VCS (vcs)->repository);
}
static DexFuture *
foundry_git_vcs_find_file (FoundryVcs *vcs,
GFile *file)
{
return _foundry_git_repository_find_file (FOUNDRY_GIT_VCS (vcs)->repository, file);
}
static DexFuture *
foundry_git_vcs_find_remote (FoundryVcs *vcs,
const char *name)
{
return _foundry_git_repository_find_remote (FOUNDRY_GIT_VCS (vcs)->repository, name);
}
static DexFuture *
foundry_git_vcs_find_commit (FoundryVcs *vcs,
const char *id)
{
return _foundry_git_repository_find_commit (FOUNDRY_GIT_VCS (vcs)->repository, id);
}
static DexFuture *
foundry_git_vcs_find_tree (FoundryVcs *vcs,
const char *id)
{
return _foundry_git_repository_find_tree (FOUNDRY_GIT_VCS (vcs)->repository, id);
}
static DexFuture *
foundry_git_vcs_blame (FoundryVcs *vcs,
FoundryVcsFile *file,
GBytes *bytes)
{
FoundryGitVcs *self = FOUNDRY_GIT_VCS (vcs);
g_autofree char *relative_path = foundry_vcs_file_dup_relative_path (file);
return _foundry_git_repository_blame (self->repository, relative_path, bytes);
}
static DexFuture *
foundry_git_vcs_diff (FoundryVcs *vcs,
FoundryVcsTree *tree_a,
FoundryVcsTree *tree_b)
{
FoundryGitVcs *self = (FoundryGitVcs *)vcs;
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (FOUNDRY_IS_GIT_TREE (tree_a));
dex_return_error_if_fail (FOUNDRY_IS_GIT_TREE (tree_b));
return _foundry_git_repository_diff (self->repository,
FOUNDRY_GIT_TREE (tree_a),
FOUNDRY_GIT_TREE (tree_b));
}
static DexFuture *
foundry_git_vcs_fetch (FoundryVcs *vcs,
FoundryVcsRemote *remote,
FoundryOperation *operation)
{
g_autoptr(FoundryAuthProvider) auth_provider = foundry_operation_dup_auth_provider (operation);
if (auth_provider == NULL)
{
g_autoptr(FoundryContext) context = foundry_contextual_dup_context (FOUNDRY_CONTEXTUAL (vcs));
auth_provider = foundry_auth_provider_new_for_context (context);
}
return _foundry_git_repository_fetch (FOUNDRY_GIT_VCS (vcs)->repository, auth_provider, remote, operation);
}
static DexFuture *
foundry_git_vcs_list_commits_with_file (FoundryVcs *vcs,
FoundryVcsFile *file)
{
FoundryGitVcs *self = (FoundryGitVcs *)vcs;
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (FOUNDRY_IS_GIT_FILE (file));
return _foundry_git_repository_list_commits_with_file (self->repository, file);
}
static DexFuture *
foundry_git_vcs_describe_line_changes (FoundryVcs *vcs,
FoundryVcsFile *file,
GBytes *contents)
{
FoundryGitVcs *self = (FoundryGitVcs *)vcs;
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (FOUNDRY_IS_GIT_FILE (file));
dex_return_error_if_fail (contents != NULL);
return _foundry_git_repository_describe_line_changes (self->repository, file, contents);
}
static DexFuture *
foundry_git_vcs_query_file_status (FoundryVcs *vcs,
GFile *file)
{
FoundryGitVcs *self = FOUNDRY_GIT_VCS (vcs);
return _foundry_git_repository_query_file_status (self->repository, file);
}
static DexFuture *
foundry_git_vcs_load_tip (FoundryVcs *vcs)
{
FoundryGitVcs *self = FOUNDRY_GIT_VCS (vcs);
return foundry_vcs_find_commit (FOUNDRY_VCS (self), "HEAD");
}
static void
foundry_git_vcs_finalize (GObject *object)
{
FoundryGitVcs *self = (FoundryGitVcs *)object;
g_clear_object (&self->monitor);
g_clear_object (&self->repository);
g_clear_object (&self->workdir);
G_OBJECT_CLASS (foundry_git_vcs_parent_class)->finalize (object);
}
static void
foundry_git_vcs_class_init (FoundryGitVcsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FoundryVcsClass *vcs_class = FOUNDRY_VCS_CLASS (klass);
object_class->finalize = foundry_git_vcs_finalize;
vcs_class->blame = foundry_git_vcs_blame;
vcs_class->dup_branch_name = foundry_git_vcs_dup_branch_name;
vcs_class->dup_id = foundry_git_vcs_dup_id;
vcs_class->dup_name = foundry_git_vcs_dup_name;
vcs_class->fetch = foundry_git_vcs_fetch;
vcs_class->find_commit = foundry_git_vcs_find_commit;
vcs_class->find_tree = foundry_git_vcs_find_tree;
vcs_class->find_file = foundry_git_vcs_find_file;
vcs_class->find_remote = foundry_git_vcs_find_remote;
vcs_class->get_priority = foundry_git_vcs_get_priority;
vcs_class->is_file_ignored = foundry_git_vcs_is_file_ignored;
vcs_class->is_ignored = foundry_git_vcs_is_ignored;
vcs_class->list_branches = foundry_git_vcs_list_branches;
vcs_class->list_files = foundry_git_vcs_list_files;
vcs_class->list_remotes = foundry_git_vcs_list_remotes;
vcs_class->list_tags = foundry_git_vcs_list_tags;
vcs_class->list_commits_with_file = foundry_git_vcs_list_commits_with_file;
vcs_class->diff = foundry_git_vcs_diff;
vcs_class->describe_line_changes = foundry_git_vcs_describe_line_changes;
vcs_class->query_file_status = foundry_git_vcs_query_file_status;
vcs_class->load_tip = foundry_git_vcs_load_tip;
}
static void
foundry_git_vcs_init (FoundryGitVcs *self)
{
}
static void
foundry_git_vcs_changed_cb (FoundryGitVcs *self,
FoundryGitMonitor *monitor)
{
g_assert (FOUNDRY_IS_GIT_VCS (self));
g_assert (FOUNDRY_IS_GIT_MONITOR (monitor));
g_object_notify (G_OBJECT (self), "branch-name");
foundry_vcs_emit_tip_changed (FOUNDRY_VCS (self));
}
static DexFuture *
foundry_git_vcs_setup_monitor_cb (DexFuture *future,
gpointer user_data)
{
FoundryGitVcs *self = user_data;
g_assert (FOUNDRY_IS_GIT_VCS (self));
if ((self->monitor = dex_await_object (dex_ref (future), NULL)))
g_signal_connect_object (self->monitor,
"changed",
G_CALLBACK (foundry_git_vcs_changed_cb),
self,
G_CONNECT_SWAPPED);
return dex_future_new_true ();
}
DexFuture *
_foundry_git_vcs_new (FoundryContext *context,
git_repository *repository)
{
FoundryGitVcs *self;
DexFuture *future;
g_return_val_if_fail (FOUNDRY_IS_CONTEXT (context), NULL);
g_return_val_if_fail (repository != NULL, NULL);
self = g_object_new (FOUNDRY_TYPE_GIT_VCS,
"context", context,
NULL);
self->workdir = g_file_new_for_path (git_repository_workdir (repository));
self->repository = _foundry_git_repository_new (g_steal_pointer (&repository));
future = _foundry_git_repository_create_monitor (self->repository);
future = dex_future_then (future,
foundry_git_vcs_setup_monitor_cb,
g_object_ref (self),
g_object_unref);
future = dex_future_finally (future,
foundry_future_return_object,
g_object_ref (self),
g_object_unref);
return g_steal_pointer (&future);
}
typedef struct _Initialize
{
GFile *directory;
guint bare : 1;
} Initialize;
static void
initialize_free (Initialize *state)
{
g_clear_object (&state->directory);
g_free (state);
}
static DexFuture *
foundry_git_initialize_thread (gpointer data)
{
Initialize *state = data;
g_autoptr(git_repository) repository = NULL;
g_autofree char *path = NULL;
g_assert (state != NULL);
g_assert (G_IS_FILE (state->directory));
g_assert (g_file_is_native (state->directory));
path = g_file_get_path (state->directory);
if (git_repository_init (&repository, path, state->bare) != 0)
return foundry_git_reject_last_error ();
return dex_future_new_true ();
}
/**
* foundry_git_initialize:
*
* Initializes a new git repository.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to
* any value if successful or rejects with error.
*/
DexFuture *
foundry_git_initialize (GFile *directory,
gboolean bare)
{
Initialize *state;
dex_return_error_if_fail (G_IS_FILE (directory));
dex_return_error_if_fail (g_file_is_native (directory));
state = g_new0 (Initialize, 1);
state->directory = g_object_ref (directory);
state->bare = !!bare;
return dex_thread_spawn ("[git-initialize]",
foundry_git_initialize_thread,
state,
(GDestroyNotify) initialize_free);
}
/**
* foundry_git_vcs_list_status:
* @self: a [class@Foundry.GitVcs]
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to a
* [iface@Gio.ListModel] of [class@Foundry.GitStatusEntry].
*/
DexFuture *
foundry_git_vcs_list_status (FoundryGitVcs *self)
{
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
return _foundry_git_repository_list_status (self->repository);
}
/**
* foundry_git_vcs_stage_entry:
* @self: a [class@Foundry.GitVcs]
* @entry: a [class@Foundry.GitStatusEntry]
* @contents: (nullable): optional contents to use instead of what is in
* the working tree.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to any value
* or rejects with error.
*/
DexFuture *
foundry_git_vcs_stage_entry (FoundryGitVcs *self,
FoundryGitStatusEntry *entry,
GBytes *contents)
{
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (FOUNDRY_IS_GIT_STATUS_ENTRY (entry));
return _foundry_git_repository_stage_entry (self->repository, entry, contents);
}
/**
* foundry_git_vcs_unstage_entry:
* @self: a [class@Foundry.GitVcs]
* @entry: a [class@Foundry.GitStatusEntry]
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to any value
* or rejects with error.
*/
DexFuture *
foundry_git_vcs_unstage_entry (FoundryGitVcs *self,
FoundryGitStatusEntry *entry)
{
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (FOUNDRY_IS_GIT_STATUS_ENTRY (entry));
return _foundry_git_repository_unstage_entry (self->repository, entry);
}
/**
* foundry_git_vcs_commit:
* @self: a [class@Foundry.GitVcs]
* @message:
* @author_name: (nullable):
* @author_email: (nullable):
*
* Simple API to create a new commit from the index.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to
* a [class@Foundry.GitCommit] or rejects with error.
*/
DexFuture *
foundry_git_vcs_commit (FoundryGitVcs *self,
const char *message,
const char *author_name,
const char *author_email)
{
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (message != NULL);
return _foundry_git_repository_commit (self->repository, message, author_name, author_email);
}
/**
* foundry_git_vcs_query_config:
* @self: a [class@Foundry.GitVcs]
* @key: the config key to query
*
* Queries a git configuration value by key from the repository.
*
* The method runs asynchronously in a background thread and returns a
* [class@Dex.Future] that resolves to the config value as a string.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to a string
* containing the config value, or rejects with an error if the key is not
* found or an error occurs
*
* Since: 1.1
*/
DexFuture *
foundry_git_vcs_query_config (FoundryGitVcs *self,
const char *key)
{
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (key != NULL);
return _foundry_git_repository_query_config (self->repository, key);
}
char *
_foundry_git_vcs_dup_git_dir (FoundryGitVcs *self)
{
g_autoptr(FoundryGitRepositoryPaths) paths = NULL;
g_return_val_if_fail (FOUNDRY_IS_GIT_VCS (self), NULL);
paths = _foundry_git_repository_dup_paths (self->repository);
return foundry_git_repository_paths_dup_git_dir (paths);
}
GFile *
_foundry_git_vcs_dup_workdir (FoundryGitVcs *self)
{
g_autoptr(FoundryGitRepositoryPaths) paths = NULL;
g_autofree char *workdir_path = NULL;
g_return_val_if_fail (FOUNDRY_IS_GIT_VCS (self), NULL);
paths = _foundry_git_repository_dup_paths (self->repository);
workdir_path = foundry_git_repository_paths_dup_workdir (paths);
return g_file_new_for_path (workdir_path);
}
FoundryGitRepositoryPaths *
_foundry_git_vcs_dup_paths (FoundryGitVcs *self)
{
g_return_val_if_fail (FOUNDRY_IS_GIT_VCS (self), NULL);
return _foundry_git_repository_dup_paths (self->repository);
}
static char *
armor_sshsig (GBytes *sshsig_bin)
{
gsize len = 0;
const guint8 *data = g_bytes_get_data (sshsig_bin, &len);
g_autofree char *b64 = g_base64_encode (data, len);
GString *s = g_string_new("-----BEGIN SSH SIGNATURE-----\n");
gsize blen = strlen (b64);
const char *iter = b64;
const char *endptr = iter + blen;
while (iter < endptr)
{
g_string_append_len (s, iter, MIN (70, endptr - iter));
g_string_append_c (s, '\n');
iter += 70;
}
g_string_append (s, "-----END SSH SIGNATURE-----");
return g_string_free (s, FALSE);
}
static inline void
put_u32 (GByteArray *b,
guint32 v)
{
guint32 be = GINT32_TO_BE(v);
g_byte_array_append(b, (guint8 *)&be, 4);
}
static inline void
put_string (GByteArray *b,
const void *data,
gsize len)
{
put_u32 (b, (guint32)len);
if (len)
g_byte_array_append (b, (const guint8 *)data, len);
}
static GBytes *
build_sshsig (const guint8 *pubkey_blob,
gsize pubkey_blob_len,
const guint8 *sha512_hash,
gsize sha512_hash_len,
const guint8 *agent_sig_blob,
gsize agent_sig_blob_len)
{
g_autoptr(GByteArray) arr = g_byte_array_new();
const gchar *namespace = "git";
const gchar *hash_alg = "sha512";
const gchar *magic = "SSHSIG";
g_assert (sha512_hash_len == 64);
/* magic is first bytes */
g_byte_array_append (arr, (const guint8 *)magic, strlen (magic));
/* uint32 version = 1 */
put_u32 (arr, 1);
/* string pubkey_blob */
put_string (arr, pubkey_blob, pubkey_blob_len);
/* string namespace "git" */
put_string (arr, namespace, strlen (namespace));
/* string reserved "" */
put_string (arr, "", 0);
/* string hash algorithm "sha512" */
put_string (arr, hash_alg, strlen (hash_alg));
/* string signature (raw agent signature blob) */
put_string (arr, agent_sig_blob, agent_sig_blob_len);
return g_byte_array_free_to_bytes (g_steal_pointer (&arr));
}
static char *
sign_bytes_ssh (GBytes *data,
const char *signing_key,
GError **error)
{
g_autoptr(GByteArray) preimage = NULL;
g_autoptr(GBytes) sig_bytes = NULL;
g_autoptr(GBytes) sshsig_bytes = NULL;
g_autoptr(GChecksum) checksum = NULL;
g_auto(GStrv) tokens = NULL;
g_autofree guint8 *hash = NULL;
g_autofree guint8 *pubkey_blob = NULL;
const char *b64;
const guint8 *agent_sig_data = NULL;
gsize agent_sig_len = 0;
gsize blob_len = 0;
gsize digest_len;
/* Parse public key line: "algo base64 [comment]" */
tokens = g_strsplit_set (signing_key, " \t", 3);
if (!tokens[0] || !tokens[1])
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Invalid SSH public key line: '%s'",
signing_key);
return NULL;
}
b64 = tokens[1];
pubkey_blob = g_base64_decode (b64, &blob_len);
if (!pubkey_blob || blob_len == 0)
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
"Failed to base64-decode SSH public key blob");
return NULL;
}
checksum = g_checksum_new (G_CHECKSUM_SHA512);
g_checksum_update (checksum,
g_bytes_get_data (data, NULL),
g_bytes_get_size (data));
digest_len = g_checksum_type_get_length (G_CHECKSUM_SHA512);
hash = g_malloc (digest_len);
g_checksum_get_digest (checksum, hash, &digest_len);
preimage = g_byte_array_new ();
g_byte_array_append (preimage, (guint8 *)"SSHSIG", strlen ("SSHSIG"));
put_string (preimage, "git", strlen ("git"));
put_string (preimage, "", 0);
put_string (preimage, "sha512", strlen ("sha512"));
put_string (preimage, hash, digest_len);
/* Sign the structure (not the raw commit content) */
if (!ssh_agent_sign_data_for_pubkey (signing_key, preimage->data, preimage->len, &sig_bytes, error))
return NULL;
agent_sig_data = g_bytes_get_data (sig_bytes, &agent_sig_len);
/* Build the complete SSH signature format */
sshsig_bytes = build_sshsig (pubkey_blob,
blob_len,
hash,
digest_len,
agent_sig_data,
agent_sig_len);
/* Armor the SSH signature for git */
return armor_sshsig (sshsig_bytes);
}
static char *
sign_bytes_gpg (GBytes *data,
const char *signing_key,
GError **error)
{
g_autoptr(GSubprocess) subprocess = NULL;
g_autoptr(GBytes) stdout_bytes = NULL;
GOutputStream *stdin_stream = NULL;
GInputStream *stdout_stream = NULL;
const guint8 *stdout_data;
gsize stdout_size;
g_assert (data != NULL);
g_assert (signing_key != NULL);
if (!(subprocess = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE |
G_SUBPROCESS_FLAGS_STDOUT_PIPE |
G_SUBPROCESS_FLAGS_STDERR_SILENCE,
error,
"gpg",
"--detach-sign",
"--armor",
"--local-user",
signing_key,
NULL)))
return NULL;
stdin_stream = g_subprocess_get_stdin_pipe (subprocess);
stdout_stream = g_subprocess_get_stdout_pipe (subprocess);
if (!g_output_stream_write_all (stdin_stream,
g_bytes_get_data (data, NULL),
g_bytes_get_size (data),
NULL, NULL, error))
return NULL;
g_output_stream_close (stdin_stream, NULL, NULL);
if (!g_subprocess_wait_check (subprocess, NULL, error))
return NULL;
if (!(stdout_bytes = g_input_stream_read_bytes (stdout_stream, G_MAXSSIZE, NULL, error)))
return NULL;
stdout_data = g_bytes_get_data (stdout_bytes, &stdout_size);
if (!g_utf8_validate ((char *)stdout_data, stdout_size, NULL))
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_DATA,
"Invalid UTF-8 received from gpg");
return NULL;
}
return g_strndup ((const char *)stdout_data, stdout_size);
}
typedef struct _SignBytes
{
GBytes *bytes;
char *signing_format;
char *signing_key;
} SignBytes;
static void
sign_bytes_free (SignBytes *state)
{
g_clear_pointer (&state->bytes, g_bytes_unref);
g_clear_pointer (&state->signing_format, g_free);
g_clear_pointer (&state->signing_key, g_free);
g_free (state);
}
static DexFuture *
foundry_git_vcs_sign_bytes_thread (gpointer user_data)
{
SignBytes *state = user_data;
g_autoptr(GError) error = NULL;
g_autofree char *ret = NULL;
g_assert (state != NULL);
g_assert (state->bytes != NULL);
g_assert (state->signing_format != NULL);
g_assert (state->signing_key != NULL);
if (foundry_str_equal0 (state->signing_format, "gpg"))
{
if (!(ret = sign_bytes_gpg (state->bytes, state->signing_key, &error)))
return dex_future_new_for_error (g_steal_pointer (&error));
return dex_future_new_take_string (g_steal_pointer (&ret));
}
if (foundry_str_equal0 (state->signing_format, "ssh"))
{
if (!(ret = sign_bytes_ssh (state->bytes, state->signing_key, &error)))
return dex_future_new_for_error (g_steal_pointer (&error));
return dex_future_new_take_string (g_steal_pointer (&ret));
}
return dex_future_new_reject (G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"Signing format `%s` is not supported",
state->signing_format);
}
char *
_foundry_git_vcs_sign_bytes (const char *signing_format,
const char *signing_key,
GBytes *bytes,
GError **error)
{
g_autoptr(DexFuture) future = NULL;
const GValue *value;
SignBytes *state;
char *ret = NULL;
g_return_val_if_fail (signing_format != NULL, NULL);
g_return_val_if_fail (signing_key != NULL, NULL);
g_return_val_if_fail (bytes != NULL, NULL);
state = g_new0 (SignBytes, 1);
state->signing_format = g_strdup (signing_format);
state->signing_key = g_strdup (signing_key);
state->bytes = g_bytes_ref (bytes);
future = foundry_git_vcs_sign_bytes_thread (state);
if ((value = dex_future_get_value (future, error)))
ret = g_value_dup_string (value);
sign_bytes_free (state);
return ret;
}
/**
* foundry_git_vcs_sign_bytes:
* @self: a [class@Foundry.GitVcs]
* @signing_format: the format such as "gpg" or "ssh" such as from the
* config value for "gpg.format".
* @signing_key: the signing key to use such as from the config
* value for "user.signingKey"
* @bytes: the bytes to sign
*
* Signs @bytes using the format and key specified.
*
* Use [method@Foundry.GitVcs.query_config] to get the
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to a
* armor contained string.
*
* Since: 1.1
*/
DexFuture *
foundry_git_vcs_sign_bytes (FoundryGitVcs *self,
const char *signing_format,
const char *signing_key,
GBytes *bytes)
{
SignBytes *state;
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
dex_return_error_if_fail (signing_format != NULL);
dex_return_error_if_fail (signing_key != NULL);
dex_return_error_if_fail (bytes != NULL);
state = g_new0 (SignBytes, 1);
state->signing_format = g_strdup (signing_format);
state->signing_key = g_strdup (signing_key);
state->bytes = g_bytes_ref (bytes);
return dex_thread_spawn ("[foundry-git-vcs-sign-bytes]",
foundry_git_vcs_sign_bytes_thread,
state,
(GDestroyNotify) sign_bytes_free);
}
/**
* foundry_git_vcs_stash:
* @self: a [class@Foundry.GitVcs]
*
* Stashes the current working directory changes.
*
* This method saves the current changes in the working directory to the stash,
* similar to running "git stash". The changes are saved with the default stash
* options.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to
* a [class@Foundry.GitCommit] representing the stash commit or
* rejects with error.
*
* Since: 1.1
*/
DexFuture *
foundry_git_vcs_stash (FoundryGitVcs *self)
{
dex_return_error_if_fail (FOUNDRY_IS_GIT_VCS (self));
return _foundry_git_repository_stash (self->repository);
}
|