QtPass Developer documentation
QtPass test coverage
Current view: top level - src/profileinit.cpp (source / functions) Coverage Total Hit
Test: QtPass Lines: 95.1 % 263 250
Test Date: 2026-12-03 23:34:20 Functions: 100.0 % 22 22
Branches: 83.4 % 283 236

             Branch data     Line data    Source code
       1                 :             : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
       2                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       3                 :             : /**
       4                 :             :  * @file
       5                 :             :  * @brief Implements ProfileInit; the environments its gpg and git processes
       6                 :             :  * run in are built by withGpgHome() and gitEnvironment() here.
       7                 :             :  */
       8                 :             : #include "profileinit.h"
       9                 :             : #include "appsettings.h"
      10                 :             : #include "executor.h"
      11                 :             : #include "gpgidgeneration.h"
      12                 :             : #include "gpgidsigner.h"
      13                 :             : #include "qtpasslogging.h"
      14                 :             : #include "userinfo.h"
      15                 :             : #include "util.h"
      16                 :             : #include <QDir>
      17                 :             : #include <QElapsedTimer>
      18                 :             : #include <QFile>
      19                 :             : #include <QFileInfo>
      20                 :             : #include <QHash>
      21                 :             : #include <QProcess>
      22                 :             : #include <QSet>
      23                 :             : #include <algorithm>
      24                 :             : #include <optional>
      25                 :             : #include <utility>
      26                 :             : 
      27                 :             : namespace {
      28                 :             : /// @p env with the configured GPG home applied as Pass::init() applies it:
      29                 :             : /// GNUPGHOME is that folder when it exists; otherwise the inherited one
      30                 :             : /// stays. The new store's .gpg-id is signed, and a signed commit made, with
      31                 :             : /// the keyring QtPass uses everywhere else, where the signing key may be
      32                 :             : /// the only place it is.
      33                 :         305 : auto withGpgHome(QProcessEnvironment env, const AppSettings &s)
      34                 :             :     -> QProcessEnvironment {
      35         [ +  + ]:         305 :   if (!s.gpgHome.isEmpty()) {
      36                 :          13 :     QDir home(s.gpgHome);
      37                 :          13 :     home.makeAbsolute();
      38      [ +  +  + ]:          13 :     if (home.exists()) {
      39                 :          20 :       env.insert(QStringLiteral("GNUPGHOME"), home.path());
      40                 :             :     }
      41                 :          13 :   }
      42                 :         305 :   return env;
      43                 :             : }
      44                 :             : 
      45                 :             : /// As Pass::updateEnv() sets it for the active store: git in @p dir does not
      46                 :             : /// look above it, so neither the identity check nor a commit reads or uses a
      47                 :             : /// repository the new store merely lies in (a dotfiles one in the home
      48                 :             : /// folder), unless the user opted into that; and paths are names to it, not
      49                 :             : /// patterns.
      50                 :          33 : auto gitEnvironment(const QString &dir, const AppSettings &s)
      51                 :             :     -> QProcessEnvironment {
      52                 :             :   QProcessEnvironment env =
      53                 :          33 :       withGpgHome(QProcessEnvironment::systemEnvironment(), s);
      54                 :             :   // As for the active store: the paths handed to git are names, not
      55                 :             :   // patterns (see Pass::updateEnv()).
      56                 :          66 :   env.insert(QStringLiteral("GIT_LITERAL_PATHSPECS"), QStringLiteral("1"));
      57                 :             :   // The identity check runs git before `git init` made the folder a
      58                 :             :   // repository, so a pushed bare-repository layout there would be read.
      59                 :          33 :   Util::requireExplicitBareRepository(env);
      60         [ +  + ]:          33 :   if (!s.gitUseParentRepository) {
      61                 :          32 :     const auto variables = Util::gitStoreVariables(dir, s.gitExecutable);
      62         [ +  + ]:          64 :     for (const auto &[name, value] : variables) {
      63                 :          32 :       env.insert(name, value);
      64                 :             :     }
      65                 :             :   }
      66                 :          33 :   return env;
      67                 :           0 : }
      68                 :             : /// "commit", and "-S" when git config pass.signcommits is true for the
      69                 :             : /// repository @p git runs in (a new one: so the user's own git config), as
      70                 :             : /// pass signs its commits.
      71                 :             : /// Time `git init` and `git add` may take while the window waits: the two
      72                 :             : /// that read what lies in the folder. A commit is not bounded: git may sign
      73                 :             : /// it (pass.signcommits or commit.gpgSign) and wait for the passphrase.
      74                 :             : constexpr int kGitSetupTimeoutMs = 2 * 60 * 1000;
      75                 :             : 
      76                 :             : /**
      77                 :             :  * @brief The first thing git would open while staging @p files in @p dir
      78                 :             :  * that it could wait on for good, or empty.
      79                 :             :  *
      80                 :             :  * git add opens the .gitattributes and .gitignore of each folder it stages
      81                 :             :  * from, by name: a FIFO there, planted in a shared folder, kept it waiting
      82                 :             :  * in open() with the window waiting on it. A link to one counts as one:
      83                 :             :  * older git follows it. A sub-folder with a .git of its own is a repository git
      84                 :             :  * looks into (its HEAD, say).
      85                 :             :  * @param dir The store.
      86                 :             :  * @param files Paths relative to @p dir that git is to stage.
      87                 :             :  * @return That path, or empty.
      88                 :             :  */
      89                 :          23 : auto blocksGit(const QString &dir, const QStringList &files) -> QString {
      90                 :          46 :   QSet<QString> folders{QStringLiteral(".")};
      91         [ +  + ]:          60 :   for (const QString &file : files) {
      92         [ +  + ]:          37 :     for (QString folder = QFileInfo(file).path();
      93   [ +  +  +  - ]:          48 :          folder != QLatin1String(".") && !folders.contains(folder);
      94                 :          22 :          folder = QFileInfo(folder).path()) {
      95                 :          11 :       folders.insert(folder);
      96                 :             :     }
      97                 :             :   }
      98                 :          23 :   const QDir base(dir);
      99                 :             :   for (const QString &folder : std::as_const(folders)) {
     100                 :          29 :     const QDir here(base.filePath(folder));
     101   [ +  -  +  + ]:          82 :     for (const char *name : {".gitattributes", ".gitignore"}) {
     102                 :          57 :       const QFileInfo info(here.filePath(QLatin1String(name)));
     103   [ +  +  +  +  :          57 :       if (info.exists() && !info.isFile()) {
                   +  + ]
     104                 :           4 :         return info.filePath();
     105                 :             :       }
     106                 :          57 :     }
     107                 :          50 :     const QFileInfo nested(here.filePath(QStringLiteral(".git")));
     108   [ +  +  +  + ]:          31 :     if (folder != QLatin1String(".") &&
     109   [ +  +  -  + ]:          11 :         (nested.exists() || nested.isSymLink())) {
     110                 :           1 :       return nested.filePath();
     111                 :             :     }
     112                 :          29 :   }
     113                 :          18 :   return {};
     114                 :          23 : }
     115                 :             : 
     116                 :             : /**
     117                 :             :  * @brief The signer for the store set up with @p s: gpg runs in the
     118                 :             :  * configured GPG home, as ImitatePass's signer runs in the store's
     119                 :             :  * environment.
     120                 :             :  * @param s The settings, with the signing key and gpg to use.
     121                 :             :  * @return The signer.
     122                 :             :  */
     123                 :          15 : auto signerFor(const AppSettings &s) -> GpgIdSigner {
     124                 :             :   GpgIdSigner signer(
     125                 :          30 :       s.gpgExecutable, GpgIdSigner::keysFromSetting(s.passSigningKey),
     126                 :          66 :       [s](const QString &app, const QStringList &args, const QString &input,
     127                 :             :           QString *out, QString *err) {
     128                 :          20 :         QProcess gpg;
     129                 :          20 :         gpg.setProcessEnvironment(
     130         [ +  + ]:          40 :             withGpgHome(QProcessEnvironment::systemEnvironment(), s));
     131                 :          40 :         return Executor::executeBlocking(gpg, app, args, input, out, err);
     132                 :          50 :       });
     133                 :          15 :   signer.setStoreRoot(s.passStore);
     134                 :          15 :   return signer;
     135                 :             : }
     136                 :             : 
     137                 :           9 : auto commitArgs(QProcess &git, const AppSettings &s) -> QStringList {
     138                 :           9 :   QString out;
     139                 :             :   const bool sign =
     140   [ +  +  -  - ]:          54 :       Executor::executeBlocking(
     141                 :           9 :           git, s.gitExecutable,
     142                 :           9 :           {QStringLiteral("config"), QStringLiteral("--bool"),
     143                 :           9 :            QStringLiteral("--get"), QStringLiteral("pass.signcommits")},
     144         [ +  + ]:          18 :           QString(), &out, nullptr) == 0 &&
     145                 :          13 :       out.trimmed() == QLatin1String("true");
     146   [ -  +  -  + ]:          18 :   return sign ? QStringList{QStringLiteral("commit"), QStringLiteral("-S")}
     147                 :          27 :               : QStringList{QStringLiteral("commit")};
     148                 :             : }
     149                 :             : } // namespace
     150                 :             : 
     151                 :         252 : auto ProfileInit::gpgEnvironment(const AppSettings &s) -> QProcessEnvironment {
     152                 :         252 :   return withGpgHome(QProcessEnvironment::systemEnvironment(), s);
     153                 :             : }
     154                 :             : 
     155                 :          11 : auto ProfileInit::needsInit(const QString &path) -> bool {
     156         [ +  + ]:          11 :   if (path.isEmpty()) {
     157                 :             :     return false;
     158                 :             :   }
     159                 :          10 :   QDir dir(path);
     160      [ +  +  + ]:          10 :   if (!dir.exists()) {
     161                 :             :     return false;
     162                 :             :   }
     163         [ +  + ]:           9 :   return !dir.exists(".gpg-id");
     164                 :          10 : }
     165                 :             : 
     166                 :          32 : auto ProfileInit::initialise(const QString &dir, const QList<UserInfo> &users,
     167                 :             :                              const AppSettings &settings, bool useGit,
     168                 :             :                              QString *note) -> bool {
     169                 :             :   // A key exported for pass counts when none is set, as in Pass::init().
     170                 :          32 :   AppSettings s = settings;
     171                 :          32 :   s.passSigningKey = GpgIdSigner::settingOrExported(s.passSigningKey);
     172                 :          32 :   QString scratch;
     173         [ +  - ]:          32 :   QString &out = note != nullptr ? *note : scratch;
     174                 :          32 :   out.clear();
     175                 :             : 
     176                 :          32 :   const QDir folder(dir);
     177   [ +  +  +  +  :          38 :   if (!folder.exists() && !Util::mkpathForStore(folder.absolutePath())) {
             +  +  +  +  
                      + ]
     178                 :           2 :     out = tr("Could not create %1.").arg(folder.absolutePath());
     179                 :           1 :     return false;
     180                 :             :   }
     181                 :             :   // Before anything is written: a refusal later would leave a .gpg-id
     182                 :             :   // behind, and a store that looks finished.
     183         [ +  + ]:          31 :   if (useGit &&
     184   [ +  +  +  +  :          55 :       refusedForGit(folder.absolutePath(),
          +  +  +  +  -  
                      - ]
     185                 :          31 :                     {QStringLiteral(".gpg-id"), QStringLiteral(".gpg-id.sig")},
     186                 :             :                     &out)) {
     187                 :             :     return false;
     188                 :             :   }
     189                 :          60 :   const QString gpgIdFile = folder.filePath(QStringLiteral(".gpg-id"));
     190                 :             :   // Before the list is written, as ImitatePass::Init() does: writing it
     191                 :             :   // takes the next generation, and a list that then cannot be signed left
     192                 :             :   // this device's record one ahead of the store's own, refused afterwards
     193                 :             :   // as a rollback.
     194      [ +  +  + ]:          30 :   if (signingKeyUnavailable(s, &out)) {
     195                 :             :     return false;
     196                 :             :   }
     197                 :          26 :   QByteArray written;
     198         [ +  + ]:          26 :   if (!writeGpgId(gpgIdFile, users, !s.passSigningKey.trimmed().isEmpty(), &out,
     199                 :             :                   &written)) {
     200                 :             :     return false;
     201                 :             :   }
     202                 :          23 :   QString sigFile;
     203         [ +  + ]:          23 :   if (!s.passSigningKey.trimmed().isEmpty()) {
     204      [ +  +  + ]:           5 :     if (!signGpgId(gpgIdFile, written, s, &out)) {
     205                 :             :       return false;
     206                 :             :     }
     207                 :           4 :     sigFile = gpgIdFile + QStringLiteral(".sig");
     208                 :             :   }
     209   [ +  +  +  + ]:          25 :   if (useGit &&
     210   [ +  +  +  + ]:          25 :       !commitGpgId(folder.absolutePath(), gpgIdFile, sigFile, s, &out)) {
     211                 :             :     return false;
     212                 :             :   }
     213                 :             : 
     214                 :             :   const QStringList existing =
     215                 :          36 :       entriesOnThisList(folder.absolutePath(), s.usePass);
     216         [ +  + ]:          18 :   if (!existing.isEmpty()) {
     217                 :          11 :     out = tr("%1 already contains %n encrypted file(s); they were not "
     218                 :             :              "re-encrypted. Switch to the profile and open Users to do that.",
     219                 :             :              nullptr, static_cast<int>(existing.size()))
     220                 :          22 :               .arg(folder.absolutePath());
     221                 :             :   }
     222                 :             :   return true;
     223                 :          64 : }
     224                 :             : 
     225                 :          30 : auto ProfileInit::signingKeyUnavailable(const AppSettings &s, QString *note)
     226                 :             :     -> bool {
     227   [ +  +  +  +  :          30 :   if (s.passSigningKey.trimmed().isEmpty() || signerFor(s).haveSecretKey()) {
             +  +  +  -  
                      - ]
     228                 :             :     return false;
     229                 :             :   }
     230                 :           4 :   *note = tr("The secret key of the signing key %1 is not available, so no "
     231                 :             :              "recipient list was written.")
     232                 :           8 :               .arg(GpgIdSigner::keysFromSetting(s.passSigningKey).constFirst());
     233                 :             :   // withGpgHome() fell back to the inherited home, as for signing.
     234         [ +  + ]:           4 :   if (!s.gpgHome.isEmpty()) {
     235                 :           1 :     QDir home(s.gpgHome);
     236                 :           1 :     home.makeAbsolute();
     237      [ +  +  - ]:           1 :     if (!home.exists()) {
     238                 :           1 :       *note += QLatin1Char(' ') + tr("The configured GPG home %1 does not "
     239                 :             :                                      "exist.")
     240                 :           3 :                                       .arg(home.path());
     241                 :             :     }
     242                 :           1 :   }
     243                 :             :   return true;
     244                 :             : }
     245                 :             : 
     246                 :          18 : auto ProfileInit::entriesOnThisList(const QString &dir, bool viaPass)
     247                 :             :     -> QStringList {
     248                 :             :   // Entries in sub-folders too: a store taken over whole has most of them
     249                 :             :   // there. Visible regular files only, not through links, .git not walked.
     250                 :             :   // Not those under a sub-folder with a .gpg-id of its own: its recipients
     251                 :             :   // did not change here, and Users on the profile's root leaves it alone,
     252                 :             :   // as pass does.
     253                 :          18 :   const QString root = QDir::cleanPath(dir);
     254                 :             :   const QString rootList =
     255                 :          54 :       QFileInfo(QDir(root).filePath(QStringLiteral(".gpg-id")))
     256                 :          18 :           .canonicalFilePath();
     257                 :             :   // What a folder's own .gpg-id decides, if it has one.
     258                 :         611 :   const auto ownList = [&rootList,
     259                 :             :                         viaPass](const QString &folder) -> std::optional<bool> {
     260                 :             :     // A list of its own: a regular file. A linked .gpg-id is no list to
     261                 :             :     // the native backend, as everywhere in QtPass, so its folder's entries
     262                 :             :     // still count; pass follows the link (`-f`) and keeps that folder on
     263                 :             :     // the linked list, so with it they do not, unless the link leads to the
     264                 :             :     // root's own list: that folder follows the root, and Users on it
     265                 :             :     // re-encrypts them.
     266                 :        1222 :     const QFileInfo list(QDir(folder).filePath(QStringLiteral(".gpg-id")));
     267   [ +  +  +  +  :         611 :     if (viaPass && list.isFile() && !rootList.isEmpty() &&
                +  -  + ]
     268      [ +  +  + ]:         614 :         list.canonicalFilePath() == rootList) {
     269                 :           1 :       return false;
     270                 :             :     }
     271      [ +  +  + ]:         610 :     if (list.isFile() &&
     272   [ +  +  +  +  :           5 :         (viaPass || (!list.isSymLink() && !list.isJunction()))) {
             +  +  +  - ]
     273                 :           4 :       return true;
     274                 :             :     }
     275                 :         606 :     return std::nullopt;
     276                 :         611 :   };
     277                 :             :   // Each folder's verdict once: climbing from every entry to the root again
     278                 :             :   // cost entries times depth, and a co-writer's deep chain of folders froze
     279                 :             :   // Finish for tens of minutes.
     280                 :          18 :   QHash<QString, bool> verdictOf;
     281                 :        4019 :   const auto underSubStore = [&root, &ownList,
     282                 :             :                               &verdictOf](const QString &entry) {
     283                 :        4019 :     QStringList climbed;
     284                 :        4019 :     QString at = QFileInfo(entry).absolutePath();
     285                 :        4019 :     bool verdict = false;
     286   [ +  +  +  +  :        9236 :     while (at != root && at.startsWith(root + QLatin1Char('/'))) {
             +  +  -  - ]
     287         [ +  + ]:        4611 :       if (const auto known = verdictOf.constFind(at);
     288                 :             :           known != verdictOf.constEnd()) {
     289                 :        4000 :         verdict = known.value();
     290                 :        4000 :         break;
     291                 :             :       }
     292                 :             :       climbed << at;
     293      [ +  +  + ]:         611 :       if (const std::optional<bool> own = ownList(at)) {
     294                 :           5 :         verdict = *own;
     295                 :           5 :         break;
     296                 :             :       }
     297                 :        1212 :       at = QFileInfo(at).absolutePath();
     298                 :             :     }
     299         [ +  + ]:        4630 :     for (const QString &folder : std::as_const(climbed)) {
     300                 :         611 :       verdictOf.insert(folder, verdict);
     301                 :             :     }
     302                 :        8038 :     return verdict;
     303                 :             :   };
     304                 :             :   QStringList entries =
     305                 :          54 :       Util::regularFilesUnder(root, {QStringLiteral("*.gpg")});
     306                 :          36 :   entries.erase(std::remove_if(entries.begin(), entries.end(), underSubStore),
     307                 :          18 :                 entries.end());
     308                 :          18 :   return entries;
     309                 :          18 : }
     310                 :             : 
     311                 :          26 : auto ProfileInit::writeGpgId(const QString &gpgIdFile,
     312                 :             :                              const QList<UserInfo> &users, bool signed_,
     313                 :             :                              QString *note, QByteArray *written) -> bool {
     314                 :          26 :   QStringList ids;
     315         [ +  + ]:          73 :   for (const UserInfo &user : users) {
     316         [ +  + ]:          47 :     if (user.enabled) {
     317                 :          25 :       ids << user.key_id;
     318                 :             :     }
     319                 :             :   }
     320         [ +  + ]:          26 :   if (ids.isEmpty()) {
     321                 :           2 :     *note = tr("No recipient selected; %1 was not written.").arg(gpgIdFile);
     322                 :           1 :     return false;
     323                 :             :   }
     324                 :             :   // As ImitatePass::writeGpgIdFile: renamed into place whole, so an
     325                 :             :   // interrupted write leaves no half .gpg-id to sign or use. Owner-only: the
     326                 :             :   // list leaks which keys the store is encrypted to.
     327                 :             :   QByteArray contents =
     328                 :          25 :       (ids.join(QLatin1Char('\n')) + QLatin1Char('\n')).toUtf8();
     329         [ +  + ]:          25 :   if (signed_) {
     330                 :             :     // Same rule as ImitatePass::writeGpgIdFile: reserved and recorded
     331                 :             :     // before the write, bound to the store root (GpgIdGeneration). A new
     332                 :             :     // profile has no list on disk to take a generation from.
     333                 :           6 :     QString why;
     334                 :             :     const std::optional<QString> store = GpgIdGeneration::storeIdFor(
     335   [ +  +  +  -  :          12 :         QFileInfo(gpgIdFile).absolutePath(), std::nullopt, &why);
                      + ]
     336         [ -  + ]:           6 :     if (!store) {
     337                 :             :       *note = std::move(why);
     338                 :           0 :       return false;
     339                 :             :     }
     340                 :             :     const std::optional<qint64> generation =
     341                 :           6 :         GpgIdGeneration::reserveNext(gpgIdFile, std::nullopt, &why);
     342         [ +  + ]:           6 :     if (!generation) {
     343                 :             :       *note = std::move(why);
     344                 :           1 :       return false;
     345                 :             :     }
     346                 :          10 :     contents = GpgIdGeneration::withHeader(*generation, QStringLiteral("."),
     347                 :             :                                            contents, *store);
     348                 :             :   }
     349                 :          24 :   QString why;
     350      [ +  +  + ]:          24 :   if (!Util::writeFileReplacing(gpgIdFile, contents, true, &why)) {
     351                 :             :     *note = std::move(why);
     352                 :           1 :     return false;
     353                 :             :   }
     354         [ +  - ]:          23 :   if (written != nullptr) {
     355                 :          23 :     *written = contents;
     356                 :             :   }
     357         [ +  + ]:          23 :   if (signed_) {
     358                 :             :     // Same as ImitatePass::writeGpgIdFile: the bytes written are the ones
     359                 :             :     // recognised; not fatal, the first read digests them otherwise.
     360                 :           5 :     QString why;
     361      [ +  -  + ]:           5 :     if (!GpgIdGeneration::recordWritten(gpgIdFile, contents, &why)) {
     362                 :           0 :       qCWarning(lcQtPass) << "Could not record the written .gpg-id:" << why;
     363                 :             :     }
     364                 :             :   }
     365                 :             :   return true;
     366                 :             : }
     367                 :             : 
     368                 :           5 : auto ProfileInit::signGpgId(const QString &gpgIdFile,
     369                 :             :                             const QByteArray &contents, const AppSettings &s,
     370                 :             :                             QString *note) -> bool {
     371                 :           5 :   const GpgIdSigner signer = signerFor(s);
     372                 :           5 :   QString err;
     373      [ +  +  + ]:           5 :   if (!signer.sign(gpgIdFile, contents, &err)) {
     374                 :           3 :     *note = err.trimmed().isEmpty()
     375   [ -  +  -  +  :           9 :                 ? tr("Could not sign %1 with %2.")
                   -  + ]
     376                 :             :                       .arg(gpgIdFile, signer.keys().first())
     377                 :           3 :                 : tr("Could not sign %1 with %2: %3")
     378                 :           6 :                       .arg(gpgIdFile, signer.keys().first(), err.trimmed());
     379                 :             :     // withGpgHome() fell back to the inherited home: gpg's "no secret key"
     380                 :             :     // would not say that the configured one is missing.
     381         [ -  + ]:           3 :     if (!s.gpgHome.isEmpty()) {
     382                 :           0 :       QDir home(s.gpgHome);
     383                 :           0 :       home.makeAbsolute();
     384      [ #  #  # ]:           0 :       if (!home.exists()) {
     385                 :             :         *note +=
     386                 :           0 :             QLatin1Char(' ') +
     387                 :           0 :             tr("The configured GPG home %1 does not exist.").arg(home.path());
     388                 :             :       }
     389                 :           0 :     }
     390                 :           3 :     return false;
     391                 :             :   }
     392                 :             :   return true;
     393                 :           5 : }
     394                 :             : 
     395                 :          31 : auto ProfileInit::runGit(QProcess &git, const AppSettings &s,
     396                 :             :                          const QString &dir, const QStringList &args,
     397                 :             :                          QString *note) -> bool {
     398   [ +  +  +  + ]:          31 :   const bool bounded = args.first() == QLatin1String("init") ||
     399         [ +  + ]:          18 :                        args.first() == QLatin1String("add");
     400                 :          31 :   QElapsedTimer clock;
     401                 :          31 :   clock.start();
     402                 :          31 :   QString err;
     403         [ +  + ]:          31 :   const int rc = Executor::executeBlocking(git, s.gitExecutable, args,
     404                 :          31 :                                            QString(), nullptr, &err, nullptr,
     405                 :             :                                            bounded ? kGitSetupTimeoutMs : -1);
     406         [ +  + ]:          31 :   if (rc == 0) {
     407                 :             :     return true;
     408                 :             :   }
     409                 :             :   // -1 is what a stopped run returns; git's own failures are positive.
     410   [ +  +  -  + ]:           6 :   if (bounded && rc == -1 && clock.elapsed() >= kGitSetupTimeoutMs) {
     411                 :           0 :     *note = tr("git %1 did not finish within %2 minutes in %3, and was "
     412                 :             :                "stopped.")
     413                 :           0 :                 .arg(args.first())
     414                 :           0 :                 .arg(kGitSetupTimeoutMs / 60000)
     415                 :           0 :                 .arg(dir);
     416                 :             :   } else {
     417                 :          12 :     *note = tr("git %1 failed in %2: %3").arg(args.first(), dir, err.trimmed());
     418                 :             :   }
     419                 :             :   return false;
     420                 :             : }
     421                 :             : 
     422                 :          23 : auto ProfileInit::refusedForGit(const QString &dir, const QStringList &files,
     423                 :             :                                 QString *note) -> bool {
     424                 :          23 :   const QString path = blocksGit(dir, files);
     425         [ +  + ]:          23 :   if (path.isEmpty()) {
     426                 :             :     return false;
     427                 :             :   }
     428         [ +  + ]:          10 :   *note = QFileInfo(path).fileName() == QLatin1String(".git")
     429   [ +  +  +  + ]:          15 :               ? tr("%1 is a Git repository inside the store. Move it out of "
     430                 :             :                    "the store, then try again.")
     431                 :           7 :                     .arg(QDir::toNativeSeparators(QFileInfo(path).path()))
     432                 :           5 :               : tr("%1 is not a regular file, and Git could wait on it for "
     433                 :             :                    "good. Remove it or make it a regular file, then try "
     434                 :             :                    "again.")
     435                 :          13 :                     .arg(QDir::toNativeSeparators(path));
     436                 :           5 :   return true;
     437                 :             : }
     438                 :             : 
     439                 :           5 : auto ProfileInit::commitGpgId(const QString &dir, const QString &gpgIdFile,
     440                 :             :                               const QString &sigFile, const AppSettings &s,
     441                 :             :                               QString *note) -> bool {
     442                 :             :   // A process of our own in the profile directory: nothing here may touch
     443                 :             :   // the shared Executor or PASSWORD_STORE_DIR of the active store.
     444                 :           5 :   QProcess git;
     445                 :           5 :   git.setWorkingDirectory(dir);
     446                 :           5 :   git.setProcessEnvironment(gitEnvironment(dir, s));
     447                 :          11 :   auto run = [&](const QStringList &args) -> bool {
     448                 :          11 :     return runGit(git, s, dir, args, note);
     449                 :           5 :   };
     450                 :          15 :   QStringList files{QFileInfo(gpgIdFile).fileName()};
     451         [ +  + ]:           5 :   if (!sigFile.isEmpty()) {
     452                 :           4 :     files << QFileInfo(sigFile).fileName();
     453                 :             :   }
     454                 :             :   // initialise() checked before writing these; again here, as signing can
     455                 :             :   // take a while and the folder is shared.
     456      [ +  +  - ]:           5 :   if (refusedForGit(dir, files, note)) {
     457                 :             :     return false;
     458                 :             :   }
     459                 :             :   const QStringList add =
     460                 :          20 :       QStringList{QStringLiteral("add"), QStringLiteral("--")} + files;
     461   [ +  +  +  +  :          15 :   if (!run({QStringLiteral("init")}) || !run(add)) {
          -  +  +  +  +  
                +  -  - ]
     462                 :             :     return false;
     463                 :             :   }
     464                 :           9 :   return run(commitArgs(git, s) +
     465                 :           6 :              QStringList{QStringLiteral("-m"),
     466                 :           3 :                          QStringLiteral("Added .gpg-id using QtPass."),
     467                 :          21 :                          QStringLiteral("--")} +
     468                 :           3 :              files);
     469                 :          10 : }
     470                 :             : 
     471                 :          12 : auto ProfileInit::initGit(const QString &dir, const AppSettings &s,
     472                 :             :                           QString *note) -> bool {
     473                 :          12 :   QProcess git;
     474                 :          12 :   git.setWorkingDirectory(dir);
     475                 :          12 :   git.setProcessEnvironment(gitEnvironment(dir, s));
     476                 :          20 :   auto run = [&](const QStringList &args) -> bool {
     477                 :          20 :     return runGit(git, s, dir, args, note);
     478                 :          12 :   };
     479                 :             :   // Stage only store files (as the re-encryption backup commit does): an
     480                 :             :   // existing folder may hold plaintext exports or swap files. Regular files
     481                 :             :   // only: a link or junction must not have its target staged.
     482                 :          12 :   QStringList files;
     483                 :          12 :   const QDir base(dir);
     484                 :             :   // Hidden directories (.git among them) are not walked, hidden files are:
     485                 :             :   // .gpg-id is one.
     486                 :          60 :   const QStringList found = Util::regularFilesUnder(
     487                 :             :       dir,
     488                 :          12 :       {QStringLiteral("*.gpg"), QStringLiteral(".gpg-id"),
     489                 :          12 :        QStringLiteral(".gpg-id.sig")},
     490                 :          12 :       nullptr, true);
     491         [ +  + ]:          30 :   for (const QString &path : found) {
     492                 :          36 :     files << base.relativeFilePath(path);
     493                 :             :   }
     494      [ +  +  + ]:          12 :   if (refusedForGit(dir, files, note)) {
     495                 :             :     return false;
     496                 :             :   }
     497   [ +  +  +  +  :          24 :   if (!run({QStringLiteral("init")})) {
                +  -  - ]
     498                 :             :     return false;
     499                 :             :   }
     500   [ +  +  +  + ]:          13 :   if (!files.isEmpty() &&
     501   [ +  +  +  +  :          31 :       !run(QStringList{QStringLiteral("add"), QStringLiteral("--")} + files)) {
          +  +  +  +  +  
          +  +  +  +  +  
                   -  - ]
     502                 :             :     return false;
     503                 :             :   }
     504                 :          18 :   return run(commitArgs(git, s) +
     505                 :           6 :              QStringList{QStringLiteral("--allow-empty"), QStringLiteral("-m"),
     506   [ +  +  +  -  :          42 :                          QStringLiteral("Added password store using QtPass.")});
                      - ]
     507                 :          24 : }
     508                 :             : 
     509                 :          16 : auto ProfileInit::gitIdentityConfigured(const QString &dir,
     510                 :             :                                         const AppSettings &s) -> bool {
     511                 :          16 :   QProcess git;
     512                 :          16 :   git.setWorkingDirectory(dir);
     513                 :          16 :   git.setProcessEnvironment(gitEnvironment(dir, s));
     514         [ +  + ]:          37 :   for (const char *key : {"user.name", "user.email"}) {
     515                 :          27 :     QString out;
     516   [ +  +  +  -  :         135 :     if (Executor::executeBlocking(git, s.gitExecutable,
                -  -  - ]
     517                 :          27 :                                   {QStringLiteral("config"),
     518                 :          27 :                                    QStringLiteral("--get"),
     519                 :             :                                    QString::fromLatin1(key)},
     520   [ +  -  +  +  :         102 :                                   QString(), &out, nullptr) != 0 ||
             +  -  +  + ]
     521         [ -  - ]:          27 :         out.trimmed().isEmpty()) {
     522                 :             :       return false;
     523                 :             :     }
     524                 :             :   }
     525                 :             :   return true;
     526                 :          70 : }
        

Generated by: LCOV version 2.0-1