Skip to content

Commit

Permalink
[Fix bbatsov#952] Exclude out-of-project VCS submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
timcharper committed Feb 3, 2016
1 parent 1559ea1 commit e9dca5a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
31 changes: 23 additions & 8 deletions projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -912,15 +912,9 @@ Files are returned as relative paths to the project root."
(t ""))))

(defun projectile-get-all-sub-projects (project)
"Get all sub-projects for a given projects.
"Get all sub-projects for a given project.
PROJECT is base directory to start search recursively."
(let* ((default-directory project)
;; search for sub-projects under current project `project'
(submodules (mapcar
(lambda (s)
(file-name-as-directory (expand-file-name s default-directory)))
(projectile-files-via-ext-command (projectile-get-sub-projects-command)))))

(let ((submodules (projectile-get-immediate-sub-projects project)))
(cond
((null submodules)
nil)
Expand All @@ -930,6 +924,27 @@ PROJECT is base directory to start search recursively."
(mapcar (lambda (s)
(projectile-get-all-sub-projects s)) submodules)))))))

(defun projectile-get-immediate-sub-projects (project)
"Get immediate sub-projects for a given project without recursing.
PROJECT is the VCS root from which to start searching, and should end
with an appropriate path delimiter, such as '/' or a '\\'."
(let* ((default-directory project)
;; search for sub-projects under current project `project'
(submodules (mapcar
(lambda (s)
(file-name-as-directory (expand-file-name s default-directory)))
(projectile-files-via-ext-command (projectile-get-sub-projects-command))))
(project-child-folder-regex
(concat "\\`"
(regexp-quote project))))

;; If project root is inside of an VCS folder, but not actually an
;; VCS root itself, external paths will leak in. Let's remove them
(-filter (lambda (path)
(string-match-p project-child-folder-regex
path))
submodules)))

(defun projectile-get-sub-projects-files ()
"Get files from sub-projects recursively."
(-flatten
Expand Down
25 changes: 25 additions & 0 deletions test/projectile-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,31 @@
(projectile-find-matching-file
"spec/models/food/sea_spec.rb"))))))))

(ert-deftest projectile-test-exclude-out-of-project-submodules ()
(projectile-test-with-files
(;; VSC root is here
"project/"
"project/.git/"
"project/.gitmodules"
;; Current project root is here:
"project/web-ui/"
"project/web-ui/.projectile"
;; VCS git submodule will return the following submodules,
;; relative to current project root, 'project/web-ui/':
"project/web-ui/vendor/client-submodule/"
"project/server/vendor/server-submodule/")
(let ((project (file-truename (expand-file-name "project/web-ui"))))
(noflet ((projectile-files-via-ext-command
(arg) (when (string= default-directory project)
'("vendor/client-submodule"
"../server/vendor/server-submodule")))
(projectile-project-root
() (file-truename (expand-file-name web-ui-path))))

;; assert that it only returns the submodule 'project/web-ui/vendor/client-submodule/'
(should (equal (list (expand-file-name "vendor/client-submodule/" project))
(projectile-get-all-sub-projects project)))))))

;; Local Variables:
;; indent-tabs-mode: nil
;; End:

0 comments on commit e9dca5a

Please sign in to comment.