gitaway 0.1.0

Reach your self-hosted git server from anywhere, with a tunnel only gitaway uses
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
//! What gitaway reads from, and writes to, a repository's git config.
//!
//! Everything goes through the `git` binary (via [`Git`]), never by parsing
//! `.git/config` directly. That way `includeIf`, global and system config,
//! `-c` overrides and `url.<base>.insteadOf` rewrites all behave exactly as
//! git itself sees them.
//!
//! # Extending
//!
//! A new query is a method on [`Repo`] that runs one git command through
//! [`Git::capture`] and maps git's exit codes explicitly. Don't treat "any
//! failure" as "not set": that hides real errors.

use super::classify::Direction;
use super::runner::{Git, RunError};

/// The git config key that pins a repository to a profile.
pub const PROFILE_KEY: &str = "gitaway.profile";

/// Queries a repository (or, outside one, the user's global git config).
#[derive(Debug, Clone)]
pub struct Repo {
    git: Git,
}

impl Repo {
    /// A repository as seen by `git` with the user's global options.
    pub fn new(git: Git) -> Self {
        Self { git }
    }

    /// `git config gitaway.profile`: repo, then global, then `includeIf`, as
    /// git resolves it. `None` if it isn't set.
    pub fn profile(&self) -> Result<Option<String>, RunError> {
        self.config_get(PROFILE_KEY)
    }

    /// Pins the current repository to a profile (`git config --local`). Fails
    /// outside a repository.
    pub fn set_profile(&self, name: &str) -> Result<(), RunError> {
        let args = ["config", "--local", PROFILE_KEY, name];
        let out = self.git.capture(&args)?;
        match out.code {
            0 => Ok(()),
            code => Err(failed(&args, code, out.stderr)),
        }
    }

    /// The names of the configured remotes, such as `origin`.
    pub fn remote_names(&self) -> Result<Vec<String>, RunError> {
        let out = self.git.capture(&["remote"])?;
        match out.code {
            0 => Ok(out.stdout.lines().map(str::to_owned).collect()),
            // Outside a repository there are no remotes.
            128 => Ok(Vec::new()),
            code => Err(failed(&["remote"], code, out.stderr)),
        }
    }

    /// Every URL git may connect to for a remote, after any `insteadOf`
    /// rewrites. For [`Direction::Push`] these are the push URLs, which may
    /// differ: git pushes to each one. Empty if there's no such remote.
    ///
    /// A fetch only uses the first URL, but all are listed: counting one
    /// git won't contact only costs a tunnel it didn't need.
    pub fn remote_urls(&self, remote: &str, direction: Direction) -> Result<Vec<String>, RunError> {
        let mut args = vec!["remote", "get-url", "--all"];
        if direction == Direction::Push {
            args.push("--push");
        }
        args.extend(["--", remote]);
        let out = self.git.capture(&args)?;
        match out.code {
            0 => Ok(out.stdout.lines().map(str::to_owned).collect()),
            // 2: no such remote. 128: not in a repository.
            2 | 128 => Ok(Vec::new()),
            code => Err(failed(&args, code, out.stderr)),
        }
    }

    /// The URLs of the repository's submodules: those already set up
    /// (`submodule.<name>.url` in its config) and those listed in its
    /// `.gitmodules`, which `git submodule update --init` reads. Empty
    /// outside a repository or in a bare one.
    ///
    /// Relative URLs (`../lib.git`) are returned as written. They're on the
    /// superproject's own server, which its remotes already cover.
    pub fn submodule_urls(&self) -> Result<Vec<String>, RunError> {
        const KEYS: &str = r"^submodule\..*\.url$";
        let mut urls = self.config_values(&["config", "--get-regexp", KEYS])?;
        let args = ["rev-parse", "--show-toplevel"];
        let top = self.git.capture(&args)?;
        match top.code {
            0 => {
                let file = format!("{}/.gitmodules", top.stdout);
                urls.extend(self.config_values(&[
                    "config",
                    "--file",
                    &file,
                    "--get-regexp",
                    KEYS,
                ])?);
            }
            // Not in a repository, or no working tree.
            128 => {}
            code => return Err(failed(&args, code, top.stderr)),
        }
        Ok(urls)
    }

    /// The values `git config --get-regexp` prints, one per `key value`
    /// line. Exit code 1 means none matched (or the file doesn't exist).
    fn config_values(&self, args: &[&str]) -> Result<Vec<String>, RunError> {
        let out = self.git.capture(args)?;
        match out.code {
            0 => Ok(out
                .stdout
                .lines()
                .filter_map(|line| line.split_once(' ').map(|(_, value)| value.to_owned()))
                .collect()),
            1 => Ok(Vec::new()),
            code => Err(failed(args, code, out.stderr)),
        }
    }

    /// A URL as git will really use it, after `url.<base>.insteadOf`
    /// rewrites. Doesn't touch the network.
    pub fn expand_url(&self, url: &str) -> Result<String, RunError> {
        let args = ["ls-remote", "--get-url", "--", url];
        let out = self.git.capture(&args)?;
        match out.code {
            0 => Ok(out.stdout),
            code => Err(failed(&args, code, out.stderr)),
        }
    }

    /// The remote a bare `git fetch` or `git push` would use, following git's
    /// own rules:
    ///
    /// - push: `branch.<b>.pushRemote`, then `remote.pushDefault`, then
    ///   `branch.<b>.remote`, then `origin`;
    /// - fetch: `branch.<b>.remote`, then `origin`.
    ///
    /// On a detached HEAD there's no branch, so only `remote.pushDefault` (for
    /// push) and `origin` apply.
    pub fn default_remote(&self, direction: Direction) -> Result<String, RunError> {
        let branch = self.current_branch()?;
        let branch_key = |suffix: &str| branch.as_ref().map(|b| format!("branch.{b}.{suffix}"));

        let mut keys = Vec::new();
        if direction == Direction::Push {
            keys.extend(branch_key("pushRemote"));
            keys.push("remote.pushDefault".to_owned());
        }
        keys.extend(branch_key("remote"));

        for key in keys {
            if let Some(remote) = self.config_get(&key)? {
                return Ok(remote);
            }
        }
        Ok("origin".to_owned())
    }

    /// The checked-out branch, or `None` on a detached HEAD or outside a
    /// repository.
    fn current_branch(&self) -> Result<Option<String>, RunError> {
        let args = ["symbolic-ref", "--quiet", "--short", "HEAD"];
        let out = self.git.capture(&args)?;
        match out.code {
            0 => Ok(Some(out.stdout)),
            // 1: detached HEAD. 128: not in a repository.
            1 | 128 => Ok(None),
            code => Err(failed(&args, code, out.stderr)),
        }
    }

    /// The proxy git would use for `url`: `http.proxy` or the most specific
    /// `http.<url>.proxy`, from every config source, as git resolves it.
    pub fn http_proxy_for(&self, url: &str) -> Result<Option<String>, RunError> {
        let args = ["config", "--get-urlmatch", "http.proxy", url];
        let out = self.git.capture(&args)?;
        match out.code {
            0 => Ok(Some(out.stdout)),
            1 => Ok(None),
            code => Err(failed(&args, code, out.stderr)),
        }
    }

    /// `remote.<name>.proxy`, which git ranks above `http.proxy` for that
    /// remote.
    pub fn remote_proxy(&self, remote: &str) -> Result<Option<String>, RunError> {
        self.config_get(&format!("remote.{remote}.proxy"))
    }

    /// `git config --get <key>`. Exit code 1 means the key isn't set.
    /// `core.sshCommand`, if set, including any `-c core.sshCommand=…`
    /// typed before the command.
    pub fn ssh_command(&self) -> Result<Option<String>, RunError> {
        self.config_get("core.sshCommand")
    }

    fn config_get(&self, key: &str) -> Result<Option<String>, RunError> {
        let args = ["config", "--get", key];
        let out = self.git.capture(&args)?;
        match out.code {
            0 => Ok(Some(out.stdout)),
            1 => Ok(None),
            code => Err(failed(&args, code, out.stderr)),
        }
    }
}

fn failed(args: &[&str], code: i32, stderr: String) -> RunError {
    RunError::Failed {
        command: args.join(" "),
        code,
        stderr,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;
    use tempfile::TempDir;

    /// git isolated from the user's own config, run in `dir`.
    fn git_in(dir: &Path) -> Git {
        Git::new(&["-C".into(), dir.into()])
            .env("GIT_CONFIG_GLOBAL", null_device())
            .env("GIT_CONFIG_NOSYSTEM", "1")
    }

    fn null_device() -> &'static str {
        if cfg!(windows) { "NUL" } else { "/dev/null" }
    }

    /// A new repository on branch `main`.
    fn repo() -> (TempDir, Repo, Git) {
        let dir = TempDir::new().unwrap();
        let git = git_in(dir.path());
        run(&git, &["init", "--quiet", "--initial-branch=main"]);
        (dir, Repo::new(git.clone()), git)
    }

    fn run(git: &Git, args: &[&str]) {
        let out = git.capture(args).unwrap();
        assert_eq!(out.code, 0, "git {args:?}: {}", out.stderr);
    }

    #[test]
    fn profile_round_trip() {
        let (_dir, repo, _git) = repo();
        assert_eq!(repo.profile().unwrap(), None);
        repo.set_profile("home").unwrap();
        assert_eq!(repo.profile().unwrap().as_deref(), Some("home"));
    }

    #[test]
    fn set_profile_outside_a_repository_fails() {
        let dir = TempDir::new().unwrap();
        let repo = Repo::new(git_in(dir.path()));
        assert!(matches!(
            repo.set_profile("home"),
            Err(RunError::Failed { .. })
        ));
    }

    #[test]
    fn outside_a_repository_there_is_nothing() {
        let dir = TempDir::new().unwrap();
        let repo = Repo::new(git_in(dir.path()));
        assert_eq!(repo.profile().unwrap(), None);
        assert!(repo.remote_names().unwrap().is_empty());
        assert!(
            repo.remote_urls("origin", Direction::Fetch)
                .unwrap()
                .is_empty()
        );
        assert!(repo.submodule_urls().unwrap().is_empty());
        assert_eq!(repo.default_remote(Direction::Push).unwrap(), "origin");
    }

    #[test]
    fn remote_urls_follow_push_urls_and_insteadof() {
        let (_dir, repo, git) = repo();
        run(&git, &["remote", "add", "origin", "lan:alice/app.git"]);
        run(
            &git,
            &["config", "url.ssh://git@192.0.2.10:2222/.insteadOf", "lan:"],
        );
        run(
            &git,
            &["remote", "add", "mirror", "https://example.com/app.git"],
        );
        run(
            &git,
            &[
                "remote",
                "set-url",
                "--push",
                "mirror",
                "ssh://git@example.net/app.git",
            ],
        );

        assert_eq!(repo.remote_names().unwrap(), ["mirror", "origin"]);
        assert_eq!(
            repo.remote_urls("origin", Direction::Fetch).unwrap(),
            ["ssh://git@192.0.2.10:2222/alice/app.git"]
        );
        assert_eq!(
            repo.remote_urls("mirror", Direction::Fetch).unwrap(),
            ["https://example.com/app.git"]
        );
        assert_eq!(
            repo.remote_urls("mirror", Direction::Push).unwrap(),
            ["ssh://git@example.net/app.git"]
        );
        assert!(
            repo.remote_urls("missing", Direction::Fetch)
                .unwrap()
                .is_empty()
        );

        // git pushes to every push URL, so every one is listed.
        run(
            &git,
            &[
                "remote",
                "set-url",
                "--add",
                "--push",
                "mirror",
                "ssh://git@192.0.2.10/app.git",
            ],
        );
        assert_eq!(
            repo.remote_urls("mirror", Direction::Push).unwrap(),
            [
                "ssh://git@example.net/app.git",
                "ssh://git@192.0.2.10/app.git"
            ]
        );
    }

    #[test]
    fn submodule_urls_come_from_gitmodules_and_the_config() {
        let (dir, repo, git) = repo();
        std::fs::write(
            dir.path().join(".gitmodules"),
            "[submodule \"lib\"]\n\tpath = lib\n\turl = ssh://git@192.0.2.10/lib.git\n",
        )
        .unwrap();
        run(
            &git,
            &[
                "config",
                "submodule.docs.url",
                "https://example.com/docs.git",
            ],
        );
        assert_eq!(
            repo.submodule_urls().unwrap(),
            [
                "https://example.com/docs.git",
                "ssh://git@192.0.2.10/lib.git"
            ]
        );
    }

    #[test]
    fn urls_are_expanded_with_insteadof() {
        let (_dir, repo, git) = repo();
        run(
            &git,
            &["config", "url.ssh://git@192.0.2.10:2222/.insteadOf", "lan:"],
        );
        assert_eq!(
            repo.expand_url("lan:alice/app.git").unwrap(),
            "ssh://git@192.0.2.10:2222/alice/app.git"
        );
        assert_eq!(
            repo.expand_url("https://example.com/app.git").unwrap(),
            "https://example.com/app.git"
        );
    }

    #[test]
    fn remote_names_starting_with_a_dash_are_not_options() {
        let (_dir, repo, _git) = repo();
        assert!(
            repo.remote_urls("--push", Direction::Fetch)
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn default_remote_follows_gits_rules() {
        let (_dir, repo, git) = repo();
        assert_eq!(repo.default_remote(Direction::Fetch).unwrap(), "origin");

        run(&git, &["config", "branch.main.remote", "upstream"]);
        assert_eq!(repo.default_remote(Direction::Fetch).unwrap(), "upstream");
        assert_eq!(repo.default_remote(Direction::Push).unwrap(), "upstream");

        run(&git, &["config", "remote.pushDefault", "fork"]);
        assert_eq!(repo.default_remote(Direction::Fetch).unwrap(), "upstream");
        assert_eq!(repo.default_remote(Direction::Push).unwrap(), "fork");

        run(&git, &["config", "branch.main.pushRemote", "mine"]);
        assert_eq!(repo.default_remote(Direction::Push).unwrap(), "mine");
    }

    #[test]
    fn detached_head_falls_back() {
        let (_dir, repo, git) = repo();
        run(
            &git,
            &[
                "-c",
                "user.name=alice",
                "-c",
                "user.email=alice@example.com",
                "commit",
                "--quiet",
                "--allow-empty",
                "-m",
                "x",
            ],
        );
        run(&git, &["checkout", "--quiet", "--detach"]);
        run(&git, &["config", "branch.main.remote", "upstream"]);
        assert_eq!(repo.default_remote(Direction::Fetch).unwrap(), "origin");
    }
}