Move logic to SysProgressDlg
[TortoiseGit.git] / src / TortoiseProc / GitLogListAction.cpp
blob1a65db9d92d17f6de74121662ea72da36dc7d32c
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - TortoiseGit
4 // Copyright (C) 2011-2012 - Sven Strickroth <email@cs-ware.de>
5 // Copyright (C) 2005-2007 Marco Costalba
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 // GitLogList.cpp : implementation file
23 #include "stdafx.h"
24 #include "TortoiseProc.h"
25 #include "GitLogList.h"
26 #include "GitRev.h"
27 //#include "VssStyle.h"
28 #include "IconMenu.h"
29 // CGitLogList
30 #include "cursor.h"
31 #include "InputDlg.h"
32 #include "GITProgressDlg.h"
33 #include "ProgressDlg.h"
34 #include "SysProgressDlg.h"
35 //#include "RepositoryBrowser.h"
36 //#include "CopyDlg.h"
37 //#include "StatGraphDlg.h"
38 #include "Logdlg.h"
39 #include "MessageBox.h"
40 #include "Registry.h"
41 #include "AppUtils.h"
42 #include "PathUtils.h"
43 #include "StringUtils.h"
44 #include "UnicodeUtils.h"
45 #include "TempFile.h"
46 //#include "GitInfo.h"
47 //#include "GitDiff.h"
48 //#include "RevisionRangeDlg.h"
49 //#include "BrowseFolder.h"
50 //#include "BlameDlg.h"
51 //#include "Blame.h"
52 //#include "GitHelpers.h"
53 #include "GitStatus.h"
54 //#include "LogDlgHelper.h"
55 //#include "CachedLogInfo.h"
56 //#include "RepositoryInfo.h"
57 //#include "EditPropertiesDlg.h"
58 #include "FileDiffDlg.h"
59 #include "CommitDlg.h"
60 #include "RebaseDlg.h"
61 #include "GitDiff.h"
62 #include "../TGitCache/CacheInterface.h"
64 IMPLEMENT_DYNAMIC(CGitLogList, CHintListCtrl)
66 int CGitLogList::RevertSelectedCommits(int parent)
68 CSysProgressDlg progress;
69 int ret = -1;
71 #if 0
72 if(!g_Git.CheckCleanWorkTree())
74 CMessageBox::Show(NULL, IDS_PROC_NOCLEAN, IDS_APPNAME, MB_OK);
77 #endif
79 if (progress.IsValid() && (this->GetSelectedCount() > 1) )
81 progress.SetTitle(CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_REVERTCOMMIT)));
82 progress.SetAnimation(IDR_MOVEANI);
83 progress.SetTime(true);
84 progress.ShowModeless(this);
87 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
89 POSITION pos = GetFirstSelectedItemPosition();
90 int i=0;
91 while(pos)
93 int index = GetNextSelectedItem(pos);
94 GitRev * r1 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(index));
96 if (progress.IsValid() && (this->GetSelectedCount() > 1) )
98 progress.FormatNonPathLine(1, IDS_PROC_REVERTCOMMIT, r1->m_CommitHash.ToString());
99 progress.FormatNonPathLine(2, _T("%s"), r1->GetSubject());
100 progress.SetProgress(i, this->GetSelectedCount());
102 i++;
104 if(r1->m_CommitHash.IsEmpty())
105 continue;
107 CString cmd, output, merge;
108 if (parent)
109 merge.Format(_T("-m %d "), parent);
110 cmd.Format(_T("git.exe revert --no-edit --no-commit %s%s"), merge, r1->m_CommitHash.ToString());
111 if (g_Git.Run(cmd, &output, CP_UTF8))
113 CString str;
114 str.LoadString(IDS_SVNACTION_FAILEDREVERT);
115 str += _T("\n");
116 str+= cmd;
117 str+= _T("\n")+output;
118 if( GetSelectedCount() == 1)
119 CMessageBox::Show(NULL, str, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
120 else
122 if(CMessageBox::Show(NULL, str, _T("TortoiseGit"),2 , IDI_ERROR, CString(MAKEINTRESOURCE(IDS_SKIPBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 2)
124 return ret;
128 else
130 ret =0;
133 if ((progress.IsValid())&&(progress.HasUserCancelled()))
134 break;
136 return ret;
138 int CGitLogList::CherryPickFrom(CString from, CString to)
140 CLogDataVector logs(&m_LogCache);
141 if(logs.ParserFromLog(NULL,-1,0,&from,&to))
142 return -1;
144 if (logs.empty())
145 return 0;
147 CSysProgressDlg progress;
148 if (progress.IsValid())
150 progress.SetTitle(CString(MAKEINTRESOURCE(IDS_PROGS_TITLE_CHERRYPICK)));
151 progress.SetAnimation(IDR_MOVEANI);
152 progress.SetTime(true);
153 progress.ShowModeless(this);
156 CBlockCacheForPath cacheBlock(g_Git.m_CurrentDir);
158 for (int i = (int)logs.size() - 1; i >= 0; i--)
160 if (progress.IsValid())
162 CString temp;
163 temp.Format(IDS_PROC_PICK, logs.GetGitRevAt(i).m_CommitHash.ToString());
164 progress.FormatPathLine(1, temp);
165 progress.FormatPathLine(2, _T("%s"), logs.GetGitRevAt(i).GetSubject());
166 progress.SetProgress(logs.size()-i, logs.size());
168 if ((progress.IsValid())&&(progress.HasUserCancelled()))
170 throw std::exception(CUnicodeUtils::GetUTF8(CString(MAKEINTRESOURCE(IDS_SVN_USERCANCELLED))));
171 return -1;
173 CString cmd,out;
174 cmd.Format(_T("git.exe cherry-pick %s"),logs.GetGitRevAt(i).m_CommitHash.ToString());
175 out.Empty();
176 if(g_Git.Run(cmd,&out,CP_UTF8))
178 throw std::exception(CUnicodeUtils::GetUTF8(CString(MAKEINTRESOURCE(IDS_PROC_CHERRYPICKFAILED)) + _T(":\r\n\r\n") + out));
179 return -1;
183 return 0;
186 void CGitLogList::ContextMenuAction(int cmd,int FirstSelect, int LastSelect, CMenu *popmenu)
188 POSITION pos = GetFirstSelectedItemPosition();
189 int indexNext = GetNextSelectedItem(pos);
190 if (indexNext < 0)
191 return;
193 GitRev* pSelLogEntry = reinterpret_cast<GitRev*>(m_arShownList.GetAt(indexNext));
195 theApp.DoWaitCursor(1);
196 switch (cmd&0xFFFF)
198 case ID_COMMIT:
200 CTGitPathList pathlist;
201 CTGitPathList selectedlist;
202 pathlist.AddPath(this->m_Path);
203 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
204 CString str;
205 CAppUtils::Commit(CString(),false,str,
206 pathlist,selectedlist,bSelectFilesForCommit);
207 //this->Refresh();
208 this->GetParent()->PostMessage(WM_COMMAND,ID_LOGDLG_REFRESH,0);
210 break;
211 case ID_GNUDIFF1: // compare with WC, unified
213 CString tempfile=GetTempFile();
214 CString command;
215 GitRev * r1 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
216 if(!r1->m_CommitHash.IsEmpty())
218 CString merge;
219 CString hash2;
220 cmd >>= 16;
221 if( (cmd&0xFFFF) == 0xFFFF)
223 merge=_T("-m");
225 else if((cmd&0xFFFF) == 0xFFFE)
227 merge=_T("-c");
229 else
231 if(cmd > r1->m_ParentHash.size())
233 CString str;
234 str.Format(IDS_PROC_NOPARENT, cmd);
235 MessageBox(str, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
236 return;
238 else
240 if(cmd>0)
241 hash2 = r1->m_ParentHash[cmd-1].ToString();
244 command.Format(_T("git.exe diff-tree %s -r -p --stat %s %s"), merge, hash2, r1->m_CommitHash.ToString());
246 else
247 command.Format(_T("git.exe diff -r -p --stat"));
249 g_Git.RunLogFile(command,tempfile);
250 CAppUtils::StartUnifiedDiffViewer(tempfile, r1->m_CommitHash.ToString().Left(g_Git.GetShortHASHLength()) + _T(":") + r1->GetSubject());
252 break;
254 case ID_GNUDIFF2: // compare two revisions, unified
256 CString tempfile=GetTempFile();
257 CString cmd;
258 GitRev * r1 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
259 GitRev * r2 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(LastSelect));
261 if( r1->m_CommitHash.IsEmpty()) {
262 cmd.Format(_T("git.exe diff -r -p --stat %s"),r2->m_CommitHash.ToString());
264 else if( r2->m_CommitHash.IsEmpty()) {
265 cmd.Format(_T("git.exe diff -r -p --stat %s"),r1->m_CommitHash.ToString());
267 else
269 cmd.Format(_T("git.exe diff-tree -r -p --stat %s %s"),r2->m_CommitHash.ToString(),r1->m_CommitHash.ToString());
272 g_Git.RunLogFile(cmd,tempfile);
273 CAppUtils::StartUnifiedDiffViewer(tempfile, r2->m_CommitHash.ToString().Left(g_Git.GetShortHASHLength()) + _T(":") + r1->m_CommitHash.ToString().Left(g_Git.GetShortHASHLength()));
276 break;
278 case ID_COMPARETWO: // compare two revisions
280 GitRev * r1 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
281 GitRev * r2 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(LastSelect));
282 if (m_Path.IsDirectory() || !(m_ShowMask & CGit::LOG_INFO_FOLLOW))
283 CGitDiff::DiffCommit(this->m_Path, r1,r2);
284 else
286 CString path1 = m_Path.GetGitPathString();
287 // start with 1 (0 = working copy changes)
288 for (int i = 1; i < FirstSelect; i++)
290 GitRev * first = reinterpret_cast<GitRev*>(m_arShownList.GetAt(i));
291 CTGitPathList list = first->GetFiles(NULL);
292 CTGitPath * file = list.LookForGitPath(path1);
293 if (file && !file->GetGitOldPathString().IsEmpty())
294 path1 = file->GetGitOldPathString();
296 CString path2 = path1;
297 for (int i = FirstSelect; i < LastSelect; i++)
299 GitRev * first = reinterpret_cast<GitRev*>(m_arShownList.GetAt(i));
300 CTGitPathList list = first->GetFiles(NULL);
301 CTGitPath * file = list.LookForGitPath(path2);
302 if (file && !file->GetGitOldPathString().IsEmpty())
303 path2 = file->GetGitOldPathString();
305 CGitDiff::DiffCommit(CTGitPath(path1), CTGitPath(path2), r1, r2);
309 break;
311 case ID_COMPARE: // compare revision with WC
313 GitRev * r1 = &m_wcRev;
314 GitRev * r2 = pSelLogEntry;
316 if (m_Path.IsDirectory() || !(m_ShowMask & CGit::LOG_INFO_FOLLOW))
317 CGitDiff::DiffCommit(this->m_Path, r1,r2);
318 else
320 CString path1 = m_Path.GetGitPathString();
321 // start with 1 (0 = working copy changes)
322 for (int i = 1; i < FirstSelect; i++)
324 GitRev * first = reinterpret_cast<GitRev*>(m_arShownList.GetAt(i));
325 CTGitPathList list = first->GetFiles(NULL);
326 CTGitPath * file = list.LookForGitPath(path1);
327 if (file && !file->GetGitOldPathString().IsEmpty())
328 path1 = file->GetGitOldPathString();
330 CGitDiff::DiffCommit(m_Path, CTGitPath(path1), r1, r2);
333 //user clicked on the menu item "compare with working copy"
334 //if (PromptShown())
336 // GitDiff diff(this, m_hWnd, true);
337 // diff.SetAlternativeTool(!!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
338 // diff.SetHEADPeg(m_LogRevision);
339 // diff.ShowCompare(m_path, GitRev::REV_WC, m_path, revSelected);
341 //else
342 // CAppUtils::StartShowCompare(m_hWnd, m_path, GitRev::REV_WC, m_path, revSelected, GitRev(), m_LogRevision, !!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
344 break;
346 case ID_COMPAREWITHPREVIOUS:
348 CFileDiffDlg dlg;
350 if (!pSelLogEntry->m_ParentHash.empty())
351 //if(m_logEntries.m_HashMap[pSelLogEntry->m_ParentHash[0]]>=0)
353 cmd>>=16;
354 cmd&=0xFFFF;
356 if(cmd == 0)
357 cmd=1;
359 if (m_Path.IsDirectory() || !(m_ShowMask & CGit::LOG_INFO_FOLLOW))
360 CGitDiff::DiffCommit(m_Path, pSelLogEntry->m_CommitHash.ToString(), pSelLogEntry->m_ParentHash[cmd - 1].ToString());
361 else
363 CString path1 = m_Path.GetGitPathString();
364 // start with 1 (0 = working copy changes)
365 for (int i = 1; i < indexNext; i++)
367 GitRev * first = reinterpret_cast<GitRev*>(m_arShownList.GetAt(i));
368 CTGitPathList list = first->GetFiles(NULL);
369 CTGitPath * file = list.LookForGitPath(path1);
370 if (file && !file->GetGitOldPathString().IsEmpty())
371 path1 = file->GetGitOldPathString();
373 CString path2 = path1;
374 GitRev * first = reinterpret_cast<GitRev*>(m_arShownList.GetAt(indexNext));
375 CTGitPathList list = first->GetFiles(NULL);
376 CTGitPath * file = list.LookForGitPath(path2);
377 if (file && !file->GetGitOldPathString().IsEmpty())
378 path2 = file->GetGitOldPathString();
380 CGitDiff::DiffCommit(CTGitPath(path1), CTGitPath(path2), pSelLogEntry->m_CommitHash.ToString(), pSelLogEntry->m_ParentHash[cmd - 1].ToString());
383 else
385 CMessageBox::Show(NULL, IDS_PROC_NOPREVIOUSVERSION, IDS_APPNAME, MB_OK);
387 //if (PromptShown())
389 // GitDiff diff(this, m_hWnd, true);
390 // diff.SetAlternativeTool(!!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
391 // diff.SetHEADPeg(m_LogRevision);
392 // diff.ShowCompare(CTGitPath(pathURL), revPrevious, CTGitPath(pathURL), revSelected);
394 //else
395 // CAppUtils::StartShowCompare(m_hWnd, CTGitPath(pathURL), revPrevious, CTGitPath(pathURL), revSelected, GitRev(), m_LogRevision, !!(GetAsyncKeyState(VK_SHIFT) & 0x8000));
397 break;
398 case ID_COPYCLIPBOARD:
400 CopySelectionToClipBoard();
402 break;
403 case ID_COPYCLIPBOARDMESSAGES:
405 if ((GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0)
406 CopySelectionToClipBoard(ID_COPY_SUBJECT);
407 else
408 CopySelectionToClipBoard(ID_COPY_MESSAGE);
410 break;
411 case ID_COPYHASH:
413 CopySelectionToClipBoard(ID_COPY_HASH);
415 break;
416 case ID_EXPORT:
418 CString str=pSelLogEntry->m_CommitHash.ToString();
419 CAppUtils::Export(&str);
421 break;
422 case ID_CREATE_BRANCH:
423 case ID_CREATE_TAG:
425 CString str = pSelLogEntry->m_CommitHash.ToString();
426 // try to guess remote branch in order to enable tracking
427 for (int i = 0; i < m_HashMap[pSelLogEntry->m_CommitHash].size(); i++)
429 if (m_HashMap[pSelLogEntry->m_CommitHash][i].Find(_T("refs/remotes/")) == 0)
431 str = m_HashMap[pSelLogEntry->m_CommitHash][i];
432 break;
435 CAppUtils::CreateBranchTag((cmd&0xFFFF) == ID_CREATE_TAG, &str);
436 ReloadHashMap();
437 Invalidate();
438 ::PostMessage(this->GetParent()->m_hWnd,MSG_REFLOG_CHANGED,0,0);
440 break;
441 case ID_SWITCHTOREV:
443 CString str = pSelLogEntry->m_CommitHash.ToString();
444 // try to guess remote branch in order to recommend good branch name and tracking
445 for (int i = 0; i < m_HashMap[pSelLogEntry->m_CommitHash].size(); i++)
447 if (m_HashMap[pSelLogEntry->m_CommitHash][i].Find(_T("refs/remotes/")) == 0)
449 str = m_HashMap[pSelLogEntry->m_CommitHash][i];
450 break;
453 CAppUtils::Switch(str);
455 ReloadHashMap();
456 Invalidate();
457 ::PostMessage(this->GetParent()->m_hWnd,MSG_REFLOG_CHANGED,0,0);
458 break;
459 case ID_SWITCHBRANCH:
460 if(popmenu)
462 CString *branch = (CString*)((CIconMenu*)popmenu)->GetMenuItemData(cmd);
463 if(branch)
465 CString name;
466 if(branch->Find(_T("refs/heads/")) ==0 )
467 name = branch->Mid(11);
468 else
469 name = *branch;
471 CAppUtils::PerformSwitch(name);
473 ReloadHashMap();
474 Invalidate();
475 ::PostMessage(this->GetParent()->m_hWnd,MSG_REFLOG_CHANGED,0,0);
477 break;
478 case ID_RESET:
480 CString str = pSelLogEntry->m_CommitHash.ToString();
481 if (CAppUtils::GitReset(&str))
483 ResetWcRev(true);
484 ReloadHashMap();
485 Invalidate();
488 break;
489 case ID_REBASE_PICK:
490 SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_PICK);
491 break;
492 case ID_REBASE_EDIT:
493 SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_EDIT);
494 break;
495 case ID_REBASE_SQUASH:
496 SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SQUASH);
497 break;
498 case ID_REBASE_SKIP:
499 SetSelectedAction(CTGitPath::LOGACTIONS_REBASE_SKIP);
500 break;
501 case ID_COMBINE_COMMIT:
503 CString head;
504 CGitHash headhash;
505 CGitHash hashFirst,hashLast;
507 int headindex=GetHeadIndex();
508 if(headindex>=0) //incase show all branch, head is not the first commits.
510 head.Format(_T("HEAD~%d"),FirstSelect-headindex);
511 hashFirst=g_Git.GetHash(head);
513 head.Format(_T("HEAD~%d"),LastSelect-headindex);
514 hashLast=g_Git.GetHash(head);
517 GitRev* pFirstEntry = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
518 GitRev* pLastEntry = reinterpret_cast<GitRev*>(m_arShownList.GetAt(LastSelect));
519 if(pFirstEntry->m_CommitHash != hashFirst || pLastEntry->m_CommitHash != hashLast)
521 CMessageBox::Show(NULL, IDS_PROC_CANNOTCOMBINE, IDS_APPNAME, MB_OK);
522 break;
525 GitRev lastRevision;
528 lastRevision.GetParentFromHash(hashLast);
530 catch (char* msg)
532 CString err(msg);
533 MessageBox(_T("Could not get parent(s) of ") + hashLast.ToString() + _T(".\nlibgit reports:\n") + err, _T("TortoiseGit"), MB_ICONERROR);
534 break;
537 headhash=g_Git.GetHash(_T("HEAD"));
539 if(!g_Git.CheckCleanWorkTree())
541 CMessageBox::Show(NULL, IDS_PROC_NOCLEAN, IDS_APPNAME, MB_OK);
542 break;
544 CString cmd,out;
546 //Use throw to abort this process (reset back to original HEAD)
549 cmd.Format(_T("git.exe reset --hard %s"),pFirstEntry->m_CommitHash.ToString());
550 if(g_Git.Run(cmd,&out,CP_UTF8))
552 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
553 throw std::exception(CUnicodeUtils::GetUTF8(CString(MAKEINTRESOURCE(IDS_PROC_COMBINE_ERRORSTEP1)) + _T("\r\n\r\n") + out));
555 cmd.Format(_T("git.exe reset --mixed %s"),hashLast.ToString());
556 if(g_Git.Run(cmd,&out,CP_UTF8))
558 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
559 throw std::exception(CUnicodeUtils::GetUTF8(CString(MAKEINTRESOURCE(IDS_PROC_COMBINE_ERRORSTEP2)) + _T("\r\n\r\n")+out));
562 CTGitPathList PathList;
563 /* don't why must add --stat to get action status*/
564 /* first -z will be omitted by gitdll*/
565 if(g_Git.GetDiffPath(&PathList,&pFirstEntry->m_CommitHash,&hashLast,"-z --stat -r"))
567 CMessageBox::Show(NULL,_T("Get Diff file list error"),_T("TortoiseGit"),MB_OK);
568 throw std::exception(CUnicodeUtils::GetUTF8(_T("Could not get changed file list aborting...\r\n\r\n")+out));
571 for(int i=0;i<PathList.GetCount();i++)
573 if(PathList[i].m_Action & CTGitPath::LOGACTIONS_ADDED)
575 cmd.Format(_T("git.exe add -- \"%s\""), PathList[i].GetGitPathString());
576 if (g_Git.Run(cmd, &out, CP_UTF8))
578 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
579 throw std::exception(CUnicodeUtils::GetUTF8(_T("Could not add new file aborting...\r\n\r\n")+out));
583 if(PathList[i].m_Action & CTGitPath::LOGACTIONS_DELETED)
585 cmd.Format(_T("git.exe rm -- \"%s\""), PathList[i].GetGitPathString());
586 if (g_Git.Run(cmd, &out, CP_UTF8))
588 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
589 throw std::exception(CUnicodeUtils::GetUTF8(_T("Could not rm file aborting...\r\n\r\n")+out));
594 CCommitDlg dlg;
595 for(int i=FirstSelect;i<=LastSelect;i++)
597 GitRev* pRev = reinterpret_cast<GitRev*>(m_arShownList.GetAt(i));
598 dlg.m_sLogMessage+=pRev->GetSubject()+_T("\n")+pRev->GetBody();
599 dlg.m_sLogMessage+=_T("\n");
601 dlg.m_bWholeProject=true;
602 dlg.m_bSelectFilesForCommit = true;
603 dlg.m_bForceCommitAmend=true;
604 if (lastRevision.ParentsCount() != 1)
606 CMessageBox::Show(NULL, _T("The following commit dialog can only show changes of oldest commit if it has exactly one parent. This is not the case right now."), _T("TortoiseGit"),MB_OK);
607 dlg.m_bAmendDiffToLastCommit = TRUE;
609 else
610 dlg.m_bAmendDiffToLastCommit = FALSE;
611 dlg.m_bNoPostActions=true;
612 dlg.m_AmendStr=dlg.m_sLogMessage;
614 if (dlg.DoModal() == IDOK)
616 if(pFirstEntry->m_CommitHash!=headhash)
618 //Commitrange firstEntry..headhash (from top of combine to original head) needs to be 'cherry-picked'
619 //on top of new commit.
620 //Use the rebase --onto command for it.
622 //All this can be done in one step using the following command:
623 //cmd.Format(_T("git.exe format-patch --stdout --binary --full-index -k %s..%s | git am -k -3"),
624 // pFirstEntry->m_CommitHash,
625 // headhash);
626 //But I am not sure if a '|' is going to work in a CreateProcess() call.
628 //Later the progress dialog could be used to execute these steps.
630 if(CherryPickFrom(pFirstEntry->m_CommitHash.ToString(),headhash))
632 CString msg;
633 msg.Format(_T("Error while cherry pick commits on top of combined commits. Aborting.\r\n\r\n"));
634 throw std::exception(CUnicodeUtils::GetUTF8(msg));
636 #if 0
637 CString currentBranch=g_Git.GetCurrentBranch();
638 cmd.Format(_T("git.exe rebase --onto \"%s\" %s %s"),
639 currentBranch,
640 pFirstEntry->m_CommitHash,
641 headhash);
642 if(g_Git.Run(cmd,&out,CP_UTF8)!=0)
644 CString msg;
645 msg.Format(_T("Error while rebasing commits on top of combined commits. Aborting.\r\n\r\n%s"),out);
646 // CMessageBox::Show(NULL,msg,_T("TortoiseGit"),MB_OK);
647 g_Git.Run(_T("git.exe rebase --abort"),&out,CP_UTF8);
648 throw std::exception(CUnicodeUtils::GetUTF8(msg));
651 //HEAD is now on <no branch>.
652 //The following steps are to get HEAD back on the original branch and reset the branch to the new HEAD
653 //To avoid 2 working copy changes, we could use git branch -f <original branch> <hash new head>
654 //And then git checkout <original branch>
655 //But I don't know if 'git branch -f' removes tracking options. So for now, do a checkout and a reset.
657 //Store new HEAD
658 CString newHead=g_Git.GetHash(CString(_T("HEAD")));
660 //Checkout working branch
661 cmd.Format(_T("git.exe checkout -f \"%s\""),currentBranch);
662 if(g_Git.Run(cmd,&out,CP_UTF8))
663 throw std::exception(CUnicodeUtils::GetUTF8(_T("Could not checkout original branch. Aborting...\r\n\r\n")+out));
665 //Reset to new HEAD
666 cmd.Format(_T("git.exe reset --hard %s"),newHead);
667 if(g_Git.Run(cmd,&out,CP_UTF8))
668 throw std::exception(CUnicodeUtils::GetUTF8(_T("Could not reset to new head. Aborting...\r\n\r\n")+out));
669 #endif
672 else
673 throw std::exception(CUnicodeUtils::GetUTF8(CString(MAKEINTRESOURCE(IDS_SVN_USERCANCELLED))));
675 catch(std::exception& e)
677 CMessageBox::Show(NULL, CUnicodeUtils::GetUnicode(CStringA(e.what())), _T("TortoiseGit"), MB_OK | MB_ICONERROR);
678 cmd.Format(_T("git.exe reset --hard %s"),headhash.ToString());
679 out.Empty();
680 if(g_Git.Run(cmd,&out,CP_UTF8))
682 CMessageBox::Show(NULL, CString(MAKEINTRESOURCE(IDS_PROC_COMBINE_ERRORRESETHEAD)) + _T("\r\n\r\n") + out, _T("TortoiseGit"), MB_OK);
685 Refresh();
687 break;
689 case ID_CHERRY_PICK:
690 if(!g_Git.CheckCleanWorkTree())
692 CMessageBox::Show(NULL, IDS_PROC_NOCLEAN, IDS_APPNAME, MB_OK);
695 else
697 CRebaseDlg dlg;
698 dlg.m_IsCherryPick = TRUE;
699 dlg.m_Upstream = this->m_CurrentBranch;
700 POSITION pos = GetFirstSelectedItemPosition();
701 while(pos)
703 int indexNext = GetNextSelectedItem(pos);
704 dlg.m_CommitList.m_logEntries.push_back( ((GitRev*)m_arShownList[indexNext])->m_CommitHash );
705 dlg.m_CommitList.m_LogCache.m_HashMap[((GitRev*)m_arShownList[indexNext])->m_CommitHash]=*(GitRev*)m_arShownList[indexNext];
706 dlg.m_CommitList.m_logEntries.GetGitRevAt(dlg.m_CommitList.m_logEntries.size()-1).GetAction(this) |= CTGitPath::LOGACTIONS_REBASE_PICK;
709 if(dlg.DoModal() == IDOK)
711 Refresh();
714 break;
715 case ID_REBASE_TO_VERSION:
716 if(!g_Git.CheckCleanWorkTree())
718 CMessageBox::Show(NULL, IDS_PROC_NOCLEAN, IDS_APPNAME, MB_OK);
721 else
723 CRebaseDlg dlg;
724 dlg.m_Upstream = pSelLogEntry->m_CommitHash;
726 if(dlg.DoModal() == IDOK)
728 Refresh();
732 break;
734 case ID_STASH_SAVE:
735 if (CAppUtils::StashSave())
736 Refresh();
737 break;
739 case ID_STASH_POP:
740 if (CAppUtils::StashPop())
741 Refresh();
742 break;
744 case ID_STASH_LIST:
745 CAppUtils::RunTortoiseProc(_T("/command:reflog /ref:refs/stash"));
746 break;
748 case ID_REFLOG_STASH_APPLY:
749 if (CAppUtils::StashApply(pSelLogEntry->m_Ref))
750 Refresh();
751 break;
753 case ID_REFLOG_DEL:
755 CString str;
756 if (GetSelectedCount() > 1)
757 str.Format(IDS_PROC_DELETENREFS, GetSelectedCount());
758 else
759 str.Format(IDS_PROC_DELETEREF, pSelLogEntry->m_Ref);
761 if (CMessageBox::Show(NULL, str, _T("TortoiseGit"), 1, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 2)
762 return;
764 POSITION pos = GetFirstSelectedItemPosition();
765 while (pos)
767 CString ref = ((GitRev *)m_arShownList[GetNextSelectedItem(pos)])->m_Ref;
768 if (ref.Find(_T("refs/")) == 0)
769 ref = ref.Mid(5);
770 int refpos = ref.ReverseFind('{');
771 if (refpos > 0 && ref.Mid(refpos, 2) != _T("@{"))
772 ref = ref.Left(refpos) + _T("@")+ ref.Mid(refpos);
774 CString cmd, out;
775 if (ref.Find(_T("stash")) == 0)
776 cmd.Format(_T("git.exe stash drop %s"), ref);
777 else
778 cmd.Format(_T("git.exe reflog delete %s"), ref);
780 if (g_Git.Run(cmd, &out, CP_UTF8))
781 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
783 ::PostMessage(this->GetParent()->m_hWnd,MSG_REFLOG_CHANGED,0,0);
786 break;
787 case ID_LOG:
789 CString cmd = _T("/command:log");
790 cmd += _T(" /path:\"")+g_Git.m_CurrentDir+_T("\" ");
791 GitRev * r1 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
792 cmd += _T(" /endrev:")+r1->m_CommitHash.ToString();
793 CAppUtils::RunTortoiseProc(cmd);
795 break;
796 case ID_CREATE_PATCH:
798 int select=this->GetSelectedCount();
799 CString cmd = _T("/command:formatpatch");
800 cmd += _T(" /path:\"")+g_Git.m_CurrentDir+_T("\" ");
802 GitRev * r1 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
803 GitRev * r2 = NULL;
804 if(select == 1)
806 cmd += _T(" /startrev:")+r1->m_CommitHash.ToString();
808 else
810 r2 = reinterpret_cast<GitRev*>(m_arShownList.GetAt(LastSelect));
811 if( this->m_IsOldFirst )
813 cmd += _T(" /startrev:")+r1->m_CommitHash.ToString()+_T("~1");
814 cmd += _T(" /endrev:")+r2->m_CommitHash.ToString();
817 else
819 cmd += _T(" /startrev:")+r2->m_CommitHash.ToString()+_T("~1");
820 cmd += _T(" /endrev:")+r1->m_CommitHash.ToString();
825 CAppUtils::RunTortoiseProc(cmd);
827 break;
828 case ID_BISECTSTART:
830 GitRev * first = reinterpret_cast<GitRev*>(m_arShownList.GetAt(FirstSelect));
831 GitRev * last = reinterpret_cast<GitRev*>(m_arShownList.GetAt(LastSelect));
832 ASSERT(first != NULL && last != NULL);
834 CString firstBad = first->m_CommitHash.ToString();
835 if (!m_HashMap[first->m_CommitHash].empty())
836 firstBad = m_HashMap[first->m_CommitHash].at(0);
837 CString lastGood = last->m_CommitHash.ToString();
838 if (!m_HashMap[last->m_CommitHash].empty())
839 lastGood = m_HashMap[last->m_CommitHash].at(0);
841 if (CAppUtils::BisectStart(lastGood, firstBad))
842 Refresh();
844 break;
845 case ID_REPOBROWSE:
847 CString sCmd;
848 sCmd.Format(_T("/command:repobrowser /path:\"%s\" /rev:%s"), g_Git.m_CurrentDir, pSelLogEntry->m_CommitHash.ToString());
849 CAppUtils::RunTortoiseProc(sCmd);
851 break;
852 case ID_PUSH:
854 CString guessAssociatedBranch;
855 if (!m_HashMap[pSelLogEntry->m_CommitHash].empty())
856 guessAssociatedBranch = m_HashMap[pSelLogEntry->m_CommitHash].at(0);
857 if (CAppUtils::Push(guessAssociatedBranch))
858 Refresh();
860 break;
861 case ID_FETCH:
863 if (CAppUtils::Fetch(_T(""), true))
864 Refresh();
866 break;
867 case ID_SHOWBRANCHES:
869 CString cmd;
870 cmd.Format(_T("git.exe branch -a --contains %s"), pSelLogEntry->m_CommitHash.ToString());
871 CProgressDlg progress;
872 progress.m_GitCmd = cmd;
873 progress.DoModal();
875 break;
876 case ID_DELETE:
878 CString *branch = (CString*)((CIconMenu*)popmenu)->GetMenuItemData(cmd);
879 if (!branch)
881 CMessageBox::Show(NULL,IDS_ERROR_NOREF,IDS_APPNAME,MB_OK|MB_ICONERROR);
882 return;
884 CString shortname;
885 CString cmd;
886 if (this->GetShortName(*branch, shortname, _T("refs/remotes/")))
888 CString msg;
889 msg.Format(IDS_PROC_DELETEREMOTEBRANCH, *branch);
890 int result = CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 3, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_PROC_DELETEREMOTEBRANCH_LOCALREMOTE)), CString(MAKEINTRESOURCE(IDS_PROC_DELETEREMOTEBRANCH_LOCAL)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON)));
891 if (result == 1)
893 CString remoteName = shortname.Left(shortname.Find('/'));
894 shortname = shortname.Mid(shortname.Find('/') + 1);
895 if(CAppUtils::IsSSHPutty())
896 CAppUtils::LaunchPAgent(NULL, &remoteName);
898 cmd.Format(L"git.exe push \"%s\" :refs/heads/%s", remoteName, shortname);
900 else if (result == 2)
901 cmd.Format(_T("git.exe branch -r -D -- %s"), shortname);
902 else
903 return;
905 else if (this->GetShortName(*branch, shortname, _T("refs/stash")))
907 if (CMessageBox::Show(NULL, IDS_PROC_DELETEALLSTASH, IDS_APPNAME, 2, IDI_QUESTION, IDS_DELETEBUTTON, IDS_ABORTBUTTON) == 1)
908 cmd.Format(_T("git.exe stash clear"));
909 else
910 return;
912 else
914 CString msg;
915 msg.Format(IDS_PROC_DELETEBRANCHTAG, *branch);
916 if (CMessageBox::Show(NULL, msg, _T("TortoiseGit"), 2, IDI_QUESTION, CString(MAKEINTRESOURCE(IDS_DELETEBUTTON)), CString(MAKEINTRESOURCE(IDS_ABORTBUTTON))) == 1)
918 if(this->GetShortName(*branch,shortname,_T("refs/heads/")))
920 cmd.Format(_T("git.exe branch -D -- %s"),shortname);
923 if(this->GetShortName(*branch,shortname,_T("refs/tags/")))
925 cmd.Format(_T("git.exe tag -d -- %s"),shortname);
929 if (!cmd.IsEmpty())
931 CString out;
932 if(g_Git.Run(cmd,&out,CP_UTF8))
934 CMessageBox::Show(NULL,out,_T("TortoiseGit"),MB_OK);
936 this->ReloadHashMap();
937 CRect rect;
938 this->GetItemRect(FirstSelect,&rect,LVIR_BOUNDS);
939 this->InvalidateRect(rect);
942 break;
944 case ID_FINDENTRY:
946 m_nSearchIndex = GetSelectionMark();
947 if (m_nSearchIndex < 0)
948 m_nSearchIndex = 0;
949 if (m_pFindDialog)
951 break;
953 else
955 m_pFindDialog = new CFindDlg();
956 m_pFindDialog->Create(this);
959 break;
960 case ID_MERGEREV:
962 CString str = pSelLogEntry->m_CommitHash.ToString();
963 if (!m_HashMap[pSelLogEntry->m_CommitHash].empty())
964 str = m_HashMap[pSelLogEntry->m_CommitHash].at(0);
965 // we need an URL to complete this command, so error out if we can't get an URL
966 if(CAppUtils::Merge(&str))
968 this->Refresh();
971 break;
972 case ID_REVERTREV:
974 int parent = 0;
975 if (GetSelectedCount() == 1)
977 parent = cmd >> 16;
978 if (parent > pSelLogEntry->m_ParentHash.size())
980 CString str;
981 str.Format(IDS_PROC_NOPARENT, parent);
982 MessageBox(str, _T("TortoiseGit"), MB_OK | MB_ICONERROR);
983 return;
987 if (!this->RevertSelectedCommits(parent))
989 if (CMessageBox::Show(m_hWnd, IDS_REVREVERTED, IDS_APPNAME, 1, IDI_QUESTION, IDS_OKBUTTON, IDS_COMMITBUTTON) == 2)
991 CTGitPathList pathlist;
992 CTGitPathList selectedlist;
993 pathlist.AddPath(this->m_Path);
994 bool bSelectFilesForCommit = !!DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\SelectFilesForCommit"), TRUE));
995 CString str;
996 CAppUtils::Commit(CString(), false, str, pathlist, selectedlist, bSelectFilesForCommit);
998 this->Refresh();
1001 break;
1002 case ID_EDITNOTE:
1004 CAppUtils::EditNote(pSelLogEntry);
1005 this->SetItemState(FirstSelect, 0, LVIS_SELECTED);
1006 this->SetItemState(FirstSelect, LVIS_SELECTED, LVIS_SELECTED);
1008 break;
1009 default:
1010 //CMessageBox::Show(NULL,_T("Have not implemented"),_T("TortoiseGit"),MB_OK);
1011 break;
1012 #if 0
1014 case ID_BLAMECOMPARE:
1016 //user clicked on the menu item "compare with working copy"
1017 //now first get the revision which is selected
1018 if (PromptShown())
1020 GitDiff diff(this, this->m_hWnd, true);
1021 diff.SetHEADPeg(m_LogRevision);
1022 diff.ShowCompare(m_path, GitRev::REV_BASE, m_path, revSelected, GitRev(), false, true);
1024 else
1025 CAppUtils::StartShowCompare(m_hWnd, m_path, GitRev::REV_BASE, m_path, revSelected, GitRev(), m_LogRevision, false, false, true);
1027 break;
1028 case ID_BLAMEWITHPREVIOUS:
1030 //user clicked on the menu item "Compare and Blame with previous revision"
1031 if (PromptShown())
1033 GitDiff diff(this, this->m_hWnd, true);
1034 diff.SetHEADPeg(m_LogRevision);
1035 diff.ShowCompare(CTGitPath(pathURL), revPrevious, CTGitPath(pathURL), revSelected, GitRev(), false, true);
1037 else
1038 CAppUtils::StartShowCompare(m_hWnd, CTGitPath(pathURL), revPrevious, CTGitPath(pathURL), revSelected, GitRev(), m_LogRevision, false, false, true);
1040 break;
1042 case ID_OPENWITH:
1043 bOpenWith = true;
1044 case ID_OPEN:
1046 CProgressDlg progDlg;
1047 progDlg.SetTitle(IDS_APPNAME);
1048 progDlg.SetAnimation(IDR_DOWNLOAD);
1049 CString sInfoLine;
1050 sInfoLine.Format(IDS_PROGRESSGETFILEREVISION, m_path.GetWinPath(), (LPCTSTR)revSelected.ToString());
1051 progDlg.SetLine(1, sInfoLine, true);
1052 SetAndClearProgressInfo(&progDlg);
1053 progDlg.ShowModeless(m_hWnd);
1054 CTGitPath tempfile = CTempFiles::Instance().GetTempFilePath(false, m_path, revSelected);
1055 bool bSuccess = true;
1056 if (!Cat(m_path, GitRev(GitRev::REV_HEAD), revSelected, tempfile))
1058 bSuccess = false;
1059 // try again, but with the selected revision as the peg revision
1060 if (!Cat(m_path, revSelected, revSelected, tempfile))
1062 progDlg.Stop();
1063 SetAndClearProgressInfo((HWND)NULL);
1064 CMessageBox::Show(this->m_hWnd, GetLastErrorMessage(), _T("TortoiseGit"), MB_ICONERROR);
1065 EnableOKButton();
1066 break;
1068 bSuccess = true;
1070 if (bSuccess)
1072 progDlg.Stop();
1073 SetAndClearProgressInfo((HWND)NULL);
1074 SetFileAttributes(tempfile.GetWinPath(), FILE_ATTRIBUTE_READONLY);
1075 int ret = 0;
1076 if (!bOpenWith)
1077 ret = (int)ShellExecute(this->m_hWnd, NULL, tempfile.GetWinPath(), NULL, NULL, SW_SHOWNORMAL);
1078 if ((ret <= HINSTANCE_ERROR)||bOpenWith)
1080 CString cmd = _T("RUNDLL32 Shell32,OpenAs_RunDLL ");
1081 cmd += tempfile.GetWinPathString() + _T(" ");
1082 CAppUtils::LaunchApplication(cmd, NULL, false);
1086 break;
1087 case ID_BLAME:
1089 CBlameDlg dlg;
1090 dlg.EndRev = revSelected;
1091 if (dlg.DoModal() == IDOK)
1093 CBlame blame;
1094 CString tempfile;
1095 CString logfile;
1096 tempfile = blame.BlameToTempFile(m_path, dlg.StartRev, dlg.EndRev, dlg.EndRev, logfile, _T(""), dlg.m_bIncludeMerge, TRUE, TRUE);
1097 if (!tempfile.IsEmpty())
1099 if (dlg.m_bTextView)
1101 //open the default text editor for the result file
1102 CAppUtils::StartTextViewer(tempfile);
1104 else
1106 CString sParams = _T("/path:\"") + m_path.GetGitPathString() + _T("\" ");
1107 if(!CAppUtils::LaunchTortoiseBlame(tempfile, logfile, CPathUtils::GetFileNameFromPath(m_path.GetFileOrDirectoryName()),sParams))
1109 break;
1113 else
1115 CMessageBox::Show(this->m_hWnd, blame.GetLastErrorMessage(), _T("TortoiseGit"), MB_ICONERROR);
1119 break;
1120 case ID_EXPORT:
1122 CString sCmd;
1123 sCmd.Format(_T("%s /command:export /path:\"%s\" /revision:%ld"),
1124 (LPCTSTR)(CPathUtils::GetAppDirectory()+_T("TortoiseProc.exe")),
1125 (LPCTSTR)pathURL, (LONG)revSelected);
1126 CAppUtils::LaunchApplication(sCmd, NULL, false);
1128 break;
1129 case ID_VIEWREV:
1131 CString url = m_ProjectProperties.sWebViewerRev;
1132 url = GetAbsoluteUrlFromRelativeUrl(url);
1133 url.Replace(_T("%REVISION%"), revSelected.ToString());
1134 if (!url.IsEmpty())
1135 ShellExecute(this->m_hWnd, _T("open"), url, NULL, NULL, SW_SHOWDEFAULT);
1137 break;
1138 case ID_VIEWPATHREV:
1140 CString relurl = pathURL;
1141 CString sRoot = GetRepositoryRoot(CTGitPath(relurl));
1142 relurl = relurl.Mid(sRoot.GetLength());
1143 CString url = m_ProjectProperties.sWebViewerPathRev;
1144 url = GetAbsoluteUrlFromRelativeUrl(url);
1145 url.Replace(_T("%REVISION%"), revSelected.ToString());
1146 url.Replace(_T("%PATH%"), relurl);
1147 if (!url.IsEmpty())
1148 ShellExecute(this->m_hWnd, _T("open"), url, NULL, NULL, SW_SHOWDEFAULT);
1150 break;
1151 #endif
1153 } // switch (cmd)
1155 theApp.DoWaitCursor(-1);
1158 void CGitLogList::SetSelectedAction(int action)
1160 POSITION pos = GetFirstSelectedItemPosition();
1161 int index;
1162 while(pos)
1164 index = GetNextSelectedItem(pos);
1165 ((GitRev*)m_arShownList[index])->GetAction(this) = action;
1166 CRect rect;
1167 this->GetItemRect(index,&rect,LVIR_BOUNDS);
1168 this->InvalidateRect(rect);
1172 void CGitLogList::ShiftSelectedAction()
1174 POSITION pos = GetFirstSelectedItemPosition();
1175 int index;
1176 while(pos)
1178 index = GetNextSelectedItem(pos);
1179 int action = ((GitRev*)m_arShownList[index])->GetAction(this);
1180 switch (action)
1182 case CTGitPath::LOGACTIONS_REBASE_PICK:
1183 action = CTGitPath::LOGACTIONS_REBASE_SKIP;
1184 break;
1185 case CTGitPath::LOGACTIONS_REBASE_SKIP:
1186 action= CTGitPath::LOGACTIONS_REBASE_EDIT;
1187 break;
1188 case CTGitPath::LOGACTIONS_REBASE_EDIT:
1189 action = CTGitPath::LOGACTIONS_REBASE_SQUASH;
1190 break;
1191 case CTGitPath::LOGACTIONS_REBASE_SQUASH:
1192 action= CTGitPath::LOGACTIONS_REBASE_PICK;
1193 break;
1195 ((GitRev*)m_arShownList[index])->GetAction(this) = action;
1196 CRect rect;
1197 this->GetItemRect(index, &rect, LVIR_BOUNDS);
1198 this->InvalidateRect(rect);