From b0c9bc920fabfd814946d555738ac7ba042154d7 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 15 Mar 2017 13:38:54 +0100 Subject: [PATCH] submodule: resolve URLs relative to main worktree It is possible to specify submodule URLs relative to the repository location. E.g. having a submodule with URL "../submodule" will look for the submodule at "repo/../submodule". With the introduction of worktrees, though, we cannot simply resolve the URL relative to the repository location itself. If the repository for which a URL is to be resolved is a working tree, we have to resolve the URL relative to the parent's repository path. Otherwise, the URL would change depending on where the working tree is located. Fix this by special-casing when we have a working tree while getting the URL base. --- src/submodule.c | 24 +++++++++---- .../submodules/testrepo/.gitted/config | Bin 294 -> 173 bytes tests/worktree/submodule.c | 33 ++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/submodule.c b/src/submodule.c index 095bbb090..ff65591d2 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -22,6 +22,7 @@ #include "iterator.h" #include "path.h" #include "index.h" +#include "worktree.h" #define GIT_MODULES_FILE ".gitmodules" @@ -2030,17 +2031,28 @@ static int lookup_default_remote(git_remote **remote, git_repository *repo) static int get_url_base(git_buf *url, git_repository *repo) { int error; + git_worktree *wt = NULL; git_remote *remote = NULL; - if (!(error = lookup_default_remote(&remote, repo))) { + if ((error = lookup_default_remote(&remote, repo)) == 0) { error = git_buf_sets(url, git_remote_url(remote)); - git_remote_free(remote); - } - else if (error == GIT_ENOTFOUND) { - /* if repository does not have a default remote, use workdir instead */ + goto out; + } else if (error != GIT_ENOTFOUND) + goto out; + else giterr_clear(); + + /* if repository does not have a default remote, use workdir instead */ + if (git_repository_is_worktree(repo)) { + if ((error = git_worktree_open_from_repository(&wt, repo)) < 0) + goto out; + error = git_buf_sets(url, wt->parent_path); + } else error = git_buf_sets(url, git_repository_workdir(repo)); - } + +out: + git_remote_free(remote); + git_worktree_free(wt); return error; } diff --git a/tests/resources/submodules/testrepo/.gitted/config b/tests/resources/submodules/testrepo/.gitted/config index d6dcad12b3e80886731a6358d7c1fc5408050a39..8e557119184b82b0622f9b335fc104b1089e82f3 100644 GIT binary patch delta 9 QcmZ3+w3cx~!Nixg022ZP4gdfE delta 137 zcmZ3>xQuB+L48qbZhlFsf>M4_W_o6xQY;r|T53shhJvkvc2R0tv3^EsVoI^TmKB%- zQCzGKQLL}U#aUXE15~OXTAW%`tY4I*UtE-|pOcxCo>^j~Uy@o}0u)Rw&Mz%WPAvxW Wi&6{n^?