forked from olipfei/cbvcs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit_libgit2.cpp
153 lines (144 loc) · 5.59 KB
/
git_libgit2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* cbvcs Code::Blocks version control system plugin
Copyright (C) 2011 Dushara Jayasinghe.
Copyright (C) 2024 Christo Joseph
cbvcs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cbvcs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cbvcs. If not, see <http://www.gnu.org/licenses/>.
*/
#include "git_libgit2.h"
#include "icommandexecuter.h"
#include <git2.h>
#include <manager.h>
#include <wx/dir.h>
#include <wx/string.h>
LibGit2::LibGit2(const wxString &project, ICommandExecuter &cmdExecutor, wxString workDirectory)
: IVersionControlSystem(project, &m_GitUpdate, &m_GitAdd, &m_GitRemove, &m_GitCommit, &m_GitDiff, &m_GitRestore, &m_GitUpdateFull),
m_workDirectory(std::move(workDirectory)),
m_CmdExecutor(cmdExecutor),
m_GitUpdate(*this, m_GitRoot, m_CmdExecutor),
m_GitAdd(*this, m_GitRoot, m_CmdExecutor),
m_GitRemove(*this, m_GitRoot, m_CmdExecutor),
m_GitCommit(*this, m_GitRoot, m_CmdExecutor),
m_GitDiff(*this, m_GitRoot, m_CmdExecutor),
m_GitRestore(*this, m_GitRoot, m_CmdExecutor),
m_GitUpdateFull(*this, m_GitRoot, m_CmdExecutor)
{
git_libgit2_init();
m_GitRoot = QueryRoot(m_workDirectory.ToUTF8().data());
}
LibGit2::~LibGit2() { git_libgit2_shutdown(); }
wxString LibGit2::QueryRoot(const char *gitWorkDirInProject)
{
git_buf root = {0};
wxString ret;
git_libgit2_init();
int error = git_repository_discover(&root, gitWorkDirInProject, 0, NULL);
if (!error)
{
git_repository *repo;
error = git_repository_open(&repo, gitWorkDirInProject);
if (0 == error)
{
const char *workDir = git_repository_workdir(repo);
wxFileName workDirFileName(workDir);
wxFileName relativePath(gitWorkDirInProject);
if (workDirFileName != relativePath)
{
fprintf(stderr, "LibGit2::%s:%d workdir [%s] and workdir of project [%s] not same\n", __FUNCTION__, __LINE__, workDir,
gitWorkDirInProject);
if (workDirFileName.ResolveLink() == relativePath.ResolveLink())
{
ret = wxString(gitWorkDirInProject);
}
else
{
fprintf(stderr, "LibGit2::%s:%d workdir [%s] and realpath of workdir of project [%s] not same\n", __FUNCTION__, __LINE__, workDir,
gitWorkDirInProject);
ret = wxString(workDir);
}
}
else
{
ret = wxString(gitWorkDirInProject);
}
git_repository_free(repo);
}
else
{
const git_error *e = git_error_last();
fprintf(stderr, "LibGit2::%s:%d git_repository_open failed : %d/%d: %s\n", __FUNCTION__, __LINE__, error, e->klass, e->message);
}
}
else
{
const git_error *e = git_error_last();
fprintf(stderr, "LibGit2::%s:%d git_repository_discover failed : %d/%d: %s\n", __FUNCTION__, __LINE__, error, e->klass, e->message);
}
git_buf_free(&root);
git_libgit2_shutdown();
fprintf(stderr, "LibGit2::%s:%d return root : %s\n", __FUNCTION__, __LINE__, ret.ToUTF8().data());
return ret;
}
/*static*/ bool LibGit2::IsGitRepo(const wxString &project, ICommandExecuter &shellUtils, wxString *workDirectory)
{
bool ret;
git_repository *repo;
git_libgit2_init();
wxString projectPath = wxPathOnly(project);
git_buf root = {0};
int error = git_repository_discover(&root, projectPath, 0, NULL);
if (!error)
{
fprintf(stderr, "LibGit2::%s:%d project file's path is repo\n", __FUNCTION__, __LINE__);
ret = true;
if (workDirectory)
{
*workDirectory = root.ptr;
}
}
else
{
fprintf(stderr, "LibGit2::%s:%d projectPath %s not workdir\n", __FUNCTION__, __LINE__, projectPath.ToUTF8().data());
ret = false;
wxString subDirPath;
wxDir dir(projectPath);
if (!dir.IsOpened())
{
wxLogError("Cannot open directory '%s'.", projectPath);
}
else
{
bool cont = dir.GetFirst(&subDirPath, wxEmptyString, wxDIR_DIRS);
while (cont)
{
wxString subDirFullPath = projectPath + wxFILE_SEP_PATH + subDirPath;
if (0 == git_repository_open(&repo, subDirFullPath))
{
fprintf(stderr, "LibGit2::%s:%d subDirFullPath %s is workdir\n", __FUNCTION__, __LINE__, subDirFullPath.ToUTF8().data());
ret = true;
if (workDirectory)
{
*workDirectory = std::move(subDirFullPath);
}
git_repository_free(repo);
break;
}
else
{
fprintf(stderr, "LibGit2::%s:%d subDirFullPath %s not workdir\n", __FUNCTION__, __LINE__, subDirFullPath.ToUTF8().data());
}
cont = dir.GetNext(&subDirPath);
}
}
}
git_buf_free(&root);
git_libgit2_shutdown();
return ret;
}