Correct some submodule edge cases

So that this role may be used for repos which both are not core
modules (and therefore do not have an entry in .gitmodules) and
also do not have matching branches, check to see if the repo
being processed is in .gitmodules.  If it is not, we should
perform the mv operation.

Also, handle the case where a repo is in .gitmodules but is not
a remote tracking branch.  In that case, we should perform the
submodule init operation to checkout the submodule ref, and we
should also add that case to the dependency chain warning check.

Finally, correct the destination directory in the submodule init
case (a typo had us using the wrong variable).

Change-Id: I1828dff9cfdbfd79da375141ea89e3b956f23190
diff --git a/roles/prepare-gerrit-repos/tasks/repo.yaml b/roles/prepare-gerrit-repos/tasks/repo.yaml
index bce358c..a4fbdb2 100644
--- a/roles/prepare-gerrit-repos/tasks/repo.yaml
+++ b/roles/prepare-gerrit-repos/tasks/repo.yaml
@@ -12,10 +12,26 @@
   set_fact:
     repo_has_dependent_change: "{{ zuul['items'] | selectattr('project.canonical_name', 'eq', project.canonical_name) | list | length | bool }}"
 
+- name: Check if repo is in submodules
+  ignore_errors: true
+  set_fact:
+    project_in_gitmodules: "{{ lookup ('ini', 'path section=submodule \"' + project_dest + '\" file=' + zuul.executor.work_root + '/' + zuul.projects[gerrit_project_name].src_dir + '/.gitmodules', errors='ignore') }}"
+- name: Coerce submodule check to boolean
+  set_fact:
+    project_in_gitmodules: "{{ project_in_gitmodules | default('') | length > 0 | bool }}"
+
+- name: Check if repo is a tracking branch
+  ignore_errors: true
+  set_fact:
+    tracking_branch: "{{ lookup ('ini', 'branch section=submodule \"' + project_dest + '\" file=' + zuul.executor.work_root + '/' + zuul.projects[gerrit_project_name].src_dir + '/.gitmodules', errors='ignore') }}"
+- name: Coerce tracking branch to boolean
+  set_fact:
+    tracking_branch: "{{ ((tracking_branch | default('')) == '.') | bool }}"
+
 - name: Check for unsatisfiable source repo condition
   when:
     - "project.canonical_name != zuul.project.canonical_name"
-    - "not project_branch_exists"
+    - "not project_branch_exists or not tracking_branch"
     - "repo_has_dependent_change"
   fail:
     msg: >-
@@ -37,11 +53,12 @@
 # If there is no matching branch we need to check out the actual sha
 # defined in the parent repo.
 - name: Update submodule
-  when: "not project_branch_exists"
-  command: "git submodule update --init {{ project.name }}"
+  when: "project_in_gitmodules and (not project_branch_exists or not tracking_branch)"
+  command: "git submodule update --init {{ project_dest }}"
   args:
     chdir: "{{ gerrit_root }}"
 
+# Else:
 - name: Move repo into place
-  when: "project_branch_exists"
+  when: "not (project_in_gitmodules and (not project_branch_exists or not tracking_branch))"
   command: "mv -T -f {{ ansible_user_dir }}/{{ project.src_dir }} {{ gerrit_root }}/{{ project_dest }}"