summaryrefslogtreecommitdiff
path: root/kernel-devel/kernel-best-practices/handout_kernel-best-practices_en.tex
blob: bd1140bc227e85ffaf4c42896e1909f2b95531f2 (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
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
\input{confighandout}

\subsection{Kernel Best-Practices}

Linux development is heavily distributed. There are many powerful tools, which
assist the distributed development. The most important and powerful ones, will
be presented in this chapter.

\subsubsection{diff and patch - the basics}

Linux kernel development is based on patches. A patch is a file which describes
the changes between an original version of a source tree and a modified one.

For simplicity, a source tree with only one file will be used as example:

orig/hello.c

\begin{lstlisting}
int main (int argc, char **argv)
{
	return 0;
}
\end{lstlisting}

This source tree will be copied:

\begin{lstlisting}
cp -a orig new
\end{lstlisting}

The new source tree be modified:

new/hello.c

\begin{lstlisting}
#include <stdio.h>

int main (int argc, char **argv)
{
	printf ("Huhu World\n");
	return 0;
}
\end{lstlisting}

The diff commands generates a patch file:

\begin{lstlisting}
diff -Nur orig new > hello.patch
\end{lstlisting}

The content of hello.patch is:

\begin{lstlisting}
--- orig/hello.c        2011-03-22 22:38:40.796426752 +0100
+++ new/hello.c 2011-03-22 22:39:02.472155769 +0100
@@ -1,4 +1,7 @@
+#include <stdio.h>
+
 int main (int argc, char **argv)
 {
+       printf ("Huhu World\n");
        return 0;
 }
\end{lstlisting}

The newly added lines are prefixed with a '+', removed lines with a '-'.

If the patch file is more complex, diffstat informs you about the content:

\begin{lstlisting}
diffstat hello.patch
 hello.c |    3 +++
 1 file changed, 3 insertions(+)
\end{lstlisting}

Anyone who has the original source version, can apply the generated patch. The
result is an updated version of the source tree:

\begin{lstlisting}
cd orig
patch -p1 < ../hello.patch
\end{lstlisting}

With the argument -pX the numbers of stripped directories (in our case new) can be defined.

With the argument -R a patch can be 'reverse applied' aka removed. The result
will be the original source tree.

Don't apply or remove the same patch more then once. If you have more than one
patch, apply and revert the patches in the right order. Else the resulting
source tree will be undefined.

If the original source differs from the one, the patch was created. The patch
may apply with 'HUNKS'. This means, the changes can be applied, but the line
numbers, given after the @@@ tag don't fit exactly.

If the original source differs from the one, the patch was created. It is
possible, that the patch won't apply anymore. Then all rejects are saved in a
file called <original file name>.rej

\subsubsection{quilt}

diff and patch work fine for one patch. But if you have to handle a queuqe of
patches it will be hard to keep the order of the patches, update a single patch,etc. quilt is a tool, for managing patch queques.

The above example, using quilt:

orig/hello.c

\begin{lstlisting}
int main (int argc, char **argv)
{
	return 0;
}
\end{lstlisting}

create a new patch:

\begin{lstlisting}
cd orig
quilt new hello.patch
quilt edit hello.c
\end{lstlisting}

your default editor will be opened, make your changes to the source:

\begin{lstlisting}
#include <stdio.h>

int main (int argc, char **argv)
{
	printf ("Huhu World\n");
	return 0;
}
\end{lstlisting}

write the changes into the patch file:

\begin{lstlisting}
quilt refresh
\end{lstlisting}

two files will be generated:

\begin{lstlisting}
ls patches/
hello.patch  series
\end{lstlisting}

hello.patch is our well-known patch. series contains the name of our patch.

The patch can be applied

\begin{lstlisting}
quilt push
File series fully applied, ends at patch patches/hello.patch
\end{lstlisting}

Then we create a new patch on top of our current queque:

\begin{lstlisting}
quilt new seperate-print-in-own-function.patch
quilt edit hello.c
\end{lstlisting}

\begin{lstlisting}
#include <stdio.h>

void print ()
{
        printf ("Huhu World\n");
}

int main (int argc, char **argv)
{
        print ();
        return 0;
}
\end{lstlisting}

both patches are now listed in the series file:

\begin{lstlisting}
cat patches/series 
hello.patch
seperate-print-in-own-function.patch
\end{lstlisting}

All patches can be reverted at once:

\begin{lstlisting}
quilt pop -a
Removing patch patches/seperate-print-in-own-function.patch
Restoring hello.c

Removing patch patches/hello.patch
Restoring hello.c

No patches applied
\end{lstlisting}

You can zip the quilt queque and share it with other programmers.

\subsubsection{git}

But if they modify the quilt queque at the same time, as you. Merging the
queques by hand may be hard. In projects like the linux kernel, it is a no go.

Currently Linus' (and many others) Kernel trees are managed by git. Git is a
very powerful version control system. The most important difference to other
systems, like subversion, is that the hole history of a project is held local.
Nobody should commit into your git tree. In git you pull the changes from other
people into your tree.

Let's have a look at git:

orig/hello.c

\begin{lstlisting}
int main (int argc, char **argv)
{
	return 0;
}
\end{lstlisting}

init git:

\begin{lstlisting}
$cd orig
$git init .
Initialized empty Git repository in /home/manut/vorlesung.git/orig/.git/
$git config user.email "manut@mecka.net"
$git config user.name "Manuel Traut"
\end{lstlisting}

The configuration is saved at .git/config
If git config --global is used the configuration will be used by all
repositories which don't have a own configuration. Then it is saved at 
~/.gitconfig

Now add the files to version control and commit your initial code.

\begin{lstlisting}
$git add hello.c
$git commit -sa
\end{lstlisting}

The default editor will be opened, to enter a commit message:

\begin{lstlisting}
initial import of the hello world source

this projects is aimed to show the use of git.

Signed-off-by: Manuel Traut <manut@mecka.net>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   hello.c
#

[master (root-commit) 473cf10] initial import of the hello world source
 1 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 hello.c

\end{lstlisting}

Now edit the source tree and generate commit your changes:

\begin{lstlisting}
$patch -p1 < ../hello.patch
$git commit -sa

add stdio include, print hello world

prints 'Huhu World' to stdout

Signed-off-by: Manuel Traut <manut@mecka.net>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   hello.c

[master 6ea27e6] add stdio include, print hello world
 1 files changed, 3 insertions(+), 0 deletions(-)

\end{lstlisting}

A hash will be generated that can be used to identify each patch. A history
and a single patch can be displayed with these commands:

\begin{lstlisting}
$git log
commit 6ea27e6199aeac63055d845ef40fecd4b594f1d7
Author: Manuel Traut <manut@mecka.net>
Date:   Tue Mar 22 23:37:13 2011 +0100

    add stdio include, print hello world
    
    prints 'Huhu World' to stdout
    
    Signed-off-by: Manuel Traut <manut@mecka.net>

commit 473cf10e95778c54d49fc6ea368e196f1347ac8a
Author: Manuel Traut <manut@mecka.net>
Date:   Tue Mar 22 23:31:50 2011 +0100

    initial import of the hello world source
    
    this projects is aimed to show the use of git.
    
    Signed-off-by: Manuel Traut <manut@mecka.net>


$git shortlog
Manuel Traut (2):
      initial import of the hello world source
      add stdio include, print hello world

$git show 473cf10e95778c54d49fc6ea368e196f1347ac8a
commit 473cf10e95778c54d49fc6ea368e196f1347ac8a
Author: Manuel Traut <manut@mecka.net>
Date:   Tue Mar 22 23:31:50 2011 +0100

    initial import of the hello world source
    
    this projects is aimed to show the use of git.
    
    Signed-off-by: Manuel Traut <manut@mecka.net>

diff --git a/hello.c b/hello.c
new file mode 100644
index 0000000..b891504
--- /dev/null
+++ b/hello.c
@@ -0,0 +1,4 @@
+int main (int argc, char **argv)
+{
+       return 0;
+}

\end{lstlisting}

To add some new functionality it's a good idea to temporarily create a new
branch:

\begin{lstlisting}
$git branch fancy-new-stuff
$git checkout fancy-new-stuff
Switched to branch 'fance-new-stuff'
\end{lstlisting}

It's possible to go back in history:

\begin{lstlisting}
git reset --hard 473cf10e95778c54d49fc6ea368e196f1347ac8a
HEAD is now at 473cf10 initial import of the hello world source
\end{lstlisting}

Make changes:

\begin{lstlisting}
#include <stdio.h>

int main (int argc, char **argv)
{
        printf ("Hello World\n");
        return 0;
}
\end{lstlisting}

and commit them:

\begin{lstlisting}
$git commit -sa

HELLO world

..not huhu world

Signed-off-by: Manuel Traut <manut@mecka.net>

[fance-new-stuff 72e3346] HELLO world
 1 files changed, 3 insertions(+), 0 deletions(-)

\end{lstlisting}

You like the new version and want to merge it into your master tree:

\begin{lstlisting}
$git checkout master
Switched to branch 'master'
$git merge fancy-new-stuff
Auto-merging hello.c
CONFLICT (content): Merge conflict in hello.c
Automatic merge failed; fix conflicts and then commit the result.
\end{lstlisting}

Git reports an CONFLICT because an automatic merge is not possible. The same
line (printf) has different content in master and fancy-new-stuff. So the merge
conflict has to be resolved by hand.

hello.c

\begin{lstlisting}
#include <stdio.h>

int main (int argc, char **argv)
{
<<<<<<< HEAD
        printf ("Huhu World\n");
=======
        printf ("Hello World\n");
>>>>>>> fancy-new-stuff
        return 0;
}
\end{lstlisting}

All lines which shouldn't be there can be removed by hand. This result needs
to be commited:

\begin{lstlisting}
$git commit -sa

Merge branch 'fance-new-stuff'

Conflicts:
        hello.c

Signed-off-by: Manuel Traut <manut@mecka.net>
#
# It looks like you may be committing a MERGE.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.
#

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#
#       modified:   hello.c
#

[master 1815151] Merge branch 'fance-new-stuff'

$git log

commit 1815151a08537bd2403605f8adb22852cfdb2d23
Merge: 6ea27e6 72e3346
Author: Manuel Traut <manut@mecka.net>
Date:   Tue Mar 22 23:57:19 2011 +0100

    Merge branch 'fance-new-stuff'
    
    Conflicts:
        hello.c
    
    Signed-off-by: Manuel Traut <manut@mecka.net>

commit 72e3346872ec9298f2b582e2859b1dbf1304f77e
Author: Manuel Traut <manut@mecka.net>
Date:   Tue Mar 22 23:49:58 2011 +0100

    HELLO world
    
    ..not huhu world
    
    Signed-off-by: Manuel Traut <manut@mecka.net>

...
\end{lstlisting}

Further exercises:

\begin{itemize}
\item pull from another repository
\item track a repository
\item what is a bare repository?
\item find bugs with git bisect
\item extract a patch queque from your git branch
\item send mails with git
\item build a svn - git bridge
\end{itemize}

\input{tailhandout}