aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/patch_sync/src/main.rs
blob: 5f9b9708adba65ba8915bb4909e5f3ff749a24e3 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

mod android_utils;
mod patch_parsing;
mod version_control;

use std::borrow::ToOwned;
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use structopt::StructOpt;

use patch_parsing::{filter_patches_by_platform, PatchCollection, PatchDictSchema, VersionRange};
use version_control::RepoSetupContext;

fn main() -> Result<()> {
    match Opt::from_args() {
        Opt::Show {
            cros_checkout_path,
            android_checkout_path,
            sync,
            keep_unmerged,
        } => show_subcmd(ShowOpt {
            cros_checkout_path,
            android_checkout_path,
            sync,
            keep_unmerged,
        }),
        Opt::Transpose {
            cros_checkout_path,
            cros_reviewers,
            old_cros_ref,
            android_checkout_path,
            android_reviewers,
            old_android_ref,
            sync,
            verbose,
            dry_run,
            no_commit,
            wip,
            disable_cq,
            uprev,
        } => transpose_subcmd(TransposeOpt {
            cros_checkout_path,
            cros_reviewers: cros_reviewers
                .map(|r| r.split(',').map(ToOwned::to_owned).collect())
                .unwrap_or_default(),
            old_cros_ref,
            android_checkout_path,
            android_reviewers: android_reviewers
                .map(|r| r.split(',').map(ToOwned::to_owned).collect())
                .unwrap_or_default(),
            old_android_ref,
            sync,
            verbose,
            dry_run,
            no_commit,
            wip,
            disable_cq,
            uprev_ebuilds: uprev,
        }),
    }
}

struct ShowOpt {
    cros_checkout_path: PathBuf,
    android_checkout_path: PathBuf,
    keep_unmerged: bool,
    sync: bool,
}

fn show_subcmd(args: ShowOpt) -> Result<()> {
    let ShowOpt {
        cros_checkout_path,
        android_checkout_path,
        keep_unmerged,
        sync,
    } = args;
    let ctx = RepoSetupContext {
        cros_checkout: cros_checkout_path,
        android_checkout: android_checkout_path,
        sync_before: sync,
        wip_mode: true,   // Has no effect, as we're not making changes
        enable_cq: false, // Has no effect, as we're not uploading anything
        uprev_ebuilds: false,
    };
    ctx.setup()?;
    let make_collection = |platform: &str, patches_fp: &Path| -> Result<PatchCollection> {
        let parsed_collection = PatchCollection::parse_from_file(patches_fp)
            .with_context(|| format!("could not parse {} PATCHES.json", platform))?;
        Ok(if keep_unmerged {
            parsed_collection
        } else {
            filter_patches_by_platform(&parsed_collection, platform).map_patches(|p| {
                // Need to do this platforms creation as Rust 1.55 cannot use "from".
                let mut platforms = BTreeSet::new();
                platforms.insert(platform.to_string());
                PatchDictSchema {
                    platforms,
                    ..p.clone()
                }
            })
        })
    };
    let cur_cros_collection = make_collection("chromiumos", &ctx.cros_patches_path())?;
    let cur_android_collection = make_collection("android", &ctx.android_patches_path())?;
    let merged = cur_cros_collection.union(&cur_android_collection)?;
    println!("{}", merged.serialize_patches()?);
    Ok(())
}

struct TransposeOpt {
    cros_checkout_path: PathBuf,
    old_cros_ref: String,
    android_checkout_path: PathBuf,
    old_android_ref: String,
    sync: bool,
    verbose: bool,
    dry_run: bool,
    no_commit: bool,
    cros_reviewers: Vec<String>,
    android_reviewers: Vec<String>,
    wip: bool,
    disable_cq: bool,
    uprev_ebuilds: bool,
}

fn transpose_subcmd(args: TransposeOpt) -> Result<()> {
    let ctx = RepoSetupContext {
        cros_checkout: args.cros_checkout_path,
        android_checkout: args.android_checkout_path,
        sync_before: args.sync,
        wip_mode: args.wip,
        enable_cq: !args.disable_cq,
        uprev_ebuilds: args.uprev_ebuilds,
    };
    ctx.setup()?;
    let cros_patches_path = ctx.cros_patches_path();
    let android_patches_path = ctx.android_patches_path();

    // Get new Patches -------------------------------------------------------
    let patch_parsing::PatchTemporalDiff {
        cur_collection: cur_cros_collection,
        new_patches: new_cros_patches,
        version_updates: cros_version_updates,
    } = patch_parsing::new_patches(
        &cros_patches_path,
        &ctx.old_cros_patch_contents(&args.old_cros_ref)?,
        "chromiumos",
    )
    .context("finding new patches for chromiumos")?;
    let patch_parsing::PatchTemporalDiff {
        cur_collection: cur_android_collection,
        new_patches: new_android_patches,
        version_updates: android_version_updates,
    } = patch_parsing::new_patches(
        &android_patches_path,
        &ctx.old_android_patch_contents(&args.old_android_ref)?,
        "android",
    )
    .context("finding new patches for android")?;

    // Have to ignore patches that are already at the destination, even if
    // the patches are new.
    let new_cros_patches = new_cros_patches.subtract(&cur_android_collection)?;
    let new_android_patches = new_android_patches.subtract(&cur_cros_collection)?;

    // Need to do an extra filtering step for Android, as AOSP doesn't
    // want patches outside of the start/end bounds.
    let android_llvm_version: u64 = {
        let android_llvm_version_str =
            android_utils::get_android_llvm_version(&ctx.android_checkout)?;
        android_llvm_version_str.parse::<u64>().with_context(|| {
            format!(
                "converting llvm version to u64: '{}'",
                android_llvm_version_str
            )
        })?
    };
    let new_android_patches = new_android_patches.filter_patches(|p| {
        match (p.get_from_version(), p.get_until_version()) {
            (Some(start), Some(end)) => start <= android_llvm_version && android_llvm_version < end,
            (Some(start), None) => start <= android_llvm_version,
            (None, Some(end)) => android_llvm_version < end,
            (None, None) => true,
        }
    });

    // Need to filter version updates to only existing patches to the other platform.
    let cros_version_updates =
        filter_version_changes(cros_version_updates, &cur_android_collection);
    let android_version_updates =
        filter_version_changes(android_version_updates, &cur_cros_collection);

    if args.verbose {
        display_patches("New patches from ChromiumOS", &new_cros_patches);
        display_version_updates("Version updates from ChromiumOS", &cros_version_updates);
        display_patches("New patches from Android", &new_android_patches);
        display_version_updates("Version updates from Android", &android_version_updates);
    }

    if args.dry_run {
        println!("--dry-run specified; skipping modifications");
        return Ok(());
    }

    modify_repos(
        &ctx,
        args.no_commit,
        ModifyOpt {
            new_cros_patches,
            cur_cros_collection,
            cros_version_updates,
            cros_reviewers: args.cros_reviewers,
            new_android_patches,
            cur_android_collection,
            android_version_updates,
            android_reviewers: args.android_reviewers,
        },
    )
}

struct ModifyOpt {
    new_cros_patches: PatchCollection,
    cur_cros_collection: PatchCollection,
    cros_version_updates: Vec<(String, Option<VersionRange>)>,
    cros_reviewers: Vec<String>,
    new_android_patches: PatchCollection,
    cur_android_collection: PatchCollection,
    android_version_updates: Vec<(String, Option<VersionRange>)>,
    android_reviewers: Vec<String>,
}

fn modify_repos(ctx: &RepoSetupContext, no_commit: bool, opt: ModifyOpt) -> Result<()> {
    // Cleanup on scope exit.
    scopeguard::defer! {
        ctx.cleanup();
    }
    // Transpose Patches -----------------------------------------------------
    let mut cur_android_collection = opt.cur_android_collection;
    let mut cur_cros_collection = opt.cur_cros_collection;
    // Apply any version ranges and new patches, then write out.
    if !opt.new_cros_patches.is_empty() || !opt.cros_version_updates.is_empty() {
        cur_android_collection =
            cur_android_collection.update_version_ranges(&opt.cros_version_updates);
        opt.new_cros_patches
            .transpose_write(&mut cur_android_collection)?;
    }
    if !opt.new_android_patches.is_empty() || !opt.android_version_updates.is_empty() {
        cur_cros_collection =
            cur_cros_collection.update_version_ranges(&opt.android_version_updates);
        opt.new_android_patches
            .transpose_write(&mut cur_cros_collection)?;
    }

    if no_commit {
        println!("--no-commit specified; not committing or uploading");
        return Ok(());
    }
    // Commit and upload for review ------------------------------------------
    // Note we want to check if the android patches are empty for CrOS, and
    // vice versa. This is a little counterintuitive.
    if !opt.new_android_patches.is_empty() {
        ctx.cros_repo_upload(&opt.cros_reviewers)
            .context("uploading chromiumos changes")?;
    }
    if !opt.new_cros_patches.is_empty() {
        if let Err(e) = android_utils::sort_android_patches(&ctx.android_checkout) {
            eprintln!(
                "Couldn't sort Android patches; continuing. Caused by: {}",
                e
            );
        }
        ctx.android_repo_upload(&opt.android_reviewers)
            .context("uploading android changes")?;
    }
    Ok(())
}

/// Filter version changes that can't apply to a given collection.
fn filter_version_changes<T>(
    version_updates: T,
    other_platform_collection: &PatchCollection,
) -> Vec<(String, Option<VersionRange>)>
where
    T: IntoIterator<Item = (String, Option<VersionRange>)>,
{
    version_updates
        .into_iter()
        .filter(|(rel_patch_path, _)| {
            other_platform_collection
                .patches
                .iter()
                .any(|p| &p.rel_patch_path == rel_patch_path)
        })
        .collect()
}

fn display_patches(prelude: &str, collection: &PatchCollection) {
    println!("{}", prelude);
    if collection.patches.is_empty() {
        println!("  [No Patches]");
        return;
    }
    println!("{}", collection);
}

fn display_version_updates(prelude: &str, version_updates: &[(String, Option<VersionRange>)]) {
    println!("{}", prelude);
    if version_updates.is_empty() {
        println!("  [No Version Changes]");
        return;
    }
    for (rel_patch_path, _) in version_updates {
        println!("*  {}", rel_patch_path);
    }
}

#[derive(Debug, structopt::StructOpt)]
#[structopt(name = "patch_sync", about = "A pipeline for syncing the patch code")]
enum Opt {
    /// Show a combined view of the PATCHES.json file, without making any changes.
    #[allow(dead_code)]
    Show {
        #[structopt(parse(from_os_str))]
        cros_checkout_path: PathBuf,
        #[structopt(parse(from_os_str))]
        android_checkout_path: PathBuf,

        /// Keep a patch's platform field even if it's not merged at that platform.
        #[structopt(long)]
        keep_unmerged: bool,

        /// Run repo sync before transposing.
        #[structopt(short, long)]
        sync: bool,
    },
    /// Transpose patches from two PATCHES.json files
    /// to each other.
    Transpose {
        /// Path to the ChromiumOS source repo checkout.
        #[structopt(long = "cros-checkout", parse(from_os_str))]
        cros_checkout_path: PathBuf,

        /// Emails to send review requests to during ChromiumOS upload.
        /// Comma separated.
        #[structopt(long = "cros-rev")]
        cros_reviewers: Option<String>,

        /// Git ref (e.g. hash) for the ChromiumOS overlay to use as the base.
        #[structopt(long = "overlay-base-ref")]
        old_cros_ref: String,

        /// Path to the Android Open Source Project source repo checkout.
        #[structopt(long = "aosp-checkout", parse(from_os_str))]
        android_checkout_path: PathBuf,

        /// Emails to send review requests to during Android upload.
        /// Comma separated.
        #[structopt(long = "aosp-rev")]
        android_reviewers: Option<String>,

        /// Git ref (e.g. hash) for the llvm_android repo to use as the base.
        #[structopt(long = "aosp-base-ref")]
        old_android_ref: String,

        /// Run repo sync before transposing.
        #[structopt(short, long)]
        sync: bool,

        /// Revbump/uprev ebuilds during transposing.
        #[structopt(long)]
        uprev: bool,

        /// Print information to stdout
        #[structopt(short, long)]
        verbose: bool,

        /// Do not change any files. Useful in combination with `--verbose`
        /// Implies `--no-commit`.
        #[structopt(long)]
        dry_run: bool,

        /// Do not commit or upload any changes made.
        #[structopt(long)]
        no_commit: bool,

        /// Upload and send things for review, but mark as WIP and send no
        /// emails.
        #[structopt(long)]
        wip: bool,

        /// Don't run CQ if set. Only has an effect if uploading.
        #[structopt(long)]
        disable_cq: bool,
    },
}