gitbound 1.0.0

A safety-first local GitHub identity manager
Documentation
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
use crate::{
    config::{Profile, expand_path},
    error::GitBoundError,
    process::{Runner, os_args},
    remote::{RemoteInfo, RemoteProtocol, parse_remote},
};
use std::{
    collections::BTreeMap,
    ffi::OsString,
    path::{Path, PathBuf},
    time::Duration,
};

const TIMEOUT: Duration = Duration::from_secs(15);
/// GitBound's own bookkeeping keys, removed wholesale on unbind.
const BINDING_KEYS: &[&str] = &["gitbound.profile", "gitbound.version"];
/// Git's identity keys that a binding writes and a snapshot must capture.
const IDENTITY_KEYS: &[&str] = &[
    "user.name",
    "user.email",
    "core.sshCommand",
    "user.signingKey",
    "gpg.format",
    "commit.gpgSign",
];
const BACKUP_KEYS: &[(&str, &str, &str)] = &[
    (
        "user.name",
        "gitbound.backupUserNamePresent",
        "gitbound.backupUserName",
    ),
    (
        "user.email",
        "gitbound.backupUserEmailPresent",
        "gitbound.backupUserEmail",
    ),
    (
        "core.sshCommand",
        "gitbound.backupSshCommandPresent",
        "gitbound.backupSshCommand",
    ),
    (
        "user.signingKey",
        "gitbound.backupSigningKeyPresent",
        "gitbound.backupSigningKey",
    ),
    (
        "gpg.format",
        "gitbound.backupSigningFormatPresent",
        "gitbound.backupSigningFormat",
    ),
    (
        "commit.gpgSign",
        "gitbound.backupCommitSigningPresent",
        "gitbound.backupCommitSigning",
    ),
];

/// The key prefix GitPersona used before the rename to GitBound. A repository
/// bound by that version still carries these names, so every read below would
/// report it as unbound — which also made `unbind` refuse it and strand the
/// backed-up original identity that `unbind` promises to restore.
const LEGACY_PREFIX: &str = "gitpersona.";
const PREFIX: &str = "gitbound.";

/// The pre-rename spelling of one of GitBound's own keys, or `None` for the
/// Git-native identity keys, which were never renamed.
fn legacy_key(key: &str) -> Option<String> {
    key.strip_prefix(PREFIX)
        .map(|rest| format!("{LEGACY_PREFIX}{rest}"))
}

pub struct Git<'a> {
    runner: &'a dyn Runner,
    cwd: Option<PathBuf>,
}

impl<'a> Git<'a> {
    pub fn new(runner: &'a dyn Runner) -> Self {
        Self { runner, cwd: None }
    }

    pub fn at(runner: &'a dyn Runner, cwd: impl AsRef<Path>) -> Self {
        Self {
            runner,
            cwd: Some(cwd.as_ref().to_path_buf()),
        }
    }

    fn run(&self, args: &[OsString]) -> Result<crate::process::ProcessOutput, GitBoundError> {
        match &self.cwd {
            Some(cwd) => self.runner.run_git_in(args, cwd, TIMEOUT),
            None => self.runner.run_git(args, TIMEOUT),
        }
    }

    pub fn ensure_repo(&self) -> Result<PathBuf, GitBoundError> {
        let output = self.run(&os_args(&["rev-parse", "--show-toplevel"]))?;
        if !output.success() {
            return Err(GitBoundError::usage(
                "current directory is not inside a Git worktree",
            ));
        }
        Ok(PathBuf::from(output.stdout.trim()))
    }

    pub fn common_dir(&self) -> Result<PathBuf, GitBoundError> {
        let root = self.ensure_repo()?;
        let output = self.run(&os_args(&["rev-parse", "--git-common-dir"]))?;
        if !output.success() {
            return Err(GitBoundError::dependency(
                "could not locate the Git common directory",
            ));
        }
        let path = PathBuf::from(output.stdout.trim());
        Ok(if path.is_absolute() {
            path
        } else {
            root.join(path)
        })
    }

    pub fn get(&self, key: &str, local: bool) -> Result<Option<String>, GitBoundError> {
        let mut args = vec![OsString::from("config")];
        if local {
            args.push(OsString::from("--local"));
        }
        args.extend([OsString::from("--get"), OsString::from(key)]);
        let output = self.run(&args)?;
        match output.code {
            Some(0) => Ok(Some(output.stdout.trim_end().to_string())),
            Some(1) => Ok(None),
            _ => Err(GitBoundError::dependency(format!(
                "git config --get {key} failed: {}",
                output.stderr.trim()
            ))),
        }
    }

    pub fn get_all(&self, key: &str, local: bool) -> Result<Vec<String>, GitBoundError> {
        let mut args = vec![OsString::from("config")];
        if local {
            args.push(OsString::from("--local"));
        }
        args.extend([OsString::from("--get-all"), OsString::from(key)]);
        let output = self.run(&args)?;
        match output.code {
            Some(0) => Ok(output.stdout.lines().map(str::to_string).collect()),
            Some(1) => Ok(Vec::new()),
            _ => Err(GitBoundError::dependency(format!(
                "git config --get-all {key} failed: {}",
                output.stderr.trim()
            ))),
        }
    }

    fn set(&self, key: &str, value: &str) -> Result<(), GitBoundError> {
        let output = self.run(&[
            OsString::from("config"),
            OsString::from("--local"),
            OsString::from(key),
            OsString::from(value),
        ])?;
        if output.success() {
            Ok(())
        } else {
            Err(GitBoundError::dependency(format!(
                "could not set {key}: {}",
                output.stderr.trim()
            )))
        }
    }

    fn unset(&self, key: &str) -> Result<(), GitBoundError> {
        let output = self.run(&[
            OsString::from("config"),
            OsString::from("--local"),
            OsString::from("--unset-all"),
            OsString::from(key),
        ])?;
        if matches!(output.code, Some(0 | 5)) {
            Ok(())
        } else {
            Err(GitBoundError::dependency(format!(
                "could not unset {key}: {}",
                output.stderr.trim()
            )))
        }
    }

    pub fn remote(&self, name: &str) -> Result<Option<RemoteInfo>, GitBoundError> {
        let output = self.run(&[
            OsString::from("remote"),
            OsString::from("get-url"),
            OsString::from(name),
        ])?;
        if !output.success() {
            return Ok(None);
        }
        parse_remote(output.stdout.trim()).map(Some)
    }

    pub fn expected_ssh_command(profile: &Profile) -> Result<String, GitBoundError> {
        let key = profile.ssh_key.as_ref().ok_or_else(|| {
            GitBoundError::usage("SSH remotes require the profile to define --ssh-key")
        })?;
        let path = expand_path(key)?;
        if !path.is_file() {
            return Err(GitBoundError::usage(format!(
                "SSH key does not exist: {}",
                path.display()
            )));
        }
        // Escape the four characters that keep their meaning inside a
        // double-quoted POSIX sh word. `!` is deliberately not escaped: it is
        // only special to interactive history expansion, and `\!` inside double
        // quotes survives literally, which would corrupt the key path.
        let escaped = path
            .to_string_lossy()
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
            .replace('$', "\\$")
            .replace('`', "\\`");
        Ok(format!("ssh -i \"{escaped}\" -o IdentitiesOnly=yes"))
    }

    /// Whether Git is configured to delegate credentials for `hostname` to the
    /// GitHub CLI. `scheme` must be the scheme of the remote actually in use:
    /// Git scopes `credential.<url>.helper` by scheme, so checking the `https`
    /// key for an `http` remote reports configuration that does not apply.
    pub fn has_compatible_credential_helper(
        &self,
        scheme: &str,
        hostname: &str,
    ) -> Result<bool, GitBoundError> {
        let scoped = format!("credential.{scheme}://{hostname}.helper");
        let values = self
            .get_all(&scoped, false)?
            .into_iter()
            .chain(self.get_all("credential.helper", false)?);
        Ok(values.into_iter().any(|value| {
            value
                .to_ascii_lowercase()
                .contains("gh auth git-credential")
        }))
    }

    pub fn binding_drifted(
        &self,
        profile: &Profile,
        remote: Option<&RemoteInfo>,
    ) -> Result<bool, GitBoundError> {
        if self.get("user.name", true)?.as_deref() != Some(profile.git_name.as_str()) {
            return Ok(true);
        }
        if self.get("user.email", true)?.as_deref() != Some(profile.git_email.as_str()) {
            return Ok(true);
        }
        // This compares exactly what `bind` writes, so drift is whichever
        // managed value no longer matches. The SSH arm must not `return`
        // early: doing so made every signing setting invisible on exactly the
        // repositories most likely to have one, so a tampered `user.signingKey`
        // read as clean and `bind` rebound over it without asking for
        // `--force`.
        let expected_ssh_command = match remote.map(|remote| remote.protocol) {
            Some(RemoteProtocol::Ssh) => Some(Self::expected_ssh_command(profile)?),
            // `bind` unsets `core.sshCommand` for every other transport, so a
            // value that reappeared is drift just as much as a wrong one.
            _ => None,
        };
        if self.get("core.sshCommand", true)? != expected_ssh_command {
            return Ok(true);
        }
        if let Some(key) = &profile.signing_key {
            if self.get("user.signingKey", true)?.as_deref() != Some(key.as_str())
                || self.get("gpg.format", true)?.as_deref()
                    != Some(profile.signing_format.as_git_value())
                || self.get("commit.gpgSign", true)?.as_deref()
                    != profile.require_signing.then_some("true")
            {
                return Ok(true);
            }
        } else if self.get("user.signingKey", true)?.is_some()
            || self.get("gpg.format", true)?.is_some()
            || self.get("commit.gpgSign", true)?.is_some()
        {
            return Ok(true);
        }
        Ok(false)
    }

    /// The bound profile name, honouring a binding written before the rename.
    ///
    /// Reads fall back to the legacy key rather than rewriting it, because
    /// `status` and `check` must not mutate the repository they inspect.
    /// [`Git::migrate_legacy_binding`] does the rewrite, and every writing
    /// path calls it first.
    pub fn binding_profile(&self, local: bool) -> Result<Option<String>, GitBoundError> {
        match self.get("gitbound.profile", local)? {
            Some(name) => Ok(Some(name)),
            None => self.get("gitpersona.profile", local),
        }
    }

    /// Move a pre-rename binding onto the current keys, reporting whether it
    /// moved.
    ///
    /// Only GitBound's own bookkeeping moves: `user.email` and the rest are
    /// Git's own names and were never renamed. A repository that already has a
    /// `gitbound.profile` is left untouched, so this is a no-op everywhere
    /// except the repositories the rename stranded.
    pub fn migrate_legacy_binding(&self) -> Result<bool, GitBoundError> {
        if self.get("gitbound.profile", true)?.is_some()
            || self.get("gitpersona.profile", true)?.is_none()
        {
            return Ok(false);
        }
        let keys = transaction_keys();
        let mut snapshot = self.snapshot(&keys)?;
        for key in &keys {
            if let Some(legacy) = legacy_key(key) {
                snapshot.insert(legacy.clone(), self.get(&legacy, true)?);
            }
        }
        let result = (|| {
            for key in &keys {
                let Some(legacy) = legacy_key(key) else {
                    continue;
                };
                if let Some(value) = self.get(&legacy, true)? {
                    self.set(key, &value)?;
                }
                self.unset(&legacy)?;
            }
            Ok(())
        })();
        if let Err(error) = result {
            let _ = self.restore(&snapshot);
            return Err(error);
        }
        Ok(true)
    }

    pub fn bind(
        &self,
        profile_name: &str,
        profile: &Profile,
        remote: Option<&RemoteInfo>,
        force: bool,
        old_profile: Option<&Profile>,
    ) -> Result<(), GitBoundError> {
        self.ensure_repo()?;
        self.migrate_legacy_binding()?;
        let existing = self.get("gitbound.profile", true)?;
        if existing.is_some() && !force {
            if let Some(old) = old_profile {
                if self.binding_drifted(old, remote)? {
                    return Err(GitBoundError::usage(
                        "managed Git settings have changed; inspect them and rerun with --force to rebind",
                    ));
                }
            } else {
                return Err(GitBoundError::usage(
                    "the repository references a missing profile; rerun with --force to repair the binding",
                ));
            }
        }

        let all_keys = transaction_keys();
        let snapshot = self.snapshot(&all_keys)?;
        let result = (|| {
            for (managed, present_key, value_key) in BACKUP_KEYS {
                if self.get(present_key, true)?.is_none() {
                    let value = self.get(managed, true)?;
                    self.set(present_key, if value.is_some() { "true" } else { "false" })?;
                    if let Some(value) = value {
                        self.set(value_key, &value)?;
                    } else {
                        self.unset(value_key)?;
                    }
                }
            }
            self.set("gitbound.profile", profile_name)?;
            self.set("gitbound.version", "2")?;
            self.set("user.name", &profile.git_name)?;
            self.set("user.email", &profile.git_email)?;
            if matches!(remote.map(|r| r.protocol), Some(RemoteProtocol::Ssh)) {
                self.set("core.sshCommand", &Self::expected_ssh_command(profile)?)?;
            } else {
                self.unset("core.sshCommand")?;
            }
            if let Some(key) = &profile.signing_key {
                self.set("user.signingKey", key)?;
                self.set("gpg.format", profile.signing_format.as_git_value())?;
                if profile.require_signing {
                    self.set("commit.gpgSign", "true")?;
                } else {
                    self.unset("commit.gpgSign")?;
                }
            } else {
                self.unset("user.signingKey")?;
                self.unset("gpg.format")?;
                self.unset("commit.gpgSign")?;
            }
            Ok(())
        })();
        if let Err(error) = result {
            let rollback = self.restore(&snapshot);
            return match rollback {
                Ok(()) => Err(error),
                Err(rollback) => Err(GitBoundError::dependency(format!(
                    "{error}; rollback also failed: {rollback}"
                ))),
            };
        }
        Ok(())
    }

    pub fn unbind(&self) -> Result<(), GitBoundError> {
        self.ensure_repo()?;
        self.migrate_legacy_binding()?;
        if self.get("gitbound.profile", true)?.is_none() {
            return Err(GitBoundError::usage(
                "repository is not bound to a GitBound profile",
            ));
        }
        let all_keys = transaction_keys();
        let snapshot = self.snapshot(&all_keys)?;
        let result = (|| {
            for (managed, present_key, value_key) in BACKUP_KEYS {
                let present = self.get(present_key, true)?.as_deref() == Some("true");
                if present {
                    match self.get(value_key, true)? {
                        Some(value) => self.set(managed, &value)?,
                        // Backup flag says the value existed, but the backup
                        // itself is missing (manual tampering). Fall back to
                        // unsetting rather than trapping the repository in a
                        // permanently bound state.
                        None => self.unset(managed)?,
                    }
                } else {
                    self.unset(managed)?;
                }
            }
            for key in BINDING_KEYS {
                self.unset(key)?;
            }
            for (_, present, value) in BACKUP_KEYS {
                self.unset(present)?;
                self.unset(value)?;
            }
            Ok(())
        })();
        if let Err(error) = result {
            let _ = self.restore(&snapshot);
            return Err(error);
        }
        Ok(())
    }

    fn snapshot(&self, keys: &[String]) -> Result<BTreeMap<String, Option<String>>, GitBoundError> {
        keys.iter()
            .map(|key| Ok((key.clone(), self.get(key, true)?)))
            .collect()
    }

    fn restore(&self, snapshot: &BTreeMap<String, Option<String>>) -> Result<(), GitBoundError> {
        for (key, value) in snapshot {
            if let Some(value) = value {
                self.set(key, value)?;
            } else {
                self.unset(key)?;
            }
        }
        Ok(())
    }
}

fn transaction_keys() -> Vec<String> {
    let mut keys = BINDING_KEYS
        .iter()
        .chain(IDENTITY_KEYS)
        .map(|key| (*key).to_string())
        .collect::<Vec<_>>();
    for (_, present, value) in BACKUP_KEYS {
        keys.push((*present).to_string());
        keys.push((*value).to_string());
    }
    keys
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ssh_command_quotes_paths() {
        let dir = tempfile::tempdir().unwrap();
        let key = dir.path().join("my key");
        std::fs::write(&key, "key").unwrap();
        let profile = Profile {
            github_user: "a".into(),
            git_name: "A".into(),
            git_email: "a@x".into(),
            hostname: "github.com".into(),
            ssh_host: None,
            ssh_key: Some(key.clone()),
            allowed_owners: vec![],
            signing_key: None,
            signing_format: crate::config::SigningFormat::Openpgp,
            require_signing: false,
        };
        let command = Git::expected_ssh_command(&profile).unwrap();
        assert!(command.contains("\""));
        assert!(command.contains("IdentitiesOnly=yes"));
    }

    #[test]
    fn ssh_command_leaves_exclamation_marks_intact() {
        // `\!` inside a double-quoted sh word survives literally, so escaping
        // it would hand ssh a key path that does not exist.
        let dir = tempfile::tempdir().unwrap();
        let key = dir.path().join("my!key");
        std::fs::write(&key, "key").unwrap();
        let profile = Profile {
            github_user: "a".into(),
            git_name: "A".into(),
            git_email: "a@x".into(),
            hostname: "github.com".into(),
            ssh_host: None,
            ssh_key: Some(key.clone()),
            allowed_owners: vec![],
            signing_key: None,
            signing_format: crate::config::SigningFormat::Openpgp,
            require_signing: false,
        };
        let command = Git::expected_ssh_command(&profile).unwrap();
        assert!(command.contains("my!key"), "{command}");
        assert!(!command.contains(r"\!"), "{command}");
    }
}