1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2011 - TortoiseSVN
4 // Copyright (C) 2012-2016 - 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.
21 #include "TortoiseProc.h"
23 #include "Revisiongraphdlg.h"
24 #include "MessageBox.h"
27 #include "UnicodeUtils.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"
43 static char THIS_FILE
[] = __FILE__
;
46 using namespace Gdiplus
;
49 void CRevisionGraphWnd::InitView()
51 m_bIsCanvasMove
= false;
56 void CRevisionGraphWnd::BuildPreview()
58 m_Preview
.DeleteObject();
62 // is there a point in drawing this at all?
64 int nodeCount
= this->m_Graph
.numberOfNodes();
65 if ((nodeCount
> REVGRAPH_PREVIEW_MAX_NODES
) || (nodeCount
== 0))
68 float origZoom
= m_fZoomFactor
;
70 CRect clientRect
= GetClientRect();
71 CSize
preViewSize (max (REVGRAPH_PREVIEW_WIDTH
, clientRect
.Width() / 4)
72 ,max (REVGRAPH_PREVIEW_HEIGHT
, clientRect
.Height() / 4));
74 // zoom the graph so that it is completely visible in the window
75 CRect graphRect
= GetGraphRect();
76 float horzfact
= float(graphRect
.Width())/float(preViewSize
.cx
);
77 float vertfact
= float(graphRect
.Height())/float(preViewSize
.cy
);
78 m_previewZoom
= min (DEFAULT_ZOOM
, 1.0f
/(max(horzfact
, vertfact
)));
80 // make sure the preview window has a minimal size
82 m_previewWidth
= (int)min (max (graphRect
.Width() * m_previewZoom
, 30), preViewSize
.cx
);
83 m_previewHeight
= (int)min (max (graphRect
.Height() * m_previewZoom
, 30), preViewSize
.cy
);
87 if (!dc
.CreateCompatibleDC(&ddc
))
90 m_Preview
.CreateCompatibleBitmap(&ddc
, m_previewWidth
, m_previewHeight
);
91 HBITMAP oldbm
= (HBITMAP
)dc
.SelectObject (m_Preview
);
93 // paint the whole graph
94 DoZoom (m_previewZoom
, false);
95 CRect
rect (0, 0, m_previewWidth
, m_previewHeight
);
98 DrawGraph(dev
, rect
, 0, 0, true);
100 // now we have a bitmap the size of the preview window
101 dc
.SelectObject(oldbm
);
104 DoZoom (origZoom
, false);
107 void CRevisionGraphWnd::SetScrollbar (int bar
, int newPos
, int clientMax
, int graphMax
)
109 SCROLLINFO ScrollInfo
= {sizeof(SCROLLINFO
), SIF_ALL
};
110 GetScrollInfo (bar
, &ScrollInfo
);
112 clientMax
= max(1, clientMax
);
113 int oldHeight
= ScrollInfo
.nMax
<= 0 ? clientMax
: ScrollInfo
.nMax
;
114 int newHeight
= static_cast<int>(graphMax
* m_fZoomFactor
);
115 int maxPos
= max (0, newHeight
- clientMax
);
116 int pos
= min (maxPos
, newPos
>= 0
118 : ScrollInfo
.nPos
* newHeight
/ oldHeight
);
120 ScrollInfo
.nPos
= pos
;
122 ScrollInfo
.nMax
= newHeight
;
123 ScrollInfo
.nPage
= clientMax
;
124 ScrollInfo
.nTrackPos
= pos
;
126 SetScrollInfo(bar
, &ScrollInfo
);
129 void CRevisionGraphWnd::SetScrollbars (int nVert
, int nHorz
)
131 CRect clientrect
= GetClientRect();
132 const CRect
& pRect
= GetGraphRect();
134 SetScrollbar (SB_VERT
, nVert
, clientrect
.Height(), pRect
.Height());
135 SetScrollbar (SB_HORZ
, nHorz
, clientrect
.Width(), pRect
.Width());
138 CRect
CRevisionGraphWnd::GetGraphRect()
143 CRect
CRevisionGraphWnd::GetClientRect()
146 CWnd::GetClientRect (&clientRect
);
150 CRect
CRevisionGraphWnd::GetWindowRect()
153 CWnd::GetWindowRect (&windowRect
);
157 CRect
CRevisionGraphWnd::GetViewRect()
160 result
.UnionRect (GetClientRect(), GetGraphRect());
164 int CRevisionGraphWnd::GetEncoderClsid(const WCHAR
* format
, CLSID
* pClsid
)
166 UINT num
= 0; // number of image encoders
167 UINT size
= 0; // size of the image encoder array in bytes
169 if (GetImageEncodersSize(&num
, &size
)!=Ok
)
172 return -1; // Failure
174 ImageCodecInfo
* pImageCodecInfo
= (ImageCodecInfo
*)(malloc(size
));
175 if (!pImageCodecInfo
)
176 return -1; // Failure
178 if (GetImageEncoders(num
, size
, pImageCodecInfo
)==Ok
)
180 for(UINT j
= 0; j
< num
; ++j
)
182 if( wcscmp(pImageCodecInfo
[j
].MimeType
, format
) == 0 )
184 *pClsid
= pImageCodecInfo
[j
].Clsid
;
185 free(pImageCodecInfo
);
191 free(pImageCodecInfo
);
192 return -1; // Failure
195 bool CRevisionGraphWnd::FetchRevisionData
196 ( const CString
& /*path*/
197 , CProgressDlg
* /*progress*/
198 , ITaskbarList3
* /*pTaskbarList*/
201 this->m_LogCache
.ClearAllParent();
202 this->m_logEntries
.ClearAll();
204 if (!m_ToRev
.IsEmpty() && !m_FromRev
.IsEmpty())
205 range
.Format(_T("%s..%s"), (LPCTSTR
)g_Git
.FixBranchName(m_FromRev
), (LPCTSTR
)g_Git
.FixBranchName(m_ToRev
));
206 else if (!m_ToRev
.IsEmpty())
208 else if (!m_FromRev
.IsEmpty())
210 DWORD infomask
= CGit::LOG_INFO_SIMPILFY_BY_DECORATION
| (m_bCurrentBranch
? 0 : m_bLocalBranches
? CGit::LOG_INFO_LOCAL_BRANCHES
: CGit::LOG_INFO_ALL_BRANCH
);
211 m_logEntries
.ParserFromLog(nullptr, 0, infomask
, &range
);
214 this->m_Graph
.clear();
218 dev
.pDC
= this->GetDC();
219 dev
.graphics
= Graphics::FromHDC(dev
.pDC
->m_hDC
);
220 dev
.graphics
->SetPageUnit (UnitPixel
);
222 m_HeadNode
= nullptr;
224 for (size_t i
= 0; i
< m_logEntries
.size(); ++i
)
227 nd
= this->m_Graph
.newNode();
229 m_GraphAttr
.width(nd
)=100;
230 m_GraphAttr
.height(nd
)=20;
231 SetNodeRect(dev
, &nd
, m_logEntries
[i
], 0);
232 if (m_logEntries
[i
] == m_HeadHash
)
236 for (size_t i
= 0; i
< m_logEntries
.size(); ++i
)
238 GitRev rev
=m_logEntries
.GetGitRevAt(i
);
239 for (size_t j
= 0; j
< rev
.m_ParentHash
.size(); ++j
)
241 if(m_logEntries
.m_HashMap
.find(rev
.m_ParentHash
[j
]) == m_logEntries
.m_HashMap
.end())
243 TRACE(_T("Can't found parent node"));
244 //new parent node as new node
246 nd
= this->m_Graph
.newNode();
247 m_Graph
.newEdge(nodes
[i
], nd
);
248 m_logEntries
.push_back(rev
.m_ParentHash
[j
]);
249 m_logEntries
.m_HashMap
[rev
.m_ParentHash
[j
]] = (int)m_logEntries
.size() -1;
251 SetNodeRect(dev
, &nd
, rev
.m_ParentHash
[j
], 0);
255 TRACE(_T("edge %d - %d\n"),i
, m_logEntries
.m_HashMap
[rev
.m_ParentHash
[j
]]);
256 m_Graph
.newEdge(nodes
[i
], nodes
[m_logEntries
.m_HashMap
[rev
.m_ParentHash
[j
]]]);
261 //this->m_OHL.layerDistance(30.0);
262 //this->m_OHL.nodeDistance(25.0);
263 //this->m_OHL.weightBalancing(0.8);
265 m_SugiyamLayout
.call(m_GraphAttr
);
270 forall_nodes(v
,m_Graph
)
272 double x
= m_GraphAttr
.x(v
) + m_GraphAttr
.width(v
)/2;
273 double y
= m_GraphAttr
.y(v
) + m_GraphAttr
.height(v
)/2;
280 this->m_GraphRect
.top
=m_GraphRect
.left
=0;
281 m_GraphRect
.bottom
= (LONG
)ymax
;
282 m_GraphRect
.right
= (LONG
)xmax
;
287 bool CRevisionGraphWnd::AnalyzeRevisionData()
290 CSyncPointer
<const CFullGraph
> fullGraph (m_state
.GetFullGraph());
291 if (fullGraph
.get() != nullptr && (fullGraph
->GetNodeCount() > 0))
295 CSyncPointer
<CAllRevisionGraphOptions
> options (m_state
.GetOptions());
298 auto visibleGraph
= std::make_unique
<CVisibleGraph
>();
299 CVisibleGraphBuilder
builder ( *fullGraph
301 , options
->GetCopyFilterOptions());
303 options
->GetModificationOptions().Apply (visibleGraph
.get());
306 for (size_t i
= 0, count
= visibleGraph
->GetRootCount(); i
< count
; ++i
)
307 index
= visibleGraph
->GetRoot (i
)->InitIndex (index
);
311 auto newLayout
= std::make_unique
<CStandardLayout
>
312 ( new CStandardLayout ( m_state
.GetFullHistory()->GetCache()
314 , m_state
.GetFullHistory()->GetWCInfo()));
315 options
->GetLayoutOptions().Apply (newLayout
.get());
316 newLayout
->Finalize();
320 m_state
.SetAnalysisResult (visibleGraph
, newLayout
);
323 return m_state
.GetNodes().get() != nullptr;
328 bool CRevisionGraphWnd::IsUpdateJobRunning() const
330 return (updateJob
.get() != nullptr) && !updateJob
->IsDone();
333 bool CRevisionGraphWnd::GetShowOverview() const
335 return m_bShowOverview
;
338 void CRevisionGraphWnd::SetShowOverview (bool value
)
340 m_bShowOverview
= value
;
345 void CRevisionGraphWnd::GetSelected
346 ( const CVisibleGraphNode
* node
352 CString repoRoot
= m_state
.GetRepositoryRoot();
354 // get path and revision
356 path
.SetFromSVN (repoRoot
+ CUnicodeUtils::GetUnicode (node
->GetPath().GetPath().c_str()));
357 rev
= head
? GitRev::REV_HEAD
: node
->GetRevision();
359 // handle 'modified WC' node
361 if (node
->GetClassification().Is (CNodeClassification::IS_MODIFIED_WC
))
363 path
.SetFromUnknown (m_sPath
);
364 rev
= GitRev::REV_WC
;
366 // don't set peg, if we aren't the first node
367 // (i.e. would not be valid for node1)
369 if (node
== m_SelectedEntry1
)
370 peg
= GitRev::REV_WC
;
374 // set head, if still necessary
376 if (head
&& !peg
.IsValid())
377 peg
= node
->GetRevision();
382 CString
CRevisionGraphWnd::GetFriendRefName(ogdf::node v
)
386 CGitHash hash
= this->m_logEntries
[v
->index()];
387 if(this->m_HashMap
.find(hash
) == m_HashMap
.end())
388 return hash
.ToString();
389 else if (m_HashMap
[hash
].empty())
390 return hash
.ToString();
391 else if(this->m_HashMap
[hash
][0].IsEmpty())
392 return hash
.ToString();
394 return m_HashMap
[hash
][0];
397 STRING_VECTOR
CRevisionGraphWnd::GetFriendRefNames(ogdf::node v
, CGit::REF_TYPE
*refTypes
, int refTypeCount
)
400 return STRING_VECTOR();
401 CGitHash hash
= m_logEntries
[v
->index()];
402 if (m_HashMap
.find(hash
) == m_HashMap
.end())
403 return STRING_VECTOR();
404 else if (m_HashMap
[hash
].empty())
405 return STRING_VECTOR();
406 else if (m_HashMap
[hash
][0].IsEmpty())
407 return STRING_VECTOR();
410 STRING_VECTOR
&all
= m_HashMap
[hash
];
412 for (size_t i
= 0; i
< all
.size(); ++i
)
414 CGit::REF_TYPE refType
;
415 CString shortName
= CGit::GetShortName(all
[i
], &refType
);
417 list
.push_back(shortName
);
420 for (int j
= 0; j
< refTypeCount
; ++j
)
421 if (refTypes
[j
] == refType
)
422 list
.push_back(shortName
);
429 void CRevisionGraphWnd::CompareRevs(const CString
& revTo
)
431 ASSERT(m_SelectedEntry1
);
432 ASSERT(!revTo
.IsEmpty() || m_SelectedEntry2
);
434 bool alternativeTool
= !!(GetAsyncKeyState(VK_SHIFT
) & 0x8000);
438 sCmd
.Format(_T("/command:showcompare %s /revision1:%s /revision2:%s"),
439 this->m_sPath
.IsEmpty() ? _T("") : (LPCTSTR
)(_T("/path:\"") + this->m_sPath
+ _T("\"")),
440 (LPCTSTR
)GetFriendRefName(m_SelectedEntry1
),
441 !revTo
.IsEmpty() ? (LPCTSTR
)revTo
: (LPCTSTR
)GetFriendRefName(m_SelectedEntry2
));
444 sCmd
+= L
" /alternative";
446 CAppUtils::RunTortoiseGitProc(sCmd
);
449 void CRevisionGraphWnd::UnifiedDiffRevs(bool bHead
)
451 ASSERT(m_SelectedEntry1
);
452 ASSERT(bHead
|| m_SelectedEntry2
);
454 bool alternativeTool
= !!(GetAsyncKeyState(VK_SHIFT
) & 0x8000);
455 CAppUtils::StartShowUnifiedDiff(m_hWnd
, CString(), GetFriendRefName(m_SelectedEntry1
), CString(),
456 bHead
? _T("HEAD"):GetFriendRefName(m_SelectedEntry2
),
460 void CRevisionGraphWnd::DoZoom (float fZoomFactor
, bool updateScrollbars
)
462 float oldzoom
= m_fZoomFactor
;
463 m_fZoomFactor
= fZoomFactor
;
465 m_nFontSize
= max(1, int(DEFAULT_ZOOM_FONT
* fZoomFactor
));
466 if (m_nFontSize
< SMALL_ZOOM_FONT_THRESHOLD
)
467 m_nFontSize
= min (SMALL_ZOOM_FONT_THRESHOLD
, int(SMALL_ZOOM_FONT
* fZoomFactor
));
469 for (int i
= 0; i
< MAXFONTS
; ++i
)
473 m_apFonts
[i
]->DeleteObject();
476 m_apFonts
[i
] = nullptr;
479 if (updateScrollbars
)
481 SCROLLINFO si1
= {sizeof(SCROLLINFO
), SIF_ALL
};
482 GetScrollInfo(SB_VERT
, &si1
);
483 SCROLLINFO si2
= {sizeof(SCROLLINFO
), SIF_ALL
};
484 GetScrollInfo(SB_HORZ
, &si2
);
488 si1
.nPos
= int(float(si1
.nPos
)*m_fZoomFactor
/oldzoom
);
489 si2
.nPos
= int(float(si2
.nPos
)*m_fZoomFactor
/oldzoom
);
490 SetScrollPos (SB_VERT
, si1
.nPos
);
491 SetScrollPos (SB_HORZ
, si2
.nPos
);