No need for an extra lookup for HEAD
[TortoiseGit.git] / src / TortoiseProc / RevisionGraph / RevisionGraphDlgFunc.cpp
blob937fd34b9d9e608eea637e0ccf0d384312f37d50
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2011 - TortoiseSVN
4 // Copyright (C) 2012-2014 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "TortoiseProc.h"
22 #include <gdiplus.h>
23 #include "Revisiongraphdlg.h"
24 #include "MessageBox.h"
25 #include "Git.h"
26 #include "TempFile.h"
27 #include "UnicodeUtils.h"
28 #include "TGitPath.h"
29 //#include "SVNInfo.h"
30 //#include ".\revisiongraphwnd.h"
31 //#include "CachedLogInfo.h"
32 //#include "RevisionGraph/IRevisionGraphLayout.h"
33 //#include "RevisionGraph/FullGraphBuilder.h"
34 //#include "RevisionGraph/FullGraphFinalizer.h"
35 //#include "RevisionGraph/VisibleGraphBuilder.h"
36 //#include "RevisionGraph/StandardLayout.h"
37 //#include "RevisionGraph/ShowWC.h"
38 //#include "RevisionGraph/ShowWCModification.h"
40 #ifdef _DEBUG
41 #define new DEBUG_NEW
42 #undef THIS_FILE
43 static char THIS_FILE[] = __FILE__;
44 #endif
46 using namespace Gdiplus;
47 using namespace ogdf;
49 void CRevisionGraphWnd::InitView()
51 m_bIsCanvasMove = false;
53 SetScrollbars();
56 void CRevisionGraphWnd::BuildPreview()
59 m_Preview.DeleteObject();
60 if (!m_bShowOverview)
61 return;
63 // is there a point in drawing this at all?
65 int nodeCount = this->m_Graph.numberOfNodes();
66 if ((nodeCount > REVGRAPH_PREVIEW_MAX_NODES) || (nodeCount == 0))
67 return;
69 float origZoom = m_fZoomFactor;
71 CRect clientRect = GetClientRect();
72 CSize preViewSize (max (REVGRAPH_PREVIEW_WIDTH, clientRect.Width() / 4)
73 ,max (REVGRAPH_PREVIEW_HEIGHT, clientRect.Height() / 4));
75 // zoom the graph so that it is completely visible in the window
76 CRect graphRect = GetGraphRect();
77 float horzfact = float(graphRect.Width())/float(preViewSize.cx);
78 float vertfact = float(graphRect.Height())/float(preViewSize.cy);
79 m_previewZoom = min (DEFAULT_ZOOM, 1.0f/(max(horzfact, vertfact)));
81 // make sure the preview window has a minimal size
83 m_previewWidth = (int)min (max (graphRect.Width() * m_previewZoom, 30), preViewSize.cx);
84 m_previewHeight = (int)min (max (graphRect.Height() * m_previewZoom, 30), preViewSize.cy);
86 CClientDC ddc(this);
87 CDC dc;
88 if (!dc.CreateCompatibleDC(&ddc))
89 return;
91 m_Preview.CreateCompatibleBitmap(&ddc, m_previewWidth, m_previewHeight);
92 HBITMAP oldbm = (HBITMAP)dc.SelectObject (m_Preview);
94 // paint the whole graph
95 DoZoom (m_previewZoom, false);
96 CRect rect (0, 0, m_previewWidth, m_previewHeight);
97 GraphicsDevice dev;
98 dev.pDC = &dc;
99 DrawGraph(dev, rect, 0, 0, true);
101 // now we have a bitmap the size of the preview window
102 dc.SelectObject(oldbm);
103 dc.DeleteDC();
105 DoZoom (origZoom, false);
109 void CRevisionGraphWnd::SetScrollbar (int bar, int newPos, int clientMax, int graphMax)
111 SCROLLINFO ScrollInfo = {sizeof(SCROLLINFO), SIF_ALL};
112 GetScrollInfo (bar, &ScrollInfo);
114 clientMax = max(1, clientMax);
115 int oldHeight = ScrollInfo.nMax <= 0 ? clientMax : ScrollInfo.nMax;
116 int newHeight = static_cast<int>(graphMax * m_fZoomFactor);
117 int maxPos = max (0, newHeight - clientMax);
118 int pos = min (maxPos, newPos >= 0
119 ? newPos
120 : ScrollInfo.nPos * newHeight / oldHeight);
122 ScrollInfo.nPos = pos;
123 ScrollInfo.nMin = 0;
124 ScrollInfo.nMax = newHeight;
125 ScrollInfo.nPage = clientMax;
126 ScrollInfo.nTrackPos = pos;
128 SetScrollInfo(bar, &ScrollInfo);
132 void CRevisionGraphWnd::SetScrollbars (int nVert, int nHorz)
134 CRect clientrect = GetClientRect();
135 const CRect& pRect = GetGraphRect();
137 SetScrollbar (SB_VERT, nVert, clientrect.Height(), pRect.Height());
138 SetScrollbar (SB_HORZ, nHorz, clientrect.Width(), pRect.Width());
141 CRect CRevisionGraphWnd::GetGraphRect()
143 return m_GraphRect;
146 CRect CRevisionGraphWnd::GetClientRect()
148 CRect clientRect;
149 CWnd::GetClientRect (&clientRect);
150 return clientRect;
153 CRect CRevisionGraphWnd::GetWindowRect()
155 CRect windowRect;
156 CWnd::GetWindowRect (&windowRect);
157 return windowRect;
160 CRect CRevisionGraphWnd::GetViewRect()
162 CRect result;
163 result.UnionRect (GetClientRect(), GetGraphRect());
164 return result;
167 int CRevisionGraphWnd::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
169 UINT num = 0; // number of image encoders
170 UINT size = 0; // size of the image encoder array in bytes
172 if (GetImageEncodersSize(&num, &size)!=Ok)
173 return -1;
174 if(size == 0)
175 return -1; // Failure
177 ImageCodecInfo* pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
178 if(pImageCodecInfo == NULL)
179 return -1; // Failure
181 if (GetImageEncoders(num, size, pImageCodecInfo)==Ok)
183 for(UINT j = 0; j < num; ++j)
185 if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
187 *pClsid = pImageCodecInfo[j].Clsid;
188 free(pImageCodecInfo);
189 return j; // Success
194 free(pImageCodecInfo);
195 return -1; // Failure
198 bool CRevisionGraphWnd::FetchRevisionData
199 ( const CString& /*path*/
200 , GitRev /*pegRevision*/
201 , CProgressDlg* /*progress*/
202 , ITaskbarList3 * /*pTaskbarList*/
203 , HWND /*hWnd*/)
205 this->m_LogCache.ClearAllParent();
206 this->m_logEntries.ClearAll();
207 CString range;
208 if (!m_ToRev.IsEmpty() && !m_FromRev.IsEmpty())
209 range.Format(_T("%s..%s"), g_Git.FixBranchName(m_FromRev), g_Git.FixBranchName(m_ToRev));
210 else if (!m_ToRev.IsEmpty())
211 range = m_ToRev;
212 else if (!m_FromRev.IsEmpty())
213 range = m_FromRev;
214 this->m_logEntries.ParserFromLog(nullptr, -1, CGit::LOG_INFO_SIMPILFY_BY_DECORATION|(this->m_bCurrentBranch? 0: CGit::LOG_INFO_ALL_BRANCH), &range);
216 ReloadHashMap();
217 this->m_Graph.clear();
219 CArray<node> nodes;
220 GraphicsDevice dev;
221 dev.pDC = this->GetDC();
222 dev.graphics = Graphics::FromHDC(dev.pDC->m_hDC);
223 dev.graphics->SetPageUnit (UnitPixel);
225 m_HeadNode = nullptr;
227 for (size_t i = 0; i < m_logEntries.size(); ++i)
229 node nd;
230 nd = this->m_Graph.newNode();
231 nodes.Add(nd);
232 m_GraphAttr.width(nd)=100;
233 m_GraphAttr.height(nd)=20;
234 SetNodeRect(dev, &nd, m_logEntries[i], 0);
235 if (m_logEntries[i] == m_HeadHash)
236 m_HeadNode = nd;
239 for (size_t i = 0; i < m_logEntries.size(); ++i)
241 GitRev rev=m_logEntries.GetGitRevAt(i);
242 for (size_t j = 0; j < rev.m_ParentHash.size(); ++j)
244 if(m_logEntries.m_HashMap.find(rev.m_ParentHash[j]) == m_logEntries.m_HashMap.end())
246 TRACE(_T("Can't found parent node"));
247 //new parent node as new node
248 node nd;
249 nd = this->m_Graph.newNode();
250 m_Graph.newEdge(nodes[i], nd);
251 m_logEntries.push_back(rev.m_ParentHash[j]);
252 m_logEntries.m_HashMap[rev.m_ParentHash[j]] = (int)m_logEntries.size() -1;
253 nodes.Add(nd);
254 SetNodeRect(dev, &nd, rev.m_ParentHash[j], 0);
256 }else
258 TRACE(_T("edge %d - %d\n"),i, m_logEntries.m_HashMap[rev.m_ParentHash[j]]);
259 m_Graph.newEdge(nodes[i], nodes[m_logEntries.m_HashMap[rev.m_ParentHash[j]]]);
264 //this->m_OHL.layerDistance(30.0);
265 //this->m_OHL.nodeDistance(25.0);
266 //this->m_OHL.weightBalancing(0.8);
268 m_SugiyamLayout.call(m_GraphAttr);
270 node v;
271 double xmax = 0;
272 double ymax = 0;
273 forall_nodes(v,m_Graph)
275 double x = m_GraphAttr.x(v) + m_GraphAttr.width(v)/2;
276 double y = m_GraphAttr.y(v) + m_GraphAttr.height(v)/2;
277 if(x>xmax)
278 xmax = x;
279 if(y>ymax)
280 ymax = y;
283 this->m_GraphRect.top=m_GraphRect.left=0;
284 m_GraphRect.bottom = (LONG)ymax;
285 m_GraphRect.right = (LONG)xmax;
287 return true;
290 bool CRevisionGraphWnd::AnalyzeRevisionData()
292 #if 0
293 CSyncPointer<const CFullGraph> fullGraph (m_state.GetFullGraph());
294 if ((fullGraph.get() != NULL) && (fullGraph->GetNodeCount() > 0))
296 // filter graph
298 CSyncPointer<CAllRevisionGraphOptions> options (m_state.GetOptions());
299 options->Prepare();
301 std::unique_ptr<CVisibleGraph> visibleGraph (new CVisibleGraph());
302 CVisibleGraphBuilder builder ( *fullGraph
303 , *visibleGraph
304 , options->GetCopyFilterOptions());
305 builder.Run();
306 options->GetModificationOptions().Apply (visibleGraph.get());
308 index_t index = 0;
309 for (size_t i = 0, count = visibleGraph->GetRootCount(); i < count; ++i)
310 index = visibleGraph->GetRoot (i)->InitIndex (index);
312 // layout nodes
314 std::unique_ptr<CStandardLayout> newLayout
315 ( new CStandardLayout ( m_state.GetFullHistory()->GetCache()
316 , visibleGraph.get()
317 , m_state.GetFullHistory()->GetWCInfo()));
318 options->GetLayoutOptions().Apply (newLayout.get());
319 newLayout->Finalize();
321 // switch state
323 m_state.SetAnalysisResult (visibleGraph, newLayout);
326 return m_state.GetNodes().get() != NULL;
327 #endif
328 return true;
331 bool CRevisionGraphWnd::IsUpdateJobRunning() const
333 return (updateJob.get() != NULL) && !updateJob->IsDone();
336 bool CRevisionGraphWnd::GetShowOverview() const
338 return m_bShowOverview;
341 void CRevisionGraphWnd::SetShowOverview (bool value)
343 m_bShowOverview = value;
344 if (m_bShowOverview)
345 BuildPreview();
347 #if 0
348 void CRevisionGraphWnd::GetSelected
349 ( const CVisibleGraphNode* node
350 , bool head
351 , CTGitPath& path
352 , GitRev& rev
353 , GitRev& peg)
356 CString repoRoot = m_state.GetRepositoryRoot();
358 // get path and revision
360 path.SetFromSVN (repoRoot + CUnicodeUtils::GetUnicode (node->GetPath().GetPath().c_str()));
361 rev = head ? GitRev::REV_HEAD : node->GetRevision();
363 // handle 'modified WC' node
365 if (node->GetClassification().Is (CNodeClassification::IS_MODIFIED_WC))
367 path.SetFromUnknown (m_sPath);
368 rev = GitRev::REV_WC;
370 // don't set peg, if we aren't the first node
371 // (i.e. would not be valid for node1)
373 if (node == m_SelectedEntry1)
374 peg = GitRev::REV_WC;
376 else
378 // set head, if still necessary
380 if (head && !peg.IsValid())
381 peg = node->GetRevision();
385 #endif
387 CString CRevisionGraphWnd::GetFriendRefName(ogdf::node v)
389 if(v == NULL)
390 return CString();
391 CGitHash hash = this->m_logEntries[v->index()];
392 if(this->m_HashMap.find(hash) == m_HashMap.end())
393 return hash.ToString();
394 else if (m_HashMap[hash].empty())
395 return hash.ToString();
396 else if(this->m_HashMap[hash][0].IsEmpty())
397 return hash.ToString();
398 else
399 return m_HashMap[hash][0];
403 STRING_VECTOR CRevisionGraphWnd::GetFriendRefNames(ogdf::node v, CGit::REF_TYPE *refTypes, int refTypeCount)
405 if (v == NULL)
406 return STRING_VECTOR();
407 CGitHash hash = m_logEntries[v->index()];
408 if (m_HashMap.find(hash) == m_HashMap.end())
409 return STRING_VECTOR();
410 else if (m_HashMap[hash].empty())
411 return STRING_VECTOR();
412 else if (m_HashMap[hash][0].IsEmpty())
413 return STRING_VECTOR();
414 else
416 STRING_VECTOR &all = m_HashMap[hash];
417 STRING_VECTOR list;
418 for (size_t i = 0; i < all.size(); ++i)
420 CGit::REF_TYPE refType;
421 CString shortName = CGit::GetShortName(all[i], &refType);
422 if (refTypes == NULL)
423 list.push_back(shortName);
424 else
426 for (int i = 0; i < refTypeCount; ++i)
427 if (refTypes[i] == refType)
428 list.push_back(shortName);
431 return list;
435 void CRevisionGraphWnd::CompareRevs(const CString& revTo)
437 ASSERT(m_SelectedEntry1 != NULL);
438 ASSERT(!revTo.IsEmpty() || m_SelectedEntry2 != NULL);
440 // bool alternativeTool = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
442 CString sCmd;
444 sCmd.Format(_T("/command:showcompare %s /revision1:%s /revision2:%s"),
445 this->m_sPath.IsEmpty() ? _T("") : (_T("/path:\"") + this->m_sPath + _T("\"")),
446 GetFriendRefName(m_SelectedEntry1),
447 !revTo.IsEmpty() ? revTo : GetFriendRefName(m_SelectedEntry2));
449 CAppUtils::RunTortoiseGitProc(sCmd);
453 void CRevisionGraphWnd::UnifiedDiffRevs(bool bHead)
455 ASSERT(m_SelectedEntry1 != NULL);
456 ASSERT(bHead || m_SelectedEntry2 != NULL);
458 bool alternativeTool = !!(GetAsyncKeyState(VK_SHIFT) & 0x8000);
459 CAppUtils::StartShowUnifiedDiff(m_hWnd, CString(), GetFriendRefName(m_SelectedEntry1), CString(),
460 bHead? _T("HEAD"):GetFriendRefName(m_SelectedEntry2),
461 alternativeTool);
465 void CRevisionGraphWnd::DoZoom (float fZoomFactor, bool updateScrollbars)
467 float oldzoom = m_fZoomFactor;
468 m_fZoomFactor = fZoomFactor;
470 m_nFontSize = max(1, int(DEFAULT_ZOOM_FONT * fZoomFactor));
471 if (m_nFontSize < SMALL_ZOOM_FONT_THRESHOLD)
472 m_nFontSize = min (SMALL_ZOOM_FONT_THRESHOLD, int(SMALL_ZOOM_FONT * fZoomFactor));
474 for (int i = 0; i < MAXFONTS; ++i)
476 if (m_apFonts[i] != NULL)
478 m_apFonts[i]->DeleteObject();
479 delete m_apFonts[i];
481 m_apFonts[i] = NULL;
484 if (updateScrollbars)
486 SCROLLINFO si1 = {sizeof(SCROLLINFO), SIF_ALL};
487 GetScrollInfo(SB_VERT, &si1);
488 SCROLLINFO si2 = {sizeof(SCROLLINFO), SIF_ALL};
489 GetScrollInfo(SB_HORZ, &si2);
491 InitView();
493 si1.nPos = int(float(si1.nPos)*m_fZoomFactor/oldzoom);
494 si2.nPos = int(float(si2.nPos)*m_fZoomFactor/oldzoom);
495 SetScrollPos (SB_VERT, si1.nPos);
496 SetScrollPos (SB_HORZ, si2.nPos);
499 Invalidate (FALSE);