# How to combine patches?

**URL:** <https://discourse.pijul.org/t/how-to-combine-patches/469>\
**Category:** Question\
**Created:** [February 8, 2020, 7:51pm UTC](https://discourse.pijul.org/t/how-to-combine-patches/469 "2020-02-08T19:51:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronjanse](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/aaronjanse/32/274_2.png) [@aaronjanse](https://discourse.pijul.org/u/aaronjanse)\
**Post date:** [February 8, 2020, 7:51pm UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/1 "2020-02-08T19:51:03Z")

</div>

I’m trying to write a long tutorial based on patches, showing & explaining to readers a series of changes to make to an initial hello world into a finished product. I thought pijul sounded perfect for this, plus, I’ve long been interested in pijul anyway.

While Git would allow me to encode my patches into ascii (can pijul do this?), pijul would allow me to fix errors made in the beginning of the tutorial then reorder the patches.

However, I can’t figure out how to combine patches in history.

Original: `A -> B -> C -> D`  
With fix: `A -> B -> C -> D -> B_fix`  
Reordered: `A -> B -> B_fix -> C -> D`  
Merged B patches: `A -> B' -> C -> D`

How would I do that last step? Is it possible?  
If not, is there a way to ascii-encode the net result of multiple patches?

---

<div class="post-metadata">

**Author:** ![pmeunier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/pmeunier/32/4_2.png) [@pmeunier](https://discourse.pijul.org/u/pmeunier)\
**Post date:** [February 8, 2020, 8:21pm UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/2 "2020-02-08T20:21:37Z")

</div>

Hi! Welcome here.

> [@aaronjanse](#):
>
> While Git would allow me to encode my patches into ascii (can pijul do this?)

Not yet. Pijul patches store more information than what Git diff would give you. In particular, Pijul knows about conflicts. Being able to show patches as ascii, even if it implies a small blowup in the size of the representation compared to binary, would be a really fundamental feature Pijul is still missing.

> [@aaronjanse](#):
>
> How would I do that last step? Is it possible?

It depends. In Pijul, patches producible independently commute, so there’s no need to explicitly reorder them (unlike in Darcs for example). The good news here is, if `C` and `D` could have been made independently from `B` and `B_fix`, you can unrecord `B` and `B_fix`, and record `B'`, without changing `C` and `D`. However, if `C` or `D` (or both) depend on `B`, then you’ll have to change them too, and we don’t have a good way to do that yet.

I hope to be able to write a command for this soon enough.

---

<div class="post-metadata">

**Author:** ![aaronjanse](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/aaronjanse/32/274_2.png) [@aaronjanse](https://discourse.pijul.org/u/aaronjanse)\
**Post date:** [February 8, 2020, 8:56pm UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/3 "2020-02-08T20:56:24Z")

</div>

> It depends. In Pijul, patches producible independently commute, so there’s no need to explicitly reorder them (unlike in Darcs for example)

Got it. Now that I think of it, a way to represent (in ascii) the net of multiple patches would do exactly what I’m looking for.

> Being able to show patches as ascii, even if it implies a small blowup in the size of the representation compared to binary, would be a really fundamental feature Pijul is still missing.

And I’d love to make a contribution to pijul, however small it may be. I was hoping to contribute to the nest’s frontend, but it seems to be closed-source (it there a thread discussing why?).

If I wanted to take a shot at creating a way to ascii-encode patches, it sounds like I’d just need a way to convert an instance of the [Patch enum](https://nest.pijul.com/pijul_org/pijul:master/662ad86eb85d3b203b01#J4ZouD18wSf) to/from a pretty string? In that case, may I ask what [PrettyPatch](https://nest.pijul.com/pijul_org/pijul:master/662ad86eb85d3b206204#JuBmQrY5jEw) does?

Thanks!

---

<div class="post-metadata">

**Author:** ![pmeunier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/pmeunier/32/4_2.png) [@pmeunier](https://discourse.pijul.org/u/pmeunier)\
**Post date:** [February 9, 2020, 6:11am UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/4 "2020-02-09T06:11:28Z")

</div>

`PrettyPatch` was a first try at this, but it was never finished. Contributing to Pijul at the moment is tricky, as I’m in the process of rewriting the foundations (libpijul), now that I understand the full story. The patch format will change once again, although not conceptually (I’m just making it smaller on disk).

But if you want to take a shot at this, you will have my full support (ask anything), and I’ll integrate your changes in the next version.

Also, after I’m done with this, open-sourcing the Nest will be my next project.

---

<div class="post-metadata">

**Author:** ![aaronjanse](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/aaronjanse/32/274_2.png) [@aaronjanse](https://discourse.pijul.org/u/aaronjanse)\
**Post date:** [February 11, 2020, 12:15am UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/5 "2020-02-11T00:15:01Z")

</div>

> [@pmeunier](#):
>
> But if you want to take a shot at this, you will have my full support (ask anything), and I’ll integrate your changes in the next version.

Awesome!

I was able to make a one-line fix (in read\_key) to PrettyPatch to allow it to correctly translate back into an UnsignedPatch.

My current (ugly) “pretty printing” of PrettyPatch is as follows:

```auto
PrettyPatch { header: PatchHeader { authors: ["Florent Becker <florent.becker@ens-lyon.org>"], name: "Fixes in the test suite", description: None, timestamp: 2018-04-08T14:59:53.077354432Z, flag: (empty) }, dependencies: ["69FSXS1UWZwqQ5AdmiBQeBtYK2B6UtZALQfnzsVdRaMCaQEpEaiqoBwBao6PeL2GphzwTCxYEiz71h7rg75L3w6P"], changes: [NewEdges { previous: 8, flag: 24, edges: [PrettyNewEdge { from: "1.MF1Bc9zzEzB", to: "1.M5JWbMBVdif", introduced_by: 1 }], inode: "1.foh4EGyS55" }, NewNodes { up_context: ["1.M5JWbMBVdif"], down_context: ["1.MQhrcxpUrFh"], flag: 0, line_num: 3, nodes: [String(" REMOTE_PIJUL=\"$PIJUL_EXE\" pijul key upload $USER@localhost\n")], inode: "1.foh4EGyS55" }, NewEdges { previous: 8, flag: 24, edges: [PrettyNewEdge { from: "1.GjNc5KjH9zP", to: "1.GZfw4WunYis", introduced_by: 1 }], inode: "1.LQM2cdzDY3" }, NewNodes { up_context: ["1.GZfw4WunYis"], down_context: ["1.Gu5H68YmmFu"], flag: 0, line_num: 4, nodes: [String(" export PIJUL_SRC_DIR=\"$BATS_TEST_DIRNAME/../..\"\n")], inode: "1.LQM2cdzDY3" }, NewEdges { previous: 8, flag: 24, edges: [PrettyNewEdge { from: "1.HEUd7kBkynw", to: "1.H4mx6wNGNXR", introduced_by: 1 }], inode: "1.LQM2cdzDY3" }, NewNodes { up_context: ["1.H4mx6wNGNXR"], down_context: ["1.HQBJ8Z1Fb4T"], flag: 0, line_num: 5, nodes: [String(" export PIJUL_EXE=\"$PIJUL_SRC_DIR/../target/debug/pijul\"\n")], inode: "1.LQM2cdzDY3" }] }

```

Turns into:

```auto
PIJUL PATCH 2018-04-08 14:59:53.077354432 UTC
Florent Becker <florent.becker@ens-lyon.org>
Fixes in the test suite
69FSXS1UWZwqQ5AdmiBQeBtYK2B6UtZALQfnzsVdRaMCaQEpEaiqoBwBao6PeL2GphzwTCxYEiz71h7rg75L3w6P
EDGES 8 24
1.foh4EGyS55
1: 1.MF1Bc9zzEzB -> 1.M5JWbMBVdif
NODES 3
0 1.foh4EGyS55 1.M5JWbMBVdif 1.MQhrcxpUrFh
+ REMOTE_PIJUL="$PIJUL_EXE" pijul key upload $USER@localhost

EDGES 8 24
1.LQM2cdzDY3
1: 1.GjNc5KjH9zP -> 1.GZfw4WunYis
NODES 4
0 1.LQM2cdzDY3 1.GZfw4WunYis 1.Gu5H68YmmFu
+ export PIJUL_SRC_DIR="$BATS_TEST_DIRNAME/../.."

EDGES 8 24
1.LQM2cdzDY3
1: 1.HEUd7kBkynw -> 1.H4mx6wNGNXR
NODES 5
0 1.LQM2cdzDY3 1.H4mx6wNGNXR 1.HQBJ8Z1Fb4T
+ export PIJUL_EXE="$PIJUL_SRC_DIR/../target/debug/pijul"

```

I think I’ll have to read more about how Pijul works in order to devise a more logical encoding ( **I’m open to suggestions!** ). I also think some fields are redundant, such as flags seemingly the same for all nodes and up/down contexts seemingly always being arrays of length one.

---

<div class="post-metadata">

**Author:** ![pmeunier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/pmeunier/32/4_2.png) [@pmeunier](https://discourse.pijul.org/u/pmeunier)\
**Post date:** [February 11, 2020, 7:04am UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/6 "2020-02-11T07:04:27Z")

</div>

Cool! The only thing that remains to do is print the contents of the edge targets in case of deletions. It’s useless for the algorithm but interesting to humans.

Deletions are just edges with `edge.flag == EdgeFlags::DELETED`.  
For conflict resolutions (which are often edges too) the display will be harder.

---

<div class="post-metadata">

**Author:** ![porky11](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/porky11/32/152_2.png) [@porky11](https://discourse.pijul.org/u/porky11)\
**Post date:** [February 11, 2020, 8:50pm UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/7 "2020-02-11T20:50:04Z")

</div>

> [@aaronjanse](#):
>
> It depends. In Pijul, patches producible independently commute, so there’s no need to explicitly reorder them (unlike in Darcs for example). The good news here is, if `C` and `D` could have been made independently from `B` and `B_fix` , you can unrecord `B` and `B_fix` , and record `B'` , without changing `C` and `D` . However, if `C` or `D` (or both) depend on `B` , then you’ll have to change them too, and we don’t have a good way to do that yet.

I think, it would be nice to be able to simply merge some patches, or even do an amend, like in git or mercurial. Especially when you have local changes in your project, which you don’t want to change, it’s a bit complicated now.  
After unrecording the patches you either have to select each change again, or you make some temporary record containing the local changes befor you unrecord, so you can just record all, and then unrecord the local changes again.

---

<div class="post-metadata">

**Author:** ![aaronjanse](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.pijul.org/aaronjanse/32/274_2.png) [@aaronjanse](https://discourse.pijul.org/u/aaronjanse)\
**Post date:** [February 12, 2020, 3:40am UTC](https://discourse.pijul.org/t/how-to-combine-patches/469/8 "2020-02-12T03:40:21Z")

</div>

> [@pmeunier](#):
>
> Cool! The only thing that remains to do is print the contents of the edge targets in case of deletions. It’s useless for the algorithm but interesting to humans.

Will do! I’ll also add context to the line additions.
