tenseleyflow/gitswitch / 63963be

Browse files

fix: verify git config against correct scope and suppress ssh logging

- git_set_config now verifies against the same scope it wrote to
- git_test_config now uses the scope parameter instead of effective config
- SSH command uses StrictHostKeyChecking=accept-new and LogLevel=ERROR
to suppress 'permanently added to known hosts' messages
Authored by espadonne
SHA
63963beb53ddbf68e92889db5a4a6ad37181c05d
Parents
88a88fe
Tree
7e982cd

1 changed file

StatusFile+-
M src/git_ops.c 35 26
src/git_ops.cmodified
@@ -110,11 +110,13 @@ int git_set_config(const account_t *account, git_scope_t scope) {
110110
         git_unset_config_value(GIT_CONFIG_CORE_SSHCOMMAND, scope);
111111
     }
112112
     
113
-    /* Verify configuration was set correctly */
114
-    git_current_config_t current_config;
115
-    if (git_get_current_config(&current_config) == 0) {
116
-        if (strcmp(current_config.name, account->name) != 0 ||
117
-            strcmp(current_config.email, account->email) != 0) {
113
+    /* Verify configuration was set correctly - check the same scope we just wrote to */
114
+    char verify_name[MAX_NAME_LEN];
115
+    char verify_email[MAX_EMAIL_LEN];
116
+    if (git_get_config_value(GIT_CONFIG_USER_NAME, verify_name, sizeof(verify_name), scope) == 0 &&
117
+        git_get_config_value(GIT_CONFIG_USER_EMAIL, verify_email, sizeof(verify_email), scope) == 0) {
118
+        if (strcmp(verify_name, account->name) != 0 ||
119
+            strcmp(verify_email, account->email) != 0) {
118120
             set_error(ERR_GIT_CONFIG_FAILED, "Git configuration verification failed");
119121
             return -1;
120122
         }
@@ -262,55 +264,62 @@ git_scope_t git_get_config_scope(const char *config_key) {
262264
 
263265
 /* Test git configuration */
264266
 int git_test_config(const account_t *account, git_scope_t scope) {
265
-    git_current_config_t current_config;
266
-    (void)scope; /* Suppress unused parameter warning */
267
-    
267
+    char verify_name[MAX_NAME_LEN];
268
+    char verify_email[MAX_EMAIL_LEN];
269
+
268270
     if (!account) {
269271
         set_error(ERR_INVALID_ARGS, "NULL account to git_test_config");
270272
         return -1;
271273
     }
272
-    
274
+
273275
     log_info("Testing git configuration for account: %s", account->name);
274
-    
275
-    /* Get current configuration and verify it matches */
276
-    if (git_get_current_config(&current_config) != 0) {
277
-        set_error(ERR_GIT_CONFIG_FAILED, "Failed to read current git configuration");
276
+
277
+    /* Get configuration from the specified scope and verify it matches */
278
+    if (git_get_config_value(GIT_CONFIG_USER_NAME, verify_name, sizeof(verify_name), scope) != 0 ||
279
+        git_get_config_value(GIT_CONFIG_USER_EMAIL, verify_email, sizeof(verify_email), scope) != 0) {
280
+        set_error(ERR_GIT_CONFIG_FAILED, "Failed to read git configuration from %s scope",
281
+                  git_scope_to_flag(scope));
278282
         return -1;
279283
     }
280
-    
281
-    if (strcmp(current_config.name, account->name) != 0) {
284
+
285
+    if (strcmp(verify_name, account->name) != 0) {
282286
         set_error(ERR_GIT_CONFIG_FAILED, "Git user.name does not match account: expected '%s', got '%s'",
283
-                  account->name, current_config.name);
287
+                  account->name, verify_name);
284288
         return -1;
285289
     }
286
-    
287
-    if (strcmp(current_config.email, account->email) != 0) {
290
+
291
+    if (strcmp(verify_email, account->email) != 0) {
288292
         set_error(ERR_GIT_CONFIG_FAILED, "Git user.email does not match account: expected '%s', got '%s'",
289
-                  account->email, current_config.email);
293
+                  account->email, verify_email);
290294
         return -1;
291295
     }
292296
     
293297
     /* Test GPG configuration if enabled */
294298
     if (account->gpg_enabled && strlen(account->gpg_key_id) > 0) {
295
-        if (strlen(current_config.signing_key) == 0) {
299
+        char signing_key[MAX_KEY_ID_LEN];
300
+        char gpg_sign[16];
301
+
302
+        if (git_get_config_value(GIT_CONFIG_USER_SIGNINGKEY, signing_key, sizeof(signing_key), scope) != 0 ||
303
+            strlen(signing_key) == 0) {
296304
             set_error(ERR_GIT_CONFIG_FAILED, "GPG signing key not configured in git");
297305
             return -1;
298306
         }
299
-        
300
-        if (!current_config.gpg_signing_enabled) {
307
+
308
+        if (git_get_config_value(GIT_CONFIG_COMMIT_GPGSIGN, gpg_sign, sizeof(gpg_sign), scope) != 0 ||
309
+            strcmp(gpg_sign, "true") != 0) {
301310
             log_warning("GPG signing is configured but not enabled");
302311
         }
303
-        
312
+
304313
         /* Test GPG key availability */
305314
         char gpg_test[256];
306
-        snprintf(gpg_test, sizeof(gpg_test), "gpg --list-secret-keys '%s' >/dev/null 2>&1", 
315
+        snprintf(gpg_test, sizeof(gpg_test), "gpg --list-secret-keys '%s' >/dev/null 2>&1",
307316
                  account->gpg_key_id);
308317
         if (system(gpg_test) != 0) {
309318
             set_error(ERR_GPG_KEY_NOT_FOUND, "GPG key not available: %s", account->gpg_key_id);
310319
             return -1;
311320
         }
312321
     }
313
-    
322
+
314323
     log_info("Git configuration test passed for %s", account->name);
315324
     return 0;
316325
 }
@@ -475,7 +484,7 @@ int git_configure_ssh(const account_t *account, git_scope_t scope) {
475484
     
476485
     /* Build SSH command with security options */
477486
     if ((size_t)snprintf(ssh_command, sizeof(ssh_command),
478
-                        "ssh -i '%s' -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no",
487
+                        "ssh -i '%s' -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -o LogLevel=ERROR",
479488
                         expanded_key_path) >= sizeof(ssh_command)) {
480489
         set_error(ERR_INVALID_ARGS, "SSH command too long");
481490
         return -1;