Index: Source/cmMakefile.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmMakefile.cxx,v
retrieving revision 1.386
diff -u -r1.386 cmMakefile.cxx
--- Source/cmMakefile.cxx	9 May 2007 12:25:45 -0000	1.386
+++ Source/cmMakefile.cxx	16 May 2007 22:06:36 -0000
@@ -1424,30 +1424,6 @@
 }
 
 #if defined(CMAKE_BUILD_WITH_CMAKE)
-cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
-{
-  // First see if the group exists.  If so, replace its regular expression.
-  for(std::vector<cmSourceGroup>::iterator sg = this->SourceGroups.begin();
-      sg != this->SourceGroups.end(); ++sg)
-    {
-    std::string sgName = sg->GetName();
-    if(sgName == name)
-      {
-      return &(*sg);
-      } 
-    else
-      {
-      cmSourceGroup *target = sg->lookupChild(name);
-
-      if(target)
-        {
-        return target;
-        }
-      }
-    }
-  return 0;
-}
-
 void cmMakefile::AddSourceGroup(const char* name, 
                                 const char* regex, 
                                 const char *parent)
Index: Source/cmMakefile.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmMakefile.h,v
retrieving revision 1.200
diff -u -r1.200 cmMakefile.h
--- Source/cmMakefile.h	13 Mar 2007 19:18:27 -0000	1.200
+++ Source/cmMakefile.h	16 May 2007 22:10:08 -0000
@@ -528,11 +528,9 @@
    */
   const std::vector<cmSourceGroup>& GetSourceGroups() const
     { return this->SourceGroups; }
+  std::vector<cmSourceGroup>& GetSourceGroups()
+    { return this->SourceGroups; }
 
-  /**
-   * Get the source group
-   */
-  cmSourceGroup* GetSourceGroup(const char* name); 
 #endif
 
   /**
Index: Source/cmSourceGroup.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSourceGroup.cxx,v
retrieving revision 1.18
diff -u -r1.18 cmSourceGroup.cxx
--- Source/cmSourceGroup.cxx	15 Mar 2006 16:02:07 -0000	1.18
+++ Source/cmSourceGroup.cxx	16 May 2007 22:07:33 -0000
@@ -105,14 +105,6 @@
       {
       return &(*iter); // if it so return it 
       }
-    // if the descendend isn't the one where looking for ask it's traverse
-    cmSourceGroup *result = iter->lookupChild(name);
-                
-    // if one of it's descendeds is the one we're looking for return it 
-    if(result)
-      {
-      return result;
-      }
     }
 
   // if no child with this name was found return NULL
Index: Source/cmSourceGroupCommand.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSourceGroupCommand.cxx,v
retrieving revision 1.16
diff -u -r1.16 cmSourceGroupCommand.cxx
--- Source/cmSourceGroupCommand.cxx	15 Mar 2006 16:02:07 -0000	1.16
+++ Source/cmSourceGroupCommand.cxx	16 May 2007 22:03:20 -0000
@@ -67,16 +67,53 @@
  
   const char *parent = NULL;
   cmSourceGroup* sg = NULL;
-  for(unsigned int i=0;i<folders.size();++i)
-    {
-    sg = this->Makefile->GetSourceGroup(folders[i].c_str());
-    if(!sg)
+  cmSourceGroup* parent_group = NULL;
+
+  // find top level source group in makefile
+  for(std::vector<cmSourceGroup>::iterator sgit = this->Makefile->GetSourceGroups().begin();
+      sgit != this->Makefile->GetSourceGroups().end();
+      ++sgit)
+  {
+      std::string sgName = sgit->GetName();
+      if(sgName == folders[0])
+        parent_group = &(*sgit);
+  }
+
+  if (parent_group == 0) {
+      this->Makefile->AddSourceGroup(folders[0].c_str(), 0, 0);
+      // find top level source group in makefile
+      for(std::vector<cmSourceGroup>::iterator sgit = this->Makefile->GetSourceGroups().begin();
+          sgit != this->Makefile->GetSourceGroups().end();
+          ++sgit)
       {
-      this->Makefile->AddSourceGroup(folders[i].c_str(), 0, parent);
+          std::string sgName = sgit->GetName();
+          if(sgName == folders[0])
+            parent_group = &(*sgit);
       }
-    sg = this->Makefile->GetSourceGroup(folders[i].c_str());
-    parent = folders[i].c_str();
-    }
+  }
+
+  unsigned folder_idx = 1;
+
+  while (parent_group != 0 && folder_idx < folders.size()) {
+      sg = parent_group->lookupChild(folders[folder_idx].c_str());
+
+      if (sg == 0) {
+          for (;folder_idx < folders.size(); ++folder_idx) {
+              parent_group->AddChild(cmSourceGroup(folders[folder_idx].c_str(), 0));
+
+              parent_group = parent_group->lookupChild(folders[folder_idx].c_str());
+
+              //assert(parent_group != 0);
+          }
+      }
+      else {
+          parent_group = sg;
+          ++folder_idx;
+      }
+  }
+
+  sg = parent_group;
+
   if(!sg)
     {
     this->SetError("Could not create or find source group");
