context BUGFIX searching for the latest revision

when the latest revision is already in the context and searching via
import callback and searchapths provides an older revisions, the newest
revision from the context must be used. On the other hand, when some
revision is present in the context (and callback/searchpaths were not
used to get it), the callback/searchpaths should be tried to get a newer
revision.
diff --git a/src/tree_schema.c b/src/tree_schema.c
index c034581..240fa6d 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -641,10 +641,6 @@
     /* make sure that the newest revision is at position 0 */
     lysp_sort_revisions(submod->revs);
 
-    if (custom_check) {
-        LY_CHECK_GOTO(custom_check((*context).ctx, NULL, submod, check_data), error);
-    }
-
     /* decide the latest revision */
     latest_sp = ly_ctx_get_submodule((*context).ctx, submod->belongsto, submod->name, NULL);
     if (latest_sp) {
@@ -652,18 +648,28 @@
             if (!latest_sp->revs) {
                 /* latest has no revision, so mod is anyway newer */
                 submod->latest_revision = latest_sp->latest_revision;
-                latest_sp->latest_revision = 0;
+                /* the latest_sp is zeroed later when the new module is being inserted into the context */
+            } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
+                submod->latest_revision = latest_sp->latest_revision;
+                /* the latest_sp is zeroed later when the new module is being inserted into the context */
             } else {
-                if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
-                    submod->latest_revision = latest_sp->latest_revision;
-                    latest_sp->latest_revision = 0;
-                }
+                latest_sp = NULL;
             }
+        } else {
+            latest_sp = NULL;
         }
     } else {
         submod->latest_revision = 1;
     }
 
+    if (custom_check) {
+        LY_CHECK_GOTO(custom_check((*context).ctx, NULL, submod, check_data), error);
+    }
+
+    if (latest_sp) {
+        latest_sp->latest_revision = 0;
+    }
+
     /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
     memcpy(&main_ctx->tpdfs_nodes, &(*context).tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
     memcpy(&main_ctx->grps_nodes, &(*context).grps_nodes, sizeof main_ctx->grps_nodes);
@@ -724,6 +730,27 @@
         mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
     }
 
+    /* decide the latest revision */
+    latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
+    if (latest) {
+        if (mod->revision) {
+            if (!latest->revision) {
+                /* latest has no revision, so mod is anyway newer */
+                mod->latest_revision = latest->latest_revision;
+                /* the latest is zeroed later when the new module is being inserted into the context */
+            } else if (strcmp(mod->revision, latest->revision) > 0) {
+                mod->latest_revision = latest->latest_revision;
+                /* the latest is zeroed later when the new module is being inserted into the context */
+            } else {
+                latest = NULL;
+            }
+        } else {
+            latest = NULL;
+        }
+    } else {
+        mod->latest_revision = 1;
+    }
+
     if (custom_check) {
         LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
     }
@@ -781,21 +808,8 @@
         LY_CHECK_GOTO(lys_extension_precompile(NULL, ctx, mod, mod->parsed->extensions, &mod->off_extensions), error);
     }
 
-    /* decide the latest revision */
-    latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
     if (latest) {
-        if (mod->revision) {
-            if (!latest->revision) {
-                /* latest has no revision, so mod is anyway newer */
-                mod->latest_revision = latest->latest_revision;
-                latest->latest_revision = 0;
-            } else if (strcmp(mod->revision, latest->revision) > 0) {
-                mod->latest_revision = latest->latest_revision;
-                latest->latest_revision = 0;
-            }
-        }
-    } else {
-        mod->latest_revision = 1;
+        latest->latest_revision = 0;
     }
 
     /* add into context */