Skip to main content

git_workflow/git/
query.rs

1//! Read-only git operations
2
3use std::path::PathBuf;
4use std::process::Command;
5
6use crate::error::{GwError, Result};
7
8/// Execute a git command and return stdout as string
9fn git_output(args: &[&str]) -> Result<String> {
10    let output = Command::new("git")
11        .args(args)
12        .output()
13        .map_err(|e| GwError::GitCommandFailed(format!("Failed to execute git: {e}")))?;
14
15    if output.status.success() {
16        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
17    } else {
18        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
19        Err(GwError::GitCommandFailed(stderr))
20    }
21}
22
23/// Execute a git command and check if it succeeded (ignoring output)
24fn git_check(args: &[&str]) -> bool {
25    Command::new("git")
26        .args(args)
27        .output()
28        .map(|o| o.status.success())
29        .unwrap_or(false)
30}
31
32/// Check if we're in a git repository
33pub fn is_git_repo() -> bool {
34    git_check(&["rev-parse", "--git-dir"])
35}
36
37/// Get the current branch name
38pub fn current_branch() -> Result<String> {
39    git_output(&["rev-parse", "--abbrev-ref", "HEAD"])
40}
41
42/// Get the worktree root (the top-level working directory of the current worktree)
43pub fn worktree_root() -> Result<PathBuf> {
44    git_output(&["rev-parse", "--show-toplevel"]).map(PathBuf::from)
45}
46
47/// Get the git directory (.git or .git/worktrees/xxx)
48pub fn git_dir() -> Result<PathBuf> {
49    git_output(&["rev-parse", "--git-dir"]).map(PathBuf::from)
50}
51
52/// Get the common git directory (always .git of main repo)
53pub fn git_common_dir() -> Result<PathBuf> {
54    git_output(&["rev-parse", "--git-common-dir"]).map(PathBuf::from)
55}
56
57/// Check if we're in a worktree (not the main repo)
58pub fn is_worktree() -> Result<bool> {
59    let git_dir = git_dir()?;
60    let common_dir = git_common_dir()?;
61    Ok(git_dir != common_dir)
62}
63
64/// Check if a branch exists locally
65pub fn branch_exists(branch: &str) -> bool {
66    git_check(&[
67        "show-ref",
68        "--verify",
69        "--quiet",
70        &format!("refs/heads/{branch}"),
71    ])
72}
73
74/// Check if a branch exists on remote origin
75pub fn remote_branch_exists(branch: &str) -> bool {
76    git_check(&["ls-remote", "--exit-code", "--heads", "origin", branch])
77}
78
79/// Get the current HEAD commit hash
80pub fn head_commit() -> Result<String> {
81    git_output(&["rev-parse", "HEAD"])
82}
83
84/// Get the short commit hash
85pub fn short_commit() -> Result<String> {
86    git_output(&["rev-parse", "--short", "HEAD"])
87}
88
89/// Get the commit message of HEAD
90pub fn head_commit_message() -> Result<String> {
91    git_output(&["log", "-1", "--format=%s"])
92}
93
94/// Check if working directory has unstaged changes
95pub fn has_unstaged_changes() -> bool {
96    !git_check(&["diff", "--quiet"])
97}
98
99/// Check if working directory has staged changes
100pub fn has_staged_changes() -> bool {
101    !git_check(&["diff", "--cached", "--quiet"])
102}
103
104/// Check if working directory has any uncommitted changes (staged or unstaged)
105pub fn has_uncommitted_changes() -> bool {
106    has_unstaged_changes() || has_staged_changes()
107}
108
109/// Check if working directory has untracked files
110pub fn has_untracked_files() -> bool {
111    git_output(&["ls-files", "--others", "--exclude-standard"])
112        .map(|s| !s.is_empty())
113        .unwrap_or(false)
114}
115
116/// Get the upstream tracking branch for a local branch, if any
117pub fn get_upstream(branch: &str) -> Option<String> {
118    git_output(&[
119        "rev-parse",
120        "--abbrev-ref",
121        &format!("{branch}@{{upstream}}"),
122    ])
123    .ok()
124    .filter(|s| !s.is_empty())
125}
126
127/// Check if a branch has a remote tracking branch
128pub fn has_remote_tracking(branch: &str) -> bool {
129    get_upstream(branch).is_some()
130}
131
132/// Count commits between two refs (exclusive..inclusive)
133pub fn commit_count(from: &str, to: &str) -> Result<usize> {
134    let output = git_output(&["rev-list", "--count", &format!("{from}..{to}")])?;
135    output
136        .parse()
137        .map_err(|_| GwError::GitCommandFailed("Failed to parse commit count".to_string()))
138}
139
140/// Get number of unpushed commits on a branch (compared to its upstream)
141pub fn unpushed_commit_count(branch: &str) -> Result<usize> {
142    let upstream = get_upstream(branch)
143        .ok_or_else(|| GwError::Other(format!("Branch '{branch}' has no upstream")))?;
144    commit_count(&upstream, branch)
145}
146
147/// Get number of commits behind upstream
148pub fn behind_upstream_count(branch: &str) -> Result<usize> {
149    let upstream = get_upstream(branch)
150        .ok_or_else(|| GwError::Other(format!("Branch '{branch}' has no upstream")))?;
151    commit_count(branch, &upstream)
152}
153
154/// Count stashes
155pub fn stash_count() -> usize {
156    git_output(&["stash", "list"])
157        .map(|s| if s.is_empty() { 0 } else { s.lines().count() })
158        .unwrap_or(0)
159}
160
161/// Get the message of the latest stash (stash@{0})
162pub fn get_latest_stash_message() -> Option<String> {
163    git_output(&["stash", "list", "-1", "--format=%gs"])
164        .ok()
165        .filter(|s| !s.is_empty())
166}
167
168/// Check if HEAD has a parent commit (i.e., we can undo)
169pub fn has_commits_to_undo() -> bool {
170    git_check(&["rev-parse", "HEAD~1"])
171}
172
173/// Get the current working directory name
174pub fn current_dir_name() -> Result<String> {
175    std::env::current_dir()
176        .map_err(GwError::Io)?
177        .file_name()
178        .and_then(|s| s.to_str())
179        .map(String::from)
180        .ok_or_else(|| GwError::Other("Could not determine current directory name".to_string()))
181}
182
183/// Get the default remote branch (origin/main or origin/master)
184pub fn get_default_remote_branch() -> Result<String> {
185    if remote_branch_exists("main") {
186        Ok("origin/main".to_string())
187    } else if remote_branch_exists("master") {
188        Ok("origin/master".to_string())
189    } else {
190        Err(GwError::Other(
191            "Neither origin/main nor origin/master exists".to_string(),
192        ))
193    }
194}
195
196/// Check if HEAD is detached
197pub fn is_detached_head() -> bool {
198    current_branch().map(|b| b == "HEAD").unwrap_or(false)
199}
200
201/// Get the top-level working directory of the repository
202pub fn repo_root() -> Result<PathBuf> {
203    git_output(&["rev-parse", "--show-toplevel"]).map(PathBuf::from)
204}