patch_crate/
lib.rs

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
//!
//! patch-crate lets rust app developer instantly make and keep fixes to crate dependencies.
//! It's a vital band-aid for those of us living on the bleeding edge.
//!
//! # Installation
//!
//! Simply run:
//! ```sh
//! cargo install patch-crate
//! ```
//!
//! # Usage
//!
//! To patch dependency one has to add the following
//! to `Cargo.toml`
//!
//! ```toml
//! [package.metadata.patch]
//! crates = ["serde"]
//! ```
//!
//! It specifies which dependency to patch (in this case
//! serde). Running:
//!
//! ```sh
//! cargo patch-crate
//! ```
//!
//! will download the sede package specified in the
//! dpendency section to the `target/patch` folder.
//!  
//! Then override the dependency using
//! `replace` like this
//!
//! ```toml
//! [patch.crates-io]
//! serde = { path = './target/patch/serde-1.0.110' }
//! ```
//!
//! fix a bug in './target/patch/serde-1.0.110' directly.
//!
//! run following to create a `patches/serde+1.0.110.patch` file
//! ```sh
//! cargo patch-crate serde
//! ```
//!
//! commit the patch file to share the fix with your team
//! ```sh
//! git add patches/serde+1.0.110.patch
//! git commit -m "fix broken-serde in serde"
//! ```

use anyhow::{anyhow, Ok, Result};
use cargo::{
    core::{
        package::{Package, PackageSet},
        registry::PackageRegistry,
        resolver::{features::CliFeatures, HasDevUnits},
        Resolve, Workspace,
    },
    ops::{get_resolved_packages, load_pkg_lockfile, resolve_with_previous},
    sources::SourceConfigMap,
    util::{cache_lock::CacheLockMode, important_paths::find_root_manifest_for_wd, GlobalContext},
};
use clap::Parser;
use fs_extra::dir::{copy, CopyOptions};
use log::*;
use std::{
    collections::HashSet,
    ffi::OsStr,
    fs,
    path::{Path, PathBuf},
};

const PATCH_EXT: &str = "patch";

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
    crates: Vec<String>,
    #[arg(short, long)]
    force: bool,
}

trait PackageExt {
    fn slug(&self) -> Result<&str>;
    fn patch_target_path(&self, workspace: &Workspace<'_>) -> Result<PathBuf>;
}

impl PackageExt for Package {
    fn slug(&self) -> Result<&str> {
        if let Some(name) = self.root().file_name().and_then(|s| s.to_str()) {
            Ok(name)
        } else {
            Err(anyhow!("Dependency Folder does not have a name"))
        }
    }

    fn patch_target_path(&self, workspace: &Workspace<'_>) -> Result<PathBuf> {
        let slug = self.slug()?;
        let patch_target_path = workspace.patch_target_folder().join(slug);
        Ok(patch_target_path)
    }
}

trait WorkspaceExt {
    fn patches_folder(&self) -> PathBuf;
    fn patch_target_folder(&self) -> PathBuf;
    fn patch_target_tmp_folder(&self) -> PathBuf;
    fn clean_patch_folder(&self) -> Result<()>;
}

impl WorkspaceExt for Workspace<'_> {
    fn patches_folder(&self) -> PathBuf {
        self.root().join("patches/")
    }
    fn patch_target_folder(&self) -> PathBuf {
        self.root().join("target/patch/")
    }
    fn patch_target_tmp_folder(&self) -> PathBuf {
        self.root().join("target/patch-tmp/")
    }

    fn clean_patch_folder(&self) -> Result<()> {
        let path = self.patch_target_folder();
        if path.exists() {
            fs::remove_dir_all(self.patch_target_folder())?;
        }
        Ok(())
    }
}

fn resolve_ws<'a>(ws: &Workspace<'a>) -> Result<(PackageSet<'a>, Resolve)> {
    let mut registry =
        PackageRegistry::new_with_source_config(ws.gctx(), SourceConfigMap::new(ws.gctx())?)?;
    registry.lock_patches();
    let resolve = {
        let prev = load_pkg_lockfile(ws)?;
        let resolve: Resolve = resolve_with_previous(
            &mut registry,
            ws,
            &CliFeatures::new_all(true),
            HasDevUnits::No,
            prev.as_ref(),
            None,
            &[],
            false,
        )?;
        resolve
    };
    let packages = get_resolved_packages(&resolve, registry)?;
    Ok((packages, resolve))
}

fn copy_package(pkg: &Package, patch_target_folder: &Path, overwrite: bool) -> Result<PathBuf> {
    fs::create_dir_all(patch_target_folder)?;
    let options = CopyOptions::new();
    let patch_target_path = patch_target_folder.join(pkg.slug()?);
    if patch_target_path.exists() {
        if overwrite {
            info!("crate: {}, copy to {:?}", pkg.name(), &patch_target_folder);
            fs::remove_dir_all(&patch_target_path)?;
        } else {
            info!(
                "crate: {}, skip, {:?} already exists.",
                pkg.name(),
                &patch_target_path
            );
            return Ok(patch_target_path);
        }
    }
    let _ = copy(pkg.root(), patch_target_folder, &options)?;
    Ok(patch_target_path)
}

fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
    let path = fs::canonicalize(path)?;
    find_root_manifest_for_wd(&path)
}

pub fn run() -> anyhow::Result<()> {
    let args = {
        let mut args = Cli::parse();
        if let Some(idx) = args.crates.iter().position(|c| c == "patch-crate") {
            args.crates.remove(idx);
        }
        args
    };

    let gctx = GlobalContext::default()?;
    let _lock = gctx.acquire_package_cache_lock(CacheLockMode::Shared)?;

    let cargo_toml_path = find_cargo_toml(&PathBuf::from("."))?;

    let workspace = Workspace::new(&cargo_toml_path, &gctx)?;

    let patches_folder = workspace.patches_folder();

    let patch_target_folder = workspace.patch_target_folder();
    let patch_target_tmp_folder = workspace.patch_target_tmp_folder();

    let (pkg_set, resolve) = resolve_ws(&workspace)?;

    if !args.crates.is_empty() {
        info!("starting patch creation.");
        if !patches_folder.exists() {
            fs::create_dir_all(&patches_folder)?;
        }
        for n in args.crates.iter() {
            // make patch
            info!("crate: {}, starting patch creation.", n);
            let pkg_id = resolve.query(n)?;
            let pkg = pkg_set.get_one(pkg_id)?;
            let patch_target_path = pkg.patch_target_path(&workspace)?;
            let patch_target_tmp_path = copy_package(pkg, &patch_target_tmp_folder, true)?;
            git::init(&patch_target_tmp_path)?;
            git::destroy(&patch_target_path)?;
            copy(
                &patch_target_path,
                &patch_target_tmp_folder,
                &CopyOptions::new().overwrite(true).copy_inside(true),
            )?;
            let patch_file = patches_folder.join(format!(
                "{}+{}.{}",
                pkg_id.name(),
                pkg_id.version(),
                PATCH_EXT
            ));
            git::create_patch(&patch_target_tmp_path, &patch_file)?;
            fs::remove_dir_all(&patch_target_tmp_folder)?;
            info!("crate: {}, create patch successfully, {:?}", n, &patch_file);
        }
    } else {
        // apply patch
        info!("applying patch");

        let custom_metadata = workspace.custom_metadata().into_iter().chain(
            workspace
                .members()
                .flat_map(|member| member.manifest().custom_metadata()),
        );

        let mut crates_to_patch = custom_metadata
            .flat_map(|m| {
                m.as_table()
                    .and_then(|table| table.get("patch"))
                    .into_iter()
                    .flat_map(|patch| patch.as_table())
                    .flat_map(|patch| patch.get("crates"))
                    .filter_map(|crates| crates.as_array())
            })
            .flatten()
            .flat_map(|s| s.as_str())
            .map(|n| resolve.query(n).and_then(|id| pkg_set.get_one(id)))
            .collect::<Result<HashSet<_>>>()?;

        if args.force {
            info!("Cleaning up patch folder.");
            workspace.clean_patch_folder()?;
        }

        if patches_folder.exists() {
            for entry in fs::read_dir(patches_folder)? {
                let entry = entry?;
                if entry.metadata()?.is_file()
                    && entry.path().extension() == Some(OsStr::new(PATCH_EXT))
                {
                    let patch_file = entry.path();
                    let filename = patch_file
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .ok_or(anyhow!("Patch file does not have a name"))?;

                    if let Some((pkg_name, version)) = filename.split_once('+') {
                        let pkg_id = resolve.query(format!("{}@{}", pkg_name, version).as_str())?;
                        let pkg = pkg_set.get_one(pkg_id)?;
                        if !crates_to_patch.contains(&pkg) {
                            warn!(
                                "crate: {}, {} is not in the [package.metadata.patch] section of Cargo.toml. Did you forget to add it?",
                                pkg_name, pkg_name
                            );
                            continue;
                        }

                        let patch_target_path = pkg.patch_target_path(&workspace)?;
                        if !patch_target_path.exists() {
                            copy_package(pkg, &patch_target_folder, args.force)?;
                            info!("crate: {}, applying patch started.", pkg_name);
                            git::init(&patch_target_path)?;
                            git::apply(&patch_target_path, &patch_file)?;
                            git::destroy(&patch_target_path)?;
                            info!(
                                "crate: {}, successfully applied patch {:?}.",
                                pkg_name, patch_file
                            );
                        } else {
                            info!("crate: {}, skip applying patch, {:?} already exists. Did you forget to add `--force`?", pkg_name, patch_target_path);
                        }
                        crates_to_patch.remove(pkg);
                    }
                }
            }
        }
        for pkg in crates_to_patch {
            copy_package(pkg, &patch_target_folder, args.force)?;
        }
    }

    info!("Done");
    Ok(())
}

mod log {
    pub use paris::*;
}

mod git {
    use std::{ffi::OsStr, fs, path::Path, process::Command};

    pub fn init(repo_dir: &Path) -> anyhow::Result<()> {
        Command::new("git")
            .current_dir(repo_dir)
            .args(["init"])
            .output()?;
        Command::new("git")
            .current_dir(repo_dir)
            .args(["add", "."])
            .output()?;
        Command::new("git")
            .current_dir(repo_dir)
            .args(["commit", "-m", "zero"])
            .output()?;
        Ok(())
    }

    pub fn apply(repo_dir: &Path, patch_file: &Path) -> anyhow::Result<()> {
        #[cfg(target_os = "windows")]
        let patch_file = patch_file
            .to_string_lossy()
            .to_string()
            .trim_start_matches(r#"\\?\"#)
            .to_string();
        #[cfg(not(target_os = "windows"))]
        let patch_file = patch_file.to_string_lossy().to_string();

        let out = Command::new("git")
            .current_dir(repo_dir)
            .args([
                "apply",
                "--ignore-space-change",
                "--ignore-whitespace",
                "--whitespace=nowarn",
                &patch_file,
            ])
            .output()?;

        if !out.status.success() {
            anyhow::bail!(String::from_utf8(out.stderr)?)
        }
        Ok(())
    }
    pub fn destroy(repo_dir: &Path) -> anyhow::Result<()> {
        let git_dir = repo_dir.join(".git");
        if git_dir.exists() {
            fs::remove_dir_all(git_dir)?;
        }
        Ok(())
    }
    pub fn create_patch(repo_dir: &Path, patch_file: &Path) -> anyhow::Result<()> {
        Command::new("git")
            .current_dir(repo_dir)
            .args(["add", "."])
            .output()?;

        let out = Command::new("git")
            .current_dir(repo_dir)
            .args([OsStr::new("diff"), OsStr::new("--staged")])
            .output()?;

        if out.status.success() {
            fs::write(patch_file, out.stdout)?;
        }
        Ok(())
    }
}