Thanks to visit codestin.com
Credit goes to github.com

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
42 changes: 19 additions & 23 deletions src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ fn branch_name_lossy(dir: &Path, hash: &str) -> Res<Option<String>> {
}

pub(crate) fn diff_unstaged(repo: &Repository) -> Res<Diff> {
let text = String::from_utf8(
Command::new("git")
let text = String::from_utf8_lossy(
&Command::new("git")
.current_dir(repo.workdir().expect("Bare repos unhandled"))
.args(["diff", "--no-ext-diff"])
.output()
.map_err(Error::GitDiff)?
.stdout,
)
.map_err(Error::GitDiffUtf8)?;
.into_owned();

Ok(Diff {
file_diffs: gitu_diff::Parser::new(&text).parse_diff().unwrap(),
Expand All @@ -143,15 +143,15 @@ pub(crate) fn diff_unstaged(repo: &Repository) -> Res<Diff> {
}

pub(crate) fn diff_staged(repo: &Repository) -> Res<Diff> {
let text = String::from_utf8(
Command::new("git")
let text = String::from_utf8_lossy(
&Command::new("git")
.current_dir(repo.workdir().expect("Bare repos unhandled"))
.args(["diff", "--no-ext-diff", "--staged"])
.output()
.map_err(Error::GitDiff)?
.stdout,
)
.map_err(Error::GitDiffUtf8)?;
.into_owned();

Ok(Diff {
file_diffs: gitu_diff::Parser::new(&text).parse_diff().unwrap(),
Expand All @@ -161,29 +161,29 @@ pub(crate) fn diff_staged(repo: &Repository) -> Res<Diff> {
}

pub(crate) fn status(dir: &Path) -> Res<status::Status> {
let text = String::from_utf8(
Command::new("git")
let text = String::from_utf8_lossy(
&Command::new("git")
.current_dir(dir)
.args(["status", "--porcelain", "--branch"])
.output()
.unwrap()
.stdout,
)
.map_err(Error::GitDiffUtf8)?;
.into_owned();

Ok(status::Status::from_str(&text).unwrap())
}

pub(crate) fn show(repo: &Repository, reference: &str) -> Res<Diff> {
let text = String::from_utf8(
Command::new("git")
let text = String::from_utf8_lossy(
&Command::new("git")
.current_dir(repo.workdir().expect("Bare repos unhandled"))
.args(["show", reference])
.output()
.map_err(Error::GitShow)?
.stdout,
)
.map_err(Error::GitShowUtf8)?;
.into_owned();

Ok(Diff {
file_diffs: gitu_diff::Parser::new(&text).parse_diff().unwrap(),
Expand All @@ -193,15 +193,15 @@ pub(crate) fn show(repo: &Repository, reference: &str) -> Res<Diff> {
}

pub(crate) fn stash_show(repo: &Repository, stash_ref: &str) -> Res<Diff> {
let text = String::from_utf8(
Command::new("git")
let text = String::from_utf8_lossy(
&Command::new("git")
.current_dir(repo.workdir().expect("Bare repos unhandled"))
.args(["stash", "show", "-p", stash_ref])
.output()
.map_err(Error::GitShow)?
.stdout,
)
.map_err(Error::GitShowUtf8)?;
.into_owned();

Ok(Diff {
file_diffs: gitu_diff::Parser::new(&text).parse_diff().unwrap(),
Expand Down Expand Up @@ -252,20 +252,16 @@ pub(crate) fn show_summary(repo: &Repository, reference: &str) -> Res<Commit> {
}

pub(crate) fn get_current_branch_name(repo: &git2::Repository) -> Res<String> {
String::from_utf8(
Ok(String::from_utf8_lossy(
get_current_branch(repo)?
.name_bytes()
.map_err(Error::CurrentBranchName)?
.to_vec(),
.map_err(Error::CurrentBranchName)?,
)
.map_err(Utf8Error::String)
.map_err(Error::BranchNameUtf8)
.into_owned())
}

pub(crate) fn get_head_name(repo: &git2::Repository) -> Res<String> {
String::from_utf8(repo.head().map_err(Error::GetHead)?.name_bytes().to_vec())
.map_err(Utf8Error::String)
.map_err(Error::BranchNameUtf8)
Ok(String::from_utf8_lossy(repo.head().map_err(Error::GetHead)?.name_bytes()).into_owned())
}

pub(crate) fn get_current_branch(repo: &git2::Repository) -> Res<Branch<'_>> {
Expand Down
4 changes: 2 additions & 2 deletions src/git/parse/status/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ fn parse_file_path(input: &str) -> IResult<&str, &str> {

fn unescape(path: String) -> String {
if path.starts_with('"') && path.ends_with('"') {
String::from_utf8(smashquote::unescape_bytes(&path.as_bytes()[1..path.len() - 1]).unwrap())
.unwrap()
String::from_utf8_lossy(&smashquote::unescape_bytes(&path.as_bytes()[1..path.len() - 1]).unwrap())
.into_owned()
} else {
path
}
Expand Down
30 changes: 9 additions & 21 deletions src/git/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ pub(crate) fn get_branch_upstream<'repo>(branch: &Branch<'repo>) -> Res<Option<B
}

pub(crate) fn get_remote_name(repo: &Repository, upstream: &Branch) -> Res<String> {
let branch_full = str::from_utf8(upstream.get().name_bytes())
.map_err(Utf8Error::Str)
.map_err(Error::BranchNameUtf8)?;
let branch_full = String::from_utf8_lossy(upstream.get().name_bytes());

String::from_utf8(
repo.branch_remote_name(branch_full)
Ok(String::from_utf8_lossy(
repo.branch_remote_name(&branch_full)
.map_err(Error::GetRemote)?
.deref()
.to_vec(),
.deref(),
)
.map_err(Utf8Error::String)
.map_err(Error::RemoteNameUtf8)
.into_owned())
}

/// If the branch has an upstream, returns the remote name and branch name in that order.
Expand All @@ -52,9 +48,7 @@ pub(crate) fn get_upstream_components(repo: &Repository) -> Res<Option<(String,
return Ok(None);
};

let branch = String::from_utf8(upstream.get().shorthand_bytes().to_vec())
.map_err(Utf8Error::String)
.map_err(Error::BranchNameUtf8)?;
let branch = String::from_utf8_lossy(upstream.get().shorthand_bytes()).into_owned();

if upstream.get().is_remote() {
let remote_name = get_remote_name(repo, &upstream)?;
Expand All @@ -70,9 +64,7 @@ pub(crate) fn get_upstream_shortname(repo: &Repository) -> Res<Option<String>> {
return Ok(None);
};
Ok(Some(
String::from_utf8(upstream.get().shorthand_bytes().to_vec())
.map_err(Utf8Error::String)
.map_err(Error::GetCurrentBranchUpstreamUtf8)?,
String::from_utf8_lossy(upstream.get().shorthand_bytes()).into_owned(),
))
}

Expand All @@ -95,9 +87,7 @@ pub(crate) fn get_push_remote(repo: &Repository) -> Res<Option<String>> {

match config.get_entry(&push_remote_cfg) {
Ok(entry) => Ok(Some(
String::from_utf8(entry.value_bytes().to_vec())
.map_err(Utf8Error::String)
.map_err(Error::ReadGitConfigUtf8)?,
String::from_utf8_lossy(entry.value_bytes()).into_owned(),
)),
Err(e) if e.class() == git2::ErrorClass::Config => get_default_push_remote(repo),
Err(e) => Err(Error::ReadGitConfig(e)),
Expand All @@ -110,9 +100,7 @@ pub(crate) fn get_default_push_remote(repo: &Repository) -> Res<Option<String>>

match config.get_entry(push_default_cfg) {
Ok(entry) => Ok(Some(
String::from_utf8(entry.value_bytes().to_vec())
.map_err(Utf8Error::String)
.map_err(Error::ReadGitConfigUtf8)?,
String::from_utf8_lossy(entry.value_bytes()).into_owned(),
)),
Err(e) if e.class() == git2::ErrorClass::Config => Ok(None),
Err(e) => Err(Error::ReadGitConfig(e)),
Expand Down
16 changes: 16 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,22 @@ fn tab_diff() {
insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn non_utf8_diff() {
let mut ctx = setup_clone!();
let mut app = ctx.init_app();

commit(&ctx.dir, "non_utf8.txt", "File with valid UTF-8");
fs::write(
ctx.dir.join("non_utf8.txt"),
b"File with invalid UTF-8: \xff\xfe\n",
)
.unwrap();
ctx.update(&mut app, keys("g"));

insta::assert_snapshot!(ctx.redact_buffer());
}

#[test]
fn ext_diff() {
let mut ctx = setup_clone!();
Expand Down
25 changes: 25 additions & 0 deletions src/tests/snapshots/gitu__tests__non_utf8_diff.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/mod.rs
expression: ctx.redact_buffer()
---
▌On branch main |
▌Your branch is ahead of 'origin/main' by 1 commit(s). |
|
Unstaged changes (1) |
modified non_utf8.txt |
@@ -1 +1 @@ |
-File with valid UTF-8 |
\ No newline at end of file |
+FileFile with invalid UTF-8: �� |
|
Recent commits |
7c3d61a main add non_utf8.txt |
b66a0bf origin/main add initial-file |
|
|
|
|
|
|
|
styles_hash: 232c1529ecd86f60