Don't delete files which contain of one line without a line ending
[TortoiseGit.git] / src / TortoiseProc / RepositoryBrowser.cpp
blobffdd6248838c03caee5bbac9ecc3ae2b300efb06
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013 - TortoiseGit
4 // Copyright (C) 2012-2013 Sven Strickroth <email@cs-ware.de>
5 // Copyright (C) 2003-2012 - TortoiseSVN
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 // RepositoryBrowser.cpp : implementation file
24 #include "stdafx.h"
25 #include "TortoiseProc.h"
26 #include "RepositoryBrowser.h"
27 #include "LogDlg.h"
28 #include "AppUtils.h"
29 #include "IconMenu.h"
30 #include "UnicodeUtils.h"
31 #include "SysImageList.h"
32 #include <sys/stat.h>
33 #include "SysInfo.h"
34 #include "registry.h"
35 #include "PathUtils.h"
36 #include "StringUtils.h"
37 #include "GitDiff.h"
39 void SetSortArrowA(CListCtrl * control, int nColumn, bool bAscending)
41 if (control == NULL)
42 return;
44 // set the sort arrow
45 CHeaderCtrl * pHeader = control->GetHeaderCtrl();
46 HDITEM HeaderItem = {0};
47 HeaderItem.mask = HDI_FORMAT;
48 for (int i = 0; i < pHeader->GetItemCount(); ++i)
50 pHeader->GetItem(i, &HeaderItem);
51 HeaderItem.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
52 pHeader->SetItem(i, &HeaderItem);
54 if (nColumn >= 0)
56 pHeader->GetItem(nColumn, &HeaderItem);
57 HeaderItem.fmt |= (bAscending ? HDF_SORTUP : HDF_SORTDOWN);
58 pHeader->SetItem(nColumn, &HeaderItem);
62 class CRepoListCompareFunc
64 public:
65 CRepoListCompareFunc(CListCtrl* pList, int col, bool desc)
66 : m_col(col)
67 , m_desc(desc)
68 , m_pList(pList)
71 static int CALLBACK StaticCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
73 return ((CRepoListCompareFunc *) lParamSort)->Compare(lParam1, lParam2);
76 int Compare(LPARAM lParam1, LPARAM lParam2)
78 CShadowFilesTree * pLeft = (CShadowFilesTree *)m_pList->GetItemData((int)lParam1);
79 CShadowFilesTree * pRight = (CShadowFilesTree *)m_pList->GetItemData((int)lParam2);
81 int result = 0;
82 switch(m_col)
84 case CRepositoryBrowser::eCol_Name:
85 result = SortStrCmp(pLeft->m_sName, pRight->m_sName);
86 if (result != 0)
87 break;
88 case CRepositoryBrowser::eCol_Extension:
89 result = m_pList->GetItemText(static_cast<int>(lParam1), 1).CompareNoCase(m_pList->GetItemText(static_cast<int>(lParam2), 1));
90 if (result == 0) // if extensions are the same, use the filename to sort
91 result = SortStrCmp(pRight->m_sName, pRight->m_sName);
92 if (result != 0)
93 break;
94 case CRepositoryBrowser::eCol_FileSize:
95 if (pLeft->m_iSize > pRight->m_iSize)
96 result = 1;
97 else if (pLeft->m_iSize < pRight->m_iSize)
98 result = -1;
99 else // fallback
100 result = SortStrCmp(pLeft->m_sName, pRight->m_sName);
103 if (m_desc)
104 result = -result;
106 if (pLeft->m_bFolder != pRight->m_bFolder)
108 if (pRight->m_bFolder)
109 result = 1;
110 else
111 result = -1;
114 return result;
116 int SortStrCmp(CString &left, CString &right)
118 if (CRepositoryBrowser::s_bSortLogical)
119 return StrCmpLogicalW(left, right);
120 return StrCmpI(left, right);
123 int m_col;
124 bool m_desc;
125 CListCtrl* m_pList;
128 // CRepositoryBrowser dialog
130 bool CRepositoryBrowser::s_bSortLogical = true;
132 IMPLEMENT_DYNAMIC(CRepositoryBrowser, CResizableStandAloneDialog)
134 CRepositoryBrowser::CRepositoryBrowser(CString rev, CWnd* pParent /*=NULL*/)
135 : CResizableStandAloneDialog(CRepositoryBrowser::IDD, pParent)
136 , m_currSortCol(0)
137 , m_currSortDesc(false)
138 , m_sRevision(rev)
139 , m_bHasWC(true)
143 CRepositoryBrowser::~CRepositoryBrowser()
147 void CRepositoryBrowser::DoDataExchange(CDataExchange* pDX)
149 CDialog::DoDataExchange(pDX);
150 DDX_Control(pDX, IDC_REPOTREE, m_RepoTree);
151 DDX_Control(pDX, IDC_REPOLIST, m_RepoList);
155 BEGIN_MESSAGE_MAP(CRepositoryBrowser, CResizableStandAloneDialog)
156 ON_NOTIFY(TVN_SELCHANGED, IDC_REPOTREE, &CRepositoryBrowser::OnTvnSelchangedRepoTree)
157 ON_WM_CONTEXTMENU()
158 ON_NOTIFY(LVN_COLUMNCLICK, IDC_REPOLIST, &CRepositoryBrowser::OnLvnColumnclickRepoList)
159 ON_NOTIFY(NM_DBLCLK, IDC_REPOLIST, &CRepositoryBrowser::OnNMDblclk_RepoList)
160 ON_BN_CLICKED(IDC_BUTTON_REVISION, &CRepositoryBrowser::OnBnClickedButtonRevision)
161 ON_WM_SETCURSOR()
162 ON_WM_MOUSEMOVE()
163 ON_WM_LBUTTONDOWN()
164 ON_WM_LBUTTONUP()
165 END_MESSAGE_MAP()
168 // CRepositoryBrowser message handlers
170 BOOL CRepositoryBrowser::OnInitDialog()
172 CResizableStandAloneDialog::OnInitDialog();
173 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
175 AddAnchor(IDC_STATIC_REPOURL, TOP_LEFT);
176 AddAnchor(IDC_REPOBROWSER_URL, TOP_LEFT, TOP_RIGHT);
177 AddAnchor(IDC_STATIC_REF, TOP_RIGHT);
178 AddAnchor(IDC_BUTTON_REVISION, TOP_RIGHT);
179 AddAnchor(IDC_REPOTREE, TOP_LEFT, BOTTOM_LEFT);
180 AddAnchor(IDC_REPOLIST, TOP_LEFT, BOTTOM_RIGHT);
181 AddAnchor(IDHELP, BOTTOM_RIGHT);
182 AddAnchor(IDOK, BOTTOM_RIGHT);
183 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
185 CRepositoryBrowser::s_bSortLogical = !CRegDWORD(L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer\\NoStrCmpLogical", 0, false, HKEY_CURRENT_USER);
186 if (CRepositoryBrowser::s_bSortLogical)
187 CRepositoryBrowser::s_bSortLogical = !CRegDWORD(L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer\\NoStrCmpLogical", 0, false, HKEY_LOCAL_MACHINE);
189 CString temp;
190 temp.LoadString(IDS_STATUSLIST_COLFILENAME);
191 m_RepoList.InsertColumn(eCol_Name, temp, 0, 150);
192 temp.LoadString(IDS_STATUSLIST_COLEXT);
193 m_RepoList.InsertColumn(eCol_Extension, temp, 0, 100);
194 temp.LoadString(IDS_LOG_SIZE);
195 m_RepoList.InsertColumn(eCol_FileSize, temp, 0, 100);
197 // set up the list control
198 // set the extended style of the list control
199 // the style LVS_EX_FULLROWSELECT interferes with the background watermark image but it's more important to be able to select in the whole row.
200 CRegDWORD regFullRowSelect(_T("Software\\TortoiseGit\\FullRowSelect"), TRUE);
201 DWORD exStyle = LVS_EX_HEADERDRAGDROP | LVS_EX_DOUBLEBUFFER | LVS_EX_INFOTIP | LVS_EX_SUBITEMIMAGES;
202 if (DWORD(regFullRowSelect))
203 exStyle |= LVS_EX_FULLROWSELECT;
204 m_RepoList.SetExtendedStyle(exStyle);
205 m_RepoList.SetImageList(&SYS_IMAGE_LIST(), LVSIL_SMALL);
206 CAppUtils::SetListCtrlBackgroundImage(m_RepoList.GetSafeHwnd(), IDI_REPOBROWSER_BKG);
208 m_RepoTree.SetImageList(&SYS_IMAGE_LIST(), TVSIL_NORMAL);
209 if (SysInfo::Instance().IsVistaOrLater())
211 DWORD exStyle = TVS_EX_FADEINOUTEXPANDOS | TVS_EX_AUTOHSCROLL | TVS_EX_DOUBLEBUFFER;
212 m_RepoTree.SetExtendedStyle(exStyle, exStyle);
215 SetWindowTheme(m_RepoTree.GetSafeHwnd(), L"Explorer", NULL);
216 SetWindowTheme(m_RepoList.GetSafeHwnd(), L"Explorer", NULL);
218 m_nIconFolder = SYS_IMAGE_LIST().GetDirIconIndex();
219 m_nOpenIconFolder = SYS_IMAGE_LIST().GetDirOpenIconIndex();
221 EnableSaveRestore(L"Reposbrowser");
223 DWORD xPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RepobrowserDivider"), 0);
224 if (xPos == 0)
226 RECT rc;
227 GetDlgItem(IDC_REPOTREE)->GetClientRect(&rc);
228 xPos = rc.right - rc.left;
230 bDragMode = false;
231 HandleDividerMove(CPoint(xPos + 20, 10), false);
233 CString sWindowTitle;
234 GetWindowText(sWindowTitle);
235 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
237 m_bHasWC = !g_GitAdminDir.IsBareRepo(g_Git.m_CurrentDir);
239 Refresh();
241 m_RepoList.SetFocus();
243 return FALSE;
246 void CRepositoryBrowser::OnOK()
248 SaveDividerPosition();
249 CResizableStandAloneDialog::OnOK();
252 void CRepositoryBrowser::OnCancel()
254 SaveDividerPosition();
255 CResizableStandAloneDialog::OnCancel();
258 void CRepositoryBrowser::OnNMDblclk_RepoList(NMHDR *pNMHDR, LRESULT *pResult)
260 *pResult = 0;
262 LPNMITEMACTIVATE pNmItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
263 if (pNmItemActivate->iItem < 0)
264 return;
266 CShadowFilesTree * pItem = (CShadowFilesTree *)m_RepoList.GetItemData(pNmItemActivate->iItem);
267 if (pItem == NULL)
268 return;
270 if (!pItem->m_bFolder)
272 OpenFile(pItem->GetFullName(), OPEN);
273 return;
275 else
277 FillListCtrlForShadowTree(pItem);
278 m_RepoTree.SelectItem(pItem->m_hTree);
282 void CRepositoryBrowser::Refresh()
284 m_RepoTree.DeleteAllItems();
285 m_RepoList.DeleteAllItems();
286 m_TreeRoot.m_ShadowTree.clear();
287 m_TreeRoot.m_sName = "";
288 m_TreeRoot.m_bFolder = true;
290 TVINSERTSTRUCT tvinsert = {0};
291 tvinsert.hParent = TVI_ROOT;
292 tvinsert.hInsertAfter = TVI_ROOT;
293 tvinsert.itemex.mask = TVIF_DI_SETITEM | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
294 tvinsert.itemex.pszText = L"/";
295 tvinsert.itemex.lParam = (LPARAM)&m_TreeRoot;
296 tvinsert.itemex.iImage = m_nIconFolder;
297 tvinsert.itemex.iSelectedImage = m_nOpenIconFolder;
298 m_TreeRoot.m_hTree= m_RepoTree.InsertItem(&tvinsert);
300 ReadTree(&m_TreeRoot);
301 m_RepoTree.Expand(m_TreeRoot.m_hTree, TVE_EXPAND);
302 FillListCtrlForShadowTree(&m_TreeRoot);
303 m_RepoTree.SelectItem(m_TreeRoot.m_hTree);
306 int CRepositoryBrowser::ReadTreeRecursive(git_repository &repo, git_tree * tree, CShadowFilesTree * treeroot)
308 size_t count = git_tree_entrycount(tree);
310 for (int i = 0; i < count; i++)
312 const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
313 if (entry == NULL)
314 continue;
315 int mode = git_tree_entry_filemode(entry);
317 CString base = CUnicodeUtils::GetUnicode(git_tree_entry_name(entry), CP_UTF8);
319 git_object *object = NULL;
320 git_tree_entry_to_object(&object, &repo, entry);
321 if (object == NULL)
322 continue;
324 CShadowFilesTree * pNextTree = &treeroot->m_ShadowTree[base];
325 pNextTree->m_sName = base;
326 pNextTree->m_pParent = treeroot;
328 if (mode & S_IFDIR)
330 pNextTree->m_bFolder = true;
332 TVINSERTSTRUCT tvinsert = {0};
333 tvinsert.hParent = treeroot->m_hTree;
334 tvinsert.hInsertAfter = TVI_SORT;
335 tvinsert.itemex.mask = TVIF_DI_SETITEM | TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE;
336 tvinsert.itemex.pszText = base.GetBuffer(base.GetLength());
337 tvinsert.itemex.lParam = (LPARAM)pNextTree;
338 tvinsert.itemex.iImage = m_nIconFolder;
339 tvinsert.itemex.iSelectedImage = m_nOpenIconFolder;
340 pNextTree->m_hTree = m_RepoTree.InsertItem(&tvinsert);
341 base.ReleaseBuffer();
343 ReadTreeRecursive(repo, (git_tree*)object, pNextTree);
345 else
347 const git_oid * oid = git_object_id(object);
349 git_blob * blob;
350 git_blob_lookup(&blob, &repo, oid);
351 if (blob == NULL)
352 continue;
354 pNextTree->m_iSize = git_blob_rawsize(blob);
355 git_blob_free(blob);
358 git_object_free(object);
361 return 0;
364 int CRepositoryBrowser::ReadTree(CShadowFilesTree * treeroot)
366 CStringA gitdir = CUnicodeUtils::GetMulti(g_Git.m_CurrentDir, CP_UTF8);
367 git_repository *repository = NULL;
368 git_commit *commit = NULL;
369 git_tree * tree = NULL;
370 int ret = 0;
373 ret = git_repository_open(&repository, gitdir.GetBuffer());
374 if (ret)
376 MessageBox(g_Git.GetGitLastErr(_T("Could not open repository.")), _T("TortoiseGit"), MB_ICONERROR);
377 break;
380 CGitHash hash;
381 if (g_Git.GetHash(hash, m_sRevision))
383 MessageBox(g_Git.GetGitLastErr(_T("Could not get hash of ") + m_sRevision + _T(".")), _T("TortoiseGit"), MB_ICONERROR);
384 break;
386 ret = git_commit_lookup(&commit, repository, (git_oid *) hash.m_hash);
387 if (ret)
389 MessageBox(g_Git.GetGitLastErr(_T("Could not lookup commit.")), _T("TortoiseGit"), MB_ICONERROR);
390 break;
393 ret = git_commit_tree(&tree, commit);
394 if (ret)
396 MessageBox(g_Git.GetGitLastErr(_T("Could not get tree of commit.")), _T("TortoiseGit"), MB_ICONERROR);
397 break;
400 ReadTreeRecursive(*repository, tree, treeroot);
402 // try to resolve hash to a branch name
403 if (m_sRevision == hash.ToString())
405 MAP_HASH_NAME map;
406 if (g_Git.GetMapHashToFriendName(map))
407 MessageBox(g_Git.GetGitLastErr(_T("Could not get all refs.")), _T("TortoiseGit"), MB_ICONERROR);
408 if (!map[hash].empty())
409 m_sRevision = map[hash].at(0);
411 this->GetDlgItem(IDC_BUTTON_REVISION)->SetWindowText(m_sRevision);
412 } while(0);
414 if (tree)
415 git_tree_free(tree);
417 if (commit)
418 git_commit_free(commit);
420 if (repository)
421 git_repository_free(repository);
423 return ret;
426 void CRepositoryBrowser::OnTvnSelchangedRepoTree(NMHDR *pNMHDR, LRESULT *pResult)
428 LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
429 *pResult = 0;
431 FillListCtrlForTreeNode(pNMTreeView->itemNew.hItem);
434 void CRepositoryBrowser::FillListCtrlForTreeNode(HTREEITEM treeNode)
436 m_RepoList.DeleteAllItems();
438 CShadowFilesTree* pTree = (CShadowFilesTree*)(m_RepoTree.GetItemData(treeNode));
439 if (pTree == NULL)
441 ASSERT(FALSE);
442 return;
445 CString url = _T("/") + pTree->GetFullName();
446 GetDlgItem(IDC_REPOBROWSER_URL)->SetWindowText(url);
448 FillListCtrlForShadowTree(pTree);
451 void CRepositoryBrowser::FillListCtrlForShadowTree(CShadowFilesTree* pTree)
453 for (TShadowFilesTreeMap::iterator itShadowTree = pTree->m_ShadowTree.begin(); itShadowTree != pTree->m_ShadowTree.end(); ++itShadowTree)
455 int icon = m_nIconFolder;
456 if (!(*itShadowTree).second.m_bFolder)
457 icon = SYS_IMAGE_LIST().GetFileIconIndex((*itShadowTree).second.m_sName);
459 int indexItem = m_RepoList.InsertItem(m_RepoList.GetItemCount(), (*itShadowTree).second.m_sName, icon);
461 m_RepoList.SetItemData(indexItem, (DWORD_PTR)&(*itShadowTree).second);
462 if (!(*itShadowTree).second.m_bFolder)
464 CString temp;
466 temp = CPathUtils::GetFileExtFromPath((*itShadowTree).second.m_sName);
467 m_RepoList.SetItemText(indexItem, eCol_Extension, temp);
469 StrFormatByteSize((*itShadowTree).second.m_iSize, temp.GetBuffer(20), 20);
470 temp.ReleaseBuffer();
471 m_RepoList.SetItemText(indexItem, eCol_FileSize, temp);
475 CRepoListCompareFunc compareFunc(&m_RepoList, m_currSortCol, m_currSortDesc);
476 m_RepoList.SortItemsEx(&CRepoListCompareFunc::StaticCompare, (DWORD_PTR)&compareFunc);
478 SetSortArrowA(&m_RepoList, m_currSortCol, !m_currSortDesc);
481 void CRepositoryBrowser::OnContextMenu(CWnd* pWndFrom, CPoint point)
483 if (pWndFrom == &m_RepoList)
484 OnContextMenu_RepoList(point);
485 else if (pWndFrom == &m_RepoTree)
486 OnContextMenu_RepoTree(point);
489 void CRepositoryBrowser::OnContextMenu_RepoTree(CPoint point)
491 CPoint clientPoint = point;
492 m_RepoTree.ScreenToClient(&clientPoint);
494 HTREEITEM hTreeItem = m_RepoTree.HitTest(clientPoint);
495 if (hTreeItem == NULL)
496 return;
498 TShadowFilesTreeList selectedLeafs;
499 selectedLeafs.push_back((CShadowFilesTree *)m_RepoTree.GetItemData(hTreeItem));
501 ShowContextMenu(point, selectedLeafs, ONLY_FOLDERS);
504 void CRepositoryBrowser::OnContextMenu_RepoList(CPoint point)
506 TShadowFilesTreeList selectedLeafs;
507 selectedLeafs.reserve(m_RepoList.GetSelectedCount());
509 bool folderSelected = false;
510 bool filesSelected = false;
512 POSITION pos = m_RepoList.GetFirstSelectedItemPosition();
513 while (pos)
515 CShadowFilesTree * item = (CShadowFilesTree *)m_RepoList.GetItemData(m_RepoList.GetNextSelectedItem(pos));
516 if (item->m_bFolder)
517 folderSelected = true;
518 else
519 filesSelected = true;
520 selectedLeafs.push_back(item);
523 eSelectionType selType = ONLY_FILES;
524 if (folderSelected && filesSelected)
525 selType = MIXED;
526 else if (folderSelected)
527 selType = ONLY_FOLDERS;
529 ShowContextMenu(point, selectedLeafs, selType);
532 void CRepositoryBrowser::ShowContextMenu(CPoint point, TShadowFilesTreeList &selectedLeafs, eSelectionType selType)
534 CIconMenu popupMenu;
535 popupMenu.CreatePopupMenu();
537 bool bAddSeparator = false;
539 if (selectedLeafs.size() == 1)
541 popupMenu.AppendMenuIcon(eCmd_Open, IDS_REPOBROWSE_OPEN, IDI_OPEN);
542 popupMenu.SetDefaultItem(eCmd_Open, FALSE);
543 if (selType == ONLY_FILES)
545 popupMenu.AppendMenuIcon(eCmd_OpenWith, IDS_LOG_POPUP_OPENWITH, IDI_OPEN);
546 popupMenu.AppendMenuIcon(eCmd_OpenWithAlternativeEditor, IDS_LOG_POPUP_VIEWREV);
549 popupMenu.AppendMenu(MF_SEPARATOR);
551 if (m_bHasWC && selType == ONLY_FILES)
553 popupMenu.AppendMenuIcon(eCmd_CompareWC, IDS_LOG_POPUP_COMPARE, IDI_DIFF);
554 bAddSeparator = true;
557 if (bAddSeparator)
558 popupMenu.AppendMenu(MF_SEPARATOR);
559 bAddSeparator = false;
561 CString temp;
562 temp.LoadString(IDS_MENULOG);
563 popupMenu.AppendMenuIcon(eCmd_ViewLog, temp, IDI_LOG);
565 if (selType == ONLY_FILES)
567 if (m_bHasWC)
568 popupMenu.AppendMenuIcon(eCmd_Blame, IDS_LOG_POPUP_BLAME, IDI_BLAME);
570 popupMenu.AppendMenu(MF_SEPARATOR);
571 temp.LoadString(IDS_LOG_POPUP_SAVE);
572 popupMenu.AppendMenuIcon(eCmd_SaveAs, temp, IDI_SAVEAS);
575 bAddSeparator = true;
578 if (selType == ONLY_FILES && m_bHasWC)
580 popupMenu.AppendMenuIcon(eCmd_Revert, IDS_LOG_POPUP_REVERTTOREV, IDI_REVERT);
581 bAddSeparator = true;
584 if (bAddSeparator)
585 popupMenu.AppendMenu(MF_SEPARATOR);
586 bAddSeparator = false;
588 popupMenu.AppendMenuIcon(eCmd_CopyPath, IDS_STATUSLIST_CONTEXT_COPY, IDI_COPYCLIP);
590 eCmd cmd = (eCmd)popupMenu.TrackPopupMenuEx(TPM_LEFTALIGN|TPM_RETURNCMD, point.x, point.y, this, 0);
591 switch(cmd)
593 case eCmd_ViewLog:
595 CString sCmd;
596 sCmd.Format(_T("/command:log /path:\"%s\\%s\""), g_Git.m_CurrentDir, selectedLeafs.at(0)->GetFullName());
597 CAppUtils::RunTortoiseGitProc(sCmd);
599 break;
600 case eCmd_Blame:
602 CAppUtils::LaunchTortoiseBlame(g_Git.m_CurrentDir + _T("\\") + selectedLeafs.at(0)->GetFullName(), m_sRevision);
604 break;
605 case eCmd_Open:
606 if (selectedLeafs.at(0)->m_bFolder)
608 FillListCtrlForTreeNode(selectedLeafs.at(0)->m_hTree);
609 m_RepoTree.SelectItem(selectedLeafs.at(0)->m_hTree);
610 return;
612 OpenFile(selectedLeafs.at(0)->GetFullName(), OPEN);
613 break;
614 case eCmd_OpenWith:
615 OpenFile(selectedLeafs.at(0)->GetFullName(), OPEN_WITH);
616 break;
617 case eCmd_OpenWithAlternativeEditor:
618 OpenFile(selectedLeafs.at(0)->GetFullName(), ALTERNATIVEEDITOR);
619 break;
620 case eCmd_CompareWC:
622 CTGitPath file(selectedLeafs.at(0)->GetFullName());
623 CGitDiff::Diff(&file, &file, GIT_REV_ZERO, m_sRevision);
625 break;
626 case eCmd_Revert:
628 int count = 0;
629 for (TShadowFilesTreeList::iterator itShadowTree = selectedLeafs.begin(); itShadowTree != selectedLeafs.end(); ++itShadowTree)
631 if (RevertItemToVersion((*itShadowTree)->GetFullName()))
632 count++;
633 else
634 break;
636 CString msg;
637 msg.Format(IDS_STATUSLIST_FILESREVERTED, count, m_sRevision);
638 MessageBox(msg, _T("TortoiseGit"), MB_OK);
640 break;
641 case eCmd_SaveAs:
642 FileSaveAs(selectedLeafs.at(0)->GetFullName());
643 break;
644 case eCmd_CopyPath:
646 CString sClipboard;
647 for (TShadowFilesTreeList::iterator itShadowTree = selectedLeafs.begin(); itShadowTree != selectedLeafs.end(); ++itShadowTree)
649 sClipboard += (*itShadowTree)->m_sName + _T("\r\n");
651 CStringUtils::WriteAsciiStringToClipboard(sClipboard);
653 break;
657 BOOL CRepositoryBrowser::PreTranslateMessage(MSG* pMsg)
659 if (pMsg->message == WM_KEYDOWN)
661 switch (pMsg->wParam)
663 case VK_F5:
665 Refresh();
667 break;
671 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
674 void CRepositoryBrowser::OnLvnColumnclickRepoList(NMHDR *pNMHDR, LRESULT *pResult)
676 LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
677 if (pResult)
678 *pResult = 0;
680 if (m_currSortCol == pNMLV->iSubItem)
681 m_currSortDesc = !m_currSortDesc;
682 else
684 m_currSortCol = pNMLV->iSubItem;
685 m_currSortDesc = false;
688 CRepoListCompareFunc compareFunc(&m_RepoList, m_currSortCol, m_currSortDesc);
689 m_RepoList.SortItemsEx(&CRepoListCompareFunc::StaticCompare, (DWORD_PTR)&compareFunc);
691 SetSortArrowA(&m_RepoList, m_currSortCol, !m_currSortDesc);
694 void CRepositoryBrowser::OnBnClickedButtonRevision()
696 // use the git log to allow selection of a version
697 CLogDlg dlg;
698 // tell the dialog to use mode for selecting revisions
699 dlg.SetSelect(true);
700 // only one revision must be selected however
701 dlg.SingleSelection(true);
702 if (dlg.DoModal() == IDOK)
704 // get selected hash if any
705 m_sRevision = dlg.GetSelectedHash();
706 Refresh();
710 void CRepositoryBrowser::SaveDividerPosition()
712 RECT rc;
713 GetDlgItem(IDC_REPOTREE)->GetClientRect(&rc);
714 CRegDWORD xPos = CRegDWORD(_T("Software\\TortoiseGit\\TortoiseProc\\ResizableState\\RepobrowserDivider"));
715 xPos = rc.right - rc.left;
718 void CRepositoryBrowser::HandleDividerMove(CPoint point, bool bDraw)
720 RECT rect, tree, list, treelist, treelistclient;
722 // create an union of the tree and list control rectangle
723 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
724 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
725 UnionRect(&treelist, &tree, &list);
726 treelistclient = treelist;
727 ScreenToClient(&treelistclient);
729 ClientToScreen(&point);
730 GetClientRect(&rect);
731 ClientToScreen(&rect);
733 CPoint point2 = point;
734 if (point2.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
735 point2.x = treelist.left + REPOBROWSER_CTRL_MIN_WIDTH;
736 if (point2.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
737 point2.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
739 point.x -= rect.left;
740 point.y -= treelist.top;
742 OffsetRect(&treelist, -treelist.left, -treelist.top);
744 if (point.x < treelist.left+REPOBROWSER_CTRL_MIN_WIDTH)
745 point.x = treelist.left+REPOBROWSER_CTRL_MIN_WIDTH;
746 if (point.x > treelist.right-REPOBROWSER_CTRL_MIN_WIDTH)
747 point.x = treelist.right-REPOBROWSER_CTRL_MIN_WIDTH;
749 if (bDraw)
751 CDC * pDC = GetDC();
752 DrawXorBar(pDC, oldx + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
753 ReleaseDC(pDC);
756 oldx = point.x;
757 oldy = point.y;
759 //position the child controls
760 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&treelist);
761 treelist.right = point2.x - 2;
762 ScreenToClient(&treelist);
763 RemoveAnchor(IDC_REPOTREE);
764 GetDlgItem(IDC_REPOTREE)->MoveWindow(&treelist);
765 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&treelist);
766 treelist.left = point2.x + 2;
767 ScreenToClient(&treelist);
768 RemoveAnchor(IDC_REPOLIST);
769 GetDlgItem(IDC_REPOLIST)->MoveWindow(&treelist);
771 AddAnchor(IDC_REPOTREE, TOP_LEFT, BOTTOM_LEFT);
772 AddAnchor(IDC_REPOLIST, TOP_LEFT, BOTTOM_RIGHT);
775 void CRepositoryBrowser::OnMouseMove(UINT nFlags, CPoint point)
777 if (bDragMode == FALSE)
778 return;
780 RECT rect, tree, list, treelist, treelistclient;
781 // create an union of the tree and list control rectangle
782 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
783 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
784 UnionRect(&treelist, &tree, &list);
785 treelistclient = treelist;
786 ScreenToClient(&treelistclient);
788 //convert the mouse coordinates relative to the top-left of
789 //the window
790 ClientToScreen(&point);
791 GetClientRect(&rect);
792 ClientToScreen(&rect);
793 point.x -= rect.left;
794 point.y -= treelist.top;
796 //same for the window coordinates - make them relative to 0,0
797 OffsetRect(&treelist, -treelist.left, -treelist.top);
799 if (point.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
800 point.x = treelist.left + REPOBROWSER_CTRL_MIN_WIDTH;
801 if (point.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
802 point.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
804 if ((nFlags & MK_LBUTTON) && (point.x != oldx))
806 CDC * pDC = GetDC();
808 if (pDC)
810 DrawXorBar(pDC, oldx + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
811 DrawXorBar(pDC, point.x + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
813 ReleaseDC(pDC);
816 oldx = point.x;
817 oldy = point.y;
820 CStandAloneDialogTmpl<CResizableDialog>::OnMouseMove(nFlags, point);
823 void CRepositoryBrowser::OnLButtonDown(UINT nFlags, CPoint point)
825 RECT rect, tree, list, treelist, treelistclient;
827 // create an union of the tree and list control rectangle
828 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&list);
829 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&tree);
830 UnionRect(&treelist, &tree, &list);
831 treelistclient = treelist;
832 ScreenToClient(&treelistclient);
834 //convert the mouse coordinates relative to the top-left of
835 //the window
836 ClientToScreen(&point);
837 GetClientRect(&rect);
838 ClientToScreen(&rect);
839 point.x -= rect.left;
840 point.y -= treelist.top;
842 //same for the window coordinates - make them relative to 0,0
843 OffsetRect(&treelist, -treelist.left, -treelist.top);
845 if (point.x < treelist.left + REPOBROWSER_CTRL_MIN_WIDTH)
846 return CStandAloneDialogTmpl < CResizableDialog>::OnLButtonDown(nFlags, point);
847 if (point.x > treelist.right - 3)
848 return CStandAloneDialogTmpl < CResizableDialog>::OnLButtonDown(nFlags, point);
849 if (point.x > treelist.right - REPOBROWSER_CTRL_MIN_WIDTH)
850 point.x = treelist.right - REPOBROWSER_CTRL_MIN_WIDTH;
852 if ((point.y < treelist.top + 3) || (point.y > treelist.bottom - 3))
853 return CStandAloneDialogTmpl<CResizableDialog>::OnLButtonDown(nFlags, point);
855 bDragMode = true;
857 SetCapture();
859 CDC * pDC = GetDC();
860 DrawXorBar(pDC, point.x + 2, treelistclient.top, 4, treelistclient.bottom - treelistclient.top - 2);
861 ReleaseDC(pDC);
863 oldx = point.x;
864 oldy = point.y;
866 CStandAloneDialogTmpl<CResizableDialog>::OnLButtonDown(nFlags, point);
869 void CRepositoryBrowser::OnLButtonUp(UINT nFlags, CPoint point)
871 if (bDragMode == FALSE)
872 return;
874 HandleDividerMove(point, true);
876 bDragMode = false;
877 ReleaseCapture();
879 CStandAloneDialogTmpl<CResizableDialog>::OnLButtonUp(nFlags, point);
882 void CRepositoryBrowser::OnCaptureChanged(CWnd *pWnd)
884 bDragMode = false;
886 __super::OnCaptureChanged(pWnd);
889 void CRepositoryBrowser::DrawXorBar(CDC * pDC, int x1, int y1, int width, int height)
891 static WORD _dotPatternBmp[8] =
893 0x0055, 0x00aa, 0x0055, 0x00aa,
894 0x0055, 0x00aa, 0x0055, 0x00aa
897 HBITMAP hbm;
898 HBRUSH hbr, hbrushOld;
900 hbm = CreateBitmap(8, 8, 1, 1, _dotPatternBmp);
901 hbr = CreatePatternBrush(hbm);
903 pDC->SetBrushOrg(x1, y1);
904 hbrushOld = (HBRUSH)pDC->SelectObject(hbr);
906 PatBlt(pDC->GetSafeHdc(), x1, y1, width, height, PATINVERT);
908 pDC->SelectObject(hbrushOld);
910 DeleteObject(hbr);
911 DeleteObject(hbm);
914 BOOL CRepositoryBrowser::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
916 if (pWnd == this)
918 RECT rect;
919 POINT pt;
920 GetClientRect(&rect);
921 GetCursorPos(&pt);
922 ScreenToClient(&pt);
923 if (PtInRect(&rect, pt))
925 ClientToScreen(&pt);
926 // are we right of the tree control?
927 GetDlgItem(IDC_REPOTREE)->GetWindowRect(&rect);
928 if ((pt.x > rect.right) && (pt.y >= rect.top + 3) && (pt.y <= rect.bottom - 3))
930 // but left of the list control?
931 GetDlgItem(IDC_REPOLIST)->GetWindowRect(&rect);
932 if (pt.x < rect.left)
934 HCURSOR hCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE));
935 SetCursor(hCur);
936 return TRUE;
941 return CStandAloneDialogTmpl<CResizableDialog>::OnSetCursor(pWnd, nHitTest, message);
944 void CRepositoryBrowser::FileSaveAs(const CString path)
946 CTGitPath gitPath(path);
948 CString filename;
949 filename.Format(_T("%s-%s%s"), gitPath.GetBaseFilename(), CGitHash(m_sRevision).ToString().Left(g_Git.GetShortHASHLength()), gitPath.GetFileExtension());
950 CFileDialog dlg(FALSE, NULL, filename, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, NULL);
952 CString cmd, out;
953 INT_PTR ret = dlg.DoModal();
954 SetCurrentDirectory(g_Git.m_CurrentDir);
955 if (ret == IDOK)
957 filename = dlg.GetPathName();
958 if (g_Git.GetOneFile(m_sRevision, gitPath, filename))
960 out.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED, gitPath.GetGitPathString(), m_sRevision, filename);
961 MessageBox(out, _T("TortoiseGit"), MB_OK);
962 return;
967 void CRepositoryBrowser::OpenFile(const CString path, eOpenType mode)
969 CTGitPath gitPath(path);
971 CString temppath;
972 CString file;
973 GetTempPath(temppath);
974 file.Format(_T("%s%s_%s%s"), temppath, gitPath.GetBaseFilename(), CGitHash(m_sRevision).ToString().Left(g_Git.GetShortHASHLength()), gitPath.GetFileExtension());
976 CString out;
977 if(g_Git.GetOneFile(m_sRevision, gitPath, file))
979 out.Format(IDS_STATUSLIST_CHECKOUTFILEFAILED, gitPath.GetGitPathString(), m_sRevision, file);
980 MessageBox(out, _T("TortoiseGit"), MB_OK);
981 return;
984 if (mode == ALTERNATIVEEDITOR)
986 CAppUtils::LaunchAlternativeEditor(file);
987 return;
989 else if (mode == OPEN)
991 int ret = HINSTANCE_ERROR;
992 ret = (int)ShellExecute(this->m_hWnd, NULL, file, NULL, NULL, SW_SHOW);
994 if (ret > HINSTANCE_ERROR)
995 return;
998 CString cmd = _T("RUNDLL32 Shell32,OpenAs_RunDLL ") + file;
999 CAppUtils::LaunchApplication(cmd, NULL, false);
1001 bool CRepositoryBrowser::RevertItemToVersion(const CString &path)
1003 CString cmd, out;
1004 cmd.Format(_T("git.exe checkout %s -- \"%s\""), m_sRevision, path);
1005 if (g_Git.Run(cmd, &out, CP_UTF8))
1007 if (MessageBox(out, _T("TortoiseGit"), MB_ICONEXCLAMATION | MB_OKCANCEL) == IDCANCEL)
1008 return false;
1011 return true;