Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace git2::Exception with Result #1104

Open
ken-matsui opened this issue Jan 19, 2025 · 6 comments
Open

Replace git2::Exception with Result #1104

ken-matsui opened this issue Jan 19, 2025 · 6 comments
Labels
good first issue Good for newcomers

Comments

@ken-matsui
Copy link
Member

No description provided.

@ken-matsui ken-matsui added the good first issue Good for newcomers label Jan 19, 2025
@adityamwagh
Copy link

adityamwagh commented Jan 29, 2025

If I understand correctly, this is to replace git2::Exception with a Result type defined in Rustify/Result.hpp right?

@ken-matsui
Copy link
Member Author

Yes, correct

@adityamwagh
Copy link

adityamwagh commented Jan 29, 2025

Would something like this suffice?

static Result<std::string> getAuthor() noexcept {
  git2::Config config;
  if (auto res = config.openDefault(); !res) {
    logger::debug("Failed to open git config: {}", res.error().message());
    return Err(to_anyhow(res.error().message()));
  }

  auto name = toml::try_find<std::string>(config, "user.name");
  if (!name) {
    logger::debug("Failed to get user.name: {}", name.error().message());
    return Err(to_anyhow(name.error().message()));
  }

  auto email = toml::try_find<std::string>(config, "user.email");
  if (!email) {
    logger::debug("Failed to get user.email: {}", email.error().message());
    return Err(to_anyhow(email.error().message()));
  }

  return Ok(*name + " <" + *email + ">");
}

@ken-matsui
Copy link
Member Author

Can’t we just return Result from git2 and propagate it via Try?

@adityamwagh
Copy link

How about this? My understanding is that Try(config.openDefault()); should store Err or Ok appropriately.

static Result<std::string> getAuthor() noexcept {
  git2::Config config;
  Try(config.openDefault());

  auto name = Try(toml::try_find<std::string>(config, "user.name"));
  auto email = Try(toml::try_find<std::string>(config, "user.email"));

  return Ok(name + " <" + email + ">");
}

@ken-matsui
Copy link
Member Author

getAuthor is not our interest in this issue. We’d like to modify git2 to use/return result, and all use of git2 will need to handle the returned result from git2 appropriately. In theory, we can make almost all functions in gut2 noexcept as a good side effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants