Drop only once used member
[TortoiseGit.git] / src / TortoiseProc / SyncDlg.cpp
blobfcd35f0d74d905e32f6125cde969d18b27672f41
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
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 // SyncDlg.cpp : implementation file
23 #include "stdafx.h"
24 #include "TortoiseProc.h"
25 #include "SyncDlg.h"
26 #include "AppUtils.h"
27 #include "progressdlg.h"
28 #include "MessageBox.h"
29 #include "ImportPatchDlg.h"
30 #include "RebaseDlg.h"
31 #include "hooks.h"
32 #include "SmartHandle.h"
33 #include "ProgressCommands/FetchProgressCommand.h"
35 // CSyncDlg dialog
37 IMPLEMENT_DYNAMIC(CSyncDlg, CResizableStandAloneDialog)
39 CSyncDlg::CSyncDlg(CWnd* pParent /*=NULL*/)
40 : CResizableStandAloneDialog(CSyncDlg::IDD, pParent)
42 m_CurrentCmd = 0;
43 m_pTooltip=&this->m_tooltips;
44 m_bInited=false;
45 m_CmdOutCurrentPos=0;
46 m_bAutoLoadPuttyKey = CAppUtils::IsSSHPutty();
47 m_bForce=false;
48 m_Gitverion = 0;
49 m_bBlock = false;
50 m_BufStart = 0;
51 m_pThread = NULL;
52 m_bAbort = false;
53 m_bDone = false;
54 m_bWantToExit = false;
55 m_GitCmdStatus = -1;
56 m_startTick = GetTickCount();
57 m_seq = 0;
60 CSyncDlg::~CSyncDlg()
64 void CSyncDlg::DoDataExchange(CDataExchange* pDX)
66 CDialog::DoDataExchange(pDX);
67 DDX_Check(pDX, IDC_CHECK_PUTTY_KEY, m_bAutoLoadPuttyKey);
68 DDX_Check(pDX, IDC_CHECK_FORCE,m_bForce);
69 DDX_Control(pDX, IDC_COMBOBOXEX_URL, m_ctrlURL);
70 DDX_Control(pDX, IDC_BUTTON_TABCTRL, m_ctrlDumyButton);
71 DDX_Control(pDX, IDC_BUTTON_PULL, m_ctrlPull);
72 DDX_Control(pDX, IDC_BUTTON_PUSH, m_ctrlPush);
73 DDX_Control(pDX, IDC_STATIC_STATUS, m_ctrlStatus);
74 DDX_Control(pDX, IDC_PROGRESS_SYNC, m_ctrlProgress);
75 DDX_Control(pDX, IDC_ANIMATE_SYNC, m_ctrlAnimate);
76 DDX_Control(pDX, IDC_BUTTON_SUBMODULE,m_ctrlSubmodule);
77 DDX_Control(pDX, IDC_BUTTON_STASH, m_ctrlStash);
78 DDX_Control(pDX, IDC_PROG_LABEL, m_ctrlProgLabel);
79 BRANCH_COMBOX_DDX;
83 BEGIN_MESSAGE_MAP(CSyncDlg, CResizableStandAloneDialog)
84 ON_BN_CLICKED(IDC_BUTTON_PULL, &CSyncDlg::OnBnClickedButtonPull)
85 ON_BN_CLICKED(IDC_BUTTON_PUSH, &CSyncDlg::OnBnClickedButtonPush)
86 ON_BN_CLICKED(IDC_BUTTON_APPLY, &CSyncDlg::OnBnClickedButtonApply)
87 ON_BN_CLICKED(IDC_BUTTON_EMAIL, &CSyncDlg::OnBnClickedButtonEmail)
88 ON_BN_CLICKED(IDC_BUTTON_MANAGE, &CSyncDlg::OnBnClickedButtonManage)
89 BRANCH_COMBOX_EVENT
90 ON_CBN_EDITCHANGE(IDC_COMBOBOXEX_URL, &CSyncDlg::OnCbnEditchangeComboboxex)
91 ON_CBN_EDITCHANGE(IDC_COMBOBOXEX_REMOTE_BRANCH, &CSyncDlg::OnCbnEditchangeComboboxex)
92 ON_MESSAGE(MSG_PROGRESSDLG_UPDATE_UI, OnProgressUpdateUI)
93 ON_MESSAGE(WM_PROG_CMD_FINISH, OnProgCmdFinish)
94 ON_NOTIFY(LVN_COLUMNCLICK, IDC_IN_LOGLIST, OnLvnInLogListColumnClick)
95 ON_BN_CLICKED(IDC_BUTTON_COMMIT, &CSyncDlg::OnBnClickedButtonCommit)
96 ON_BN_CLICKED(IDC_BUTTON_SUBMODULE, &CSyncDlg::OnBnClickedButtonSubmodule)
97 ON_BN_CLICKED(IDC_BUTTON_STASH, &CSyncDlg::OnBnClickedButtonStash)
98 ON_WM_TIMER()
99 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
100 ON_BN_CLICKED(IDC_CHECK_FORCE, &CSyncDlg::OnBnClickedCheckForce)
101 ON_BN_CLICKED(IDC_LOG, &CSyncDlg::OnBnClickedLog)
102 ON_WM_DESTROY()
103 END_MESSAGE_MAP()
106 void CSyncDlg::EnableControlButton(bool bEnabled)
108 GetDlgItem(IDC_BUTTON_PULL)->EnableWindow(bEnabled);
109 GetDlgItem(IDC_BUTTON_PUSH)->EnableWindow(bEnabled);
110 GetDlgItem(IDC_BUTTON_APPLY)->EnableWindow(bEnabled);
111 GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(bEnabled);
112 GetDlgItem(IDOK)->EnableWindow(bEnabled);
113 GetDlgItem(IDC_BUTTON_SUBMODULE)->EnableWindow(bEnabled);
114 GetDlgItem(IDC_BUTTON_STASH)->EnableWindow(bEnabled);
116 // CSyncDlg message handlers
118 void CSyncDlg::OnBnClickedButtonPull()
120 int CurrentEntry;
121 CurrentEntry = (int)this->m_ctrlPull.GetCurrentEntry();
122 this->m_regPullButton = CurrentEntry;
124 this->m_bAbort=false;
125 this->m_GitCmdList.clear();
126 m_ctrlCmdOut.SetWindowTextW(_T(""));
127 m_LogText = "";
129 this->UpdateData();
130 UpdateCombox();
132 if (g_Git.GetHash(m_oldHash, _T("HEAD")))
134 MessageBox(g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
135 return;
138 m_refList.Clear();
139 m_newHashMap.clear();
140 m_oldHashMap.clear();
142 if( CurrentEntry == 0)
144 CGitHash localBranchHash;
145 if (g_Git.GetHash(localBranchHash, m_strLocalBranch))
147 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + m_strLocalBranch + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
148 return;
150 if (localBranchHash != m_oldHash)
152 CMessageBox::Show(NULL, IDS_PROC_SYNC_PULLWRONGBRANCH, IDS_APPNAME, MB_OK | MB_ICONERROR);
153 return;
157 if(this->m_strURL.IsEmpty())
159 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
160 return;
163 if (m_bAutoLoadPuttyKey && CurrentEntry != 3) // CurrentEntry (Remote Update) handles this on its own)
165 CAppUtils::LaunchPAgent(NULL,&this->m_strURL);
168 this->SwitchToRun();
169 if (g_Git.GetMapHashToFriendName(m_oldHashMap))
170 MessageBox(g_Git.GetGitLastErr(_T("Could not get all refs.")), _T("TortoiseGit"), MB_ICONERROR);
172 CString force;
173 if(this->m_bForce)
174 force = _T(" --force ");
176 CString cmd;
178 ShowTab(IDC_CMD_LOG);
180 this->m_ctrlTabCtrl.ShowTab(IDC_REFLIST-1,true);
181 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,false);
182 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
183 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CONFLICT-1,false);
185 ///Pull
186 if(CurrentEntry == 0) //Pull
188 CString remotebranch;
189 remotebranch = m_strRemoteBranch;
191 if(!IsURL())
193 CString pullRemote, pullBranch;
194 g_Git.GetRemoteTrackedBranch(m_strLocalBranch, pullRemote, pullBranch);
195 if(pullBranch == remotebranch && pullRemote == this->m_strURL)
196 remotebranch.Empty();
199 if(m_Gitverion >= 0x01070203) //above 1.7.0.2
200 force += _T("--progress ");
202 cmd.Format(_T("git.exe pull -v --no-rebase %s \"%s\" %s"),
203 force,
204 m_strURL,
205 remotebranch);
207 m_CurrentCmd = GIT_COMMAND_PULL;
208 m_GitCmdList.push_back(cmd);
210 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
211 if (m_pThread==NULL)
213 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
215 else
217 m_pThread->m_bAutoDelete = TRUE;
218 m_pThread->ResumeThread();
223 ///Fetch
224 if(CurrentEntry == 1 || CurrentEntry ==2 ) //Fetch
226 CString remotebranch;
227 if(this->IsURL() || m_strRemoteBranch.IsEmpty())
229 remotebranch=this->m_strRemoteBranch;
232 else
234 remotebranch.Format(_T("remotes/%s/%s"),
235 m_strURL,m_strRemoteBranch);
236 CGitHash remoteBranchHash;
237 g_Git.GetHash(remoteBranchHash, remotebranch);
238 if (remoteBranchHash.IsEmpty())
239 remotebranch=m_strRemoteBranch;
240 else
241 remotebranch=m_strRemoteBranch+_T(":")+remotebranch;
244 if(CurrentEntry == 1)
245 m_CurrentCmd = GIT_COMMAND_FETCH;
246 else
247 m_CurrentCmd = GIT_COMMAND_FETCHANDREBASE;
249 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
251 CString refspec;
252 // current libgit2 only supports well formated refspec
253 refspec.Format(_T("refs/heads/%s:refs/remotes/%s/%s"), m_strRemoteBranch, m_strURL, m_strRemoteBranch);
255 FetchProgressCommand fetchProgressCommand;
256 fetchProgressCommand.SetUrl(m_strURL);
257 fetchProgressCommand.SetRefSpec(refspec);
258 m_GitProgressList.SetCommand(&fetchProgressCommand);
259 m_GitProgressList.Init();
260 ShowTab(IDC_CMD_GIT_PROG);
262 else
264 if(m_Gitverion >= 0x01070203) //above 1.7.0.2
265 force += _T("--progress ");
267 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"),
268 force,
269 m_strURL,
270 remotebranch);
272 m_GitCmdList.push_back(cmd);
274 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
275 if (m_pThread==NULL)
277 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
279 else
281 m_pThread->m_bAutoDelete = TRUE;
282 m_pThread->ResumeThread();
287 ///Remote Update
288 if(CurrentEntry == 3)
290 if (m_bAutoLoadPuttyKey)
292 STRING_VECTOR list;
293 if (!g_Git.GetRemoteList(list))
295 for (size_t i = 0; i < list.size(); ++i)
296 CAppUtils::LaunchPAgent(NULL, &list[i]);
300 m_CurrentCmd = GIT_COMMAND_REMOTE;
301 cmd=_T("git.exe remote update");
302 m_GitCmdList.push_back(cmd);
304 InterlockedExchange(&m_bBlock, TRUE);
306 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
307 if (m_pThread==NULL)
309 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
310 InterlockedExchange(&m_bBlock, FALSE);
312 else
314 m_pThread->m_bAutoDelete = TRUE;
315 m_pThread->ResumeThread();
319 ///Cleanup stale remote banches
320 if(CurrentEntry == 4)
322 m_CurrentCmd = GIT_COMMAND_REMOTE;
323 cmd.Format(_T("git.exe remote prune \"%s\""), m_strURL);
324 m_GitCmdList.push_back(cmd);
326 InterlockedExchange(&m_bBlock, TRUE);
328 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
329 if (m_pThread==NULL)
331 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
332 InterlockedExchange(&m_bBlock, FALSE);
334 else
336 m_pThread->m_bAutoDelete = TRUE;
337 m_pThread->ResumeThread();
342 void CSyncDlg::PullComplete()
344 EnableControlButton(true);
345 SwitchToInput();
346 this->FetchOutList(true);
348 CGitHash newhash;
349 if (g_Git.GetHash(newhash, _T("HEAD")))
350 MessageBox(g_Git.GetGitLastErr(_T("Could not get HEAD hash after pulling.")), _T("TortoiseGit"), MB_ICONERROR);
352 if( this ->m_GitCmdStatus )
354 CTGitPathList list;
355 if(g_Git.ListConflictFile(list))
357 this->m_ctrlCmdOut.SetSel(-1,-1);
358 this->m_ctrlCmdOut.ReplaceSel(_T("Get conflict files fail\n"));
360 this->ShowTab(IDC_CMD_LOG);
361 return;
364 if (!list.IsEmpty())
366 this->m_ConflictFileList.Clear();
367 CTGitPathList list;
368 CTGitPath path;
369 list.AddPath(path);
371 this->m_ConflictFileList.GetStatus(&list,true);
372 this->m_ConflictFileList.Show(CTGitPath::LOGACTIONS_UNMERGED,
373 CTGitPath::LOGACTIONS_UNMERGED);
375 this->ShowTab(IDC_IN_CONFLICT);
377 else
378 this->ShowTab(IDC_CMD_LOG);
381 else
383 if(newhash == this->m_oldHash)
385 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
386 this->m_InLogList.ShowText(CString(MAKEINTRESOURCE(IDS_UPTODATE)));
387 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
388 this->ShowTab(IDC_REFLIST);
390 else
392 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,true);
393 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
395 this->AddDiffFileList(&m_InChangeFileList, &m_arInChangeList, newhash.ToString(), m_oldHash.ToString());
397 CString range;
398 range.Format(_T("%s..%s"), m_oldHash.ToString(), newhash.ToString());
399 m_InLogList.FillGitLog(nullptr, &range, CGit::LOG_INFO_STAT| CGit::LOG_INFO_FILESTATE | CGit::LOG_INFO_SHOW_MERGEDFILE);
400 this->ShowTab(IDC_IN_LOGLIST);
405 void CSyncDlg::FetchComplete()
407 EnableControlButton(true);
408 SwitchToInput();
409 this->FetchOutList(true);
411 if (g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
412 ShowTab(IDC_CMD_GIT_PROG);
413 else
414 ShowTab(IDC_REFLIST);
416 if (m_GitCmdStatus)
417 return;
419 if (m_CurrentCmd != GIT_COMMAND_FETCHANDREBASE)
420 return;
422 CString remote;
423 CString remotebranch;
424 CString upstream;
425 m_ctrlURL.GetWindowText(remote);
426 if (!remote.IsEmpty())
428 STRING_VECTOR remotes;
429 g_Git.GetRemoteList(remotes);
430 if (std::find(remotes.begin(), remotes.end(), remote) == remotes.end())
431 remote.Empty();
433 m_ctrlRemoteBranch.GetWindowText(remotebranch);
434 if (!remote.IsEmpty() && !remotebranch.IsEmpty())
435 upstream = _T("remotes/") + remote + _T("/") + remotebranch;
437 CAppUtils::RebaseAfterFetch(upstream);
440 void CSyncDlg::StashComplete()
442 EnableControlButton(true);
443 INT_PTR entry = m_ctrlStash.GetCurrentEntry();
444 if (entry != 1 && entry != 2)
445 return;
447 SwitchToInput();
448 if (m_GitCmdStatus)
450 CTGitPathList list;
451 if (g_Git.ListConflictFile(list))
453 m_ctrlCmdOut.SetSel(-1, -1);
454 m_ctrlCmdOut.ReplaceSel(_T("Get conflict files fail\n"));
456 ShowTab(IDC_CMD_LOG);
457 return;
460 if (!list.IsEmpty())
462 m_ConflictFileList.Clear();
463 CTGitPathList list;
464 CTGitPath path;
465 list.AddPath(path);
467 m_ConflictFileList.GetStatus(&list,true);
468 m_ConflictFileList.Show(CTGitPath::LOGACTIONS_UNMERGED, CTGitPath::LOGACTIONS_UNMERGED);
470 ShowTab(IDC_IN_CONFLICT);
472 else
473 ShowTab(IDC_CMD_LOG);
477 void CSyncDlg::OnBnClickedButtonPush()
479 this->UpdateData();
480 UpdateCombox();
481 m_ctrlCmdOut.SetWindowTextW(_T(""));
482 m_LogText = "";
484 if(this->m_strURL.IsEmpty())
486 CMessageBox::Show(NULL, IDS_PROC_GITCONFIG_URLEMPTY, IDS_APPNAME, MB_OK | MB_ICONERROR);
487 return;
490 this->m_regPushButton=(DWORD)this->m_ctrlPush.GetCurrentEntry();
491 this->SwitchToRun();
492 this->m_bAbort=false;
493 this->m_GitCmdList.clear();
495 ShowTab(IDC_CMD_LOG);
497 CString cmd;
498 CString arg;
500 CString error;
501 DWORD exitcode = 0xFFFFFFFF;
502 if (CHooks::Instance().PrePush(g_Git.m_CurrentDir, exitcode, error))
504 if (exitcode)
506 CString temp;
507 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
508 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
509 return ;
513 CString refName = g_Git.FixBranchName(m_strLocalBranch);
514 switch (m_ctrlPush.GetCurrentEntry())
516 case 1:
517 arg += _T(" --tags ");
518 break;
519 case 2:
520 refName = _T("refs/notes/commits"); //default ref for notes
521 break;
524 if(this->m_bForce)
525 arg += _T(" --force ");
527 if(m_Gitverion >= 0x01070203) //above 1.7.0.2
528 arg += _T("--progress ");
530 cmd.Format(_T("git.exe push -v %s \"%s\" %s"),
531 arg,
532 m_strURL,
533 refName);
535 if (!m_strRemoteBranch.IsEmpty() && m_ctrlPush.GetCurrentEntry() != 2)
537 cmd += _T(":") + m_strRemoteBranch;
540 m_GitCmdList.push_back(cmd);
542 m_CurrentCmd = GIT_COMMAND_PUSH;
544 if(this->m_bAutoLoadPuttyKey)
546 CAppUtils::LaunchPAgent(NULL,&this->m_strURL);
549 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
550 if (m_pThread==NULL)
552 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
554 else
556 m_pThread->m_bAutoDelete = TRUE;
557 m_pThread->ResumeThread();
561 void CSyncDlg::OnBnClickedButtonApply()
563 CGitHash oldhash;
564 if (g_Git.GetHash(oldhash, _T("HEAD")))
566 MessageBox(g_Git.GetGitLastErr(_T("Could not get HEAD hash.")), _T("TortoiseGit"), MB_ICONERROR);
567 return;
570 CImportPatchDlg dlg;
571 CString cmd,output;
573 if(dlg.DoModal() == IDOK)
575 int err=0;
576 for (int i = 0; i < dlg.m_PathList.GetCount(); ++i)
578 cmd.Format(_T("git.exe am \"%s\""),dlg.m_PathList[i].GetGitPathString());
580 if (g_Git.Run(cmd, &output, CP_UTF8))
582 CMessageBox::Show(NULL,output,_T("TortoiseGit"),MB_OK);
584 err=1;
585 break;
587 this->m_ctrlCmdOut.SetSel(-1,-1);
588 this->m_ctrlCmdOut.ReplaceSel(cmd+_T("\n"));
589 this->m_ctrlCmdOut.SetSel(-1,-1);
590 this->m_ctrlCmdOut.ReplaceSel(output);
593 CGitHash newhash;
594 if (g_Git.GetHash(newhash, _T("HEAD")))
596 MessageBox(g_Git.GetGitLastErr(_T("Could not get HEAD hash after applying patches.")), _T("TortoiseGit"), MB_ICONERROR);
597 return;
600 this->m_InLogList.Clear();
601 this->m_InChangeFileList.Clear();
603 if(newhash == oldhash)
605 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
606 this->m_InLogList.ShowText(_T("No commits get from patch"));
607 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
610 else
612 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,true);
613 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
615 CString range;
616 range.Format(_T("%s..%s"), m_oldHash.ToString(), newhash.ToString());
617 this->AddDiffFileList(&m_InChangeFileList, &m_arInChangeList, newhash.ToString(), oldhash.ToString());
618 m_InLogList.FillGitLog(nullptr, &range, CGit::LOG_INFO_STAT| CGit::LOG_INFO_FILESTATE | CGit::LOG_INFO_SHOW_MERGEDFILE);
620 this->FetchOutList(true);
623 this->m_ctrlTabCtrl.ShowTab(IDC_CMD_LOG-1,true);
625 if(err)
627 this->ShowTab(IDC_CMD_LOG);
629 else
631 this->ShowTab(IDC_IN_LOGLIST);
636 void CSyncDlg::OnBnClickedButtonEmail()
638 CString cmd, out, err;
640 this->m_strLocalBranch = this->m_ctrlLocalBranch.GetString();
641 this->m_ctrlRemoteBranch.GetWindowText(this->m_strRemoteBranch);
642 this->m_ctrlURL.GetWindowText(this->m_strURL);
643 m_strURL=m_strURL.Trim();
644 m_strRemoteBranch=m_strRemoteBranch.Trim();
646 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
647 g_Git.m_CurrentDir,
648 m_strURL+_T('/')+m_strRemoteBranch,g_Git.FixBranchName(m_strLocalBranch));
650 if (g_Git.Run(cmd, &out, &err, CP_UTF8))
652 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
653 return ;
656 CAppUtils::SendPatchMail(cmd,out);
659 void CSyncDlg::ShowProgressCtrl(bool bShow)
661 int b=bShow?SW_NORMAL:SW_HIDE;
662 this->m_ctrlAnimate.ShowWindow(b);
663 this->m_ctrlProgress.ShowWindow(b);
664 this->m_ctrlProgLabel.ShowWindow(b);
665 this->m_ctrlAnimate.Open(IDR_DOWNLOAD);
666 if(b == SW_NORMAL)
667 this->m_ctrlAnimate.Play(0, UINT_MAX, UINT_MAX);
668 else
669 this->m_ctrlAnimate.Stop();
671 void CSyncDlg::ShowInputCtrl(bool bShow)
673 int b=bShow?SW_NORMAL:SW_HIDE;
674 this->m_ctrlURL.ShowWindow(b);
675 this->m_ctrlLocalBranch.ShowWindow(b);
676 this->m_ctrlRemoteBranch.ShowWindow(b);
677 this->GetDlgItem(IDC_BUTTON_LOCAL_BRANCH)->ShowWindow(b);
678 this->GetDlgItem(IDC_BUTTON_REMOTE_BRANCH)->ShowWindow(b);
679 this->GetDlgItem(IDC_STATIC_LOCAL_BRANCH)->ShowWindow(b);
680 this->GetDlgItem(IDC_STATIC_REMOTE_BRANCH)->ShowWindow(b);
681 this->GetDlgItem(IDC_BUTTON_MANAGE)->ShowWindow(b);
682 this->GetDlgItem(IDC_CHECK_PUTTY_KEY)->ShowWindow(b);
683 this->GetDlgItem(IDC_CHECK_FORCE)->ShowWindow(b);
684 this->GetDlgItem(IDC_STATIC_REMOTE_URL)->ShowWindow(b);
686 BOOL CSyncDlg::OnInitDialog()
688 CResizableStandAloneDialog::OnInitDialog();
689 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
691 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
692 // do this, Explorer would be unable to send that message to our window if we
693 // were running elevated. It's OK to make the call all the time, since if we're
694 // not elevated, this is a no-op.
695 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
696 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
697 CAutoLibrary hUser = AtlLoadSystemLibraryUsingFullPath(_T("user32.dll"));
698 if (hUser)
700 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
701 if (pfnChangeWindowMessageFilterEx)
703 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
706 m_pTaskbarList.Release();
707 if (FAILED(m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList)))
708 m_pTaskbarList = nullptr;
710 this->GetDlgItem(IDC_CHECK_PUTTY_KEY)->EnableWindow(CAppUtils::IsSSHPutty());
713 this->m_ctrlAnimate.ShowWindow(SW_NORMAL);
714 this->m_ctrlAnimate.Open(IDR_DOWNLOAD);
715 this->m_ctrlAnimate.Play(0,-1,-1);
718 // ------------------ Create Tabctrl -----------
719 CWnd *pwnd=this->GetDlgItem(IDC_BUTTON_TABCTRL);
720 CRect rectDummy;
721 pwnd->GetWindowRect(&rectDummy);
722 this->ScreenToClient(rectDummy);
724 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_SYNC_TAB))
726 TRACE0("Failed to create output tab window\n");
727 return FALSE; // fail to create
729 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
731 // -------------Create Command Log Ctrl ---------
732 DWORD dwStyle;
733 dwStyle= ES_MULTILINE | ES_READONLY | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL |WS_VSCROLL ;
735 if( !m_ctrlCmdOut.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_CMD_LOG))
737 TRACE0("Failed to create Log commits window\n");
738 return FALSE; // fail to create
741 // set the font to use in the log message view, configured in the settings dialog
742 CFont m_logFont;
743 CAppUtils::CreateFontForLogs(m_logFont);
744 //GetDlgItem(IDC_CMD_LOG)->SetFont(&m_logFont);
745 m_ctrlCmdOut.SetFont(&m_logFont);
746 m_ctrlTabCtrl.InsertTab(&m_ctrlCmdOut, CString(MAKEINTRESOURCE(IDS_LOG)), -1);
748 //m_ctrlCmdOut.ReplaceSel(_T("Hello"));
750 //---------- Create in coming list ctrl -----------
751 dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | LVS_OWNERDATA | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;;
753 if( !m_InLogList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_IN_LOGLIST))
755 TRACE0("Failed to create output commits window\n");
756 return FALSE; // fail to create
759 m_ctrlTabCtrl.InsertTab(&m_InLogList, CString(MAKEINTRESOURCE(IDS_PROC_SYNC_INCOMMITS)), -1);
761 m_InLogList.m_ColumnRegKey=_T("SyncIn");
762 m_InLogList.InsertGitColumn();
764 //----------- Create In Change file list -----------
765 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP |LVS_SINGLESEL |WS_CHILD | WS_VISIBLE;
767 if( !m_InChangeFileList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_IN_CHANGELIST))
769 TRACE0("Failed to create output change files window\n");
770 return FALSE; // fail to create
772 m_ctrlTabCtrl.InsertTab(&m_InChangeFileList, CString(MAKEINTRESOURCE(IDS_PROC_SYNC_INCHANGELIST)), -1);
774 m_InChangeFileList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("InSyncDlg"),
775 (CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_COMPARETWOREVISIONS) |
776 CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_GNUDIFF2REVISIONS)), false, true, GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL);
779 //---------- Create Conflict List Ctrl -----------------
780 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
782 if( !m_ConflictFileList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_IN_CONFLICT))
784 TRACE0("Failed to create output change files window\n");
785 return FALSE; // fail to create
787 m_ctrlTabCtrl.InsertTab(&m_ConflictFileList, CString(MAKEINTRESOURCE(IDS_PROC_SYNC_CONFLICTS)), -1);
789 m_ConflictFileList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("ConflictSyncDlg"),
790 (CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_COMPARETWOREVISIONS) |
791 CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_GNUDIFF2REVISIONS) |
792 GITSLC_POPCONFLICT|GITSLC_POPRESOLVE),false);
795 //---------- Create Commit Out List Ctrl---------------
797 dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | LVS_OWNERDATA | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;;
799 if( !m_OutLogList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_OUT_LOGLIST))
801 TRACE0("Failed to create output commits window\n");
802 return FALSE; // fail to create
806 m_ctrlTabCtrl.InsertTab(&m_OutLogList, CString(MAKEINTRESOURCE(IDS_PROC_SYNC_OUTCOMMITS)), -1);
808 m_OutLogList.m_ColumnRegKey = _T("SyncOut");
809 m_OutLogList.InsertGitColumn();
811 //------------- Create Change File List Control ----------------
813 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP |LVS_SINGLESEL |WS_CHILD | WS_VISIBLE;
815 if( !m_OutChangeFileList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_OUT_CHANGELIST))
817 TRACE0("Failed to create output change files window\n");
818 return FALSE; // fail to create
820 m_ctrlTabCtrl.InsertTab(&m_OutChangeFileList, CString(MAKEINTRESOURCE(IDS_PROC_SYNC_OUTCHANGELIST)), -1);
822 m_OutChangeFileList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD | GITSLC_COLDEL, _T("OutSyncDlg"),
823 (CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_COMPARETWOREVISIONS) |
824 CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_GNUDIFF2REVISIONS)), false, true, GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD | GITSLC_COLDEL);
826 if (!m_GitProgressList.Create(dwStyle | LVS_OWNERDATA, rectDummy, &m_ctrlTabCtrl, IDC_CMD_GIT_PROG))
828 TRACE0("Failed to create Git Progress List Window\n");
829 return FALSE; // fail to create
831 m_ctrlTabCtrl.InsertTab(&m_GitProgressList, CString(MAKEINTRESOURCE(IDS_LOG)), -1);
832 m_GitProgressList.m_pAnimate = &m_ctrlAnimate;
833 m_GitProgressList.m_pPostWnd = this;
834 m_GitProgressList.m_pProgressLabelCtrl = &m_ctrlProgLabel;
835 m_GitProgressList.m_pProgControl = &m_ctrlProgress;
836 m_GitProgressList.m_pTaskbarList = m_pTaskbarList;
838 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
839 DWORD exStyle = LVS_EX_HEADERDRAGDROP | LVS_EX_DOUBLEBUFFER | LVS_EX_INFOTIP | LVS_EX_FULLROWSELECT;
840 m_refList.Create(dwStyle, rectDummy, &m_ctrlTabCtrl, IDC_REFLIST);
841 m_refList.SetExtendedStyle(exStyle);
842 m_refList.Init();
843 m_ctrlTabCtrl.InsertTab(&m_refList, CString(MAKEINTRESOURCE(IDS_REFLIST)), -1);
845 this->m_tooltips.Create(this);
847 AddAnchor(IDC_SYNC_TAB,TOP_LEFT,BOTTOM_RIGHT);
849 AddAnchor(IDC_GROUP_INFO,TOP_LEFT,TOP_RIGHT);
850 AddAnchor(IDC_COMBOBOXEX_URL,TOP_LEFT,TOP_RIGHT);
851 AddAnchor(IDC_BUTTON_MANAGE,TOP_RIGHT);
852 AddAnchor(IDC_BUTTON_PULL,BOTTOM_LEFT);
853 AddAnchor(IDC_BUTTON_PUSH,BOTTOM_LEFT);
854 AddAnchor(IDC_BUTTON_SUBMODULE,BOTTOM_LEFT);
855 AddAnchor(IDC_BUTTON_STASH,BOTTOM_LEFT);
856 AddAnchor(IDC_BUTTON_APPLY,BOTTOM_RIGHT);
857 AddAnchor(IDC_BUTTON_EMAIL,BOTTOM_RIGHT);
858 AddAnchor(IDC_PROGRESS_SYNC,TOP_LEFT,TOP_RIGHT);
859 AddAnchor(IDOK,BOTTOM_RIGHT);
860 AddAnchor(IDHELP,BOTTOM_RIGHT);
861 AddAnchor(IDC_STATIC_STATUS, BOTTOM_LEFT, BOTTOM_RIGHT);
862 AddAnchor(IDC_ANIMATE_SYNC,TOP_LEFT);
863 AddAnchor(IDC_BUTTON_COMMIT,BOTTOM_LEFT);
864 AddAnchor(IDC_LOG, BOTTOM_LEFT);
866 // do not use BRANCH_COMBOX_ADD_ANCHOR here, we want to have different stylings
867 AddAnchor(IDC_COMBOBOXEX_LOCAL_BRANCH, TOP_LEFT,TOP_CENTER);
868 AddAnchor(IDC_COMBOBOXEX_REMOTE_BRANCH, TOP_CENTER, TOP_RIGHT);
869 AddAnchor(IDC_BUTTON_LOCAL_BRANCH, TOP_CENTER);
870 AddAnchor(IDC_BUTTON_REMOTE_BRANCH, TOP_RIGHT);
871 AddAnchor(IDC_STATIC_REMOTE_BRANCH, TOP_CENTER);
872 AddAnchor(IDC_PROG_LABEL, TOP_LEFT);
874 AdjustControlSize(IDC_CHECK_PUTTY_KEY);
875 AdjustControlSize(IDC_CHECK_FORCE);
877 CString WorkingDir=g_Git.m_CurrentDir;
878 WorkingDir.Replace(_T(':'),_T('_'));
879 m_RegKeyRemoteBranch = CString(_T("Software\\TortoiseGit\\History\\SyncBranch\\"))+WorkingDir;
882 this->AddOthersToAnchor();
884 this->m_ctrlPush.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_PUSH)));
885 this->m_ctrlPush.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_PUSHTAGS)));
886 this->m_ctrlPush.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_PUSHNOTES)));
888 this->m_ctrlPull.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_PULL)));
889 this->m_ctrlPull.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_FETCH)));
890 this->m_ctrlPull.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_FETCHREBASE)));
891 this->m_ctrlPull.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_REMOTEUPDATE)));
892 this->m_ctrlPull.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_CLEANUPSTALEBRANCHES)));
894 this->m_ctrlSubmodule.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_SUBKODULEUPDATE)));
895 this->m_ctrlSubmodule.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_SUBKODULEINIT)));
896 this->m_ctrlSubmodule.AddEntry(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_SUBKODULESYNC)));
898 this->m_ctrlStash.AddEntry(CString(MAKEINTRESOURCE(IDS_MENUSTASHSAVE)));
899 this->m_ctrlStash.AddEntry(CString(MAKEINTRESOURCE(IDS_MENUSTASHPOP)));
900 this->m_ctrlStash.AddEntry(CString(MAKEINTRESOURCE(IDS_MENUSTASHAPPLY)));
902 WorkingDir.Replace(_T(':'),_T('_'));
904 CString regkey ;
905 regkey.Format(_T("Software\\TortoiseGit\\TortoiseProc\\Sync\\%s"),WorkingDir);
907 this->m_regPullButton = CRegDWORD(regkey+_T("\\Pull"),0);
908 this->m_regPushButton = CRegDWORD(regkey+_T("\\Push"),0);
909 this->m_regSubmoduleButton = CRegDWORD(regkey+_T("\\Submodule"));
910 this->m_regAutoLoadPutty = CRegDWORD(regkey + _T("\\AutoLoadPutty"), CAppUtils::IsSSHPutty());
912 this->UpdateData();
913 this->m_bAutoLoadPuttyKey = m_regAutoLoadPutty;
914 if(!CAppUtils::IsSSHPutty())
915 m_bAutoLoadPuttyKey = false;
916 this->UpdateData(FALSE);
918 this->m_ctrlPull.SetCurrentEntry(this->m_regPullButton);
919 this->m_ctrlPush.SetCurrentEntry(this->m_regPushButton);
920 this->m_ctrlSubmodule.SetCurrentEntry(this->m_regSubmoduleButton);
922 CString sWindowTitle;
923 GetWindowText(sWindowTitle);
924 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
926 EnableSaveRestore(_T("SyncDlg"));
928 m_ctrlURL.SetCaseSensitive(TRUE);
929 m_ctrlURL.SetURLHistory(true);
930 this->m_ctrlURL.LoadHistory(CString(_T("Software\\TortoiseGit\\History\\SyncURL\\"))+WorkingDir, _T("url"));
932 STRING_VECTOR list;
934 if(!g_Git.GetRemoteList(list))
936 for (unsigned int i = 0; i < list.size(); ++i)
938 m_ctrlURL.AddString(list[i]);
941 m_ctrlURL.SetCurSel(0);
942 m_ctrlRemoteBranch.SetCurSel(0);
944 this->LoadBranchInfo();
946 this->m_bInited=true;
947 FetchOutList();
949 m_ctrlTabCtrl.ShowTab(IDC_CMD_LOG-1,false);
950 m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,false);
951 m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
952 m_ctrlTabCtrl.ShowTab(IDC_IN_CONFLICT-1,false);
953 m_ctrlTabCtrl.ShowTab(IDC_CMD_GIT_PROG-1, false);
954 m_ctrlTabCtrl.ShowTab(IDC_REFLIST-1, false);
956 m_ctrlRemoteBranch.m_bWantReturn = TRUE;
957 m_ctrlURL.m_bWantReturn = TRUE;
959 this->m_Gitverion = CAppUtils::GetMsysgitVersion();
961 if (m_seq > 0 && (DWORD)CRegDWORD(_T("Software\\TortoiseGit\\SyncDialogRandomPos")))
963 m_seq %= 5;
964 RECT rect;
965 GetWindowRect(&rect);
966 rect.top -= m_seq * 30;
967 rect.bottom -= m_seq * 30;
968 if (rect.top < 0)
970 rect.top += 150;
971 rect.bottom += 150;
973 MoveWindow(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
976 return TRUE; // return TRUE unless you set the focus to a control
977 // EXCEPTION: OCX Property Pages should return FALSE
980 void CSyncDlg::OnBnClickedButtonManage()
982 CAppUtils::LaunchRemoteSetting();
983 Refresh();
986 void CSyncDlg::Refresh()
988 theApp.DoWaitCursor(1);
990 int lastSelected = m_ctrlURL.GetCurSel();
991 CString url;
992 this->m_ctrlURL.GetWindowText(url);
994 this->m_ctrlURL.Reset();
995 CString workingDir = g_Git.m_CurrentDir;
996 workingDir.Replace(_T(':'), _T('_'));
997 this->m_ctrlURL.LoadHistory(_T("Software\\TortoiseGit\\History\\SyncURL\\") + workingDir, _T("url"));
999 STRING_VECTOR list;
1000 bool found = false;
1001 if (!g_Git.GetRemoteList(list))
1003 for (size_t i = 0; i < list.size(); ++i)
1005 m_ctrlURL.AddString(list[i]);
1006 if (list[i] == url)
1007 found = true;
1010 if (lastSelected >= 0 && !found)
1012 m_ctrlURL.SetCurSel(0);
1013 m_ctrlURL.GetWindowText(url);
1016 CString local;
1017 CString remote;
1018 this->m_ctrlLocalBranch.GetWindowText(local);
1019 this->m_ctrlRemoteBranch.GetWindowText(remote);
1021 this->LoadBranchInfo();
1023 this->m_ctrlLocalBranch.AddString(local);
1024 this->m_ctrlRemoteBranch.AddString(remote);
1025 this->m_ctrlURL.AddString(url);
1027 m_OutLogList.ShowText(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_REFRESHING)));
1028 this->FetchOutList(true);
1029 theApp.DoWaitCursor(-1);
1032 BOOL CSyncDlg::PreTranslateMessage(MSG* pMsg)
1034 if (pMsg->message == WM_KEYDOWN)
1036 switch (pMsg->wParam)
1039 case VK_F5:
1041 if (m_bBlock)
1042 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
1043 Refresh();
1045 break;
1047 /* Avoid TAB control destroy but dialog exist*/
1048 case VK_ESCAPE:
1049 case VK_CANCEL:
1051 TCHAR buff[128] = { 0 };
1052 ::GetClassName(pMsg->hwnd,buff,128);
1054 /* Use MSFTEDIT_CLASS http://msdn.microsoft.com/en-us/library/bb531344.aspx */
1055 if (_tcsnicmp(buff, MSFTEDIT_CLASS, 128) == 0 || //Unicode and MFC 2012 and later
1056 _tcsnicmp(buff, RICHEDIT_CLASS, 128) == 0 || //ANSI or MFC 2010
1057 _tcsnicmp(buff, _T("SysListView32"), 128) == 0)
1059 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
1060 return TRUE;
1065 m_tooltips.RelayEvent(pMsg);
1066 return __super::PreTranslateMessage(pMsg);
1068 void CSyncDlg::FetchOutList(bool force)
1070 if(!m_bInited)
1071 return;
1072 m_OutChangeFileList.Clear();
1073 this->m_OutLogList.Clear();
1075 CString remote;
1076 this->m_ctrlURL.GetWindowText(remote);
1077 CString remotebranch;
1078 this->m_ctrlRemoteBranch.GetWindowText(remotebranch);
1079 remotebranch=remote+_T("/")+remotebranch;
1080 CGitHash remotebranchHash;
1081 g_Git.GetHash(remotebranchHash, remotebranch);
1083 if(IsURL())
1085 CString str;
1086 str.LoadString(IDS_PROC_SYNC_PUSH_UNKNOWN);
1087 m_OutLogList.ShowText(str);
1088 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,FALSE);
1089 m_OutLocalBranch.Empty();
1090 m_OutRemoteBranch.Empty();
1092 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
1093 return ;
1096 else if(remotebranchHash.IsEmpty())
1098 CString str;
1099 str.Format(IDS_PROC_SYNC_PUSH_UNKNOWNBRANCH, remotebranch);
1100 m_OutLogList.ShowText(str);
1101 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,FALSE);
1102 m_OutLocalBranch.Empty();
1103 m_OutRemoteBranch.Empty();
1105 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
1106 return ;
1108 else
1110 CString localbranch;
1111 localbranch=this->m_ctrlLocalBranch.GetString();
1113 if(localbranch != m_OutLocalBranch || m_OutRemoteBranch != remotebranch || force)
1115 m_OutLogList.ClearText();
1117 CGitHash base, localBranchHash;
1118 bool isFastForward = g_Git.IsFastForward(remotebranch, localbranch, &base);
1120 if (g_Git.GetHash(localBranchHash, localbranch))
1122 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of \"") + localbranch + _T("\".")), _T("TortoiseGit"), MB_ICONERROR);
1123 return;
1125 if (remotebranchHash == localBranchHash)
1127 CString str;
1128 str.Format(IDS_PROC_SYNC_COMMITSAHEAD, 0, remotebranch);
1129 m_OutLogList.ShowText(str);
1130 this->m_ctrlStatus.SetWindowText(str);
1131 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,FALSE);
1132 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
1134 else if (isFastForward || m_bForce)
1136 CString range;
1137 range.Format(_T("%s..%s"), g_Git.FixBranchName(remotebranch), g_Git.FixBranchName(localbranch));
1138 //fast forward
1139 m_OutLogList.FillGitLog(nullptr, &range, CGit::LOG_INFO_STAT | CGit::LOG_INFO_FILESTATE | CGit::LOG_INFO_SHOW_MERGEDFILE);
1140 CString str;
1141 str.Format(IDS_PROC_SYNC_COMMITSAHEAD, m_OutLogList.GetItemCount(), remotebranch);
1142 this->m_ctrlStatus.SetWindowText(str);
1144 if (isFastForward)
1145 AddDiffFileList(&m_OutChangeFileList, &m_arOutChangeList, localbranch, remotebranch);
1146 else
1148 AddDiffFileList(&m_OutChangeFileList, &m_arOutChangeList, localbranch, base.ToString());
1151 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,TRUE);
1152 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(TRUE);
1154 else
1156 CString str;
1157 str.Format(IDS_PROC_SYNC_NOFASTFORWARD, localbranch, remotebranch);
1158 m_OutLogList.ShowText(str);
1159 this->m_ctrlStatus.SetWindowText(str);
1160 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID() - 1, FALSE);
1161 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
1164 this->m_OutLocalBranch=localbranch;
1165 this->m_OutRemoteBranch=remotebranch;
1170 bool CSyncDlg::IsURL()
1172 CString str;
1173 this->m_ctrlURL.GetWindowText(str);
1174 if(str.Find(_T('\\'))>=0 || str.Find(_T('/'))>=0)
1175 return true;
1176 else
1177 return false;
1179 void CSyncDlg::OnCbnEditchangeComboboxex()
1181 SetTimer(IDT_INPUT, 1000, NULL);
1182 this->m_OutLogList.ShowText(CString(MAKEINTRESOURCE(IDS_PROC_SYNC_WAINTINPUT)));
1184 //this->FetchOutList();
1187 UINT CSyncDlg::ProgressThread()
1189 m_startTick = GetTickCount();
1190 m_bDone = false;
1191 STRING_VECTOR list;
1192 CProgressDlg::RunCmdList(this, m_GitCmdList, list, true, nullptr, &this->m_bAbort, &this->m_Databuf);
1193 InterlockedExchange(&m_bBlock, FALSE);
1194 return 0;
1198 LRESULT CSyncDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)
1200 if(wParam == MSG_PROGRESSDLG_START)
1202 m_BufStart = 0;
1203 m_ctrlAnimate.Play(0, UINT_MAX, UINT_MAX);
1204 this->m_ctrlProgress.SetPos(0);
1205 if (m_pTaskbarList)
1207 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
1208 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 100);
1212 if(wParam == MSG_PROGRESSDLG_END || wParam == MSG_PROGRESSDLG_FAILED)
1214 DWORD tickSpent = GetTickCount() - m_startTick;
1215 CString strEndTime = CLoglistUtils::FormatDateAndTime(CTime::GetCurrentTime(), DATE_SHORTDATE, true, false);
1217 m_BufStart = 0;
1218 m_Databuf.m_critSec.Lock();
1219 m_Databuf.clear();
1220 m_Databuf.m_critSec.Unlock();
1222 m_bDone = true;
1223 m_ctrlAnimate.Stop();
1224 m_ctrlProgress.SetPos(100);
1225 //this->DialogEnableWindow(IDOK,TRUE);
1227 DWORD exitCode = (DWORD)lParam;
1228 if (exitCode)
1230 if (m_pTaskbarList)
1232 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_ERROR);
1233 m_pTaskbarList->SetProgressValue(m_hWnd, 100, 100);
1235 CString log;
1236 log.Format(IDS_PROC_PROGRESS_GITUNCLEANEXIT, exitCode);
1237 CString err;
1238 err.Format(_T("\r\n\r\n%s (%lu ms @ %s)\r\n"), log, tickSpent, strEndTime);
1239 CProgressDlg::InsertColorText(this->m_ctrlCmdOut, err, RGB(255,0,0));
1240 PlaySound((LPCTSTR)SND_ALIAS_SYSTEMEXCLAMATION, NULL, SND_ALIAS_ID | SND_ASYNC);
1242 else
1244 if (m_pTaskbarList)
1245 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
1246 CString temp;
1247 temp.LoadString(IDS_SUCCESS);
1248 CString log;
1249 log.Format(_T("\r\n%s (%lu ms @ %s)\r\n"), temp, tickSpent, strEndTime);
1250 CProgressDlg::InsertColorText(this->m_ctrlCmdOut, log, RGB(0,0,255));
1252 m_GitCmdStatus = exitCode;
1254 //if(wParam == MSG_PROGRESSDLG_END)
1255 RunPostAction();
1258 if(lParam != 0)
1259 ParserCmdOutput((char)lParam);
1260 else
1262 m_Databuf.m_critSec.Lock();
1263 for (size_t i = m_BufStart; i < m_Databuf.size(); ++i)
1265 char c = m_Databuf[m_BufStart];
1266 ++m_BufStart;
1267 m_Databuf.m_critSec.Unlock();
1268 ParserCmdOutput(c);
1270 m_Databuf.m_critSec.Lock();
1273 if (m_BufStart > 1000)
1275 m_Databuf.erase(m_Databuf.begin(), m_Databuf.begin() + m_BufStart);
1276 m_BufStart = 0;
1278 m_Databuf.m_critSec.Unlock();
1281 return 0;
1284 static std::map<CString, CGitHash> * HashMapToRefMap(MAP_HASH_NAME &map)
1286 auto rmap = new std::map<CString, CGitHash>();
1287 for (auto mit = map.begin(); mit != map.end(); ++mit)
1289 for (auto rit = mit->second.begin(); rit != mit->second.end(); ++rit)
1291 rmap->insert(std::make_pair(*rit, mit->first));
1294 return rmap;
1297 void CSyncDlg::FillNewRefMap()
1299 m_refList.Clear();
1300 m_newHashMap.clear();
1301 CAutoRepository repo(g_Git.GetGitRepository());
1302 if (!repo)
1304 CMessageBox::Show(m_hWnd, CGit::GetLibGit2LastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
1305 return;
1308 if (CGit::GetMapHashToFriendName(repo, m_newHashMap))
1310 MessageBox(CGit::GetLibGit2LastErr(_T("Could not get all refs.")), _T("TortoiseGit"), MB_ICONERROR);
1311 return;
1314 auto oldRefMap = HashMapToRefMap(m_oldHashMap);
1315 auto newRefMap = HashMapToRefMap(m_newHashMap);
1316 for (auto oit = oldRefMap->begin(); oit != oldRefMap->end(); ++oit)
1318 bool found = false;
1319 for (auto nit = newRefMap->begin(); nit != newRefMap->end(); ++nit)
1321 // changed ref
1322 if (oit->first == nit->first)
1324 found = true;
1325 m_refList.AddEntry(repo, oit->first, &oit->second, &nit->second);
1326 break;
1329 // deleted ref
1330 if (!found)
1332 m_refList.AddEntry(repo, oit->first, &oit->second, nullptr);
1335 for (auto nit = newRefMap->begin(); nit != newRefMap->end(); ++nit)
1337 bool found = false;
1338 for (auto oit = oldRefMap->begin(); oit != oldRefMap->end(); ++oit)
1340 if (oit->first == nit->first)
1342 found = true;
1343 break;
1346 // new ref
1347 if (!found)
1349 m_refList.AddEntry(repo, nit->first, nullptr, &nit->second);
1352 delete oldRefMap;
1353 delete newRefMap;
1354 m_refList.Show();
1357 void CSyncDlg::RunPostAction()
1359 if (m_bWantToExit)
1360 return;
1362 FillNewRefMap();
1364 if (this->m_CurrentCmd == GIT_COMMAND_PUSH)
1366 DWORD exitcode = 0xFFFFFFFF;
1367 CString error;
1368 if (CHooks::Instance().PostPush(g_Git.m_CurrentDir, exitcode, error))
1370 if (exitcode)
1372 CString temp;
1373 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
1374 CMessageBox::Show(nullptr, temp,_T("TortoiseGit"), MB_OK | MB_ICONERROR);
1375 return;
1379 EnableControlButton(true);
1380 SwitchToInput();
1381 this->FetchOutList(true);
1383 else if (this->m_CurrentCmd == GIT_COMMAND_PULL)
1385 PullComplete();
1387 else if (this->m_CurrentCmd == GIT_COMMAND_FETCH || this->m_CurrentCmd == GIT_COMMAND_FETCHANDREBASE)
1389 FetchComplete();
1391 else if (this->m_CurrentCmd == GIT_COMMAND_SUBMODULE)
1393 //this->m_ctrlCmdOut.SetSel(-1,-1);
1394 //this->m_ctrlCmdOut.ReplaceSel(_T("Done\r\n"));
1395 //this->m_ctrlCmdOut.SetSel(-1,-1);
1396 EnableControlButton(true);
1397 SwitchToInput();
1399 else if (this->m_CurrentCmd == GIT_COMMAND_STASH)
1401 StashComplete();
1403 else if (this->m_CurrentCmd == GIT_COMMAND_REMOTE)
1405 this->FetchOutList(true);
1406 EnableControlButton(true);
1407 SwitchToInput();
1408 ShowTab(IDC_REFLIST);
1411 void CSyncDlg::ParserCmdOutput(char ch)
1413 CProgressDlg::ParserCmdOutput(m_ctrlCmdOut,m_ctrlProgress,m_hWnd,m_pTaskbarList,m_LogText,ch);
1415 void CSyncDlg::OnBnClickedButtonCommit()
1417 CString cmd = _T("/command:commit");
1418 cmd += _T(" /path:\"");
1419 cmd += g_Git.m_CurrentDir;
1420 cmd += _T("\"");
1422 CAppUtils::RunTortoiseGitProc(cmd);
1425 void CSyncDlg::OnOK()
1427 UpdateCombox();
1428 this->UpdateData();
1429 m_ctrlURL.SaveHistory();
1430 SaveHistory();
1431 m_regAutoLoadPutty = this->m_bAutoLoadPuttyKey;
1432 m_tooltips.Pop();
1433 __super::OnOK();
1436 void CSyncDlg::OnCancel()
1438 m_bAbort = true;
1439 if (m_bDone)
1441 CResizableStandAloneDialog::OnCancel();
1442 return;
1445 if (g_Git.m_CurrentGitPi.hProcess)
1447 DWORD dwConfirmKillProcess = CRegDWORD(_T("Software\\TortoiseGit\\ConfirmKillProcess"));
1448 if (dwConfirmKillProcess && CMessageBox::Show(m_hWnd, IDS_PROC_CONFIRMKILLPROCESS, IDS_APPNAME, MB_YESNO | MB_ICONQUESTION) != IDYES)
1449 return;
1450 if (::GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0))
1452 ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess, 10000);
1454 else
1456 GetLastError();
1459 CProgressDlg::KillProcessTree(g_Git.m_CurrentGitPi.dwProcessId);
1462 ::WaitForSingleObject(g_Git.m_CurrentGitPi.hProcess ,10000);
1463 m_tooltips.Pop();
1464 CResizableStandAloneDialog::OnCancel();
1467 void CSyncDlg::OnBnClickedButtonSubmodule()
1469 this->UpdateData();
1470 UpdateCombox();
1471 m_ctrlCmdOut.SetWindowTextW(_T(""));
1472 m_LogText = "";
1474 this->m_regSubmoduleButton = (DWORD)this->m_ctrlSubmodule.GetCurrentEntry();
1476 this->SwitchToRun();
1478 this->m_bAbort=false;
1479 this->m_GitCmdList.clear();
1481 ShowTab(IDC_CMD_LOG);
1483 CString cmd;
1485 switch (m_ctrlSubmodule.GetCurrentEntry())
1487 case 0:
1488 cmd=_T("git.exe submodule update --init");
1489 break;
1490 case 1:
1491 cmd=_T("git.exe submodule init");
1492 break;
1493 case 2:
1494 cmd=_T("git.exe submodule sync");
1495 break;
1498 m_GitCmdList.push_back(cmd);
1500 m_CurrentCmd = GIT_COMMAND_SUBMODULE;
1502 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
1503 if (m_pThread==NULL)
1505 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
1507 else
1509 m_pThread->m_bAutoDelete = TRUE;
1510 m_pThread->ResumeThread();
1514 void CSyncDlg::OnBnClickedButtonStash()
1516 UpdateData();
1517 UpdateCombox();
1518 m_ctrlCmdOut.SetWindowTextW(_T(""));
1519 m_LogText = "";
1521 SwitchToRun();
1523 m_bAbort = false;
1524 m_GitCmdList.clear();
1526 ShowTab(IDC_CMD_LOG);
1528 m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST - 1, false);
1529 m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST -1, false);
1530 m_ctrlTabCtrl.ShowTab(IDC_IN_CONFLICT -1, false);
1532 CString cmd;
1533 switch (m_ctrlStash.GetCurrentEntry())
1535 case 0:
1536 cmd = _T("git.exe stash save");
1537 break;
1538 case 1:
1539 cmd = _T("git.exe stash pop");
1540 break;
1541 case 2:
1542 cmd = _T("git.exe stash apply");
1543 break;
1546 m_GitCmdList.push_back(cmd);
1547 m_CurrentCmd = GIT_COMMAND_STASH;
1549 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
1550 if (!m_pThread)
1552 //ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
1554 else
1556 m_pThread->m_bAutoDelete = TRUE;
1557 m_pThread->ResumeThread();
1561 void CSyncDlg::OnTimer(UINT_PTR nIDEvent)
1563 if( nIDEvent == IDT_INPUT)
1565 KillTimer(IDT_INPUT);
1566 this->FetchOutList(true);
1571 void CSyncDlg::OnLvnInLogListColumnClick(NMHDR * /* pNMHDR */, LRESULT *pResult)
1573 *pResult = 0;
1576 LRESULT CSyncDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
1578 m_pTaskbarList.Release();
1579 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
1580 m_GitProgressList.m_pTaskbarList = m_pTaskbarList;
1581 SetUUIDOverlayIcon(m_hWnd);
1582 return 0;
1585 void CSyncDlg::OnBnClickedCheckForce()
1587 UpdateData();
1590 void CSyncDlg::OnBnClickedLog()
1592 CString cmd = _T("/command:log");
1593 cmd += _T(" /path:\"");
1594 cmd += g_Git.m_CurrentDir;
1595 cmd += _T("\"");
1597 CAppUtils::RunTortoiseGitProc(cmd);
1600 LRESULT CSyncDlg::OnProgCmdFinish(WPARAM /*wParam*/, LPARAM /*lParam*/)
1602 RefreshCursor();
1603 RunPostAction();
1604 return 0;
1607 void CSyncDlg::OnDestroy()
1609 m_bWantToExit = true;
1610 __super::OnDestroy();