browsing/browsing.rs
1// This file is part of radicle-surf
2// <https://github.com/radicle-dev/radicle-surf>
3//
4// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License version 3 or
8// later as published by the Free Software Foundation.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18//! An example of browsing a git repo using `radicle-surf`.
19//!
20//! How to run:
21//!
22//! cargo run --example browsing <git_repo_path>
23//!
24//! This program browses the given repo and prints out the files and
25//! the directories in a tree-like structure.
26
27use radicle_surf::{
28 fs::{self, Directory},
29 Repository,
30};
31use std::{env, time::Instant};
32
33fn main() {
34 let repo_path = match env::args().nth(1) {
35 Some(path) => path,
36 None => {
37 print_usage();
38 return;
39 }
40 };
41 let repo = Repository::discover(repo_path).unwrap();
42 let now = Instant::now();
43 let head = repo.head().unwrap();
44 let root = repo.root_dir(head).unwrap();
45 print_directory(&root, &repo, 0);
46
47 let elapsed_millis = now.elapsed().as_millis();
48 println!("browse with print: {elapsed_millis} ms");
49}
50
51fn print_directory(d: &Directory, repo: &Repository, indent_level: usize) {
52 let indent = " ".repeat(indent_level * 4);
53 println!("{}{}/", &indent, d.name());
54 for entry in d.entries(repo).unwrap() {
55 match entry {
56 fs::Entry::File(f) => println!(" {}{}", &indent, f.name()),
57 fs::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1),
58 fs::Entry::Submodule(s) => println!(" {}{}", &indent, s.name()),
59 }
60 }
61}
62
63fn print_usage() {
64 println!("Usage:");
65 println!("cargo run --example browsing <repo_path>");
66}