From 8266410092b22465eb81638f595cf841859bc914 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 14 Aug 2022 12:32:39 -0500 Subject: [PATCH] feat: New branch and sha1 repository settings - `repository`: Branch name in the local clone to check out - `sha1`: An explicit revision to reset to. Fixes #27 --- CHANGELOG.md | 1 + schemas/settings-schema.json | 56 +++++++++---------- .../Config/Settings/SettingsValues.cs | 2 + src/TrashLib/Repo/RepoUpdater.cs | 8 ++- src/VersionControl/GitRepository.cs | 6 +- src/VersionControl/IGitRepository.cs | 2 +- wiki/Settings-Reference.md | 19 ++++++- 7 files changed, 56 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b65d087..759b0c0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 YAML format, ready to copy & paste. - Docker: New `edge` tag for experimental and potentially unstable builds on `master`. Includes both the latest Docker and Recyclarr changes to allow users to try them out before an official release. +- Settings: New `branch` and `sha1` Repository settings. (#27) ### Changed diff --git a/schemas/settings-schema.json b/schemas/settings-schema.json index a017866f..75ccadfb 100644 --- a/schemas/settings-schema.json +++ b/schemas/settings-schema.json @@ -1,24 +1,12 @@ { - "$schema": "http://json-schema.org/draft-06/schema#", - "$ref": "#/definitions/Settings", - "definitions": { - "Settings": { - "type": "object", - "additionalProperties": false, - "title": "Recyclarr Settings", - "description": "Optional settings to control the behavior of Recyclarr", - "properties": { - "repository": { - "$ref": "#/definitions/Repository" - }, - "enable_ssl_certificate_validation": { - "type": "boolean", - "title": "Allow SSL certificate validation for Sonarr & Radarr", - "description": "If set to `false`, SSL certificates are not validated. This is useful if you are connecting to a Sonarr or Radarr instance using `https` and it is set up with self-signed certificates. Note that disabling this setting is a **security risk** and should be avoided unless you are absolutely sure what you are doing." - } - } - }, - "Repository": { + "$schema": "https://json-schema.org/draft-07/schema", + "$id": "https://raw.githubusercontent.com/recyclarr/recyclarr/master/schemas/settings-schema.json", + "type": "object", + "title": "Recyclarr Settings", + "description": "Optional settings to control the behavior of Recyclarr", + "additionalProperties": false, + "properties": { + "repository": { "type": "object", "additionalProperties": false, "title": "Settings for the git repo", @@ -26,20 +14,26 @@ "properties": { "clone_url": { "type": "string", - "title": "Clone URL to the trash guides git repository", - "description": "A URL compatible with `git clone` that is used to clone the Trash Guides repository. This setting exists for enthusiasts that may want to instead have Recyclarr pull data from a fork instead of the official repository.", "format": "uri", - "qt-uri-protocols": [ - "http", - "https", - "ssh", - "git" - ], - "qt-uri-extensions": [ - ".git" - ] + "title": "Clone URL to the trash guides git repository", + "description": "A URL compatible with `git clone` that is used to clone the Trash Guides repository. This setting exists for enthusiasts that may want to instead have Recyclarr pull data from a fork instead of the official repository." + }, + "branch": { + "type": "string", + "title": "The name of a branch to check out in the repository" + }, + "sha1": { + "type": "string", + "title": "A SHA1 (commit hash) in Git to use", + "description": "If specified, it overrides the `branch` setting. This SHA1 is passed to `git reset --hard` to force your local clone to this specific revision in the repository. If not specified, only the `branch` controls what revision is used in the repo." } } + }, + "enable_ssl_certificate_validation": { + "type": "boolean", + "title": "Allow SSL certificate validation for Sonarr & Radarr", + "description": "If set to `false`, SSL certificates are not validated. This is useful if you are connecting to a Sonarr or Radarr instance using `https` and it is set up with self-signed certificates. Note that disabling this setting is a **security risk** and should be avoided unless you are absolutely sure what you are doing.", + "default": false } } } diff --git a/src/TrashLib/Config/Settings/SettingsValues.cs b/src/TrashLib/Config/Settings/SettingsValues.cs index 66dab0c0..5409e44e 100644 --- a/src/TrashLib/Config/Settings/SettingsValues.cs +++ b/src/TrashLib/Config/Settings/SettingsValues.cs @@ -3,6 +3,8 @@ namespace TrashLib.Config.Settings; public record TrashRepository { public string CloneUrl { get; init; } = "https://github.com/TRaSH-/Guides.git"; + public string Branch { get; init; } = "master"; + public string? Sha1 { get; init; } } public record SettingsValues diff --git a/src/TrashLib/Repo/RepoUpdater.cs b/src/TrashLib/Repo/RepoUpdater.cs index b0bb692a..dd0d9b70 100644 --- a/src/TrashLib/Repo/RepoUpdater.cs +++ b/src/TrashLib/Repo/RepoUpdater.cs @@ -54,16 +54,20 @@ public class RepoUpdater : IRepoUpdater { var repoSettings = _settingsProvider.Settings.Repository; var cloneUrl = repoSettings.CloneUrl; - const string branch = "master"; + var branch = repoSettings.Branch; _log.Debug("Using Branch & Clone URL: {Branch}, {Url}", branch, cloneUrl); + if (repoSettings.Sha1 is not null) + { + _log.Warning("Using explicit SHA1 for local repository: {Sha1}", repoSettings.Sha1); + } try { using var repo = _repositoryFactory.CreateAndCloneIfNeeded(cloneUrl, RepoPath.FullName, branch); repo.ForceCheckout(branch); repo.Fetch(); - repo.ResetHard($"origin/{branch}"); + repo.ResetHard(repoSettings.Sha1 ?? $"origin/{branch}"); } catch (LibGit2SharpException e) { diff --git a/src/VersionControl/GitRepository.cs b/src/VersionControl/GitRepository.cs index 93a507a9..fd6f4c71 100644 --- a/src/VersionControl/GitRepository.cs +++ b/src/VersionControl/GitRepository.cs @@ -1,3 +1,4 @@ +using Common.Extensions; using LibGit2Sharp; namespace VersionControl; @@ -35,9 +36,10 @@ public sealed class GitRepository : IGitRepository Commands.Fetch(_repo.Value, origin.Name, origin.FetchRefSpecs.Select(s => s.Specification), null, ""); } - public void ResetHard(string toBranch) + public void ResetHard(string toBranchOrSha1) { - var commit = _repo.Value.Branches[toBranch].Tip; + var branch = _repo.Value.Branches.FirstOrDefault(b => b.FriendlyName.ContainsIgnoreCase(toBranchOrSha1)); + var commit = branch is not null ? branch.Tip : _repo.Value.Lookup(toBranchOrSha1); _repo.Value.Reset(ResetMode.Hard, commit); } diff --git a/src/VersionControl/IGitRepository.cs b/src/VersionControl/IGitRepository.cs index 2cf3e285..9c9c7e3a 100644 --- a/src/VersionControl/IGitRepository.cs +++ b/src/VersionControl/IGitRepository.cs @@ -4,6 +4,6 @@ public interface IGitRepository : IDisposable { void ForceCheckout(string branch); void Fetch(string remote = "origin"); - void ResetHard(string toBranch); + void ResetHard(string toBranchOrSha1); void SetRemote(string name, string newUrl); } diff --git a/wiki/Settings-Reference.md b/wiki/Settings-Reference.md index b711a693..e384d4af 100644 --- a/wiki/Settings-Reference.md +++ b/wiki/Settings-Reference.md @@ -1,7 +1,8 @@ This page contains the YAML reference for Recyclarr settings. Settings support was introduced in version `1.7.0`. -The `settings.yml` file is located in the following locations depending on your platform: +The `settings.yml` file is located in the following locations (by default) depending on your +platform: | Platform | Location | | -------- | ------------------------------------------------------ | @@ -36,6 +37,7 @@ page. Table of Contents +- [Global Settings](#global-settings) - [Repository Settings](#repository-settings) ## Global Settings @@ -52,14 +54,27 @@ enable_ssl_certificate_validation: true ## Repository Settings +**NOTE**: Never edit files in the locally cloned repo managed by Recyclarr. It will always destroy +local changes in that repository. + ```yml repository: clone_url: https://github.com/TRaSH-/Guides.git + branch: master + sha1: e27659e3f90d9b60c1f0b0f204b2530bb2299b41 ``` -- `clone_url`
+- `clone_url` *(Default: `https://github.com/TRaSH-/Guides.git`)*
A URL compatible with `git clone` that is used to clone the [Trash Guides repository][official_repo]. This setting exists for enthusiasts that may want to instead have Recyclarr pull data from a fork instead of the official repository. +- `branch` *(Default: `master`)*
+ The name of a branch to check out in the repository. + +- `sha1` *(Default: empty)*
+ A SHA1 (commit hash) in Git to use. If specified, it overrides the `branch` setting. This SHA1 is + passed to `git reset --hard` to force your local clone to this specific revision in the + repository. If not specified, only the `branch` controls what revision is used in the repo. + [official_repo]: https://github.com/TRaSH-/Guides