forked from OpenCoven/coven-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_utils.rs
More file actions
259 lines (227 loc) · 8.08 KB
/
Copy pathgit_utils.rs
File metadata and controls
259 lines (227 loc) · 8.08 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! Git utilities for Coven Code.
//! Mirrors src/utils/git.ts (926 lines) and src/utils/git/ subdirectory.
use crate::hosted_review::CanonicalRepoIdentity;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::process::Command;
// ---------------------------------------------------------------------------
// Repository discovery
// ---------------------------------------------------------------------------
/// Walk up the directory tree to find the nearest `.git` directory.
pub fn get_repo_root(start: &Path) -> Option<PathBuf> {
let mut current = start.to_path_buf();
loop {
let git_dir = current.join(".git");
if git_dir.exists() {
return Some(current);
}
if !current.pop() {
return None;
}
}
}
/// Run a git command in `repo_root` and return stdout as a String.
/// Returns empty string on failure (non-zero exit, not-a-repo, etc.).
fn git_output(repo_root: &Path, args: &[&str]) -> String {
Command::new("git")
.current_dir(repo_root)
.args(args)
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.unwrap_or_default()
}
// ---------------------------------------------------------------------------
// Branch / status
// ---------------------------------------------------------------------------
/// Return the current branch name (or "HEAD" if detached).
pub fn get_current_branch(repo_root: &Path) -> String {
let branch = git_output(repo_root, &["rev-parse", "--abbrev-ref", "HEAD"]);
if branch.is_empty() {
"HEAD".to_string()
} else {
branch
}
}
pub fn get_origin_remote_url(repo_root: &Path) -> Option<String> {
let remote = git_output(repo_root, &["remote", "get-url", "origin"]);
(!remote.is_empty()).then_some(remote)
}
pub fn canonical_repo_identity_from_origin(repo_root: &Path) -> Option<CanonicalRepoIdentity> {
let remote = get_origin_remote_url(repo_root)?;
CanonicalRepoIdentity::from_git_remote_url(&remote)
}
pub fn local_project_id_from_identity(identity: &CanonicalRepoIdentity) -> String {
let mut hasher = Sha256::new();
hasher.update(identity.canonical_string().as_bytes());
let digest = hex::encode(hasher.finalize());
format!("local-git-{}", &digest[..16])
}
pub fn local_project_id_from_origin(repo_root: &Path) -> Option<String> {
canonical_repo_identity_from_origin(repo_root)
.map(|identity| local_project_id_from_identity(&identity))
}
/// Return list of files modified (staged or unstaged).
pub fn list_modified_files(repo_root: &Path) -> Vec<PathBuf> {
let output = git_output(repo_root, &["diff", "--name-only", "HEAD"]);
if output.is_empty() {
return Vec::new();
}
output.lines().map(|l| repo_root.join(l)).collect()
}
// ---------------------------------------------------------------------------
// Diff
// ---------------------------------------------------------------------------
/// Return the staged diff (index vs HEAD).
pub fn get_staged_diff(repo_root: &Path) -> String {
git_output(repo_root, &["diff", "--cached"])
}
/// Return the unstaged diff (working tree vs index).
pub fn get_unstaged_diff(repo_root: &Path) -> String {
git_output(repo_root, &["diff"])
}
/// Return the diff for a specific file since a given commit (or HEAD).
pub fn get_file_diff(repo_root: &Path, path: &Path, since_commit: Option<&str>) -> String {
let commit = since_commit.unwrap_or("HEAD");
let path_str = path.to_string_lossy();
git_output(repo_root, &["diff", commit, "--", &path_str])
}
// ---------------------------------------------------------------------------
// History
// ---------------------------------------------------------------------------
/// A single git commit summary.
#[derive(Debug, Clone)]
pub struct CommitInfo {
pub hash: String,
pub short_hash: String,
pub author: String,
pub date: String,
pub subject: String,
}
/// Return the last `n` commits in the repository.
pub fn get_commit_history(repo_root: &Path, n: usize) -> Vec<CommitInfo> {
let format = "%H%x1f%h%x1f%an%x1f%ad%x1f%s%x1e";
let n_str = n.to_string();
let output = git_output(
repo_root,
&[
"log",
&format!("-{}", n_str),
&format!("--format={}", format),
"--date=short",
],
);
output
.split('\x1e')
.filter(|s| !s.trim().is_empty())
.filter_map(|entry| {
let parts: Vec<&str> = entry.trim().splitn(5, '\x1f').collect();
if parts.len() == 5 {
Some(CommitInfo {
hash: parts[0].to_string(),
short_hash: parts[1].to_string(),
author: parts[2].to_string(),
date: parts[3].to_string(),
subject: parts[4].to_string(),
})
} else {
None
}
})
.collect()
}
// ---------------------------------------------------------------------------
// Branch operations
// ---------------------------------------------------------------------------
/// Create and switch to a new branch.
pub fn create_branch(repo_root: &Path, name: &str) -> bool {
Command::new("git")
.current_dir(repo_root)
.args(["checkout", "-b", name])
.status()
.map(|s| s.success())
.unwrap_or(false)
}
/// Switch to an existing branch.
pub fn switch_branch(repo_root: &Path, name: &str) -> bool {
Command::new("git")
.current_dir(repo_root)
.args(["checkout", name])
.status()
.map(|s| s.success())
.unwrap_or(false)
}
// ---------------------------------------------------------------------------
// Stash
// ---------------------------------------------------------------------------
/// Stash uncommitted changes with an optional message.
pub fn stash(repo_root: &Path, message: Option<&str>) -> bool {
let mut args = vec!["stash", "push"];
let msg_flag;
if let Some(m) = message {
msg_flag = format!("-m {}", m);
args.push(&msg_flag);
}
Command::new("git")
.current_dir(repo_root)
.args(&args)
.status()
.map(|s| s.success())
.unwrap_or(false)
}
/// Pop the top stash entry.
pub fn stash_pop(repo_root: &Path) -> bool {
Command::new("git")
.current_dir(repo_root)
.args(["stash", "pop"])
.status()
.map(|s| s.success())
.unwrap_or(false)
}
// ---------------------------------------------------------------------------
// .gitignore check
// ---------------------------------------------------------------------------
/// Returns `true` if the given path is git-ignored.
pub fn is_ignored(repo_root: &Path, path: &Path) -> bool {
let path_str = path.to_string_lossy();
Command::new("git")
.current_dir(repo_root)
.args(["check-ignore", "-q", &path_str])
.status()
.map(|s| s.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn get_repo_root_finds_git() {
// Run from within the src-rust workspace which has .git
let result = get_repo_root(Path::new("."));
// Should find the repo root (may or may not exist in test env)
// Just verify it doesn't panic.
let _ = result;
}
#[test]
fn commit_info_parse() {
// smoke test — just ensure it doesn't panic with empty output
let commits = get_commit_history(Path::new("."), 0);
assert!(commits.is_empty());
}
#[test]
fn local_project_id_normalizes_equivalent_https_and_ssh_remotes() {
let https = CanonicalRepoIdentity::from_git_remote_url(
"https://github.com/OpenCoven/coven-code.git",
)
.unwrap();
let ssh =
CanonicalRepoIdentity::from_git_remote_url("git@github.com:OpenCoven/coven-code.git")
.unwrap();
assert_eq!(
local_project_id_from_identity(&https),
local_project_id_from_identity(&ssh)
);
}
}