anchor.diff:  %% Internal functions
   %% ===================================================================
   
  +deps_per_profile(Profile, {Apps, State}) ->
  +    Locks = rebar_state:get(State, {locks, Profile}, []),
  +    ProfileDeps = rebar_state:get(State, {deps, Profile}, []),
  +    {ok, NewApps, NewState} = handle_deps(Profile, State, ProfileDeps, Locks),
  +    {NewApps++Apps, NewState}.
  +
  +find_cycles(Apps) ->
  +    case rebar_digraph:compile_order(Apps) of
  +        {error, {cycles, Cycles}} -> {cycles, Cycles};
  +        {error, Error} -> {error, Error};
  +        {ok, _} -> no_cycle
  +    end.
  +
  +compile_order(Source, ProjectApps) ->
  +    case rebar_digraph:compile_order(Source) of
  +        {ok, Sort} ->
  +            %% Valid apps are compiled and good
  +            {ok, lists:dropwhile(fun rebar_app_info:valid/1, Sort -- ProjectApps)};
  +        {error, Error} ->
  +            {error, Error}
  +    end.
  +
  +update_pkg_deps(Profile, Packages, PkgDeps, Graph, Upgrade, Seen, State) ->
  +    case PkgDeps of
  +        [] -> %% No pkg deps
  +            {[], State};
  +        PkgDeps ->
  +            %% Find pkg deps needed
  +            S = case rebar_digraph:cull_deps(Graph, PkgDeps) of
  +                {ok, [], _} ->
  +                    throw({rebar_digraph, no_solution});
  +                {ok, Solution, []} ->
  +                    Solution;
  +                {ok, Solution, Discarded} ->
  +                    [warn_skip_pkg(Pkg) || Pkg <- Discarded],
  +                    Solution
  +            end,
  +            update_pkg_deps(Profile, S, Packages, Upgrade, Seen, State)
  +    end.
  +
   update_pkg_deps(Profile, Pkgs, Packages, Upgrade, Seen, State) ->
       %% Create app_info record for each pkg dep
       DepsDir = rebar_dir:deps_dir(State),
       {Solved, _, State1}
           = lists:foldl(fun(Pkg, {Acc, SeenAcc, StateAcc}) ->
  -                              AppInfo = package_to_app(DepsDir
  -                                                      ,Packages
  -                                                      ,Pkg),
  -                              {SeenAcc1, StateAcc1} = maybe_lock(Profile, AppInfo, SeenAcc, StateAcc, 0),
  -                              case maybe_fetch(AppInfo, Upgrade, SeenAcc1, State) of
  -                                  true ->
  -                                      {[AppInfo | Acc], SeenAcc1, StateAcc1};
  -                                  false ->
  -                                      {Acc, SeenAcc1, StateAcc1}
  -                              end
  +                        handle_pkg_dep(Profile, Pkg, Packages, Upgrade, DepsDir, Acc, SeenAcc, StateAcc)
                         end, {[], Seen, State}, Pkgs),
       {Solved, State1}.
   
  +handle_pkg_dep(Profile, Pkg, Packages, Upgrade, DepsDir, Fetched, Seen, State) ->
  +    AppInfo = package_to_app(DepsDir, Packages, Pkg),
  +    {NewSeen, NewState} = maybe_lock(Profile, AppInfo, Seen, State, 0),
  +    case maybe_fetch(AppInfo, Upgrade, NewSeen, NewState) of
  +        true ->
  +            {[AppInfo | Fetched], NewSeen, NewState};
  +        false ->
  +            {Fetched, NewSeen, NewState}
  +    end.
  +
  +
   maybe_lock(Profile, AppInfo, Seen, State, Level) ->
  -    Name = rebar_app_info:name(AppInfo),
       case Profile of
           default ->
  +            Name = rebar_app_info:name(AppInfo),
               case sets:is_element(Name, Seen) of
                   false ->
  -                    AppName = rebar_app_info:name(AppInfo),
                       Locks = rebar_state:lock(State),
  -                    case lists:any(fun(App) -> rebar_app_info:name(App) =:= AppName end, Locks) of
  +                    case lists:any(fun(App) -> rebar_app_info:name(App) =:= Name end, Locks) of
                           true ->
                               {sets:add_element(Name, Seen), State};
                           false ->
  
positive.code:                   sets:set(binary()), rebar_state:t()) -> boolean().
  maybe_fetch(AppInfo, Upgrade, Seen, State) ->
      AppDir = ec_cnv:to_list(rebar_app_info:dir(AppInfo)),
      Apps = rebar_app_discover:find_apps(["_checkouts"], all),
      case rebar_app_utils:find(rebar_app_info:name(AppInfo), Apps) of
          {ok, _} ->
              %% Don't fetch dep if it exists in the _checkouts dir
              false;
          error ->
              case not app_exists(AppDir) of
                  true ->
                      fetch_app(AppInfo, AppDir, State);
  
negative.code: 
  -spec update_src_deps(atom(), non_neg_integer(), list(), list(), list(), rebar_state:t(), boolean(), sets:set(binary()), list()) -> {rebar_state:t(), list(), list(), sets:set(binary())}.
  update_src_deps(Profile, Level, SrcDeps, PkgDeps, SrcApps, State, Upgrade, Seen, Locks) ->
      case lists:foldl(
              fun(AppInfo, {SrcDepsAcc, PkgDepsAcc, SrcAppsAcc, StateAcc, SeenAcc, LocksAcc}) ->
                      update_src_dep(AppInfo, Profile, Level,
                                     SrcDepsAcc, PkgDepsAcc, SrcAppsAcc, StateAcc,
                                     Upgrade, SeenAcc, Locks, LocksAcc)
              end,
              {[], PkgDeps, SrcApps, State, Seen, Locks},
              rebar_utils:sort_deps(SrcDeps)) of
          {[], NewPkgDeps, NewSrcApps, State1, Seen1, _NewLocks} ->
              {State1, NewSrcApps, NewPkgDeps, Seen1};
          {NewSrcDeps, NewPkgDeps, NewSrcApps, State1, Seen1, NewLocks} ->
              update_src_deps(Profile, Level+1, NewSrcDeps, NewPkgDeps, NewSrcApps, State1, Upgrade, Seen1, NewLocks)
      end.
  
  update_src_dep(AppInfo, Profile, Level, SrcDeps, PkgDeps, SrcApps, State, Upgrade, Seen, BaseLocks, Locks) ->
      %% If not seen, add to list of locks to write out
      Name = rebar_app_info:name(AppInfo),
      case sets:is_element(Name, Seen) of
          true ->
              update_seen_src_dep(AppInfo, Level,
                                  SrcDeps, PkgDeps, SrcApps,
                                  State, Upgrade, Seen, BaseLocks, Locks);
          false ->
              update_unseen_src_dep(AppInfo, Profile, Level,
                                    SrcDeps, PkgDeps, SrcApps,
                                    State, Upgrade, Seen, Locks)
  
      end.
  
  update_seen_src_dep(AppInfo, Level, SrcDeps, PkgDeps, SrcApps, State, Upgrade, Seen, BaseLocks, Locks) ->
      Name = rebar_app_info:name(AppInfo),
      %% If seen from lock file don't print warning about skipping
      case lists:keymember(Name, 1, BaseLocks) of
          false ->
              warn_skip_deps(AppInfo);
          true ->
              ok
      end,
      %% scan for app children here if upgrading
      case Upgrade of
          false ->
              {SrcDeps, PkgDeps, SrcApps, State, Seen, Locks};
          true ->
              {NewSrcDeps, NewPkgDeps, NewSrcApps, NewState, NewLocks}
              = handle_dep(AppInfo, SrcDeps, PkgDeps, SrcApps,
                           Level, State, Locks),
              {NewSrcDeps, NewPkgDeps, NewSrcApps, NewState, Seen, NewLocks}
      end.
  
  update_unseen_src_dep(AppInfo, Profile, Level, SrcDeps, PkgDeps, SrcApps, State, Upgrade, Seen, Locks) ->
      {NewSeen, State1} = maybe_lock(Profile, AppInfo, Seen, State, Level),
      {NewSrcDeps, NewPkgDeps, NewSrcApps, State2, NewLocks}
      = case Upgrade of
          true ->
              handle_upgrade(AppInfo, SrcDeps, PkgDeps, SrcApps,
                             Level, State1, Locks);
          _ ->
              %% why do we use the old state there?
              maybe_fetch(AppInfo, false, Seen, State),
              handle_dep(AppInfo, SrcDeps, PkgDeps, SrcApps,
                         Level, State1, Locks)
      end,
      {NewSrcDeps, NewPkgDeps, NewSrcApps, State2, NewSeen, NewLocks}.
  
  handle_upgrade(AppInfo, SrcDeps, PkgDeps, SrcApps, Level, State, Locks) ->
      Name = rebar_app_info:name(AppInfo),
      case lists:keyfind(Name, 1, Locks) of
          false ->
              case maybe_fetch(AppInfo, true, sets:new(), State) of
                  true ->
                      handle_dep(AppInfo, SrcDeps, PkgDeps, SrcApps,
                                 Level, State, Locks);
  
                  false ->
                      {[AppInfo|SrcDeps], PkgDeps, SrcApps, State, Locks}
  