KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_merge_blob_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
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 3
9 * of the License, or (at your option) any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
26
27#include <git2/buffer.h>
28#include <git2/deprecated.h>
29
30#include <cstring>
31
32
33namespace KIGIT
34{
35
36std::string BlobToString( git_blob* aBlob )
37{
38 if( !aBlob )
39 return {};
40
41 const char* raw = static_cast<const char*>( git_blob_rawcontent( aBlob ) );
42 std::size_t size = git_blob_rawsize( aBlob );
43 return std::string( raw, size );
44}
45
46
47int WriteToGitBuf( git_buf* aBuf, const std::string& aContent )
48{
49 if( !aBuf )
50 return -1;
51
52 if( git_buf_grow( aBuf, aContent.size() + 1 ) != 0 || !aBuf->ptr )
53 return -1;
54
55 std::memcpy( aBuf->ptr, aContent.data(), aContent.size() );
56 aBuf->ptr[aContent.size()] = '\0';
57 aBuf->size = aContent.size();
58 return 0;
59}
60
61
62int LoadMergeBlobs( const git_merge_driver_source* aSource, MERGE_BLOBS& aBlobs )
63{
64 git_repository* repo = const_cast<git_repository*>( git_merge_driver_source_repo( aSource ) );
65 const git_index_entry* ancestor = git_merge_driver_source_ancestor( aSource );
66 const git_index_entry* ours = git_merge_driver_source_ours( aSource );
67 const git_index_entry* theirs = git_merge_driver_source_theirs( aSource );
68
69 if( !ours || !theirs )
70 return GIT_PASSTHROUGH;
71
72 git_blob* ancestorBlob = nullptr;
73 git_blob* oursBlob = nullptr;
74 git_blob* theirsBlob = nullptr;
75
76 if( ancestor && git_blob_lookup( &ancestorBlob, repo, &ancestor->id ) != 0 )
77 return GIT_ENOTFOUND;
78
79 GitBlobPtr ancestorPtr( ancestorBlob );
80
81 if( git_blob_lookup( &oursBlob, repo, &ours->id ) != 0 )
82 return GIT_ENOTFOUND;
83
84 GitBlobPtr oursPtr( oursBlob );
85
86 if( git_blob_lookup( &theirsBlob, repo, &theirs->id ) != 0 )
87 return GIT_ENOTFOUND;
88
89 GitBlobPtr theirsPtr( theirsBlob );
90
91 aBlobs.ancestor = ancestorBlob ? BlobToString( ancestorBlob ) : std::string{};
92 aBlobs.ours = BlobToString( oursBlob );
93 aBlobs.theirs = BlobToString( theirsBlob );
94
95 return 0;
96}
97
98
99bool TryTrivialMerge( const MERGE_BLOBS& aBlobs, git_buf* aResult, int* aRc )
100{
101 if( aBlobs.ours == aBlobs.theirs )
102 {
103 *aRc = WriteToGitBuf( aResult, aBlobs.ours );
104 return true;
105 }
106
107 if( !aBlobs.ancestor.empty() && aBlobs.ancestor == aBlobs.ours )
108 {
109 *aRc = WriteToGitBuf( aResult, aBlobs.theirs );
110 return true;
111 }
112
113 if( !aBlobs.ancestor.empty() && aBlobs.ancestor == aBlobs.theirs )
114 {
115 *aRc = WriteToGitBuf( aResult, aBlobs.ours );
116 return true;
117 }
118
119 return false;
120}
121
122} // namespace KIGIT
int WriteToGitBuf(git_buf *aBuf, const std::string &aContent)
Allocate a libgit2-owned buffer big enough for aContent and copy the bytes plus a trailing NUL.
bool TryTrivialMerge(const MERGE_BLOBS &aBlobs, git_buf *aResult, int *aRc)
Resolve the trivial 3-way cases (identical sides, or one side unchanged from the ancestor).
int LoadMergeBlobs(const git_merge_driver_source *aSource, MERGE_BLOBS &aBlobs)
Look up the ancestor/ours/theirs blobs of a merge-driver source and decode them into aBlobs.
std::unique_ptr< git_blob, decltype([](git_blob *aBlob) { git_blob_free(aBlob); })> GitBlobPtr
A unique pointer for git_blob objects with automatic cleanup.
std::string BlobToString(git_blob *aBlob)
Copy a libgit2 blob's raw bytes into a std::string.
Decoded ancestor/ours/theirs blob contents for a 3-way merge driver.
std::string ancestor
Empty when there is no common ancestor.