Add PostCmdActions to GitProgressList
[TortoiseGit.git] / src / TortoiseProc / ProgressCommands / FetchProgressCommand.cpp
blob9a3ec6c2b21f0b190720a9490ff76241987e37fd
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 "FetchProgressCommand.h"
21 #include "AppUtils.h"
23 bool FetchProgressCommand::Run(CGitProgressList* list, CString& sWindowTitle, int& /*m_itemCountTotal*/, int& /*m_itemCount*/)
25 if (!g_Git.UsingLibGit2(CGit::GIT_CMD_FETCH))
27 // should never run to here
28 ASSERT(0);
29 return false;
32 list->SetWindowTitle(IDS_PROGRS_TITLE_FETCH, g_Git.m_CurrentDir, sWindowTitle);
33 list->SetBackgroundImage(IDI_UPDATE_BKG);
34 list->ReportCmd(CString(MAKEINTRESOURCE(IDS_PROGRS_TITLE_FETCH)) + _T(" ") + m_url.GetGitPathString() + _T(" ") + m_RefSpec);
36 CStringA url = CUnicodeUtils::GetUTF8(m_url.GetGitPathString());
38 CSmartAnimation animate(list->m_pAnimate);
40 CAutoRepository repo(g_Git.GetGitRepository());
41 if (!repo)
43 list->ReportGitError();
44 return false;
47 CAutoRemote remote;
48 // first try with a named remote (e.g. "origin")
49 if (git_remote_load(remote.GetPointer(), repo, url) < 0)
51 // retry with repository located at a specific url
52 if (git_remote_create_anonymous(remote.GetPointer(), repo, url, nullptr) < 0)
54 list->ReportGitError();
55 return false;
59 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
60 callbacks.update_tips = RemoteUpdatetipsCallback;
61 callbacks.sideband_progress = RemoteProgressCallback;
62 callbacks.transfer_progress = FetchCallback;
63 callbacks.completion = RemoteCompletionCallback;
64 callbacks.credentials = CAppUtils::Git2GetUserPassword;
65 callbacks.payload = list;
67 git_remote_set_callbacks(remote, &callbacks);
68 git_remote_set_autotag(remote, (git_remote_autotag_option_t)m_AutoTag);
70 if (!m_RefSpec.IsEmpty() && git_remote_add_fetch(remote, CUnicodeUtils::GetUTF8(m_RefSpec)))
71 goto error;
73 // Connect to the remote end specifying that we want to fetch
74 // information from it.
75 if (git_remote_connect(remote, GIT_DIRECTION_FETCH) < 0)
76 goto error;
78 // Download the packfile and index it. This function updates the
79 // amount of received data and the indexer stats which lets you
80 // inform the user about progress.
81 if (git_remote_download(remote) < 0)
82 goto error;
84 // Update the references in the remote's namespace to point to the
85 // right commits. This may be needed even if there was no packfile
86 // to download, which can happen e.g. when the branches have been
87 // changed but all the neede objects are available locally.
88 if (git_remote_update_tips(remote, nullptr, nullptr) < 0)
89 goto error;
91 git_remote_disconnect(remote);
93 // Not setting m_PostCmdCallback here, as clone is only called from AppUtils.cpp
95 return true;
97 error:
98 list->ReportGitError();
99 return false;