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
use std::process::Command;
use colored::Colorize;
use crate::diagnostics::{log_diagnostic, DiagnosticKind};
pub fn execute_branch_git() {
println!(
"\n{} {}",
" Git ".black().italic().on_bright_yellow(),
"repository status".yellow().italic(),
);
match Command::new("git").args(&["branch", "-a"]).output() {
Ok(output) => {
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
// Log to user BRANCH INFORMATION:
println!("\n{}\n", " BRANCH INFORMATION: ".black().on_cyan());
// Collect local branches
let local_branches = stdout
.split("\n")
.filter(|line| !line.contains("remotes") && !line.is_empty())
.collect::<Vec<&str>>();
// Log local branches
println!(" {}\n", " LOCAL ".black().on_magenta());
let mut i = 0;
let local_branches_len = local_branches.len();
for mut branch in local_branches {
branch = branch.trim();
i += 1;
// check if on first or last branch
if i == 1 {
print!(" {} ", "/-".magenta());
} else if i == local_branches_len {
print!(" {} ", "\\-".magenta());
} else {
print!(" {} ", "--".magenta());
}
// check if branch is current branch
if branch.contains('*') {
print!("{}", "**".red());
let branch_name = branch.split('*').last().unwrap();
branch = branch_name.trim();
print!(" {}", branch.cyan());
println!("{}{}", " <-".green(), " ACTIVE ".red());
} else {
println!("{}", branch.cyan());
}
// check if branch is ahead or behind
// TODO move this to state
//if branch.contains('[') {
// println!(
// "{}{}",
// " <-".black().italic(),
// " AHEAD ".bright_yellow()
// );
//} else if branch.contains(']') {
// println!(
// "{}{}",
// " <-".black().italic(),
// " BEHIND ".bright_yellow()
// );
//}
// Get the latest commit hash for the branch
let mut latest_commit_hash = String::new();
match Command::new("git")
.args(&["log", "-1", "--pretty=%H", branch])
.output()
{
Ok(output) => {
latest_commit_hash =
String::from_utf8_lossy(&output.stdout).to_string();
}
Err(error) => {
log_diagnostic(DiagnosticKind::Error {
subject: "git log",
body: &format!("{}", error),
});
}
}
// print the separator
if i != local_branches_len {
print!(" {}", "|".blue());
} else {
print!(" {}", " ".blue());
}
// print latest commit
if latest_commit_hash.is_empty() {
print!(" {}", "No commits".bright_black().italic());
} else {
print!(
" {} {}",
"Latest commit:".bright_yellow().italic(),
latest_commit_hash.white().italic()
);
}
}
// Collect remote branches
let remote_branches = stdout
.split("\n")
.filter(|line| line.contains("remotes"))
.collect::<Vec<&str>>();
// Log remote branches
println!("\n {}\n", " REMOTE ".black().on_magenta());
let mut i = 0;
let remote_branches_len = remote_branches.len();
for mut branch in remote_branches {
branch = branch.trim();
let branch_raw = branch.replace("remotes/origin/", "");
branch = branch_raw.as_str();
i += 1;
// check if on first or last branch
if i == 1 {
print!(" {} ", "/-".magenta());
} else if i == remote_branches_len {
print!(" {} ", "\\-".magenta());
} else {
print!(" {} ", "--".magenta());
}
println!("{}", branch.cyan());
// print the separator
if i != remote_branches_len {
print!(" {}", "|".blue());
}
// Get the latest commit hash for the branch
let mut latest_commit_hash = String::new();
match Command::new("git")
.args(&["log", "-1", "--pretty=%H", branch])
.output()
{
Ok(output) => {
latest_commit_hash =
String::from_utf8_lossy(&output.stdout).to_string();
}
Err(error) => {
log_diagnostic(DiagnosticKind::Error {
subject: "git log",
body: &format!("{}", error),
});
}
}
// print latest commit
if latest_commit_hash.is_empty() {
println!(" {}", "No commits".bright_black().italic());
} else {
println!(
" {} {}",
"Latest commit:".bright_yellow().italic(),
latest_commit_hash.white().italic()
);
}
}
}
Err(error) => {
println!("{}", error);
}
}
// match Command::new("git").arg("reset").output() {
// Ok(_) /* git add -A doesn't emit any output */ => {
// execute_state_git();
// }
// Err(error) => log_diagnostic(
// DiagnosticKind::Error {
// subject: "failed to stage (git)",
// body: &format!("{}", error)
// }
// )
// }
}
pub fn execute_branch_mercurial() {
log_diagnostic(DiagnosticKind::WorkInProgress {
feature: "scud branch (mercurial)",
});
}
pub fn execute_branch_breezy() {
log_diagnostic(DiagnosticKind::WorkInProgress {
feature: "scud branch (breezy)",
});
}