giti 0.1.3

A tool to easily query Git repos for basic information. Can be used via CLI or as a library.
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
use color_eyre::Result;
use gix::date::Time;
use gix::{discover, remote::Direction, Remote, Repository};
use std::{borrow::Cow, fs, path::Path};
use yansi::Color::{self, *};
use yansi::Paint;

use crate::cli::{InfoArgs, OpenPage};
use crate::util::calculate_directory_size;
use crate::util::format_display_time;

const INDENT: &str = "  ";

/// Represents a parsed git remote URL with owner and repo name
#[derive(Debug, Clone)]
pub struct ParsedRemoteUrl {
    pub host: String,
    pub owner: String,
    pub repo: String,
}

impl ParsedRemoteUrl {
    /// Parse a git remote URL into its components
    ///
    /// Supports various formats:
    /// - https://github.com/owner/repo.git
    /// - https://github.com/owner/repo
    /// - git@github.com:owner/repo.git
    /// - git@github.com:owner/repo
    /// - ssh://git@github.com/owner/repo.git
    pub fn parse(url: &str) -> Result<Self> {
        let url = url.trim();

        // Handle SSH URLs like git@github.com:owner/repo.git
        if url.starts_with("git@") {
            return Self::parse_ssh_url(url);
        }

        // Handle ssh:// URLs like ssh://git@github.com/owner/repo.git
        if url.starts_with("ssh://") {
            return Self::parse_ssh_protocol_url(url);
        }

        // Handle HTTPS/HTTP URLs
        if url.starts_with("https://") || url.starts_with("http://") || url.starts_with("git://") {
            return Self::parse_https_url(url);
        }

        Err(color_eyre::eyre::eyre!("Unsupported URL format: {}", url))
    }

    fn parse_ssh_url(url: &str) -> Result<Self> {
        // git@github.com:owner/repo.git
        let without_prefix = url.strip_prefix("git@").ok_or_else(|| {
            color_eyre::eyre::eyre!("Invalid SSH URL format")
        })?;

        let (host, path) = without_prefix.split_once(':').ok_or_else(|| {
            color_eyre::eyre::eyre!("Invalid SSH URL format: missing ':'")
        })?;

        Self::parse_owner_repo(host, path)
    }

    fn parse_ssh_protocol_url(url: &str) -> Result<Self> {
        // ssh://git@github.com/owner/repo.git
        let without_prefix = url.strip_prefix("ssh://git@").ok_or_else(|| {
            color_eyre::eyre::eyre!("Invalid SSH protocol URL format")
        })?;

        let (host, path) = without_prefix.split_once('/').ok_or_else(|| {
            color_eyre::eyre::eyre!("Invalid SSH protocol URL format: missing '/'")
        })?;

        Self::parse_owner_repo(host, path)
    }

    fn parse_https_url(url: &str) -> Result<Self> {
        // https://github.com/owner/repo.git
        let parsed = url::Url::parse(url)?;

        let host = parsed.host_str().ok_or_else(|| {
            color_eyre::eyre::eyre!("URL has no host")
        })?.to_string();

        let path = parsed.path().trim_start_matches('/');

        Self::parse_owner_repo(&host, path)
    }

    fn parse_owner_repo(host: &str, path: &str) -> Result<Self> {
        let path = path.trim_end_matches('/');
        let path = path.strip_suffix(".git").unwrap_or(path);

        let parts: Vec<&str> = path.split('/').collect();

        if parts.len() < 2 {
            return Err(color_eyre::eyre::eyre!(
                "Could not extract owner/repo from path: {}",
                path
            ));
        }

        Ok(Self {
            host: host.to_string(),
            owner: parts[0].to_string(),
            repo: parts[1].to_string(),
        })
    }

    /// Get the base web URL for this repository
    pub fn base_url(&self) -> String {
        format!("https://{}/{}/{}", self.host, self.owner, self.repo)
    }

    /// Get the URL for a specific page on the forge
    pub fn page_url(&self, page: &OpenPage, forge_override: Option<&str>) -> String {
        let host = match forge_override {
            Some(f) => match f.to_lowercase().as_str() {
                "github" | "gh" => "github.com".to_string(),
                "gitlab" | "gl" => "gitlab.com".to_string(),
                "bitbucket" | "bb" => "bitbucket.org".to_string(),
                "codeberg" | "cb" => "codeberg.org".to_string(),
                "sourcehut" | "sr" | "srht" => "sr.ht".to_string(),
                _ => f.to_string(),
            },
            None => self.host.clone(),
        };

        let base = format!("https://{}/{}/{}", host, self.owner, self.repo);

        // Determine the forge type for URL construction
        let forge_type = self.detect_forge_type(&host);

        match page {
            OpenPage::Repo | OpenPage::Code => base,
            OpenPage::Author => format!("https://{}/{}", host, self.owner),
            OpenPage::Issues => match forge_type {
                ForgeType::Bitbucket => format!("{}/issues", base),
                ForgeType::SourceHut => format!("https://todo.sr.ht/~{}/{}", self.owner, self.repo),
                _ => format!("{}/issues", base),
            },
            OpenPage::Pulls => match forge_type {
                ForgeType::GitLab => format!("{}/-/merge_requests", base),
                ForgeType::Bitbucket => format!("{}/pull-requests", base),
                ForgeType::SourceHut => format!("https://lists.sr.ht/~{}/{}", self.owner, self.repo),
                _ => format!("{}/pulls", base),
            },
            OpenPage::Wiki => match forge_type {
                ForgeType::GitLab => format!("{}/-/wikis/home", base),
                ForgeType::Bitbucket => format!("{}/wiki", base),
                ForgeType::SourceHut => base, // SourceHut doesn't have wikis
                _ => format!("{}/wiki", base),
            },
            OpenPage::Commits => match forge_type {
                ForgeType::GitLab => format!("{}/-/commits/main", base),
                ForgeType::Bitbucket => format!("{}/commits", base),
                ForgeType::SourceHut => format!("{}/log", base),
                _ => format!("{}/commits/main", base),
            },
            OpenPage::Branches => match forge_type {
                ForgeType::GitLab => format!("{}/-/branches", base),
                ForgeType::Bitbucket => format!("{}/branches", base),
                ForgeType::SourceHut => format!("{}/refs", base),
                _ => format!("{}/branches", base),
            },
            OpenPage::Tags => match forge_type {
                ForgeType::GitLab => format!("{}/-/tags", base),
                ForgeType::Bitbucket => format!("{}/downloads/?tab=tags", base),
                ForgeType::SourceHut => format!("{}/refs", base),
                _ => format!("{}/tags", base),
            },
            OpenPage::Releases => match forge_type {
                ForgeType::GitLab => format!("{}/-/releases", base),
                ForgeType::Bitbucket => format!("{}/downloads", base),
                ForgeType::SourceHut => format!("{}/refs", base),
                _ => format!("{}/releases", base),
            },
        }
    }

    fn detect_forge_type(&self, host: &str) -> ForgeType {
        let host_lower = host.to_lowercase();
        if host_lower.contains("github") {
            ForgeType::GitHub
        } else if host_lower.contains("gitlab") {
            ForgeType::GitLab
        } else if host_lower.contains("bitbucket") {
            ForgeType::Bitbucket
        } else if host_lower.contains("codeberg") {
            ForgeType::Codeberg
        } else if host_lower.contains("sr.ht") || host_lower.contains("sourcehut") {
            ForgeType::SourceHut
        } else {
            // Default to GitHub-style URLs for unknown forges
            ForgeType::GitHub
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum ForgeType {
    GitHub,
    GitLab,
    Bitbucket,
    Codeberg,
    SourceHut,
}

pub struct Repo {
    repo: Repository,
}

impl Repo {
    pub fn new<T: AsRef<Path>>(path: T) -> Result<Self> {
        let repo = discover(path)?;
        Ok(Self { repo })
    }

    pub fn git_dir(&self) -> &Path {
        self.repo.git_dir()
    }

    pub fn work_dir(&self) -> Option<&Path> {
        self.repo.workdir()
    }

    pub fn work_path(&self) -> String {
        match self.work_dir() {
            Some(dir) => match dir.canonicalize() {
                // return the canonicalized path
                Ok(path) => path.to_string_lossy().to_string(),
                // return the original path
                Err(_) => dir.to_string_lossy().to_string(),
            },
            // this must be a bare repository
            None => "NO WORK DIR".to_owned(),
        }
    }

    pub fn remotes(&'_ self) -> Vec<Remote<'_>> {
        let mut remotes = Vec::new();

        for remote_name in self.repo.remote_names() {
            if let Ok(remote) = self.repo.find_remote(&*remote_name) {
                remotes.push(remote);
            }
        }

        remotes
    }

    /// Get the URL for a specific remote
    pub fn get_remote_url(&self, remote_name: &str) -> Result<String> {
        let remote = self.repo.find_remote(remote_name).map_err(|_| {
            color_eyre::eyre::eyre!("Remote '{}' not found", remote_name)
        })?;

        let url = remote.url(Direction::Fetch).ok_or_else(|| {
            color_eyre::eyre::eyre!("Remote '{}' has no fetch URL", remote_name)
        })?;

        Ok(url.to_string())
    }

    /// Get the web URL for a specific page
    pub fn get_web_url(
        &self,
        remote_name: &str,
        page: &OpenPage,
        forge_override: Option<&str>,
    ) -> Result<String> {
        let remote_url = self.get_remote_url(remote_name)?;
        let parsed = ParsedRemoteUrl::parse(&remote_url)?;
        Ok(parsed.page_url(page, forge_override))
    }

    pub fn repo_urls(&self) -> Cow<'_, str> {
        let mut urls: Vec<String> = Vec::default();

        let remotes = self.remotes();
        if remotes.is_empty() {
            urls.push(format!("{INDENT}No Remotes"));
        } else {
            urls.push(format!("{INDENT}Repo URLs:"));
            for remote in remotes {
                if let Some(url) = remote.url(Direction::Fetch) {
                    urls.push(format!(
                        "{}{} {}",
                        INDENT.repeat(2),
                        remote
                            .name()
                            .map(|n| n.as_symbol().unwrap_or_default())
                            .unwrap_or_default(),
                        url
                    ));
                }
            }
        }

        urls.join("\n").into()
    }

    pub fn last_update(&self) -> Cow<'_, str> {
        match self.get_most_recent_commit_time() {
            Ok(time) => time.into(),
            Err(_) => "Unknown".into(),
        }
    }

    fn get_most_recent_commit_time(&self) -> Result<String> {
        let mut times: Vec<Time> = Vec::default();

        // Iterate through all references to find the most recent commit
        for reference_result in self.repo.references()?.all()? {
            let reference = match reference_result {
                Ok(r) => r,
                Err(_) => continue,
            };

            let target = reference.target();
            if let Some(oid) = target.try_id() {
                if let Ok(commit) = self.repo.find_object(oid)?.try_into_commit() {
                    let commit_time = commit.time()?;
                    times.push(commit_time);
                }
            }
        }

        // times are stored as seconds since the epoch, the largest should thus be the most recent
        match times.iter().max() {
            Some(time) => {
                // TODO Claude used this method, it works but there should be a cleaner way
                let datetime =
                    std::time::UNIX_EPOCH + std::time::Duration::from_secs(time.seconds as u64);
                Ok(format_display_time(datetime))
            }
            None => Ok("NO REFERENCES FOUND".to_owned()),
        }
    }

    pub fn last_fetch(&self) -> Cow<'_, str> {
        if let Some(work_dir) = self.work_dir() {
            let git_dir = work_dir.join(".git");

            // Check for FETCH_HEAD first
            let fetch_head_path = git_dir.join("FETCH_HEAD");
            if let Ok(metadata) = fs::metadata(&fetch_head_path) {
                if let Ok(modified) = metadata.modified() {
                    return format!("{INDENT}Last fetched: {}", format_display_time(modified))
                        .into();
                }
            }

            // If FETCH_HEAD not found, check for HEAD
            let head_path = git_dir.join("HEAD");
            if let Ok(metadata) = fs::metadata(&head_path) {
                if let Ok(modified) = metadata.modified() {
                    return format!("{INDENT}Last cloned: {}", format_display_time(modified))
                        .into();
                }
            }
        }

        "Unknown".into()
    }

    pub fn repo_size(&self) -> Cow<'_, str> {
        // For bare repositories, use the repository's git directory
        match calculate_directory_size(self.repo.git_dir()) {
            Ok(size) => format!("{INDENT}Repo Size: {}", self.format_size(size)).into(),
            Err(_) => "Unknown".into(),
        }
    }

    pub fn total_size(&self) -> Cow<'_, str> {
        if let Some(work_dir) = self.work_dir() {
            match calculate_directory_size(work_dir) {
                Ok(size) => format!("{INDENT}Total Size: {}", self.format_size(size)).into(),
                Err(_) => "Unknown".into(),
            }
        } else {
            // For bare repositories, use the repository's git directory
            match calculate_directory_size(self.repo.git_dir()) {
                Ok(size) => format!("{INDENT}Total Size: {}", self.format_size(size)).into(),
                Err(_) => "Unknown".into(),
            }
        }
    }

    fn format_size(&self, size: u64) -> String {
        const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
        const COLOR: &[Color] = &[Blue, Green, Green, Yellow, Red];
        let mut size = size as f64;
        let mut unit_index = 0;

        while size >= 1024.0 && unit_index < UNITS.len() - 1 {
            size /= 1024.0;
            unit_index += 1;
        }

        if unit_index == 0 {
            format!("{} {}", size as u64, UNITS[unit_index])
        } else {
            format!("{:.1} {}", size, UNITS[unit_index])
        }
        .paint(COLOR[unit_index])
        .to_string()
    }

    pub fn print_tsv_info(&self, args: &InfoArgs) {
        let mut values = Vec::new();

        values.push(
            self.work_dir()
                .map(|r| r.to_string_lossy())
                .unwrap_or_default(),
        );

        if args.repo_urls {
            values.push(self.repo_urls());
        }

        if args.last_update {
            values.push(self.last_update());
        }

        if args.last_fetch {
            values.push(self.last_fetch());
        }

        if args.repo_size {
            values.push(self.repo_size());
        }

        if args.size {
            values.push(self.total_size());
        }

        println!("{}", values.join("\t"));
    }

    pub fn print_human_readable_info(&self,args: &InfoArgs) {
        println!("Repository: {}", self.work_path());

        if args.repo_urls {
            println!("{}", self.repo_urls());
        }

        if args.last_update {
            println!("  Last Update: {}", self.last_update());
        }

        if args.last_fetch {
            println!("{}", self.last_fetch());
        }

        if args.repo_size {
            println!("{}", self.repo_size());
        }

        if args.size {
            println!("{}", self.total_size());
        }

        println!();
    }
}