Skip to content

Commit

Permalink
No longer require Git user configuration to be present
Browse files Browse the repository at this point in the history
Stop requiring Git user configuration to be present and use reasonable
default values instead. The defaults can be overwritten via the
GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables.

Closes: #58
  • Loading branch information
d-e-s-o committed Aug 15, 2024
1 parent 1b9d225 commit a4ce5f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
----------
- Fixed handling of renamed packages
- Stop requiring Git user configuration to be present by using
reasonable defaults
- Added `Dockerfile` and adjusted CI to build and publish Docker image
to GHCR
- Include and manage `Cargo.lock` file in repository
Expand Down
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@ RUN apt-get update && \
apt-get install -y git && \
apt-get clean && rm -rf /var/lib/apt/lists/*

RUN git config --global user.name = "cargo-http-registry"
RUN git config --global user.email = "[email protected]"

COPY --from=builder /app/target/release/cargo-http-registry /usr/local/bin
ENTRYPOINT ["cargo-http-registry"]
39 changes: 33 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (C) 2020-2023 The cargo-http-registry Developers
// Copyright (C) 2020-2024 The cargo-http-registry Developers
// SPDX-License-Identifier: GPL-3.0-or-later

use std::collections::BTreeMap;
use std::env;
use std::ffi::OsString;
use std::fs::create_dir_all;
use std::fs::File;
use std::fs::OpenOptions;
Expand All @@ -19,13 +21,20 @@ use anyhow::Context as _;
use anyhow::Result;

use git2::Repository;
use git2::Signature;

use serde::Deserialize;
use serde::Serialize;
use serde_json::from_reader;
use serde_json::to_writer_pretty;


/// The default user to use when creating a commit.
const GIT_USER: &str = "cargo-http-registry";
/// The default email to use when creating a commit.
const GIT_EMAIL: &str = "[email protected]";


/// Parse the port from the given URL.
fn parse_port(url: &str) -> Result<u16> {
let addr = url
Expand Down Expand Up @@ -122,6 +131,10 @@ struct Config {
pub struct Index {
/// The root directory of the index.
root: PathBuf,
/// The user to author git commits as.
git_user: String,
/// The email address used when creating git commits.
git_email: String,
/// The git repository inside the index.
repository: Repository,
}
Expand All @@ -132,13 +145,29 @@ impl Index {
P: Into<PathBuf>,
{
fn inner(root: PathBuf, addr: &SocketAddr) -> Result<Index> {
let git_user = env::var_os("GIT_AUTHOR_NAME").unwrap_or_else(|| OsString::from(GIT_USER));
let git_user = git_user
.to_str()
.context("GIT_AUTHOR_NAME does not contain valid UTF-8")?
.to_string();
let git_email = env::var_os("GIT_AUTHOR_EMAIL").unwrap_or_else(|| OsString::from(GIT_EMAIL));
let git_email = git_email
.to_str()
.context("GIT_AUTHOR_EMAIL does not contain valid UTF-8")?
.to_string();

create_dir_all(&root)
.with_context(|| format!("failed to create directory {}", root.display()))?;

let repository = Repository::init(&root)
.with_context(|| format!("failed to initialize git repository {}", root.display()))?;

let mut index = Index { root, repository };
let mut index = Index {
root,
git_user,
git_email,
repository,
};
index.ensure_has_commit()?;
index.ensure_config(addr)?;
index.ensure_index_symlink()?;
Expand Down Expand Up @@ -198,10 +227,8 @@ impl Index {
.is_empty()
.context("unable to check git repository empty status")?;

let signature = self
.repository
.signature()
.context("failed to retrieve git signature object")?;
let signature = Signature::now(&self.git_user, &self.git_email)
.context("failed to create git signature object")?;

if empty {
self
Expand Down

0 comments on commit a4ce5f3

Please sign in to comment.