Skip to content

Commit 5f69ab0

Browse files
committed
Add debugging utilities
1 parent 82826b1 commit 5f69ab0

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ gitbutler-forge = { path = "crates/gitbutler-forge" }
6666
gitbutler-hunk-dependency = { path = "crates/gitbutler-hunk-dependency" }
6767
gitbutler-settings = { path = "crates/gitbutler-settings" }
6868
gitbutler-workspace = { path = "crates/gitbutler-workspace" }
69+
but-debugging = { path = "crates/but-debugging" }
6970
but-core = { path = "crates/but-core" }
7071
but-workspace = { path = "crates/but-workspace" }
7172

crates/but-debugging/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "but-debugging"
3+
version = "0.0.0"
4+
edition = "2021"
5+
authors = ["GitButler <[email protected]>"]
6+
publish = false
7+
8+
[lib]
9+
doctest = false
10+
11+
[dependencies]
12+
13+
[dev-dependencies]

crates/but-debugging/src/lib.rs

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/// But Debugging contains utilities that aid in debugging gitbutler.
2+
/// Utilities defined inside of but-debugging should not be relied upon in
3+
/// tests or used in production code.
4+
use std::{path::Path, process::Command};
5+
6+
/// Options passed to `git log`
7+
pub struct LogOptions {
8+
/// Controls whether the `--oneline` flag is passed.
9+
/// default: true
10+
pub oneline: bool,
11+
/// Controls whether the `--graph` flag is passed.
12+
/// default: true
13+
pub graph: bool,
14+
/// Controls whether the `--all` flag is passed.
15+
/// default: false
16+
pub all: bool,
17+
/// The reference that should be logged
18+
/// default: "HEAD"
19+
pub reference: String,
20+
}
21+
22+
impl Default for LogOptions {
23+
fn default() -> Self {
24+
Self {
25+
oneline: true,
26+
graph: true,
27+
all: false,
28+
reference: "HEAD".to_string(),
29+
}
30+
}
31+
}
32+
33+
impl LogOptions {
34+
pub fn oneline(&mut self, oneline: bool) -> &mut Self {
35+
self.oneline = oneline;
36+
self
37+
}
38+
39+
pub fn graph(&mut self, graph: bool) -> &mut Self {
40+
self.graph = graph;
41+
self
42+
}
43+
44+
pub fn reference(&mut self, reference: String) -> &mut Self {
45+
self.reference = reference;
46+
self
47+
}
48+
49+
pub fn all(&mut self, all: bool) -> &mut Self {
50+
self.all = all;
51+
self
52+
}
53+
}
54+
55+
/// Runs `git log` and passes the output to println!
56+
#[deprecated = "git_log should not be used in production code or testing infastructure"]
57+
pub fn git_log(path: &Path, options: &LogOptions) {
58+
let path = if path.ends_with(".git") {
59+
path.parent().unwrap()
60+
} else {
61+
path
62+
};
63+
let mut command = Command::new("git");
64+
command.current_dir(path);
65+
command.arg("log");
66+
if options.oneline {
67+
command.arg("--oneline");
68+
}
69+
if options.graph {
70+
command.arg("--graph");
71+
}
72+
if options.all {
73+
command.arg("--all");
74+
}
75+
command.arg("--decorate=short");
76+
command.arg(options.reference.clone());
77+
let result = command.output().unwrap().stdout;
78+
println!("{:?}", command);
79+
println!("{}", std::str::from_utf8(&result).unwrap());
80+
}
81+
82+
/// Options passed to `git ls-tree`
83+
pub struct LsTreeOptions {
84+
/// Controls whether the `-r` flag should be passed.
85+
/// default: true
86+
pub recursive: bool,
87+
/// The reference that should be peeled to a tree
88+
/// default: "HEAD"
89+
pub reference: String,
90+
}
91+
92+
impl Default for LsTreeOptions {
93+
fn default() -> Self {
94+
Self {
95+
recursive: true,
96+
reference: "HEAD".to_string(),
97+
}
98+
}
99+
}
100+
101+
impl LsTreeOptions {
102+
pub fn recursive(&mut self, recursive: bool) -> &mut Self {
103+
self.recursive = recursive;
104+
self
105+
}
106+
107+
pub fn reference(&mut self, reference: String) -> &mut Self {
108+
self.reference = reference;
109+
self
110+
}
111+
}
112+
113+
/// Runs `git ls-tree` and passes the output to println!
114+
#[deprecated = "git_ls_tree should not be used in production code or testing infastructure"]
115+
pub fn git_ls_tree(path: &Path, options: &LsTreeOptions) {
116+
let path = if path.ends_with(".git") {
117+
path.parent().unwrap()
118+
} else {
119+
path
120+
};
121+
let mut command = Command::new("git");
122+
command.current_dir(path);
123+
command.arg("ls-tree");
124+
if options.recursive {
125+
command.arg("-r");
126+
}
127+
command.arg(options.reference.clone());
128+
let result = command.output().unwrap().stdout;
129+
println!("{:?}", command);
130+
println!("{}", std::str::from_utf8(&result).unwrap());
131+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

crates/gitbutler-branch-actions/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ urlencoding = "2.1.3"
4949
reqwest = { version = "0.12.9", features = ["json"] }
5050
toml.workspace = true
5151
uuid.workspace = true
52+
but-debugging.workspace = true
5253

5354
[dev-dependencies]
5455
once_cell = "1.20"

0 commit comments

Comments
 (0)