|
| 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 | +} |
0 commit comments