Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,14 @@

// Validate any stored token at startup. If the /user endpoint returns 401,
// the token is stale – clear it so subsequent requests don't carry a bad header.
let auth_user = match github.fetch_authenticated_user() {

Check failure on line 188 in src/app/mod.rs

View workflow job for this annotation

GitHub Actions / check

match can be simplified with `.unwrap_or_default()`
Ok(user) => user,
Err(_) => None,
};
if auth_user.is_none() && token.is_some() {
log::warn!("stored token rejected by GitHub, clearing it");
let _ = auth::clear_token();
github = crate::provider::create_provider(
crate::provider::ProviderKind::GitHub,
None,
)?;
github = crate::provider::create_provider(crate::provider::ProviderKind::GitHub, None)?;
}

if account.preferred_clone_dir.trim().is_empty() {
Expand All @@ -211,9 +208,7 @@
"Stored token is invalid. Press t to set a new one or continue anonymously."
.to_string()
}
None => {
"No token set. Press t to save one or continue anonymously.".to_string()
}
None => "No token set. Press t to save one or continue anonymously.".to_string(),
};

Ok(Self {
Expand Down Expand Up @@ -430,11 +425,8 @@

fn test_app() -> App {
App {
github: crate::provider::create_provider(
crate::provider::ProviderKind::GitHub,
None,
)
.expect("client"),
github: crate::provider::create_provider(crate::provider::ProviderKind::GitHub, None)
.expect("client"),
account: crate::config::AccountConfig {
preferred_clone_dir: ".".to_string(),
last_branch_by_repo: Default::default(),
Expand Down Expand Up @@ -642,11 +634,8 @@
#[test]
fn lazy_tree_progress_advances_limit() {
let mut app = App {
github: crate::provider::create_provider(
crate::provider::ProviderKind::GitHub,
None,
)
.expect("client"),
github: crate::provider::create_provider(crate::provider::ProviderKind::GitHub, None)
.expect("client"),
account: crate::config::AccountConfig {
preferred_clone_dir: ".".to_string(),
last_branch_by_repo: Default::default(),
Expand Down
6 changes: 5 additions & 1 deletion src/app/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ mod tests {
}
// Verify load_theme_by_name returns Berlin
let berlin = load_theme_by_name("Berlin");
assert_eq!(berlin.palette.len(), 16, "Berlin palette should have 16 entries");
assert_eq!(
berlin.palette.len(),
16,
"Berlin palette should have 16 entries"
);
assert_eq!(berlin.palette[0], [0, 0, 0], "first entry should be black");
// Apply Berlin theme
init_theme(&berlin);
Expand Down
6 changes: 2 additions & 4 deletions src/cli/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,8 @@ pub fn merge(branch: &str) -> Result<()> {

pub fn download_file(repo: &str, path: &str, r#ref: Option<&str>, out: &PathBuf) -> Result<()> {
let token = auth::load_token()?;
let client = crate::provider::create_provider(
crate::provider::ProviderKind::GitHub,
token.as_deref(),
)?;
let client =
crate::provider::create_provider(crate::provider::ProviderKind::GitHub, token.as_deref())?;

let bytes = match r#ref {
Some(branch) if !branch.trim().is_empty() => {
Expand Down
12 changes: 6 additions & 6 deletions src/cli/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@
.downcast_ref::<GitHubError>()
.map(|gh_err| match gh_err {
GitHubError::Api { status, body } if *status == 404 || body.contains("Not Found") => {
format!("repository '{full_name}' not found on GitHub")
}
GitHubError::Unauthorized => {
"authentication required — run 'gitnapse auth set' or 'gitnapse auth oauth login'"
.to_string()
}
format!("repository '{full_name}' not found on GitHub")
}
GitHubError::Unauthorized => {
"authentication required — run 'gitnapse auth set' or 'gitnapse auth oauth login'"
.to_string()
}
GitHubError::RateLimited {
remaining: _,
reset,
Expand All @@ -129,7 +129,7 @@
_ => format!("{gh_err}"),
})
.unwrap_or_else(|| format!("{e}"));
msg

Check failure on line 132 in src/cli/helpers.rs

View workflow job for this annotation

GitHub Actions / check

returning the result of a `let` binding from a block
}

pub fn make_client() -> Result<Arc<dyn GitProvider>> {
Expand Down
25 changes: 20 additions & 5 deletions src/config/keybindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,31 @@ fn parse_keybinding(s: &str) -> Option<BoundKey> {
let s = s.trim();
if let Some(rest) = s.strip_prefix("Ctrl+").or_else(|| s.strip_prefix("ctrl+")) {
let code = str_to_keycode(rest)?;
Some(BoundKey { code, modifiers: KeyModifiers::CONTROL })
} else if let Some(rest) = s.strip_prefix("Shift+").or_else(|| s.strip_prefix("shift+")) {
Some(BoundKey {
code,
modifiers: KeyModifiers::CONTROL,
})
} else if let Some(rest) = s
.strip_prefix("Shift+")
.or_else(|| s.strip_prefix("shift+"))
{
let code = str_to_keycode(rest)?;
Some(BoundKey { code, modifiers: KeyModifiers::SHIFT })
Some(BoundKey {
code,
modifiers: KeyModifiers::SHIFT,
})
} else if let Some(rest) = s.strip_prefix("Alt+").or_else(|| s.strip_prefix("alt+")) {
let code = str_to_keycode(rest)?;
Some(BoundKey { code, modifiers: KeyModifiers::ALT })
Some(BoundKey {
code,
modifiers: KeyModifiers::ALT,
})
} else {
let code = str_to_keycode(s)?;
Some(BoundKey { code, modifiers: KeyModifiers::NONE })
Some(BoundKey {
code,
modifiers: KeyModifiers::NONE,
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
mod ci;
mod compare;
mod content;
mod prs;
mod provider_impl;
mod prs;
mod releases;
mod repos;

Expand Down
29 changes: 10 additions & 19 deletions src/github/provider_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ impl GitProvider for GitHubClient {
self.fetch_repo_tree(full_name, branch).map_err(Into::into)
}

fn fetch_starred_repos(&self, page: u32, per_page: u8) -> Result<Vec<crate::models::RepoSummary>> {
fn fetch_starred_repos(
&self,
page: u32,
per_page: u8,
) -> Result<Vec<crate::models::RepoSummary>> {
self.fetch_starred_repos(page, per_page).map_err(Into::into)
}

Expand Down Expand Up @@ -142,22 +146,12 @@ impl GitProvider for GitHubClient {
Ok(())
}

fn update_pull_request(
&self,
full_name: &str,
number: u64,
state: &str,
) -> Result<()> {
fn update_pull_request(&self, full_name: &str, number: u64, state: &str) -> Result<()> {
let _ = self.update_pull_request(full_name, number, state)?;
Ok(())
}

fn create_pull_request_comment(
&self,
full_name: &str,
number: u64,
body: &str,
) -> Result<()> {
fn create_pull_request_comment(&self, full_name: &str, number: u64, body: &str) -> Result<()> {
let _ = self.create_pull_request_comment(full_name, number, body)?;
Ok(())
}
Expand Down Expand Up @@ -190,7 +184,8 @@ impl GitProvider for GitHubClient {
base: &str,
head: &str,
) -> Result<crate::models::CompareResponse> {
self.fetch_compare(full_name, base, head).map_err(Into::into)
self.fetch_compare(full_name, base, head)
.map_err(Into::into)
}

fn fetch_check_runs(
Expand All @@ -211,11 +206,7 @@ impl GitProvider for GitHubClient {
.map_err(Into::into)
}

fn fetch_releases(
&self,
full_name: &str,
per_page: u8,
) -> Result<Vec<crate::models::Release>> {
fn fetch_releases(&self, full_name: &str, per_page: u8) -> Result<Vec<crate::models::Release>> {
self.fetch_releases(full_name, per_page).map_err(Into::into)
}

Expand Down
42 changes: 30 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ fn main() -> Result<()> {
}

fn dispatch(cmd: Option<Command>) -> Result<()> {
use Command::{Run, DownloadFile, Clone, Commit, Push, Pull, Fetch, Checkout, Diff, Stash, Tag, Status, Log, Branch, Reset, Pr, Issue, Ci, Compare, Remote, Config, Merge, Release, Repo, Search, Auth};
use Command::{
Auth, Branch, Checkout, Ci, Clone, Commit, Compare, Config, Diff, DownloadFile, Fetch,
Issue, Log, Merge, Pr, Pull, Push, Release, Remote, Repo, Reset, Run, Search, Stash,
Status, Tag,
};
match cmd {
Some(Run(args)) => app::run_with_options(args.into()),
Some(DownloadFile(args)) => {
Expand Down Expand Up @@ -141,7 +145,7 @@ fn dispatch(cmd: Option<Command>) -> Result<()> {
}

fn dispatch_stash(action: cli::StashAction) -> Result<()> {
use cli::StashAction::{Push, Pop, List};
use cli::StashAction::{List, Pop, Push};
match action {
Push { message } => cli::stash_push(message.as_deref()),
Pop => cli::stash_pop(),
Expand All @@ -150,16 +154,20 @@ fn dispatch_stash(action: cli::StashAction) -> Result<()> {
}

fn dispatch_tag(action: cli::TagAction) -> Result<()> {
use cli::TagAction::{List, Create, Delete};
use cli::TagAction::{Create, Delete, List};
match action {
List { pattern } => cli::tag_list(pattern.as_deref()),
Create { name, message, target } => cli::tag_create(&name, message.as_deref(), target.as_deref()),
Create {
name,
message,
target,
} => cli::tag_create(&name, message.as_deref(), target.as_deref()),
Delete { name } => cli::tag_delete(&name),
}
}

fn dispatch_pr(action: cli::PrAction) -> Result<()> {
use cli::PrAction::{List, Create, Merge};
use cli::PrAction::{Create, List, Merge};
match action {
List(a) => cli::pr_list(&a.repo, &a.state),
Create(a) => cli::pr_create(&a.repo, &a.title, &a.head, &a.base, a.body.as_deref()),
Expand All @@ -168,7 +176,7 @@ fn dispatch_pr(action: cli::PrAction) -> Result<()> {
}

fn dispatch_issue(action: cli::IssueAction) -> Result<()> {
use cli::IssueAction::{List, Create, Close};
use cli::IssueAction::{Close, Create, List};
match action {
List(a) => cli::issue_list(&a.repo, &a.state),
Create(a) => cli::issue_create(&a.repo, &a.title, a.body.as_deref()),
Expand All @@ -177,7 +185,7 @@ fn dispatch_issue(action: cli::IssueAction) -> Result<()> {
}

fn dispatch_remote(action: cli::RemoteAction) -> Result<()> {
use cli::RemoteAction::{List, Add, Remove, Rename};
use cli::RemoteAction::{Add, List, Remove, Rename};
match action {
List => cli::remote_list(),
Add(a) => cli::remote_add(&a.name, &a.url),
Expand All @@ -187,7 +195,7 @@ fn dispatch_remote(action: cli::RemoteAction) -> Result<()> {
}

fn dispatch_config(action: cli::ConfigAction) -> Result<()> {
use cli::ConfigAction::{Get, Set, List};
use cli::ConfigAction::{Get, List, Set};
match action {
Get(a) => cli::config_get(&a.key),
Set(a) => cli::config_set(&a.key, &a.value),
Expand All @@ -196,10 +204,16 @@ fn dispatch_config(action: cli::ConfigAction) -> Result<()> {
}

fn dispatch_release(action: cli::ReleaseAction) -> Result<()> {
use cli::ReleaseAction::{List, Create};
use cli::ReleaseAction::{Create, List};
match action {
List(a) => cli::release_list(&a.repo),
Create(a) => cli::release_create(&a.repo, &a.tag_name, a.name.as_deref(), a.body.as_deref(), a.prerelease),
Create(a) => cli::release_create(
&a.repo,
&a.tag_name,
a.name.as_deref(),
a.body.as_deref(),
a.prerelease,
),
}
}

Expand All @@ -211,7 +225,7 @@ fn dispatch_repo(action: cli::RepoAction) -> Result<()> {
}

fn dispatch_auth(action: cli::AuthAction) -> Result<()> {
use cli::AuthAction::{Set, Clear, Status, Oauth};
use cli::AuthAction::{Clear, Oauth, Set, Status};
match action {
Set { token } => auth::set_token_cli(token),
Clear => auth::clear_token_cli(),
Expand All @@ -223,7 +237,11 @@ fn dispatch_auth(action: cli::AuthAction) -> Result<()> {
fn dispatch_oauth(action: cli::OauthAction) -> Result<()> {
use cli::OauthAction::{Login, Status};
match action {
Login { client_id, scope, timeout_secs } => oauth::oauth_device_login_cli(client_id, scope, timeout_secs),
Login {
client_id,
scope,
timeout_secs,
} => oauth::oauth_device_login_cli(client_id, scope, timeout_secs),
Status => oauth::oauth_status_cli(),
}
}
6 changes: 2 additions & 4 deletions src/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,8 @@ pub fn oauth_status_cli() -> Result<()> {
return Ok(());
}

let client = crate::provider::create_provider(
crate::provider::ProviderKind::GitHub,
token.as_deref(),
)?;
let client =
crate::provider::create_provider(crate::provider::ProviderKind::GitHub, token.as_deref())?;
let user = client.fetch_authenticated_user()?;
let authenticated = user.is_some();

Expand Down
Loading
Loading