summaryrefslogtreecommitdiff
path: root/src/other_tools/ops_maps/import_to_git_map.cpp
blob: bec7c1e79d22ff86d821f20a9d60a9f150bbf0b7 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/other_tools/ops_maps/import_to_git_map.hpp"

#include <memory>
#include <optional>

#include "fmt/core.h"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/other_tools/git_operations/git_ops_types.hpp"
#include "src/other_tools/git_operations/git_repo_remote.hpp"
#include "src/utils/cpp/tmp_dir.hpp"

namespace {

void KeepCommitAndSetTree(
    gsl::not_null<CriticalGitOpMap*> const& critical_git_op_map,
    std::string const& commit,
    GitCASPtr const& just_git_cas,
    StorageConfig const& storage_config,
    gsl::not_null<TaskSystem*> const& ts,
    ImportToGitMap::SetterPtr const& setter,
    ImportToGitMap::LoggerPtr const& logger) {
    // Keep tag for commit
    GitOpKey op_key = {.params =
                           {
                               storage_config.GitRoot(),     // target_path
                               commit,                       // git_hash
                               "Keep referenced tree alive"  // message
                           },
                       .op_type = GitOpType::KEEP_TAG};
    critical_git_op_map->ConsumeAfterKeysReady(
        ts,
        {std::move(op_key)},
        [commit, just_git_cas, storage_config, setter, logger](
            auto const& values) {
            GitOpValue op_result = *values[0];
            // check flag
            if (not op_result.result) {
                (*logger)("Keep tag failed",
                          /*fatal=*/true);
                return;
            }
            auto just_git_repo = GitRepoRemote::Open(just_git_cas);
            if (not just_git_repo) {
                (*logger)(fmt::format("Could not open Git repository {}",
                                      storage_config.GitRoot().string()),
                          /*fatal=*/true);
                return;
            }
            // get tree id and return it
            auto wrapped_logger = std::make_shared<AsyncMapConsumerLogger>(
                [logger, commit](auto const& msg, bool fatal) {
                    (*logger)(
                        fmt::format("While getting subtree from commit {}:\n{}",
                                    commit,
                                    msg),
                        fatal);
                });
            auto res = just_git_repo->GetSubtreeFromCommit(
                commit, ".", wrapped_logger);
            if (not res) {
                return;
            }
            (*setter)(std::pair<std::string, GitCASPtr>(*std::move(res),
                                                        just_git_cas));
        },
        [logger, commit, target_path = storage_config.GitRoot()](
            auto const& msg, bool fatal) {
            (*logger)(fmt::format("While running critical Git op KEEP_TAG for "
                                  "commit {} in target {}:\n{}",
                                  commit,
                                  target_path.string(),
                                  msg),
                      fatal);
        });
}

}  // namespace

auto CreateImportToGitMap(
    gsl::not_null<CriticalGitOpMap*> const& critical_git_op_map,
    std::string const& git_bin,
    std::vector<std::string> const& launcher,
    gsl::not_null<StorageConfig const*> const& storage_config,
    std::size_t jobs) -> ImportToGitMap {
    auto import_to_git = [critical_git_op_map,
                          git_bin,
                          launcher,
                          storage_config](auto ts,
                                          auto setter,
                                          auto logger,
                                          auto /*unused*/,
                                          auto const& key) {
        // The repository path that imports the content must be separate from
        // the content path, to avoid polluting the entries
        auto repo_dir = storage_config->CreateTypedTmpDir("import-repo");
        if (not repo_dir) {
            (*logger)(fmt::format("Failed to create import repository tmp "
                                  "directory for target {}",
                                  key.target_path.string()),
                      true);
            return;
        }
        // Commit content from target_path via the tmp repository
        GitOpKey op_key = {.params =
                               {
                                   repo_dir->GetPath(),  // target_path
                                   "",                   // git_hash
                                   fmt::format("Content of {} {}",
                                               key.repo_type,
                                               key.content),  // message
                                   key.target_path            // source_path
                               },
                           .op_type = GitOpType::INITIAL_COMMIT};
        critical_git_op_map->ConsumeAfterKeysReady(
            ts,
            {std::move(op_key)},
            [repo_dir,  // keep repo_dir alive
             critical_git_op_map,
             git_bin,
             launcher,
             storage_config,
             ts,
             setter,
             logger](auto const& values) {
                GitOpValue op_result = *values[0];
                // check flag
                if (not op_result.result) {
                    (*logger)("Initial commit failed",
                              /*fatal=*/true);
                    return;
                }
                // ensure Git cache
                // define Git operation to be done
                GitOpKey op_key = {
                    .params =
                        {
                            storage_config->GitRoot(),  // target_path
                            "",                         // git_hash
                            std::nullopt,               // message
                            std::nullopt,               // source_path
                            true                        // init_bare
                        },
                    .op_type = GitOpType::ENSURE_INIT};
                critical_git_op_map->ConsumeAfterKeysReady(
                    ts,
                    {std::move(op_key)},
                    [repo_dir,  // keep repo_dir alive
                     critical_git_op_map,
                     commit = *op_result.result,
                     git_bin,
                     launcher,
                     storage_config,
                     ts,
                     setter,
                     logger](auto const& values) {
                        GitOpValue op_result = *values[0];
                        // check flag
                        if (not op_result.result) {
                            (*logger)("Git init failed",
                                      /*fatal=*/true);
                            return;
                        }
                        // fetch all into Git cache
                        auto just_git_repo =
                            GitRepoRemote::Open(op_result.git_cas);
                        if (not just_git_repo) {
                            (*logger)(
                                fmt::format("Could not open Git cache "
                                            "repository {}",
                                            storage_config->GitRoot().string()),
                                /*fatal=*/true);
                            return;
                        }
                        auto const& target_path = repo_dir->GetPath();
                        auto wrapped_logger =
                            std::make_shared<AsyncMapConsumerLogger>(
                                [logger, target_path](auto const& msg,
                                                      bool fatal) {
                                    (*logger)(fmt::format("While fetch via tmp "
                                                          "repo from {}:\n{}",
                                                          target_path.string(),
                                                          msg),
                                              fatal);
                                });
                        if (not just_git_repo->FetchViaTmpRepo(
                                *storage_config,
                                target_path.string(),
                                std::nullopt,
                                std::vector<std::string>{} /* inherit_env */,
                                git_bin,
                                launcher,
                                wrapped_logger)) {
                            return;
                        }
                        // setup a wrapped_logger
                        wrapped_logger =
                            std::make_shared<AsyncMapConsumerLogger>(
                                [logger, target_path](auto const& msg,
                                                      bool fatal) {
                                    (*logger)(
                                        fmt::format("While doing keep commit "
                                                    "and setting Git tree for "
                                                    "target {}:\n{}",
                                                    target_path.string(),
                                                    msg),
                                        fatal);
                                });
                        KeepCommitAndSetTree(critical_git_op_map,
                                             commit,
                                             op_result.git_cas, /*just_git_cas*/
                                             *storage_config,
                                             ts,
                                             setter,
                                             wrapped_logger);
                    },
                    [logger, target_path = storage_config->GitRoot()](
                        auto const& msg, bool fatal) {
                        (*logger)(fmt::format("While running critical Git "
                                              "op ENSURE_INIT bare for "
                                              "target {}:\n{}",
                                              target_path.string(),
                                              msg),
                                  fatal);
                    });
            },
            [logger, target_path = key.target_path](auto const& msg,
                                                    bool fatal) {
                (*logger)(fmt::format("While running critical Git op "
                                      "INITIAL_COMMIT for target {}:\n{}",
                                      target_path.string(),
                                      msg),
                          fatal);
            });
    };
    return AsyncMapConsumer<CommitInfo, std::pair<std::string, GitCASPtr>>(
        import_to_git, jobs);
}