forked from emlab-ucsb/adv_git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslides.html
More file actions
541 lines (348 loc) · 10.4 KB
/
Copy pathslides.html
File metadata and controls
541 lines (348 loc) · 10.4 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<title>Advanced git</title>
<meta charset="utf-8" />
<meta name="author" content="🐝, 🐜, 🐞, 🐝, 🐞, 🐞, 🐝" />
<link href="libs/remark-css/default.css" rel="stylesheet" />
<link href="libs/remark-css/default-fonts.css" rel="stylesheet" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Advanced <code>git</code>
## Best practices and debugging
### 🐝, 🐜, 🐞, 🐝, 🐞, 🐞, 🐝
### SFG
### 2019/08/21
---
# Outline
If you haven't, `fork` and `clone` the repo from: [https://github.com/emlab-ucsb/adv_git.git](https://github.com/emlab-ucsb/adv_git.git)
- Boring
- Best practices
- Hands-on
- Repo tour
- Tests
- functions / packages
- `shiny` Apps
- Finding the error
- Fixing the error
---
class: center, middle, inverse
# Best practices
---
# Use the terminal
- It doesn't have to be scary
- You'll mostly use `pull`, `add`, `commit`, and `push`
- It forces you to think about what you have to `stage` and `commit`
- It's good to get comfortable, especially when you have to fix a repo
---
# Commit frequency
- Try to keep commits as small as possible
- Don't just `stage` all and `commit` "updated repo" (we all do it)
- If possible, break it down to files / topics / improvements
- If possible, break it down to sections within a file
- You don't have to `push` all your changes
- `commit` through the day, and `push` at the end
---
# Testing your code
Create tests for possible breaks:
- Test for data integrity
- Number of records
- Dimensions of a key `data.frame`
- Dates in your `Raster*` objects
- Test for function behavior
- Returns correct output
- Handles `NA`s or `NULL`s correctly
- Test for know breaks
--
**Test before pushing big changes**
---
# Using tags
If something (ever) works, mark it!
- After your `commit`, you can do:
`git tag -a version -m "message"`
For example:
```bash
**run tests in R**
git add scripts/foo.R #Stages the foo.R script
git commit -m "Added foo.R script" #This is our commit message
git tag -a v0.1 -m "foo.R working" #Add a tagg and a message
```
---
# Using tags
In the repo, try:
`git tag`
You should get:
```bash
v0.1
v0.2
v0.3
```
Not very informative, right?
---
# Using tags
Now try:
`git show v0.2`
```bash
tag v0.3
Tagger: jcvdav <juancarlos.villader@gmail.com>
Date: Mon Aug 19 16:27:56 2019 -0700
Tests working with more precision
commit c581a28de5a13d576654cf6ae6491142f551d9bf (tag: v0.3)
Author: jcvdav <juancarlos.villader@gmail.com>
Date: Mon Aug 19 16:27:20 2019 -0700
Updated tests for more precision
```
---
# Using tags
Now try
```bash
git logline
# that's a shortcut for
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
```
or
```bash
git logone
# short for
git log --pretty=oneline
```
---
class: center, middle, inverse
# Repo tour
---
# Repo
```
-- adv.Rproj
-- basic_app
|__app.R
|__tests
-- DESCRIPTION
-- man
|__gal2lit.Rd
-- NAMESPACE
-- R
|__gal2lit.R
-- README.md
-- tests
|__testthat
|__testthat.R
```
---
class: center, middle, inverse
# Tests
---
# Shiny Apps
- The shiny app is under `basic_app/app.R`
- Run `shiny::runApp("basic_app")` or open the file and run the app
- Stop the instance
- Now let's run the tests:
- Run `shinytest::testApp("basic_app")` or
- Open the `app.R` file
- Top-right corner, click on `Run App` >> `Run Tests`
- Top-right corner, click on `Compare Results`
- Inspect the differences:
- The app is not returning the correct conversion of gallons to liters
- But is this an error in the app, or in the underlying code?
---
# Packages / functions
- In your root directory for the repo, look for a `tests` folder
- There is a `testthat.R` file, and folder with the same name
- Open `tests/testthat/test_gal2lit.R`
- To test the package (*i.e.* run all tests):
- You can run `devtools::test()`
- Take advantage of the Rstudio IDE:
- Go to the `Build` pane
- Click on `More`
- Select `Test package` (or simply `Ctrl` + `Shift` + `T`)
---
```bash
==> devtools::test()
Loading adv
Testing adv
v | OK F W S | Context
x | 0 2 | gal2lit converts correctly
---------------------------------------------
test_gal2lit.R:8: failure: gal2lit
gal2lit(1) not equal to 3.78541.
1/1 mismatches
[1] 3.79 - 3.79 == -0.00041
test_gal2lit.R:9: failure: gal2lit
gal2lit(1/3.78541) not equal to 1.
1/1 mismatches
[1] 1 - 1 == -0.000108
---------------------------------------------
v | 3 | gal2lit handles input types
== Results ==================================
OK: 3
Failed: 2
Warnings: 0
Skipped: 0
```
---
class: center, middle, inverse
# Well 💩, 💩, 💩, 💩, 💩, 💩, 💩
Turns out our basic function is not working...
Now what?
We (sort of?) know *where* the error is (this is an easy one)
But *when* / *how* / *why* / (**who???** 😠)?
Enter: `git bisect`
---
# `git bisect`
## Why not go into GitHub?
- You want to be able to track incremental changes
- Let's you see how changes relate to tests / status of the repo
- If you use the `git bisect` approach, no need to leave RStudio!
---
# `git bisect`
## What does it do?
- You start by telling it that the present commit (state of the repo) is `bad`
- You then tell it when things worked (remember `tags`?)
- It choses a point (roughly) between them and checks out a version of how things looked like
- Then, you iterate:
- Running your tests
- Tagging each commit as `good` or `bad`
- `git bisect` will advance in the logical direction
- Do until it finds the last good commit
---
# `git bisect`
## Workflow: finding
```bash
git logline #To see the history
git bisect start #Start bisecting
git bisect bad #Tagg current state as bad
git bisect good HASH / tag #Specify a state where things worked
# git bisect will take you to somewhere in the middle
**Run tests in R**
# If it works
git bisect good
#marks this point as working and moves forward in time
# If it fails
git bisect bad
#marks this pointas not working and moves backward in time
**Run tests in R**
... #Until test pass
```
---
# `git bisect`
## Workflow: fixing
We know *when* (*i.e.* HASH) things changed, let's dig deeper:
```bash
git bisect reset #Exit bisect mode (go back to the present)
git show HASH #Check the changes of the guilty commit
```
---
# `git bisect`
## Workflow: fixing

---
Let's fix the problem:
```bash
git branch #To check existing branches
git checkout -b fix_bug #To create a new branch and switch into it
git revert HASH #Will undo changes in that commit
# It is equivalent to just making the change yourself
# in `gal2lit.R` and `add` and `commit` the result
**Run tests in R**
```
---
# `git bisect`
## Workflow
```bash
git checkout master #Get back to master branch
git branch #Confirm that you are on master
git merge fix_bug master #Bring the changes in fix_bug into master
**Run tests in R** #optional, at this point
git tag -a vx.x -m "Fixed bug" #(Optionally, add a tag)
git push #Push changes
```
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlight.output": true,
"highlightLines": false,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();</script>
<script>
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>