Files
radicle-git/radicle-surf/examples/browsing.rs
T
Fintan Halpenny 2391c25e95 surf: add submodule support
Git [submodules] are encoded as commits when walking a Git tree. To
allow the support of them in `radicle-surf`, the tree entry is checked
for this case. The entry will be treated such that it is has a name,
prefix, Oid, and the submodule's URL.

It's important to note that the Oid is not usable within the context
of the repository that is being browsed, since they are entirely
separate.

Instead, the URL should be used in conjunction with the Oid for
browsing purposes outside of `radicle-surf`. However, in a future
iteration it may be considered to be able to browse the local contents
of the submodule, if possible.

N.b. getting the submodule and its URL is not supported for a bare
repository, since it will complain about not having a working tree.

[submodules]: https://git-scm.com/book/en/v2/Git-Tools-Submodules

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
2024-03-06 15:59:57 +00:00

67 lines
2.1 KiB
Rust

// This file is part of radicle-surf
// <https://github.com/radicle-dev/radicle-surf>
//
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! An example of browsing a git repo using `radicle-surf`.
//!
//! How to run:
//!
//! cargo run --example browsing <git_repo_path>
//!
//! This program browses the given repo and prints out the files and
//! the directories in a tree-like structure.
use radicle_surf::{
fs::{self, Directory},
Repository,
};
use std::{env, time::Instant};
fn main() {
let repo_path = match env::args().nth(1) {
Some(path) => path,
None => {
print_usage();
return;
},
};
let repo = Repository::discover(repo_path).unwrap();
let now = Instant::now();
let head = repo.head().unwrap();
let root = repo.root_dir(head).unwrap();
print_directory(&root, &repo, 0);
let elapsed_millis = now.elapsed().as_millis();
println!("browse with print: {elapsed_millis} ms");
}
fn print_directory(d: &Directory, repo: &Repository, indent_level: usize) {
let indent = " ".repeat(indent_level * 4);
println!("{}{}/", &indent, d.name());
for entry in d.entries(repo).unwrap() {
match entry {
fs::Entry::File(f) => println!(" {}{}", &indent, f.name()),
fs::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1),
fs::Entry::Submodule(s) => println!(" {}{}", &indent, s.name()),
}
}
}
fn print_usage() {
println!("Usage:");
println!("cargo run --example browsing <repo_path>");
}