From f4ffd7a8fb412b896a05ba32ddca9573e9ddc7bf Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Tue, 14 Apr 2020 15:16:36 -0700 Subject: git-storage: fix up old broken local caches to use the right branch If you had one of the unfortunate local git caches with a local HEAD just pointing to 'master', this will make note of that and then fix it up to use the proper branch name in the cache repository. [Dirk Hohndel: demoted from error to fprintf as most users won't care] Signed-off-by: Linus Torvalds Signed-off-by: Dirk Hohndel --- core/git-access.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'core/git-access.c') diff --git a/core/git-access.c b/core/git-access.c index b015524cb..1a18746d8 100644 --- a/core/git-access.c +++ b/core/git-access.c @@ -652,6 +652,7 @@ static git_repository *update_local_repo(const char *localdir, const char *remot { int error; git_repository *repo = NULL; + git_reference *head; if (verbose) fprintf(stderr, "git storage: update local repo\n"); @@ -664,6 +665,21 @@ static git_repository *update_local_repo(const char *localdir, const char *remot report_error("Unable to open git cache repository at %s: %s", localdir, giterr_last()->message); return NULL; } + + /* Check the HEAD being the right branch */ + if (!git_repository_head(&head, repo)) { + const char *name; + if (!git_branch_name(&name, head)) { + if (strcmp(name, branch)) { + char *branchref = format_string("refs/heads/%s", branch); + fprintf(stderr, "Setting cache branch from '%s' to '%s'", name, branch); + git_repository_set_head(repo, branchref); + free(branchref); + } + } + git_reference_free(head); + } + if (!git_local_only) sync_with_remote(repo, remote, branch, rt); -- cgit v1.2.3-70-g09d2