Allow to cancel cloning when using libgit2
[TortoiseGit.git] / src / TortoiseProc / ProgressCommands / RemoteProgressCommand.cpp
blob960306e26b303a191051385ed78cb6e02f950102
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2014 - 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.
19 #include "stdafx.h"
20 #include "RemoteProgressCommand.h"
21 #include "../TortoiseShell/resource.h"
22 #include "AppUtils.h"
24 int RemoteProgressCommand::RemoteProgressCallback(const char* str, int len, void* data)
26 ((CGitProgressList::Payload*)data)->list->SetProgressLabelText(CUnicodeUtils::GetUnicode(CStringA(str, len)));
27 if (((CGitProgressList::Payload*)data)->list->m_bCancelled)
29 giterr_set_str(GITERR_NONE, "User cancelled.");
30 return GIT_EUSER;
32 return 0;
35 int RemoteProgressCommand::RemoteCompletionCallback(git_remote_completion_type /*type*/, void* /*data*/)
37 // this method is unused by libgit2 so far
38 // TODO: "m_pAnimate->Stop();" and "m_pAnimate->ShowWindow(SW_HIDE);", cleanup possible in GitProgressList::Notify
39 return 0;
42 int RemoteProgressCommand::RemoteUpdatetipsCallback(const char* refname, const git_oid* oldOid, const git_oid* newOid, void* data)
44 auto ptr = (CGitProgressList::Payload*)data;
45 bool nonff = false;
46 if (!git_oid_iszero(oldOid) && !git_oid_iszero(newOid))
48 git_oid baseOid = { 0 };
49 if (!git_merge_base(&baseOid, ptr->repo, newOid, oldOid))
50 if (!git_oid_equal(oldOid, &baseOid))
51 nonff = true;
54 CString change;
55 if (!git_oid_iszero(oldOid) && !git_oid_iszero(newOid))
57 if (git_oid_equal(oldOid, newOid))
59 change = CString(MAKEINTRESOURCE(IDS_SAME));
61 else
63 size_t ahead = 0, behind = 0;
64 if (!git_graph_ahead_behind(&ahead, &behind, ptr->repo, newOid, oldOid))
66 if (ahead > 0 && behind == 0)
68 change.Format(IDS_FORWARDN, ahead);
70 else if (ahead == 0 && behind > 0)
72 change.Format(IDS_REWINDN, behind);
74 else
76 git_commit* oldCommit, * newCommit;
77 git_time_t oldTime = 0, newTime = 0;
78 if (!git_commit_lookup(&oldCommit, ptr->repo, oldOid))
79 oldTime = git_commit_committer(oldCommit)->when.time;
80 if (!git_commit_lookup(&newCommit, ptr->repo, newOid))
81 newTime = git_commit_committer(newCommit)->when.time;
82 if (oldTime < newTime)
83 change = CString(MAKEINTRESOURCE(IDS_SUBMODULEDIFF_NEWERTIME));
84 else if (oldTime > newTime)
85 change = CString(MAKEINTRESOURCE(IDS_SUBMODULEDIFF_OLDERTIME));
86 else
87 change = CString(MAKEINTRESOURCE(IDS_SUBMODULEDIFF_SAMETIME));
92 else if (!git_oid_iszero(oldOid))
93 change = CString(MAKEINTRESOURCE(IDS_DELETED));
94 else if (!git_oid_iszero(newOid))
95 change = CString(MAKEINTRESOURCE(IDS_NEW));
97 ptr->list->AddNotify(new RefUpdateNotificationData(refname, oldOid, newOid, change));
98 return 0;
101 RemoteProgressCommand::RefUpdateNotificationData::RefUpdateNotificationData(const char* refname, const git_oid* oldOid, const git_oid* newOid, const CString& change)
102 : NotificationData()
104 CString str = CUnicodeUtils::GetUnicode(refname);
105 m_NewHash = newOid->id;
106 m_OldHash = oldOid->id;
107 sActionColumnText.LoadString(IDS_GITACTION_UPDATE_REF);
108 sPathColumnText.Format(_T("%s\t %s -> %s (%s)"), str, m_OldHash.ToString().Left(g_Git.GetShortHASHLength()), m_NewHash.ToString().Left(g_Git.GetShortHASHLength()), change);
111 void RemoteProgressCommand::RefUpdateNotificationData::GetContextMenu(CIconMenu& popup, CGitProgressList::ContextMenuActionList& actions)
113 actions.push_back([&]()
115 CString cmd = _T("/command:log");
116 cmd += _T(" /path:\"") + g_Git.m_CurrentDir + _T("\"");
117 if (!m_OldHash.IsEmpty())
118 cmd += _T(" /startrev:") + m_OldHash.ToString();
119 if (!m_NewHash.IsEmpty())
120 cmd += _T(" /endrev:") + m_NewHash.ToString();
121 CAppUtils::RunTortoiseGitProc(cmd);
123 popup.AppendMenuIcon(actions.size(), IDS_MENULOG, IDI_LOG);