/ radicle-cli / examples / rad-patch-ahead-behind.md
rad-patch-ahead-behind.md
  1  In this example, we explore how the `ahead/behind` indicator works, and what is
  2  shown as diffs in the case of divergent branches.
  3  
  4  First we add the `CONTRIBUTORS` file to `master`, which contains one entry:
  5  ```
  6  $ git checkout -q master
  7  $ git add CONTRIBUTORS
  8  $ git commit -a -q -m "Add contributors"
  9  $ git push rad master
 10  $ cat CONTRIBUTORS
 11  Alice Jones
 12  ```
 13  
 14  Then we create a feature branch which adds another entry:
 15  ```
 16  $ git checkout -q -b feature/1
 17  $ sed -i '$a Alan K' CONTRIBUTORS
 18  $ git commit -a -q -m "Add Alan"
 19  ```
 20  
 21  We go back to master, and add a different second entry, essentially forking
 22  the history:
 23  ```
 24  $ git checkout -q master
 25  $ sed -i '$a Jason Bourne' CONTRIBUTORS
 26  $ git commit -a -q -m "Add Jason"
 27  $ git push rad master
 28  $ git log --graph --decorate --abbrev-commit --pretty=oneline --all
 29  * 5c88a79 (feature/1) Add Alan
 30  | * e101a99 (HEAD -> master, rad/master) Add Jason
 31  |/ [..]
 32  * f64fb2c Add contributors
 33  * f2de534 Second commit
 34  * 08c788d Initial commit
 35  ```
 36  
 37  Then we create a patch from `feature/1`:
 38  ``` (stderr)
 39  $ git push rad feature/1:refs/patches
 40  ✓ Patch 71e51dfcf7ca124a75ec6e0cb21b13bf86b8bb2e opened
 41  To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
 42   * [new reference]   feature/1 -> refs/patches
 43  ```
 44  
 45  When listing, we see that it has one addition:
 46  ```
 47  $ rad patch list
 48  ╭────────────────────────────────────────────────────────────────────────╮
 49  │ ●  ID       Title     Author                  Head     +   -   Updated │
 50  ├────────────────────────────────────────────────────────────────────────┤
 51  │ ●  71e51df  Add Alan  z6MknSL…StBU8Vi  (you)  5c88a79  +1  -0  now     │
 52  ╰────────────────────────────────────────────────────────────────────────╯
 53  ```
 54  
 55  When showing the patch, we see that it is `ahead 1, behind 1`, since master has
 56  diverged by one commit:
 57  ```
 58  $ rad patch show -v -p 71e51df
 59  ╭────────────────────────────────────────────────────╮
 60  │ Title     Add Alan                                 │
 61  │ Patch     71e51dfcf7ca124a75ec6e0cb21b13bf86b8bb2e │
 62  │ Author    z6MknSL…StBU8Vi (you)                    │
 63  │ Head      5c88a79d75f5c2b4cc51ee6f163d2db91ee198d7 │
 64  │ Base      f64fb2c8fe28f7c458c72ec8d700373924794943 │
 65  │ Branches  feature/1                                │
 66  │ Commits   ahead 1, behind 1                        │
 67  │ Status    open                                     │
 68  ├────────────────────────────────────────────────────┤
 69  │ 5c88a79 Add Alan                                   │
 70  ├────────────────────────────────────────────────────┤
 71  │ ● opened by z6MknSL…StBU8Vi (you) now              │
 72  ╰────────────────────────────────────────────────────╯
 73  
 74  commit 5c88a79d75f5c2b4cc51ee6f163d2db91ee198d7
 75  Author: radicle <radicle@localhost>
 76  Date:   Thu Dec 15 17:28:04 2022 +0000
 77  
 78      Add Alan
 79  
 80  diff --git a/CONTRIBUTORS b/CONTRIBUTORS
 81  index 3f60d25..6829c43 100644
 82  --- a/CONTRIBUTORS
 83  +++ b/CONTRIBUTORS
 84  @@ -1 +1,2 @@
 85   Alice Jones
 86  +Alan K
 87  
 88  ```
 89  
 90  Then, we stack another change onto `feature/1`, adding another contributor:
 91  ``` (stderr)
 92  $ git checkout -q -b feature/2 feature/1
 93  $ sed -i '$a Mel Farna' CONTRIBUTORS
 94  $ git commit -a -q -m "Add Mel"
 95  $ git push -o patch.message="Add Mel" rad HEAD:refs/patches
 96  ✓ Patch 364cc2809f14c1bc74a8868159e87eb3844eb7e2 opened
 97  To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
 98   * [new reference]   HEAD -> refs/patches
 99  ```
100  
101  When we look at the patch, we see that it has both commits, because this new
102  patch uses the same base as the previous patch:
103  ```
104  $ rad patch show -v 364cc2809f14c1bc74a8868159e87eb3844eb7e2
105  ╭────────────────────────────────────────────────────╮
106  │ Title     Add Mel                                  │
107  │ Patch     364cc2809f14c1bc74a8868159e87eb3844eb7e2 │
108  │ Author    z6MknSL…StBU8Vi (you)                    │
109  │ Head      7f63fcbcf23fc39eea784c091ad3d20d7e4bd005 │
110  │ Base      f64fb2c8fe28f7c458c72ec8d700373924794943 │
111  │ Branches  feature/2                                │
112  │ Commits   ahead 2, behind 1                        │
113  │ Status    open                                     │
114  ├────────────────────────────────────────────────────┤
115  │ 7f63fcb Add Mel                                    │
116  │ 5c88a79 Add Alan                                   │
117  ├────────────────────────────────────────────────────┤
118  │ ● opened by z6MknSL…StBU8Vi (you) now              │
119  ╰────────────────────────────────────────────────────╯
120  ```
121  
122  If we want to instead create a "stacked" patch, we can do so with the
123  `patch.base` push option:
124  
125  ``` (stderr)
126  $ git push -o patch.message="Add Mel #2" -o patch.base=HEAD^ rad HEAD:refs/patches
127  ✓ Patch 11ab7fbec82c3aed393d7a696d6b3c7714735056 opened
128  To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
129   * [new reference]   HEAD -> refs/patches
130  ```
131  
132  As you'll notice, using the previous patch as the base, we only see commit
133  `7f63fcb` listed for this new patch.
134  
135  However, since the patch is still intended to be merged into `master`, we see
136  that it is still two commits ahead and one behind from `master`.
137  
138  ```
139  $ rad patch show -v 11ab7fbec82c3aed393d7a696d6b3c7714735056
140  ╭────────────────────────────────────────────────────╮
141  │ Title     Add Mel #2                               │
142  │ Patch     11ab7fbec82c3aed393d7a696d6b3c7714735056 │
143  │ Author    z6MknSL…StBU8Vi (you)                    │
144  │ Head      7f63fcbcf23fc39eea784c091ad3d20d7e4bd005 │
145  │ Base      5c88a79d75f5c2b4cc51ee6f163d2db91ee198d7 │
146  │ Branches  feature/2                                │
147  │ Commits   ahead 2, behind 1                        │
148  │ Status    open                                     │
149  ├────────────────────────────────────────────────────┤
150  │ 7f63fcb Add Mel                                    │
151  ├────────────────────────────────────────────────────┤
152  │ ● opened by z6MknSL…StBU8Vi (you) now              │
153  ╰────────────────────────────────────────────────────╯
154  ```