Skip to main content

bitbucket_cli/api/
commits.rs

1use anyhow::Result;
2
3use super::BitbucketClient;
4use crate::api::util::urlencode_segment;
5use crate::models::{CommitSummary, Paginated};
6
7impl BitbucketClient {
8    /// List commits in a repository, newest first.
9    ///
10    /// With `revision` set, lists the history reachable from that branch,
11    /// tag, or commit hash; otherwise lists from the default branch.
12    pub async fn list_commits(
13        &self,
14        workspace: &str,
15        repo_slug: &str,
16        revision: Option<&str>,
17        page: Option<u32>,
18        pagelen: Option<u32>,
19    ) -> Result<Paginated<CommitSummary>> {
20        let path = match revision {
21            Some(rev) => format!(
22                "/repositories/{}/{}/commits/{}",
23                workspace,
24                repo_slug,
25                urlencode_segment(rev)
26            ),
27            None => format!("/repositories/{}/{}/commits", workspace, repo_slug),
28        };
29
30        let mut params = Vec::new();
31        if let Some(p) = page {
32            params.push(("page", p.to_string()));
33        }
34        if let Some(len) = pagelen {
35            params.push(("pagelen", len.to_string()));
36        }
37
38        let param_refs: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, v.as_str())).collect();
39        self.get_with_query(&path, &param_refs).await
40    }
41
42    /// Get a single commit by hash.
43    pub async fn get_commit(
44        &self,
45        workspace: &str,
46        repo_slug: &str,
47        hash: &str,
48    ) -> Result<CommitSummary> {
49        let path = format!(
50            "/repositories/{}/{}/commit/{}",
51            workspace,
52            repo_slug,
53            urlencode_segment(hash)
54        );
55        self.get(&path).await
56    }
57
58    /// Get a raw unified diff for a commit or revision spec.
59    ///
60    /// `spec` is either a single revision (diff against its first parent) or
61    /// a `source..destination` pair of revisions.
62    pub async fn get_diff(&self, workspace: &str, repo_slug: &str, spec: &str) -> Result<String> {
63        let path = format!(
64            "/repositories/{}/{}/diff/{}",
65            workspace,
66            repo_slug,
67            urlencode_segment(spec)
68        );
69        self.get_text(&path, None).await
70    }
71}