Fix stash problem when svn dcommit at dirty working space
[TortoiseGit.git] / src / TGitCache / ShellUpdater.cpp
blob7d409c8e38b136e3a0adacd39360d4c210860b39
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "StdAfx.h"
21 #include "shlobj.h"
22 #include "GitStatusCache.h"
24 CShellUpdater::CShellUpdater(void)
26 m_hWakeEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
27 m_hTerminationEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
28 m_hThread = INVALID_HANDLE_VALUE;
29 m_bRunning = FALSE;
30 m_bItemsAddedSinceLastUpdate = false;
33 CShellUpdater::~CShellUpdater(void)
35 Stop();
38 void CShellUpdater::Stop()
40 InterlockedExchange(&m_bRunning, FALSE);
41 if (m_hTerminationEvent != INVALID_HANDLE_VALUE)
43 SetEvent(m_hTerminationEvent);
44 if(WaitForSingleObject(m_hThread, 200) != WAIT_OBJECT_0)
46 ATLTRACE("Error terminating shell updater thread\n");
48 CloseHandle(m_hThread);
49 m_hThread = INVALID_HANDLE_VALUE;
50 CloseHandle(m_hTerminationEvent);
51 m_hTerminationEvent = INVALID_HANDLE_VALUE;
52 CloseHandle(m_hWakeEvent);
53 m_hWakeEvent = INVALID_HANDLE_VALUE;
57 void CShellUpdater::Initialise()
59 // Don't call Initialize more than once
60 ATLASSERT(m_hThread == INVALID_HANDLE_VALUE);
62 // Just start the worker thread.
63 // It will wait for event being signaled.
64 // If m_hWakeEvent is already signaled the worker thread
65 // will behave properly (with normal priority at worst).
67 InterlockedExchange(&m_bRunning, TRUE);
68 unsigned int threadId;
69 m_hThread = (HANDLE)_beginthreadex(NULL,0,ThreadEntry,this,0,&threadId);
70 SetThreadPriority(m_hThread, THREAD_PRIORITY_LOWEST);
73 void CShellUpdater::AddPathForUpdate(const CTGitPath& path)
76 AutoLocker lock(m_critSec);
77 m_pathsToUpdate.push_back(path);
79 // set this flag while we are synced
80 // with the worker thread
81 m_bItemsAddedSinceLastUpdate = true;
84 SetEvent(m_hWakeEvent);
88 unsigned int CShellUpdater::ThreadEntry(void* pContext)
90 ((CShellUpdater*)pContext)->WorkerThread();
91 return 0;
94 void CShellUpdater::WorkerThread()
96 HANDLE hWaitHandles[2];
97 hWaitHandles[0] = m_hTerminationEvent;
98 hWaitHandles[1] = m_hWakeEvent;
100 for(;;)
102 DWORD waitResult = WaitForMultipleObjects(sizeof(hWaitHandles)/sizeof(hWaitHandles[0]), hWaitHandles, FALSE, INFINITE);
104 // exit event/working loop if the first event (m_hTerminationEvent)
105 // has been signaled or if one of the events has been abandoned
106 // (i.e. ~CShellUpdater() is being executed)
107 if(waitResult == WAIT_OBJECT_0 || waitResult == WAIT_ABANDONED_0 || waitResult == WAIT_ABANDONED_0+1)
109 // Termination event
110 break;
112 // wait some time before we notify the shell
113 Sleep(50);
114 for(;;)
116 CTGitPath workingPath;
117 if (!m_bRunning)
118 return;
119 Sleep(0);
121 AutoLocker lock(m_critSec);
122 if(m_pathsToUpdate.empty())
124 // Nothing left to do
125 break;
128 if(m_bItemsAddedSinceLastUpdate)
130 m_pathsToUpdate.erase(std::unique(m_pathsToUpdate.begin(), m_pathsToUpdate.end(), &CTGitPath::PredLeftEquivalentToRight), m_pathsToUpdate.end());
131 m_bItemsAddedSinceLastUpdate = false;
134 workingPath = m_pathsToUpdate.front();
135 m_pathsToUpdate.pop_front();
137 if (workingPath.IsEmpty())
138 continue;
139 ATLTRACE(_T("Update notifications for: %s\n"), workingPath.GetWinPath());
140 if (workingPath.IsDirectory())
142 // check if the path is monitored by the watcher. If it isn't, then we have to invalidate the cache
143 // for that path and add it to the watcher.
144 if (!CGitStatusCache::Instance().IsPathWatched(workingPath))
146 if (workingPath.HasAdminDir())
147 CGitStatusCache::Instance().AddPathToWatch(workingPath);
149 // first send a notification about a sub folder change, so explorer doesn't discard
150 // the folder notification. Since we only know for sure that the subversion admin
151 // dir is present, we send a notification for that folder.
152 CString admindir = workingPath.GetWinPathString() + _T("\\") + g_GitAdminDir.GetAdminDirName();
153 SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, (LPCTSTR)admindir, NULL);
154 SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, workingPath.GetWinPath(), NULL);
155 // Sending an UPDATEDIR notification somehow overwrites/deletes the UPDATEITEM message. And without
156 // that message, the folder overlays in the current view don't get updated without hitting F5.
157 // Drawback is, without UPDATEDIR, the left tree view isn't always updated...
159 //SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH | SHCNF_FLUSHNOWAIT, workingPath.GetWinPath(), NULL);
161 else
162 SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH | SHCNF_FLUSHNOWAIT, workingPath.GetWinPath(), NULL);
165 _endthread();