share code for calculation of relative times in log
[TortoiseGit.git] / src / TortoiseProc / SyncDlg.cpp
blobd245ed977f6ac779504edc219bbba4132d1c88fc
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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 "PathUtils.h"
31 #include "RebaseDlg.h"
32 #include "hooks.h"
34 // CSyncDlg dialog
36 IMPLEMENT_DYNAMIC(CSyncDlg, CResizableStandAloneDialog)
38 CSyncDlg::CSyncDlg(CWnd* pParent /*=NULL*/)
39 : CResizableStandAloneDialog(CSyncDlg::IDD, pParent)
41 m_pTooltip=&this->m_tooltips;
42 m_bInited=false;
43 m_CmdOutCurrentPos=0;
44 m_bAutoLoadPuttyKey = CAppUtils::IsSSHPutty();
45 m_bForce=false;
46 m_bBlock = false;
49 CSyncDlg::~CSyncDlg()
53 void CSyncDlg::DoDataExchange(CDataExchange* pDX)
55 CDialog::DoDataExchange(pDX);
56 DDX_Check(pDX, IDC_CHECK_PUTTY_KEY, m_bAutoLoadPuttyKey);
57 DDX_Check(pDX, IDC_CHECK_FORCE,m_bForce);
58 DDX_Control(pDX, IDC_COMBOBOXEX_URL, m_ctrlURL);
59 DDX_Control(pDX, IDC_BUTTON_TABCTRL, m_ctrlDumyButton);
60 DDX_Control(pDX, IDC_BUTTON_PULL, m_ctrlPull);
61 DDX_Control(pDX, IDC_BUTTON_PUSH, m_ctrlPush);
62 DDX_Control(pDX, IDC_STATIC_STATUS, m_ctrlStatus);
63 DDX_Control(pDX, IDC_PROGRESS_SYNC, m_ctrlProgress);
64 DDX_Control(pDX, IDC_ANIMATE_SYNC, m_ctrlAnimate);
65 DDX_Control(pDX, IDC_BUTTON_SUBMODULE,m_ctrlSubmodule);
66 BRANCH_COMBOX_DDX;
70 BEGIN_MESSAGE_MAP(CSyncDlg, CResizableStandAloneDialog)
71 ON_BN_CLICKED(IDC_BUTTON_PULL, &CSyncDlg::OnBnClickedButtonPull)
72 ON_BN_CLICKED(IDC_BUTTON_PUSH, &CSyncDlg::OnBnClickedButtonPush)
73 ON_BN_CLICKED(IDC_BUTTON_APPLY, &CSyncDlg::OnBnClickedButtonApply)
74 ON_BN_CLICKED(IDC_BUTTON_EMAIL, &CSyncDlg::OnBnClickedButtonEmail)
75 ON_BN_CLICKED(IDC_BUTTON_MANAGE, &CSyncDlg::OnBnClickedButtonManage)
76 BRANCH_COMBOX_EVENT
77 ON_CBN_EDITCHANGE(IDC_COMBOBOXEX_URL, &CSyncDlg::OnCbnEditchangeComboboxex)
78 ON_CBN_EDITCHANGE(IDC_COMBOBOXEX_REMOTE_BRANCH, &CSyncDlg::OnCbnEditchangeComboboxex)
79 ON_MESSAGE(MSG_PROGRESSDLG_UPDATE_UI, OnProgressUpdateUI)
80 ON_BN_CLICKED(IDC_BUTTON_COMMIT, &CSyncDlg::OnBnClickedButtonCommit)
81 ON_BN_CLICKED(IDC_BUTTON_SUBMODULE, &CSyncDlg::OnBnClickedButtonSubmodule)
82 ON_WM_TIMER()
83 ON_REGISTERED_MESSAGE(WM_TASKBARBTNCREATED, OnTaskbarBtnCreated)
84 END_MESSAGE_MAP()
87 void CSyncDlg::EnableControlButton(bool bEnabled)
89 GetDlgItem(IDC_BUTTON_PULL)->EnableWindow(bEnabled);
90 GetDlgItem(IDC_BUTTON_PUSH)->EnableWindow(bEnabled);
91 GetDlgItem(IDC_BUTTON_APPLY)->EnableWindow(bEnabled);
92 GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(bEnabled);
93 GetDlgItem(IDOK)->EnableWindow(bEnabled);
94 GetDlgItem(IDC_BUTTON_SUBMODULE)->EnableWindow(bEnabled);
96 // CSyncDlg message handlers
98 void CSyncDlg::OnBnClickedButtonPull()
100 int CurrentEntry;
101 CurrentEntry = this->m_ctrlPull.GetCurrentEntry();
102 this->m_regPullButton = CurrentEntry;
104 this->m_bAbort=false;
105 this->m_GitCmdList.clear();
107 this->UpdateData();
108 UpdateCombox();
110 m_oldHash = g_Git.GetHash(_T("HEAD"));
112 if( CurrentEntry == 0)
114 if( g_Git.GetHash(this->m_strLocalBranch) != m_oldHash)
116 CMessageBox::Show(NULL,_T("Pull require local branch must be current branch"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);
117 return;
121 if(this->m_strURL.IsEmpty())
123 CMessageBox::Show(NULL,_T("URL can't Empty"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);
124 return;
127 if(this->m_bAutoLoadPuttyKey)
129 CAppUtils::LaunchPAgent(NULL,&this->m_strURL);
132 this->SwitchToRun();
134 CString force;
135 if(this->m_bForce)
136 force = _T(" --force ");
138 CString cmd;
140 ShowTab(IDC_CMD_LOG);
142 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,false);
143 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
144 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CONFLICT-1,false);
146 this->GetDlgItem(IDC_BUTTON_COMMIT)->ShowWindow(SW_HIDE);
148 ///Pull
149 if(CurrentEntry == 0) //Pull
151 CString remotebranch;
152 remotebranch = m_strRemoteBranch;
154 if(!IsURL())
156 CString configName;
157 configName.Format(L"branch.%s.merge", this->m_strLocalBranch);
158 CString pullBranch = CGit::StripRefName(g_Git.GetConfigValue(configName));
160 configName.Format(L"branch.%s.remote", m_strLocalBranch);
161 CString pullRemote = g_Git.GetConfigValue(configName);
163 if(pullBranch == remotebranch && pullRemote == this->m_strURL)
164 remotebranch.Empty();
167 if(m_Gitverion >= 0x01070203) //above 1.7.0.2
168 force += _T("--progress ");
170 cmd.Format(_T("git.exe pull -v %s \"%s\" %s"),
171 force,
172 m_strURL,
173 remotebranch);
175 m_CurrentCmd = GIT_COMMAND_PULL;
176 m_GitCmdList.push_back(cmd);
178 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
179 if (m_pThread==NULL)
181 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
183 else
185 m_pThread->m_bAutoDelete = TRUE;
186 m_pThread->ResumeThread();
191 ///Fetch
192 if(CurrentEntry == 1 || CurrentEntry ==2 ) //Fetch
194 CString remotebranch;
195 if(this->IsURL() || m_strRemoteBranch.IsEmpty())
197 remotebranch=this->m_strRemoteBranch;
200 else
202 remotebranch.Format(_T("remotes/%s/%s"),
203 m_strURL,m_strRemoteBranch);
204 if(g_Git.GetHash(remotebranch).IsEmpty())
205 remotebranch=m_strRemoteBranch;
206 else
207 remotebranch=m_strRemoteBranch+_T(":")+remotebranch;
210 if(m_Gitverion >= 0x01070203) //above 1.7.0.2
211 force += _T("--progress ");
213 cmd.Format(_T("git.exe fetch -v %s \"%s\" %s"),
214 force,
215 m_strURL,
216 remotebranch);
218 if(CurrentEntry == 1)
219 m_CurrentCmd = GIT_COMMAND_FETCH;
220 else
221 m_CurrentCmd = GIT_COMMAND_FETCHANDREBASE;
222 m_GitCmdList.push_back(cmd);
224 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
225 if (m_pThread==NULL)
227 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
229 else
231 m_pThread->m_bAutoDelete = TRUE;
232 m_pThread->ResumeThread();
236 ///Remote Update
237 if(CurrentEntry == 3)
239 m_CurrentCmd = GIT_COMMAND_REMOTE;
240 cmd=_T("git.exe remote update");
241 m_GitCmdList.push_back(cmd);
243 InterlockedExchange(&m_bBlock, TRUE);
245 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
246 if (m_pThread==NULL)
248 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
249 InterlockedExchange(&m_bBlock, FALSE);
251 else
253 m_pThread->m_bAutoDelete = TRUE;
254 m_pThread->ResumeThread();
259 void CSyncDlg::PullComplete()
261 EnableControlButton(true);
262 SwitchToInput();
263 this->FetchOutList(true);
265 CString newhash;
266 newhash = g_Git.GetHash(_T("HEAD"));
268 if( this ->m_GitCmdStatus )
270 CTGitPathList list;
271 if(g_Git.ListConflictFile(list))
273 this->m_ctrlCmdOut.SetSel(-1,-1);
274 this->m_ctrlCmdOut.ReplaceSel(_T("Get conflict files fail\n"));
276 this->ShowTab(IDC_CMD_LOG);
277 return;
280 if(list.GetCount()>0)
282 this->m_ConflictFileList.Clear();
283 CTGitPathList list;
284 CTGitPath path;
285 list.AddPath(path);
287 this->m_ConflictFileList.GetStatus(&list,true);
288 this->m_ConflictFileList.Show(CTGitPath::LOGACTIONS_UNMERGED,
289 CTGitPath::LOGACTIONS_UNMERGED);
291 this->ShowTab(IDC_IN_CONFLICT);
293 this->GetDlgItem(IDC_BUTTON_COMMIT)->ShowWindow(SW_NORMAL);
295 else
296 this->ShowTab(IDC_CMD_LOG);
299 else
301 if(newhash == this->m_oldHash)
303 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
304 this->m_InLogList.ShowText(_T("No commits get after pull"));
305 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
307 else
309 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,true);
310 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
312 CString oldhash=m_oldHash.ToString();
313 this->AddDiffFileList(&m_InChangeFileList,&m_arInChangeList,newhash,oldhash);
315 m_InLogList.FillGitLog(NULL,CGit:: LOG_INFO_STAT| CGit::LOG_INFO_FILESTATE | CGit::LOG_INFO_SHOW_MERGEDFILE,
316 &oldhash,&newhash);
318 this->ShowTab(IDC_IN_LOGLIST);
322 void CSyncDlg::FetchComplete()
324 EnableControlButton(true);
325 SwitchToInput();
326 this->FetchOutList(true);
328 ShowTab(IDC_CMD_LOG);
329 if( (!this->m_GitCmdStatus) && this->m_CurrentCmd == GIT_COMMAND_FETCHANDREBASE)
331 CRebaseDlg dlg;
332 dlg.m_PostButtonTexts.Add(_T("Email &Patch..."));
333 int response = dlg.DoModal();
334 if(response == IDOK)
336 return ;
339 if(response == IDC_REBASE_POST_BUTTON)
341 CString cmd, out, err;
342 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
343 g_Git.m_CurrentDir,
344 g_Git.FixBranchName(dlg.m_Upstream),
345 g_Git.FixBranchName(dlg.m_Branch));
346 if(g_Git.Run(cmd, &out, &err, CP_ACP))
348 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
349 return ;
352 CAppUtils::SendPatchMail(cmd,out);
357 void CSyncDlg::OnBnClickedButtonPush()
359 this->UpdateData();
360 UpdateCombox();
362 if(this->m_strURL.IsEmpty())
364 CMessageBox::Show(NULL,_T("URL can't Empty"),_T("TortoiseGit"),MB_OK|MB_ICONERROR);
365 return;
368 this->m_regPushButton=this->m_ctrlPush.GetCurrentEntry();
369 this->SwitchToRun();
370 this->m_bAbort=false;
371 this->m_GitCmdList.clear();
373 ShowTab(IDC_CMD_LOG);
375 CString cmd;
376 CString arg;
378 CString error;
379 DWORD exitcode;
380 CTGitPathList list;
381 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
383 if (CHooks::Instance().PrePush(list,exitcode, error))
385 if (exitcode)
387 CString temp;
388 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
389 //ReportError(temp);
390 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
391 return ;
395 switch (m_ctrlPush.GetCurrentEntry())
397 case 1:
398 arg += _T(" --tags ");
399 break;
400 case 2:
401 arg += _T(" --all ");
402 break;
405 if(this->m_bForce)
406 arg += _T(" --force ");
408 if(m_Gitverion >= 0x01070203) //above 1.7.0.2
409 arg += _T("--progress ");
411 cmd.Format(_T("git.exe push -v %s \"%s\" %s"),
412 arg,
413 m_strURL,
414 g_Git.FixBranchName(m_strLocalBranch));
416 if (!m_strRemoteBranch.IsEmpty())
418 cmd += _T(":") + m_strRemoteBranch;
421 m_GitCmdList.push_back(cmd);
423 m_CurrentCmd = GIT_COMMAND_PUSH;
425 if(this->m_bAutoLoadPuttyKey)
427 CAppUtils::LaunchPAgent(NULL,&this->m_strURL);
430 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
431 if (m_pThread==NULL)
433 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
435 else
437 m_pThread->m_bAutoDelete = TRUE;
438 m_pThread->ResumeThread();
442 void CSyncDlg::OnBnClickedButtonApply()
444 CString oldhash;
445 oldhash=g_Git.GetHash(_T("HEAD"));
447 CImportPatchDlg dlg;
448 CString cmd,output;
450 if(dlg.DoModal() == IDOK)
452 int err=0;
453 for(int i=0;i<dlg.m_PathList.GetCount();i++)
455 cmd.Format(_T("git.exe am \"%s\""),dlg.m_PathList[i].GetGitPathString());
457 if(g_Git.Run(cmd,&output,CP_ACP))
459 CMessageBox::Show(NULL,output,_T("TortoiseGit"),MB_OK);
461 err=1;
462 break;
464 this->m_ctrlCmdOut.SetSel(-1,-1);
465 this->m_ctrlCmdOut.ReplaceSel(cmd+_T("\n"));
466 this->m_ctrlCmdOut.SetSel(-1,-1);
467 this->m_ctrlCmdOut.ReplaceSel(output);
470 CString newhash=g_Git.GetHash(_T("HEAD"));
472 this->m_InLogList.Clear();
473 this->m_InChangeFileList.Clear();
475 if(newhash == oldhash)
477 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
478 this->m_InLogList.ShowText(_T("No commits get from patch"));
479 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
482 else
484 this->m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,true);
485 this->m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,true);
487 this->AddDiffFileList(&m_InChangeFileList,&m_arInChangeList,newhash,oldhash);
488 m_InLogList.FillGitLog(NULL,CGit:: LOG_INFO_STAT| CGit::LOG_INFO_FILESTATE | CGit::LOG_INFO_SHOW_MERGEDFILE,
489 &oldhash,&newhash);
491 this->FetchOutList(true);
494 this->m_ctrlTabCtrl.ShowTab(IDC_CMD_LOG-1,true);
496 if(err)
498 this->ShowTab(IDC_CMD_LOG);
500 else
502 this->ShowTab(IDC_IN_LOGLIST);
507 void CSyncDlg::OnBnClickedButtonEmail()
509 CString cmd, out, err;
511 this->m_strLocalBranch = this->m_ctrlLocalBranch.GetString();
512 this->m_ctrlRemoteBranch.GetWindowText(this->m_strRemoteBranch);
513 this->m_ctrlURL.GetWindowText(this->m_strURL);
514 m_strURL=m_strURL.Trim();
515 m_strRemoteBranch=m_strRemoteBranch.Trim();
517 cmd.Format(_T("git.exe format-patch -o \"%s\" %s..%s"),
518 g_Git.m_CurrentDir,
519 m_strURL+_T('/')+m_strRemoteBranch,g_Git.FixBranchName(m_strLocalBranch));
521 if (g_Git.Run(cmd, &out, &err, CP_ACP))
523 CMessageBox::Show(NULL, out + L"\n" + err, _T("TortoiseGit"), MB_OK|MB_ICONERROR);
524 return ;
527 CAppUtils::SendPatchMail(cmd,out);
530 void CSyncDlg::ShowProgressCtrl(bool bShow)
532 int b=bShow?SW_NORMAL:SW_HIDE;
533 this->m_ctrlAnimate.ShowWindow(b);
534 this->m_ctrlProgress.ShowWindow(b);
535 this->m_ctrlAnimate.Open(IDR_DOWNLOAD);
536 if(b == SW_NORMAL)
537 this->m_ctrlAnimate.Play(0,-1,-1);
538 else
539 this->m_ctrlAnimate.Stop();
541 void CSyncDlg::ShowInputCtrl(bool bShow)
543 int b=bShow?SW_NORMAL:SW_HIDE;
544 this->m_ctrlURL.ShowWindow(b);
545 this->m_ctrlLocalBranch.ShowWindow(b);
546 this->m_ctrlRemoteBranch.ShowWindow(b);
547 this->GetDlgItem(IDC_BUTTON_LOCAL_BRANCH)->ShowWindow(b);
548 this->GetDlgItem(IDC_BUTTON_REMOTE_BRANCH)->ShowWindow(b);
549 this->GetDlgItem(IDC_STATIC_LOCAL_BRANCH)->ShowWindow(b);
550 this->GetDlgItem(IDC_STATIC_REMOTE_BRANCH)->ShowWindow(b);
551 this->GetDlgItem(IDC_BUTTON_MANAGE)->ShowWindow(b);
552 this->GetDlgItem(IDC_CHECK_PUTTY_KEY)->ShowWindow(b);
553 this->GetDlgItem(IDC_CHECK_FORCE)->ShowWindow(b);
554 this->GetDlgItem(IDC_STATIC_REMOTE_URL)->ShowWindow(b);
556 BOOL CSyncDlg::OnInitDialog()
558 CResizableStandAloneDialog::OnInitDialog();
559 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
561 // Let the TaskbarButtonCreated message through the UIPI filter. If we don't
562 // do this, Explorer would be unable to send that message to our window if we
563 // were running elevated. It's OK to make the call all the time, since if we're
564 // not elevated, this is a no-op.
565 CHANGEFILTERSTRUCT cfs = { sizeof(CHANGEFILTERSTRUCT) };
566 typedef BOOL STDAPICALLTYPE ChangeWindowMessageFilterExDFN(HWND hWnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
567 HMODULE hUser = ::LoadLibrary(_T("user32.dll"));
568 if (hUser)
570 ChangeWindowMessageFilterExDFN *pfnChangeWindowMessageFilterEx = (ChangeWindowMessageFilterExDFN*)GetProcAddress(hUser, "ChangeWindowMessageFilterEx");
571 if (pfnChangeWindowMessageFilterEx)
573 pfnChangeWindowMessageFilterEx(m_hWnd, WM_TASKBARBTNCREATED, MSGFLT_ALLOW, &cfs);
575 FreeLibrary(hUser);
577 m_pTaskbarList.Release();
578 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
580 this->GetDlgItem(IDC_CHECK_PUTTY_KEY)->EnableWindow(CAppUtils::IsSSHPutty());
583 this->m_ctrlAnimate.ShowWindow(SW_NORMAL);
584 this->m_ctrlAnimate.Open(IDR_DOWNLOAD);
585 this->m_ctrlAnimate.Play(0,-1,-1);
588 // ------------------ Create Tabctrl -----------
589 CWnd *pwnd=this->GetDlgItem(IDC_BUTTON_TABCTRL);
590 CRect rectDummy;
591 pwnd->GetWindowRect(&rectDummy);
592 this->ScreenToClient(rectDummy);
594 if (!m_ctrlTabCtrl.Create(CMFCTabCtrl::STYLE_FLAT, rectDummy, this, IDC_SYNC_TAB))
596 TRACE0("Failed to create output tab window\n");
597 return FALSE; // fail to create
599 m_ctrlTabCtrl.SetResizeMode(CMFCTabCtrl::RESIZE_NO);
601 // -------------Create Command Log Ctrl ---------
602 DWORD dwStyle;
603 dwStyle= ES_MULTILINE | ES_READONLY | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL |WS_VSCROLL ;
605 if( !m_ctrlCmdOut.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_CMD_LOG))
607 TRACE0("Failed to create Log commits window\n");
608 return FALSE; // fail to create
611 CHARFORMAT m_Format;
612 memset(&m_Format, 0, sizeof(CHARFORMAT));
613 m_Format.cbSize = sizeof(CHARFORMAT);
614 m_Format.dwMask = CFM_FACE | CFM_BOLD;
615 wcsncpy(m_Format.szFaceName, L"Courier New", LF_FACESIZE - 1);
616 m_ctrlCmdOut.SetDefaultCharFormat(m_Format);
618 m_ctrlTabCtrl.InsertTab(&m_ctrlCmdOut,_T("Log"),-1);
620 //m_ctrlCmdOut.ReplaceSel(_T("Hello"));
622 //---------- Create in coming list ctrl -----------
623 dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | LVS_OWNERDATA | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;;
625 if( !m_InLogList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_IN_LOGLIST))
627 TRACE0("Failed to create output commits window\n");
628 return FALSE; // fail to create
631 m_ctrlTabCtrl.InsertTab(&m_InLogList,_T("In Commits"),-1);
633 m_InLogList.m_ColumnRegKey=_T("SyncIn");
634 m_InLogList.InsertGitColumn();
636 //----------- Create In Change file list -----------
637 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP |LVS_SINGLESEL |WS_CHILD | WS_VISIBLE;
639 if( !m_InChangeFileList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_IN_CHANGELIST))
641 TRACE0("Failed to create output change files window\n");
642 return FALSE; // fail to create
644 m_ctrlTabCtrl.InsertTab(&m_InChangeFileList,_T("In ChangeList"),-1);
646 m_InChangeFileList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("OutSyncDlg"),
647 (CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_COMPARETWO)|
648 CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_GNUDIFF2)),false);
651 //---------- Create Conflict List Ctrl -----------------
652 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP |LVS_SINGLESEL |WS_CHILD | WS_VISIBLE;
654 if( !m_ConflictFileList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_IN_CONFLICT))
656 TRACE0("Failed to create output change files window\n");
657 return FALSE; // fail to create
659 m_ctrlTabCtrl.InsertTab(&m_ConflictFileList,_T("Conflict"),-1);
661 m_ConflictFileList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("OutSyncDlg"),
662 (CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_COMPARETWO)|
663 CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_GNUDIFF2)|
664 GITSLC_POPCONFLICT|GITSLC_POPRESOLVE),false);
667 //---------- Create Commit Out List Ctrl---------------
669 dwStyle =LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | LVS_OWNERDATA | WS_BORDER | WS_TABSTOP | WS_CHILD | WS_VISIBLE;;
671 if( !m_OutLogList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_OUT_LOGLIST))
673 TRACE0("Failed to create output commits window\n");
674 return FALSE; // fail to create
678 m_ctrlTabCtrl.InsertTab(&m_OutLogList,_T("Out Commits"),-1);
680 m_OutLogList.m_ColumnRegKey = _T("SyncOut");
681 m_OutLogList.InsertGitColumn();
683 //------------- Create Change File List Control ----------------
685 dwStyle = LVS_REPORT | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP |LVS_SINGLESEL |WS_CHILD | WS_VISIBLE;
687 if( !m_OutChangeFileList.Create(dwStyle,rectDummy,&m_ctrlTabCtrl,IDC_OUT_CHANGELIST))
689 TRACE0("Failed to create output change files window\n");
690 return FALSE; // fail to create
692 m_ctrlTabCtrl.InsertTab(&m_OutChangeFileList,_T("Out ChangeList"),-1);
694 m_OutChangeFileList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS |GITSLC_COLADD|GITSLC_COLDEL , _T("OutSyncDlg"),
695 (CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_COMPARETWO)|
696 CGitStatusListCtrl::GetContextMenuBit(CGitStatusListCtrl::IDGITLC_GNUDIFF2)),false);
698 this->m_tooltips.Create(this);
700 AddAnchor(IDC_SYNC_TAB,TOP_LEFT,BOTTOM_RIGHT);
702 AddAnchor(IDC_GROUP_INFO,TOP_LEFT,TOP_RIGHT);
703 AddAnchor(IDC_COMBOBOXEX_URL,TOP_LEFT,TOP_RIGHT);
704 AddAnchor(IDC_BUTTON_MANAGE,TOP_RIGHT);
705 AddAnchor(IDC_BUTTON_PULL,BOTTOM_LEFT);
706 AddAnchor(IDC_BUTTON_PUSH,BOTTOM_LEFT);
707 AddAnchor(IDC_BUTTON_SUBMODULE,BOTTOM_LEFT);
708 AddAnchor(IDC_BUTTON_APPLY,BOTTOM_RIGHT);
709 AddAnchor(IDC_BUTTON_EMAIL,BOTTOM_RIGHT);
710 AddAnchor(IDC_PROGRESS_SYNC,TOP_LEFT,TOP_RIGHT);
711 AddAnchor(IDOK,BOTTOM_RIGHT);
712 AddAnchor(IDHELP,BOTTOM_RIGHT);
713 AddAnchor(IDC_STATIC_STATUS,BOTTOM_LEFT);
714 AddAnchor(IDC_ANIMATE_SYNC,TOP_LEFT);
715 AddAnchor(IDC_BUTTON_COMMIT,BOTTOM_LEFT);
717 BRANCH_COMBOX_ADD_ANCHOR();
719 this->GetDlgItem(IDC_BUTTON_COMMIT)->ShowWindow(SW_HIDE);
721 CString WorkingDir=g_Git.m_CurrentDir;
722 WorkingDir.Replace(_T(':'),_T('_'));
723 m_RegKeyRemoteBranch = CString(_T("Software\\TortoiseGit\\History\\SyncBranch\\"))+WorkingDir;
726 this->AddOthersToAnchor();
728 this->m_ctrlPush.AddEntry(CString(_T("Pus&h")));
729 this->m_ctrlPush.AddEntry(CString(_T("Push ta&gs")));
730 ///this->m_ctrlPush.AddEntry(CString(_T("Push All")));
732 this->m_ctrlPull.AddEntry(CString(_T("&Pull")));
733 this->m_ctrlPull.AddEntry(CString(_T("Fetc&h")));
734 this->m_ctrlPull.AddEntry(CString(_T("Fetch&&Re&base")));
735 this->m_ctrlPull.AddEntry(CString(_T("Remote Update")));
737 this->m_ctrlSubmodule.AddEntry(CString(_T("Submodule Update")));
738 this->m_ctrlSubmodule.AddEntry(CString(_T("Submodule Init")));
739 this->m_ctrlSubmodule.AddEntry(CString(_T("Submodule Sync")));
741 WorkingDir.Replace(_T(':'),_T('_'));
743 CString regkey ;
744 regkey.Format(_T("Software\\TortoiseGit\\TortoiseProc\\Sync\\%s"),WorkingDir);
746 this->m_regPullButton = CRegDWORD(regkey+_T("\\Pull"),0);
747 this->m_regPushButton = CRegDWORD(regkey+_T("\\Push"),0);
748 this->m_regSubmoduleButton = CRegDWORD(regkey+_T("\\Submodule"));
749 this->m_regAutoLoadPutty = CRegDWORD(regkey + _T("\\AutoLoadPutty"), CAppUtils::IsSSHPutty());
751 m_tooltips.Create(this);
752 this->UpdateData();
753 this->m_bAutoLoadPuttyKey = m_regAutoLoadPutty;
754 if(!CAppUtils::IsSSHPutty())
755 m_bAutoLoadPuttyKey = false;
756 this->UpdateData(FALSE);
758 this->m_ctrlPull.SetCurrentEntry(this->m_regPullButton);
759 this->m_ctrlPush.SetCurrentEntry(this->m_regPushButton);
760 this->m_ctrlSubmodule.SetCurrentEntry(this->m_regSubmoduleButton);
762 CString sWindowTitle;
763 GetWindowText(sWindowTitle);
764 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
766 EnableSaveRestore(_T("SyncDlg"));
768 this->m_ctrlURL.LoadHistory(CString(_T("Software\\TortoiseGit\\History\\SyncURL\\"))+WorkingDir, _T("url"));
770 STRING_VECTOR list;
772 if(!g_Git.GetRemoteList(list))
774 for(unsigned int i=0;i<list.size();i++)
776 m_ctrlURL.AddString(list[i]);
779 m_ctrlURL.SetCurSel(0);
780 m_ctrlRemoteBranch.SetCurSel(0);
781 m_ctrlURL.SetURLHistory(true);
783 this->LoadBranchInfo();
785 this->m_bInited=true;
786 FetchOutList();
788 m_ctrlTabCtrl.ShowTab(IDC_CMD_LOG-1,false);
789 m_ctrlTabCtrl.ShowTab(IDC_IN_LOGLIST-1,false);
790 m_ctrlTabCtrl.ShowTab(IDC_IN_CHANGELIST-1,false);
791 m_ctrlTabCtrl.ShowTab(IDC_IN_CONFLICT-1,false);
793 m_ctrlRemoteBranch.m_bWantReturn = TRUE;
794 m_ctrlURL.m_bWantReturn = TRUE;
796 this->m_Gitverion = CAppUtils::GetMsysgitVersion();
798 return TRUE; // return TRUE unless you set the focus to a control
799 // EXCEPTION: OCX Property Pages should return FALSE
802 void CSyncDlg::OnBnClickedButtonManage()
804 CAppUtils::LaunchRemoteSetting();
805 Refresh();
808 void CSyncDlg::Refresh()
810 theApp.DoWaitCursor(1);
812 CString local;
813 CString remote;
814 CString url;
815 this->m_ctrlLocalBranch.GetWindowText(local);
816 this->m_ctrlRemoteBranch.GetWindowText(remote);
817 this->m_ctrlURL.GetWindowText(url);
819 this->LoadBranchInfo();
821 this->m_ctrlLocalBranch.AddString(local);
822 this->m_ctrlRemoteBranch.AddString(remote);
823 this->m_ctrlURL.AddString(url);
825 m_OutLogList.ShowText(_T("Refresh ..."));
826 this->FetchOutList(true);
827 theApp.DoWaitCursor(-1);
830 BOOL CSyncDlg::PreTranslateMessage(MSG* pMsg)
832 if (pMsg->message == WM_KEYDOWN)
834 switch (pMsg->wParam)
837 case VK_F5:
839 if (m_bBlock)
840 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
841 Refresh();
843 break;
845 /* Avoid TAB control destroy but dialog exist*/
846 case VK_ESCAPE:
847 case VK_CANCEL:
849 TCHAR buff[128];
850 ::GetClassName(pMsg->hwnd,buff,128);
852 if(_tcsnicmp(buff,_T("RichEdit20W"),128)==0)
854 this->PostMessage(WM_KEYDOWN,VK_ESCAPE,0);
855 return TRUE;
860 m_tooltips.RelayEvent(pMsg);
861 return __super::PreTranslateMessage(pMsg);
863 void CSyncDlg::FetchOutList(bool force)
865 if(!m_bInited)
866 return;
867 m_OutChangeFileList.Clear();
868 this->m_OutLogList.Clear();
870 CString remote;
871 this->m_ctrlURL.GetWindowText(remote);
872 CString remotebranch;
873 this->m_ctrlRemoteBranch.GetWindowText(remotebranch);
874 remotebranch=remote+_T("/")+remotebranch;
876 if(IsURL())
878 CString str;
879 str=_T("Don't know what will push because you enter URL");
880 m_OutLogList.ShowText(str);
881 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,FALSE);
882 m_OutLocalBranch.Empty();
883 m_OutRemoteBranch.Empty();
885 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
886 return ;
889 else if(g_Git.GetHash(remotebranch).IsEmpty())
891 CString str;
892 str.Format(_T("Don't know what will push because unknown \"%s\""),remotebranch);
893 m_OutLogList.ShowText(str);
894 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,FALSE);
895 m_OutLocalBranch.Empty();
896 m_OutRemoteBranch.Empty();
898 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
899 return ;
901 else
903 CString localbranch;
904 localbranch=this->m_ctrlLocalBranch.GetString();
906 if(localbranch != m_OutLocalBranch || m_OutRemoteBranch != remotebranch || force)
908 m_OutLogList.ClearText();
909 m_OutLogList.FillGitLog(NULL,CGit:: LOG_INFO_STAT| CGit::LOG_INFO_FILESTATE | CGit::LOG_INFO_SHOW_MERGEDFILE,
910 &remotebranch,&localbranch);
912 CString str;
913 if(m_OutLogList.GetItemCount() == 0)
915 str.Format(_T("No commits ahead \"%s\""),remotebranch);
916 m_OutLogList.ShowText(str);
917 this->m_ctrlStatus.SetWindowText(str);
918 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,FALSE);
919 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(FALSE);
921 else
923 str.Format(_T("%d commits ahead \"%s\""),m_OutLogList.GetItemCount(),remotebranch);
924 this->m_ctrlStatus.SetWindowText(str);
926 AddDiffFileList(&m_OutChangeFileList,&m_arOutChangeList,localbranch,remotebranch);
928 this->m_ctrlTabCtrl.ShowTab(m_OutChangeFileList.GetDlgCtrlID()-1,TRUE);
929 this->GetDlgItem(IDC_BUTTON_EMAIL)->EnableWindow(TRUE);
932 this->m_OutLocalBranch=localbranch;
933 this->m_OutRemoteBranch=remotebranch;
938 bool CSyncDlg::IsURL()
940 CString str;
941 this->m_ctrlURL.GetWindowText(str);
942 if(str.Find(_T('\\'))>=0 || str.Find(_T('/'))>=0)
943 return true;
944 else
945 return false;
947 void CSyncDlg::OnCbnEditchangeComboboxex()
949 SetTimer(IDT_INPUT, 1000, NULL);
950 this->m_OutLogList.ShowText(_T("Wait for input"));
952 //this->FetchOutList();
955 UINT CSyncDlg::ProgressThread()
957 m_GitCmdStatus=CProgressDlg::RunCmdList(this,m_GitCmdList,true,NULL,&this->m_bAbort);
958 InterlockedExchange(&m_bBlock, FALSE);
959 return 0;
963 LRESULT CSyncDlg::OnProgressUpdateUI(WPARAM wParam,LPARAM lParam)
965 if(wParam == MSG_PROGRESSDLG_START)
967 m_ctrlAnimate.Play(0,-1,-1);
968 this->m_ctrlProgress.SetPos(0);
969 if (m_pTaskbarList)
971 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NORMAL);
972 m_pTaskbarList->SetProgressValue(m_hWnd, 0, 100);
976 if(wParam == MSG_PROGRESSDLG_END || wParam == MSG_PROGRESSDLG_FAILED)
978 //m_bDone = true;
979 m_ctrlAnimate.Stop();
980 m_ctrlProgress.SetPos(100);
981 //this->DialogEnableWindow(IDOK,TRUE);
982 if (m_pTaskbarList)
983 m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
985 //if(wParam == MSG_PROGRESSDLG_END)
986 if(this->m_CurrentCmd == GIT_COMMAND_PUSH )
988 if(!m_GitCmdStatus)
990 CTGitPathList list;
991 list.AddPath(CTGitPath(g_Git.m_CurrentDir));
992 DWORD exitcode;
993 CString error;
994 if (CHooks::Instance().PostPush(list,exitcode, error))
996 if (exitcode)
998 CString temp;
999 temp.Format(IDS_ERR_HOOKFAILED, (LPCTSTR)error);
1000 //ReportError(temp);
1001 CMessageBox::Show(NULL,temp,_T("TortoiseGit"),MB_OK|MB_ICONERROR);
1002 return false;
1007 EnableControlButton(true);
1008 SwitchToInput();
1009 this->FetchOutList(true);
1011 if(this->m_CurrentCmd == GIT_COMMAND_PULL )
1013 PullComplete();
1015 if(this->m_CurrentCmd == GIT_COMMAND_FETCH || this->m_CurrentCmd == GIT_COMMAND_FETCHANDREBASE)
1017 FetchComplete();
1019 if(this->m_CurrentCmd == GIT_COMMAND_SUBMODULE)
1021 //this->m_ctrlCmdOut.SetSel(-1,-1);
1022 //this->m_ctrlCmdOut.ReplaceSel(_T("Done\r\n"));
1023 //this->m_ctrlCmdOut.SetSel(-1,-1);
1024 EnableControlButton(true);
1025 SwitchToInput();
1027 if(this->m_CurrentCmd == GIT_COMMAND_REMOTE)
1029 this->FetchOutList(true);
1030 EnableControlButton(true);
1031 SwitchToInput();
1035 if(lParam != 0)
1036 ParserCmdOutput((char)lParam);
1038 return 0;
1042 void CSyncDlg::ParserCmdOutput(char ch)
1044 CProgressDlg::ParserCmdOutput(m_ctrlCmdOut,m_ctrlProgress,m_hWnd,m_pTaskbarList,m_LogText,ch);
1046 void CSyncDlg::OnBnClickedButtonCommit()
1048 CString proc=CPathUtils::GetAppDirectory();
1049 proc += _T("TortoiseProc.exe /command:commit");
1050 proc += _T(" /path:\"");
1051 proc += g_Git.m_CurrentDir;
1053 CAppUtils::LaunchApplication(proc,IDS_ERROR_CANNON_FIND_TORTOISEPROC,false);
1056 void CSyncDlg::OnOK()
1058 UpdateCombox();
1059 this->UpdateData();
1060 m_ctrlURL.SaveHistory();
1061 SaveHistory();
1062 m_regAutoLoadPutty = this->m_bAutoLoadPuttyKey;
1063 __super::OnOK();
1066 void CSyncDlg::OnBnClickedButtonSubmodule()
1068 this->UpdateData();
1069 UpdateCombox();
1071 this->m_regSubmoduleButton = this->m_ctrlSubmodule.GetCurrentEntry();
1073 this->SwitchToRun();
1075 this->m_bAbort=false;
1076 this->m_GitCmdList.clear();
1078 ShowTab(IDC_CMD_LOG);
1080 CString cmd;
1082 switch (m_ctrlSubmodule.GetCurrentEntry())
1084 case 0:
1085 cmd=_T("git.exe submodule update --init");
1086 break;
1087 case 1:
1088 cmd=_T("git.exe submodule init");
1089 break;
1090 case 2:
1091 cmd=_T("git.exe submodule sync");
1092 break;
1095 m_GitCmdList.push_back(cmd);
1097 m_CurrentCmd = GIT_COMMAND_SUBMODULE;
1099 m_pThread = AfxBeginThread(ProgressThreadEntry, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
1100 if (m_pThread==NULL)
1102 // ReportError(CString(MAKEINTRESOURCE(IDS_ERR_THREADSTARTFAILED)));
1104 else
1106 m_pThread->m_bAutoDelete = TRUE;
1107 m_pThread->ResumeThread();
1111 void CSyncDlg::OnTimer(UINT_PTR nIDEvent)
1113 if( nIDEvent == IDT_INPUT)
1115 KillTimer(IDT_INPUT);
1116 this->FetchOutList(true);
1120 LRESULT CSyncDlg::OnTaskbarBtnCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
1122 m_pTaskbarList.Release();
1123 m_pTaskbarList.CoCreateInstance(CLSID_TaskbarList);
1124 return 0;