aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/patch_sync/src/main.rs
blob: cec523bba074bc6a4f6d7d90e18c81bad1121081 (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
mod patch_parsing;
mod version_control;

use anyhow::{Context, Result};
use std::borrow::ToOwned;
use std::path::PathBuf;
use structopt::StructOpt;

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

fn show_subcmd(
    cros_checkout_path: PathBuf,
    android_checkout_path: PathBuf,
    sync: bool,
) -> Result<()> {
    let ctx = version_control::RepoSetupContext {
        cros_checkout: cros_checkout_path,
        android_checkout: android_checkout_path,
        sync_before: sync,
    };
    ctx.setup()?;
    let cros_patches_path = ctx.cros_patches_path();
    let android_patches_path = ctx.android_patches_path();
    let cur_cros_collection = patch_parsing::PatchCollection::parse_from_file(&cros_patches_path)
        .context("could not parse cros PATCHES.json")?;
    let cur_android_collection =
        patch_parsing::PatchCollection::parse_from_file(&android_patches_path)
            .context("could not parse android PATCHES.json")?;
    let merged = cur_cros_collection.union(&cur_android_collection)?;
    println!("{}", merged.serialize_patches()?);
    Ok(())
}

#[allow(dead_code)]
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>,
}

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

    // Chromium OS Patches ----------------------------------------------------
    let mut cur_cros_collection =
        patch_parsing::PatchCollection::parse_from_file(&cros_patches_path)
            .context("parsing cros PATCHES.json")?
            .filter_patches(|p| p.platforms.contains("chromiumos"));
    let new_cros_patches: patch_parsing::PatchCollection = {
        let cros_old_patches_json = ctx.old_cros_patch_contents(&args.old_cros_ref)?;
        let old_cros_collection = patch_parsing::PatchCollection::parse_from_str(
            cros_patches_path.parent().unwrap().to_path_buf(),
            &cros_old_patches_json,
        )?;
        cur_cros_collection.subtract(&old_cros_collection)?
    };

    // Android Patches -------------------------------------------------------
    let mut cur_android_collection =
        patch_parsing::PatchCollection::parse_from_file(&android_patches_path)
            .context("parsing android PATCHES.json")?
            .filter_patches(|p| p.platforms.contains("android"));
    let new_android_patches: patch_parsing::PatchCollection = {
        let android_old_patches_json = ctx.old_android_patch_contents(&args.old_android_ref)?;
        let old_android_collection = patch_parsing::PatchCollection::parse_from_str(
            android_patches_path.parent().unwrap().to_path_buf(),
            &android_old_patches_json,
        )?;
        cur_android_collection.subtract(&old_android_collection)?
    };

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

    // Transpose Patches -----------------------------------------------------
    if !new_cros_patches.is_empty() {
        new_cros_patches.transpose_write(&mut cur_android_collection)?;
    }
    if !new_android_patches.is_empty() {
        new_android_patches.transpose_write(&mut cur_cros_collection)?;
    }

    if args.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 !new_android_patches.is_empty() {
        ctx.cros_repo_upload(&args.cros_reviewers)
            .context("uploading chromiumos changes")?;
    }
    if !new_cros_patches.is_empty() {
        ctx.android_repo_upload(&args.android_reviewers)
            .context("uploading android changes")?;
    }
    Ok(())
}

#[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,
        #[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 Chromium OS upload.
        /// Comma separated.
        #[structopt(long = "cros-rev")]
        cros_reviewers: 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: 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,

        /// 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,
    },
}