From b70425a3af79f387295acfa5246d6d0d8ba4e49c Mon Sep 17 00:00:00 2001
From: "Alexander V. Lukyanov" <lavv17f@gmail.com>
Date: Sat, 13 Oct 2018 00:10:27 +0300
Subject: [PATCH 06/23] fixed sorting of FileSet when some files begin with a
 tilde (fixes #476)

reason: FileSet::Merge no longer sorts the files.
---
 src/FileSet.h    | 1 +
 src/NetAccess.cc | 9 +++++----
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/FileSet.h b/src/FileSet.h
index 88c9563..e686e1a 100644
--- a/src/FileSet.h
+++ b/src/FileSet.h
@@ -204,6 +204,7 @@ public:
    void	 rewind() { ind=0; }
    FileInfo *curr();
    FileInfo *next();
+   FileInfo *borrow_curr() { return Borrow(ind--); }
 
    void	 LocalRemove(const char *dir);
    void	 LocalUtime(const char *dir,bool only_dirs=false,bool flat=false);
diff --git a/src/NetAccess.cc b/src/NetAccess.cc
index 62dd8ab..98a8329 100644
--- a/src/NetAccess.cc
+++ b/src/NetAccess.cc
@@ -541,19 +541,20 @@ do_again:
 got_fileset:
       if(set)
       {
-	 bool need_resort=false;
 	 set->rewind();
 	 for(file=set->curr(); file!=0; file=set->next())
 	 {
 	    // tilde is special.
 	    if(file->name[0]=='~')
 	    {
+	       // can't just update the name - it will break sorting
+	       file=set->borrow_curr();
 	       file->name.set_substr(0,0,"./");
-	       need_resort=true;
+	       if(!result)
+		  result=new FileSet();
+	       result->Add(file);
 	    }
 	 }
-	 if(need_resort && !result)
-	    result=new FileSet; // Merge will sort the names
 	 if(result)
 	 {
 	    result->Merge(set);
-- 
1.7.12.4

