38#include <wx/filename.h>
39#include <wx/stdpaths.h>
60 wxString tmp = wxStandardPaths::Get().GetTempDir() + wxFileName::GetPathSeparator()
61 + wxString::Format( wxS(
"kicad_qa_cmp_%lu_%ld" ),
62 static_cast<unsigned long>( wxGetProcessId() ),
65 wxFileName::Mkdir( tmp, 0700, wxPATH_MKDIR_FULL );
75 git_repository_free(
m_repo );
78 wxFileName::Rmdir(
m_repoPath, wxPATH_RMDIR_RECURSIVE );
80 git_libgit2_shutdown();
85 void writeFile(
const wxString& aFile,
const wxString& aContent )
87 wxString filePath =
m_repoPath + wxFileName::GetPathSeparator() + aFile;
88 std::ofstream f( filePath.ToStdString(), std::ios::trunc );
89 f << aContent.ToStdString();
95 wxRemoveFile(
m_repoPath + wxFileName::GetPathSeparator() + aFile );
102 git_index*
index =
nullptr;
109 char* paths[] = {
const_cast<char*
>(
"*" ) };
110 git_strarray pathspec = { paths, 1 };
112 if( git_index_add_all(
index, &pathspec, GIT_INDEX_ADD_DEFAULT,
nullptr,
nullptr ) != 0 )
115 git_index_write(
index );
119 if( git_index_write_tree( &treeOid,
index ) != 0 )
122 git_tree* tree =
nullptr;
124 if( git_tree_lookup( &tree,
m_repo, &treeOid ) != 0 )
129 git_signature* sig =
nullptr;
133 git_commit* parent =
nullptr;
134 git_reference* headRef =
nullptr;
136 if( git_repository_head( &headRef,
m_repo ) == 0 )
138 git_reference_peel(
reinterpret_cast<git_object**
>( &parent ), headRef,
140 git_reference_free( headRef );
144 const git_commit* parents[1] = { parent };
145 const git_commit** parentsPtr = parent ? parents :
nullptr;
146 size_t parentsCount = parent ? 1 : 0;
150 return git_commit_create( &commitOid,
m_repo,
"HEAD", sig, sig,
nullptr,
151 aMessage.ToUTF8().data(), tree, parentsCount, parentsPtr )
165 git_repository_init_options initOpts;
166 git_repository_init_options_init( &initOpts, GIT_REPOSITORY_INIT_OPTIONS_VERSION );
167 initOpts.initial_head =
"master";
169 if( git_repository_init_ext( &
m_repo,
m_repoPath.ToUTF8().data(), &initOpts ) != 0 )
172 git_config* cfg =
nullptr;
174 if( git_repository_config( &cfg,
m_repo ) == 0 )
178 git_config_free( cfg );
181 writeFile( wxT(
"a.txt" ), wxT(
"alpha\n" ) );
182 writeFile( wxT(
"b.txt" ), wxT(
"bravo\n" ) );
184 if( !
commitAll( wxT(
"First commit" ) ) )
187 writeFile( wxT(
"a.txt" ), wxT(
"alpha modified\n" ) );
189 writeFile( wxT(
"c.txt" ), wxT(
"charlie\n" ) );
191 return commitAll( wxT(
"Second commit" ) );
213 BOOST_CHECK( head.get() !=
nullptr );
216 BOOST_CHECK( prev.get() !=
nullptr );
219 BOOST_CHECK( head.get() != prev.get() );
250 git_diff* diff =
nullptr;
251 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
254 git_diff_tree_to_tree( &diff, repo(), baseTree.get(), headTree.get(), &opts ), 0 );
258 std::map<wxString, git_delta_t> seen;
260 [&seen](
const git_diff_delta& aDelta )
262 const char*
path = aDelta.new_file.path ? aDelta.new_file.path
263 : aDelta.old_file.path;
264 seen[wxString::FromUTF8(
path )] = aDelta.status;
268 BOOST_CHECK( seen.at( wxT(
"a.txt" ) ) == GIT_DELTA_MODIFIED );
269 BOOST_CHECK( seen.at( wxT(
"b.txt" ) ) == GIT_DELTA_DELETED );
270 BOOST_CHECK( seen.at( wxT(
"c.txt" ) ) == GIT_DELTA_ADDED );
278 std::vector<KIGIT::CHANGED_FILE> changes =
281 std::map<wxString, KIGIT::FILE_CHANGE_STATUS> byPath;
284 byPath[f.path] = f.status;
static bool empty(const wxTextEntryBase *aCtrl)
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
std::vector< CHANGED_FILE > CompareRefs(git_repository *aRepo, const wxString &aBaseRef, const wxString &aHeadRef)
Compare two git refs (branch / tag / commit OID) within a repository and return the per-file change l...
git_tree * ResolveRefToTree(git_repository *aRepo, const wxString &aRef)
Resolve a string ref (branch name, short OID, full OID, tag) to its tree.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
void CollectDiffDeltas(git_diff *aDiff, const std::function< void(const git_diff_delta &)> &aCallback)
Walk every delta in a computed diff, invoking aCallback once per delta.
std::unique_ptr< git_signature, decltype([](git_signature *aSignature) { git_signature_free(aSignature); })> GitSignaturePtr
A unique pointer for git_signature objects with automatic cleanup.
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex); })> GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
std::unique_ptr< git_diff, decltype([](git_diff *aDiff) { git_diff_free(aDiff); })> GitDiffPtr
A unique pointer for git_diff objects with automatic cleanup.
Build a temp working repo with two commits.
git_repository * repo() const
void removeFile(const wxString &aFile)
bool commitAll(const wxString &aMessage)
Stage every change (including deletions) and create a commit on HEAD.
void writeFile(const wxString &aFile, const wxString &aContent)
Write a file in the working tree (or remove it if aRemove is set).
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
static const char * TEST_AUTHOR_NAME
BOOST_AUTO_TEST_CASE(ResolveRefToTree_Valid)
static const char * TEST_AUTHOR_EMAIL
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")