ghgrab 0.1.15

A TUI-based tool to download specific files or folders from GitHub repositories
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
use anyhow::{anyhow, Context, Result};
use serde::Deserialize;
use url::Url;

#[derive(Debug, Clone)]
pub struct GitHubUrl {
    pub owner: String,
    pub repo: String,
    pub branch: String,
    pub path: String,
}

impl GitHubUrl {
    pub fn parse(url_str: &str) -> Result<Self> {
        let url = Url::parse(url_str).context("Invalid URL format")?;

        if url.host_str() != Some("github.com") {
            return Err(anyhow!("Not a GitHub URL"));
        }

        let path_segments: Vec<&str> = url
            .path_segments()
            .ok_or_else(|| anyhow!("Invalid URL path"))?
            .collect();

        if path_segments.len() < 2 {
            return Err(anyhow!("URL must contain owner and repository"));
        }

        let owner = path_segments[0].to_string();
        let repo = path_segments[1].to_string();

        let (branch, path) = if path_segments.len() >= 4
            && (path_segments[2] == "tree" || path_segments[2] == "blob")
        {
            let branch = path_segments[3].to_string();
            let path = if path_segments.len() > 4 {
                path_segments[4..].join("/")
            } else {
                String::new()
            };
            (branch, path)
        } else {
            ("main".to_string(), String::new())
        };

        Ok(GitHubUrl {
            owner,
            repo,
            branch,
            path,
        })
    }

    pub fn api_url(&self) -> String {
        let base = format!(
            "https://api.github.com/repos/{}/{}/contents",
            self.owner, self.repo
        );
        if self.path.is_empty() {
            format!("{}?ref={}", base, self.branch)
        } else {
            format!("{}/{}?ref={}", base, self.path, self.branch)
        }
    }
}

#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct RepoItem {
    pub name: String,
    #[serde(rename = "type")]
    pub item_type: String,
    pub path: String,
    pub download_url: Option<String>,
    pub url: String,
    #[allow(dead_code)]
    pub size: Option<u64>,
    #[serde(skip)]
    pub selected: bool,
    #[serde(skip)]
    pub lfs_oid: Option<String>,
    #[serde(skip)]
    pub lfs_size: Option<u64>,
    #[serde(skip)]
    pub lfs_download_url: Option<String>,
}

impl RepoItem {
    pub fn is_dir(&self) -> bool {
        self.item_type == "dir"
    }

    pub fn is_file(&self) -> bool {
        self.item_type == "file"
    }

    pub fn is_lfs(&self) -> bool {
        self.lfs_oid.is_some()
    }

    pub fn actual_size(&self) -> Option<u64> {
        self.lfs_size.or(self.size)
    }

    pub fn actual_download_url(&self) -> Option<&String> {
        self.lfs_download_url
            .as_ref()
            .or(self.download_url.as_ref())
    }
}

#[derive(Debug, Clone)]
pub struct LfsPointer {
    pub oid: String,
    pub size: u64,
}

impl LfsPointer {
    pub fn parse(content: &str) -> Option<Self> {
        if !content.starts_with("version https://git-lfs.github.com/spec/v1") {
            return None;
        }

        let mut oid = None;
        let mut size = None;

        for line in content.lines() {
            if line.starts_with("oid sha256:") {
                oid = Some(line.trim_start_matches("oid sha256:").to_string());
            } else if line.starts_with("size ") {
                size = line.trim_start_matches("size ").parse().ok();
            }
        }

        match (oid, size) {
            (Some(oid), Some(size)) => Some(LfsPointer { oid, size }),
            _ => None,
        }
    }
}

#[derive(Debug, serde::Serialize)]
struct LfsBatchRequest {
    operation: String,
    transfers: Vec<String>,
    objects: Vec<LfsObject>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct LfsObject {
    oid: String,
    size: u64,
}

#[derive(Debug, serde::Deserialize)]
struct LfsBatchResponse {
    objects: Vec<LfsResponseObject>,
}

#[derive(Debug, serde::Deserialize)]
struct LfsResponseObject {
    #[allow(dead_code)]
    oid: String,
    #[allow(dead_code)]
    size: u64,
    actions: Option<LfsActions>,
}

#[derive(Debug, serde::Deserialize)]
struct LfsActions {
    download: Option<LfsDownloadAction>,
}

#[derive(Debug, serde::Deserialize)]
struct LfsDownloadAction {
    href: String,
}

#[derive(Clone)]
pub struct GitHubClient {
    client: reqwest::Client,
}

impl GitHubClient {
    pub fn new() -> Result<Self> {
        let client = reqwest::Client::builder()
            .user_agent("ghgrab/0.1.0")
            .build()
            .context("Failed to create HTTP client")?;
        Ok(GitHubClient { client })
    }

    pub async fn fetch_contents(&self, url: &str) -> Result<Vec<RepoItem>> {
        let response = self
            .client
            .get(url)
            .send()
            .await
            .context("Failed to send request to GitHub API")?;

        if response.status().as_u16() == 403 {
            return Err(anyhow!("Rate limit exceeded. Please try again later."));
        }

        if response.status().as_u16() == 404 {
            return Err(anyhow!("Path not found in repository"));
        }

        if !response.status().is_success() {
            return Err(anyhow!("GitHub API error: {}", response.status()));
        }

        let items: Vec<RepoItem> = response
            .json()
            .await
            .context("Failed to parse GitHub API response")?;

        Ok(items)
    }

    // Fetch raw content
    pub async fn fetch_raw_content(&self, url: &str) -> Result<String> {
        let response = self
            .client
            .get(url)
            .send()
            .await
            .context("Failed to fetch raw content")?;

        if !response.status().is_success() {
            return Err(anyhow!("Failed to fetch file content"));
        }

        let content = response.text().await.context("Failed to read content")?;
        Ok(content)
    }

    // Call LFS batch API
    pub async fn get_lfs_download_url(
        &self,
        owner: &str,
        repo: &str,
        oid: &str,
        size: u64,
    ) -> Result<String> {
        let batch_url = format!(
            "https://github.com/{}/{}.git/info/lfs/objects/batch",
            owner, repo
        );

        let request = LfsBatchRequest {
            operation: "download".to_string(),
            transfers: vec!["basic".to_string()],
            objects: vec![LfsObject {
                oid: oid.to_string(),
                size,
            }],
        };

        let response = self
            .client
            .post(&batch_url)
            .header("Accept", "application/vnd.git-lfs+json")
            .header("Content-Type", "application/vnd.git-lfs+json")
            .json(&request)
            .send()
            .await
            .context("Failed to call LFS batch API")?;

        if !response.status().is_success() {
            return Err(anyhow!("LFS batch API error: {}", response.status()));
        }

        let batch_response: LfsBatchResponse = response
            .json()
            .await
            .context("Failed to parse LFS response")?;

        batch_response
            .objects
            .into_iter()
            .next()
            .and_then(|obj| obj.actions)
            .and_then(|actions| actions.download)
            .map(|download| download.href)
            .ok_or_else(|| anyhow!("No download URL in LFS response"))
    }

    pub async fn resolve_lfs_files(
        &self,
        items: &mut [RepoItem],
        owner: &str,
        repo: &str,
        branch: &str,
    ) {
        for item in items.iter_mut() {
            if item.is_file() {
                if let Some(size) = item.size {
                    if size < 1024 {
                        if let Some(download_url) = &item.download_url {
                            if let Ok(content) = self.fetch_raw_content(download_url).await {
                                if let Some(pointer) = LfsPointer::parse(&content) {
                                    item.lfs_oid = Some(pointer.oid.clone());
                                    item.lfs_size = Some(pointer.size);

                                    if let Ok(lfs_url) = self
                                        .get_lfs_download_url(
                                            owner,
                                            repo,
                                            &pointer.oid,
                                            pointer.size,
                                        )
                                        .await
                                    {
                                        item.lfs_download_url = Some(lfs_url);
                                    } else {
                                        let media_url = format!(
                                            "https://media.githubusercontent.com/media/{}/{}/{}/{}",
                                            owner, repo, branch, item.path
                                        );
                                        item.lfs_download_url = Some(media_url);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

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

    // --- GitHubUrl parsing tests ---

    #[test]
    fn test_parse_github_url() {
        let url = "https://github.com/rust-lang/rust/tree/master/src/tools";
        let parsed = GitHubUrl::parse(url).unwrap();
        assert_eq!(parsed.owner, "rust-lang");
        assert_eq!(parsed.repo, "rust");
        assert_eq!(parsed.branch, "master");
        assert_eq!(parsed.path, "src/tools");
    }

    #[test]
    fn test_parse_root_url() {
        let url = "https://github.com/rust-lang/rust";
        let parsed = GitHubUrl::parse(url).unwrap();
        assert_eq!(parsed.owner, "rust-lang");
        assert_eq!(parsed.repo, "rust");
        assert_eq!(parsed.branch, "main");
        assert_eq!(parsed.path, "");
    }

    #[test]
    fn test_parse_blob_url() {
        let url = "https://github.com/owner/repo/blob/main/src/lib.rs";
        let parsed = GitHubUrl::parse(url).unwrap();
        assert_eq!(parsed.owner, "owner");
        assert_eq!(parsed.repo, "repo");
        assert_eq!(parsed.branch, "main");
        assert_eq!(parsed.path, "src/lib.rs");
    }

    #[test]
    fn test_parse_branch_only_url() {
        let url = "https://github.com/owner/repo/tree/develop";
        let parsed = GitHubUrl::parse(url).unwrap();
        assert_eq!(parsed.owner, "owner");
        assert_eq!(parsed.repo, "repo");
        assert_eq!(parsed.branch, "develop");
        assert_eq!(parsed.path, "");
    }

    #[test]
    fn test_parse_deep_path() {
        let url = "https://github.com/org/project/tree/v2.0/src/core/engine";
        let parsed = GitHubUrl::parse(url).unwrap();
        assert_eq!(parsed.owner, "org");
        assert_eq!(parsed.repo, "project");
        assert_eq!(parsed.branch, "v2.0");
        assert_eq!(parsed.path, "src/core/engine");
    }

    #[test]
    fn test_parse_invalid_non_github_url() {
        let url = "https://gitlab.com/user/repo";
        assert!(GitHubUrl::parse(url).is_err());
    }

    #[test]
    fn test_parse_invalid_not_a_url() {
        assert!(GitHubUrl::parse("not a url").is_err());
    }

    #[test]
    fn test_parse_invalid_no_repo() {
        let url = "https://github.com/owner";
        assert!(GitHubUrl::parse(url).is_err());
    }

    // --- api_url tests ---

    #[test]
    fn test_api_url_with_path() {
        let gh = GitHubUrl {
            owner: "owner".into(),
            repo: "repo".into(),
            branch: "main".into(),
            path: "src/lib.rs".into(),
        };
        assert_eq!(
            gh.api_url(),
            "https://api.github.com/repos/owner/repo/contents/src/lib.rs?ref=main"
        );
    }

    #[test]
    fn test_api_url_without_path() {
        let gh = GitHubUrl {
            owner: "owner".into(),
            repo: "repo".into(),
            branch: "main".into(),
            path: String::new(),
        };
        assert_eq!(
            gh.api_url(),
            "https://api.github.com/repos/owner/repo/contents?ref=main"
        );
    }

    // --- LfsPointer tests ---

    #[test]
    fn test_lfs_pointer_parse_valid() {
        let content =
            "version https://git-lfs.github.com/spec/v1\noid sha256:abc123def456\nsize 12345";
        let pointer = LfsPointer::parse(content).unwrap();
        assert_eq!(pointer.oid, "abc123def456");
        assert_eq!(pointer.size, 12345);
    }

    #[test]
    fn test_lfs_pointer_parse_not_lfs() {
        let content = "This is just a regular file content";
        assert!(LfsPointer::parse(content).is_none());
    }

    #[test]
    fn test_lfs_pointer_parse_missing_oid() {
        let content = "version https://git-lfs.github.com/spec/v1\nsize 12345";
        assert!(LfsPointer::parse(content).is_none());
    }

    #[test]
    fn test_lfs_pointer_parse_missing_size() {
        let content = "version https://git-lfs.github.com/spec/v1\noid sha256:abc123";
        assert!(LfsPointer::parse(content).is_none());
    }

    // --- RepoItem tests ---

    fn make_test_item(item_type: &str) -> RepoItem {
        RepoItem {
            name: "test.rs".to_string(),
            item_type: item_type.to_string(),
            path: "src/test.rs".to_string(),
            download_url: Some("https://example.com/test.rs".to_string()),
            url: "https://api.github.com/repos/o/r/contents/src/test.rs".to_string(),
            size: Some(1024),
            selected: false,
            lfs_oid: None,
            lfs_size: None,
            lfs_download_url: None,
        }
    }

    #[test]
    fn test_repo_item_is_dir() {
        let item = make_test_item("dir");
        assert!(item.is_dir());
        assert!(!item.is_file());
    }

    #[test]
    fn test_repo_item_is_file() {
        let item = make_test_item("file");
        assert!(item.is_file());
        assert!(!item.is_dir());
    }

    #[test]
    fn test_repo_item_not_lfs() {
        let item = make_test_item("file");
        assert!(!item.is_lfs());
        assert_eq!(item.actual_size(), Some(1024));
        assert_eq!(
            item.actual_download_url().map(|s| s.as_str()),
            Some("https://example.com/test.rs")
        );
    }

    #[test]
    fn test_repo_item_lfs() {
        let mut item = make_test_item("file");
        item.lfs_oid = Some("abc123".to_string());
        item.lfs_size = Some(999999);
        item.lfs_download_url = Some("https://lfs.example.com/abc123".to_string());

        assert!(item.is_lfs());
        assert_eq!(item.actual_size(), Some(999999));
        assert_eq!(
            item.actual_download_url().map(|s| s.as_str()),
            Some("https://lfs.example.com/abc123")
        );
    }
}