KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_author.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, see <https://www.gnu.org/licenses/>.
18 */
19
22
23#include <git2.h>
24#include <wx/utils.h>
25
26
27bool KIGIT::ReadConfigString( git_config* aConfig, const char* aKey, wxString& aValue )
28{
29 git_config_entry* entry = nullptr;
30 int result = git_config_get_entry( &entry, aConfig, aKey );
31 GitConfigEntryPtr entryPtr( entry );
32
33 if( result != GIT_OK || !entry )
34 return false;
35
36 aValue = wxString::FromUTF8( entry->value );
37 return true;
38}
39
40
42{
43 LIBGIT2_SCOPE libgit2;
44
45 aLocal = GitUserConfig();
46 aGlobal = GitUserConfig();
47
48 git_config* config = nullptr;
49
50 // Without a repository only the levels above it exist
51 if( ( aRepo ? git_repository_config( &config, aRepo ) : git_config_open_default( &config ) ) != GIT_OK )
52 return;
53
54 GitConfigPtr configPtr( config );
55
56 auto readLevel = []( git_config* aAll, git_config_level_t aLevel, GitUserConfig& aOut )
57 {
58 git_config* level = nullptr;
59
60 if( git_config_open_level( &level, aAll, aLevel ) != GIT_OK )
61 return;
62
63 GitConfigPtr levelPtr( level );
64 wxString value;
65
66 if( aOut.authorName.IsEmpty() && ReadConfigString( level, "user.name", value ) )
67 aOut.authorName = value;
68
69 if( aOut.authorEmail.IsEmpty() && ReadConfigString( level, "user.email", value ) )
70 aOut.authorEmail = value;
71
72 aOut.hasName = !aOut.authorName.IsEmpty();
73 aOut.hasEmail = !aOut.authorEmail.IsEmpty();
74 };
75
76 readLevel( config, GIT_CONFIG_LEVEL_LOCAL, aLocal );
77
78 for( git_config_level_t level : { GIT_CONFIG_LEVEL_GLOBAL, GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_SYSTEM,
79 GIT_CONFIG_LEVEL_PROGRAMDATA } )
80 {
81 readLevel( config, level, aGlobal );
82 }
83}
84
85
87 const GitUserConfig& aGlobal, const wxString& aAccountName,
88 const wxString& aAccountEmail )
89{
90 auto firstSet = []( std::initializer_list<const wxString*> aCandidates ) -> wxString
91 {
92 for( const wxString* candidate : aCandidates )
93 {
94 if( !candidate->IsEmpty() )
95 return *candidate;
96 }
97
98 return wxString();
99 };
100
101 const wxString noValue;
102 const bool custom = !aPrefs.useDefaultAuthor;
103
104 GitUserConfig author;
105 author.authorName = firstSet( { &aLocal.authorName, custom ? &aPrefs.authorName : &noValue, &aGlobal.authorName,
106 &aAccountName } );
107 author.authorEmail = firstSet( { &aLocal.authorEmail, custom ? &aPrefs.authorEmail : &noValue,
108 &aGlobal.authorEmail, &aAccountEmail } );
109 author.hasName = !author.authorName.IsEmpty();
110 author.hasEmail = !author.authorEmail.IsEmpty();
111 return author;
112}
113
114
116{
117 if( aBefore.useDefaultAuthor != aAfter.useDefaultAuthor )
118 return true;
119
120 return !aAfter.useDefaultAuthor
121 && ( aBefore.authorName != aAfter.authorName || aBefore.authorEmail != aAfter.authorEmail );
122}
APIEXPORT bool ReadConfigString(git_config *aConfig, const char *aKey, wxString &aValue)
Read a git config value, decoded as UTF-8.
APIEXPORT void ReadAuthorConfig(git_repository *aRepo, GitUserConfig &aLocal, GitUserConfig &aGlobal)
Read the author set in a repository's own config and the one set in the global, XDG or system config.
APIEXPORT GitUserConfig ResolveAuthor(const COMMON_SETTINGS::GIT &aPrefs, const GitUserConfig &aLocal, const GitUserConfig &aGlobal, const wxString &aAccountName=wxEmptyString, const wxString &aAccountEmail=wxEmptyString)
Resolve the author recorded for new commits.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
std::unique_ptr< git_config_entry, decltype([](git_config_entry *aEntry) { git_config_entry_free(aEntry); })> GitConfigEntryPtr
A unique pointer for git_config_entry objects with automatic cleanup.
APIEXPORT bool AuthorPreferenceChanged(const COMMON_SETTINGS::GIT &aBefore, const COMMON_SETTINGS::GIT &aAfter)
Whether aAfter changes the author ResolveAuthor gives against aBefore.
Holds libgit2 initialized while it lives, for a helper that can run before any git backend exists.
wxString result
Test unit parsing edge cases and error handling.