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

use anyhow::{Context, Result};
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,
            old_cros_ref,
            android_checkout_path,
            old_android_ref,
            sync,
            verbose,
            dry_run,
            no_commit,
        } => transpose_subcmd(TransposeOpt {
            cros_checkout_path,
            old_cros_ref,
            android_checkout_path,
            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,
}

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")?;
    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")?;
    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)?
    };

    // Transpose Patches -----------------------------------------------------
    new_cros_patches.transpose_write(&mut cur_cros_collection)?;
    new_android_patches.transpose_write(&mut cur_android_collection)?;

    if !args.no_commit {
        return Ok(());
    }
    // Commit and upload for review ------------------------------------------
    ctx.cros_repo_upload()
        .context("uploading chromiumos changes")?;
    ctx.android_repo_upload()
        .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,

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

        /// 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` and `--no-upload`.
        #[structopt(long)]
        dry_run: bool,

        /// Do not commit any changes made.
        /// Implies `--no-upload`.
        #[structopt(long)]
        no_commit: bool,
    },
}