Commits

Bob Ippolito committed bf3875e

shorten paths further by analyzing cwd

  • Participants
  • Parent commits ebb0e8c
  • Branches codepath

Comments (0)

Files changed (1)

src/rebar_codepath.erl

 %% @doc Wrapper for code module path calls for debugging purposes.
 -module(rebar_codepath).
 -export([add_path/1, add_patha/1, add_pathsa/1, get_path/0, set_path/1]).
--export([pretty_path/0, pretty_path/2]).
+-export([pretty_path/0, pretty_path/3]).
 -include("rebar.hrl").
 
 add_path(Path) ->
         true ->
             ok;
         false ->
-            Orig = pretty_path(OrigPath),
-            New = pretty_path(NewPath),
+            Root = code:root_dir(),
+            {ok, Cwd} = file:get_cwd(),
+            Orig = pretty_path(Root, Cwd, OrigPath),
+            New = pretty_path(Root, Cwd, NewPath),
             OrigS = ordsets:from_list(Orig),
             NewS = ordsets:from_list(New),
             Added = ordsets:subtract(NewS, OrigS),
             Deleted = ordsets:subtract(OrigS, NewS),
-            ?DEBUG("Code path: ~p (added: ~p, deleted: ~p)\n",
-                   [New, Added, Deleted])
+            ?DEBUG("Code path: ~p\n"
+                   "       Added: ~p\n"
+                   "       Deleted: ~p\n"
+                   "       Cwd = ~p\n",
+                   [New, Added, Deleted, Cwd])
+
     end,
     Result.
 
     pretty_path(code:get_path()).
 
 pretty_path(Paths) ->
-    pretty_path(code:root_dir(), Paths).
+    {ok, Cwd} = file:get_cwd(),
+    pretty_path(code:root_dir(), Cwd, Paths).
 
 path_prefix(Path1, Path1) ->
     true;
        (lists:prefix(Path1 ++ "/", Path2) orelse
         lists:prefix(Path1 ++ "\\", Path2))).
 
-pretty_path(_Root, []) ->
+cwd_normalize(Cwd, Path) ->
+    cwd_normalize(Cwd, Path, 0).
+
+cwd_normalize(Cwd, Path, N) when Cwd =:= "/" orelse Cwd =:= "." orelse N >= 3 ->
+    Path;
+cwd_normalize(Cwd, Path, N) ->
+    case path_prefix(Cwd, Path) of
+        true when N =:= 0 ->
+            "." ++ lists:nthtail(length(Cwd), Path);
+        true ->
+            filename:join(lists:duplicate(N, "..")) ++
+                lists:nthtail(length(Cwd), Path);
+        false ->
+            cwd_normalize(filename:dirname(Cwd), Path, 1 + N)
+    end.
+
+pretty_path(_Root, _Cwd, []) ->
     [];
-pretty_path(Root, [Path | Rest]) ->
+pretty_path(Root, Cwd, [Path | Rest]) ->
     case path_prefix(Root, Path) of
         true ->
             F = fun (P) -> path_prefix(Root, P) end,
-            [otp | pretty_path(Root, lists:dropwhile(F, Rest))];
+            [otp | pretty_path(Root, Cwd, lists:dropwhile(F, Rest))];
         false ->
-            [Path | pretty_path(Root, Rest)]
+            [cwd_normalize(Cwd, Path) | pretty_path(Root, Cwd, Rest)]
     end.