101-103-modify.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. .. _modify:
  2. Modify content
  3. --------------
  4. So far, we've only added new content to the dataset. And we have not done
  5. much to that content up to this point, to be honest. Let's see what happens if
  6. we add content, and then modify it.
  7. For this, in the root of ``DataLad-101``, create a plain text file
  8. called ``notes.txt``. It will contain all of the notes that you take
  9. throughout the course.
  10. Let's write a short summary of how to create a DataLad dataset from scratch:
  11. "One can create a new dataset with 'datalad create
  12. [--description] PATH'. The dataset is created empty".
  13. This is meant to be a note you would take in an educational course.
  14. You can take this note and write it to a file with an editor of your choice.
  15. The code snippet, however, contains this note within the start and end part of a
  16. `heredoc <https://en.wikipedia.org/wiki/Here_document>`_.
  17. You can also copy the full code snippet, starting
  18. from ``cat << EOT > notes.txt``, including the ``EOT`` in the last line, in your
  19. terminal to write this note from the terminal (without any editor) into ``notes.txt``.
  20. .. index:: here-document, heredoc
  21. .. find-out-more:: How does a heredoc (here-document) work?
  22. The code snippet makes sure to write lines of text into a
  23. file (that so far does not exist) called ``notes.txt``.
  24. To do this, the content of the "document" is wrapped in between
  25. *delimiting identifiers*. Here, these identifiers are *EOT* (short
  26. for "end of text"), but naming is arbitrary as long as the two identifiers
  27. are identical. The first "EOT" identifies the start of the text stream, and
  28. the second "EOT" terminates the text stream.
  29. The characters ``<<`` redirect the text stream into
  30. `"standard input" (stdin) <https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)>`_,
  31. the standard location that provides the *input* for a command.
  32. Thus, the text stream becomes the input for the
  33. `cat command <https://en.wikipedia.org/wiki/Cat_(Unix)>`_, which takes
  34. the input and writes it to
  35. `"standard output" (stdout) <https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)>`_.
  36. Lastly, the ``>`` character takes ``stdout`` can creates a new file
  37. ``notes.txt`` with ``stdout`` as its contents.
  38. It might seem like a slightly convoluted way to create a text file with
  39. a note in it. But it allows to write notes from the terminal, enabling
  40. this book to create commands you can execute with nothing other than your terminal.
  41. You are free to copy-paste the snippets with the heredocs,
  42. or find a workflow that suites you better. The only thing important is that
  43. you create and modify a ``.txt`` file over the course of the Basics part of this
  44. handbook.
  45. Running this command will create ``notes.txt`` in the
  46. root of your ``DataLad-101`` dataset:
  47. .. index:: heredoc
  48. pair: heredoc; on Windows in a terminal
  49. .. windows-wit:: Heredocs don't work under non-Git-Bash Windows terminals
  50. .. include:: topic/heredoc-windows.rst
  51. .. index::
  52. pair: create heredoc; in a terminal
  53. .. runrecord:: _examples/DL-101-103-101
  54. :language: console
  55. :workdir: dl-101/DataLad-101
  56. :cast: 01_dataset_basics
  57. :notes: Let's find out how we can modify files in dataset. Lets create a text file with notes about the DataLad commands we learned. (maybe explain here docs)
  58. $ cat << EOT > notes.txt
  59. One can create a new dataset with 'datalad create [--description] PATH'.
  60. The dataset is created empty
  61. EOT
  62. .. index::
  63. pair: check dataset for modification; with DataLad
  64. Run :dlcmd:`status` to confirm that there is a new, untracked file:
  65. .. runrecord:: _examples/DL-101-103-102
  66. :language: console
  67. :workdir: dl-101/DataLad-101
  68. :cast: 01_dataset_basics
  69. :notes: As expected, there is a new file in the dataset. At first the file is untracked. We can save without a path specification because it is the only existing modification
  70. $ datalad status
  71. .. index::
  72. pair: save dataset modification; with DataLad
  73. Save the current state of this file in your dataset's history. Because it is the only modification
  74. in the dataset, there is no need to specify a path.
  75. .. runrecord:: _examples/DL-101-103-103
  76. :language: console
  77. :workdir: dl-101/DataLad-101
  78. :cast: 01_dataset_basics
  79. $ datalad save -m "Add notes on datalad create"
  80. But now, let's see how *changing* tracked content works.
  81. Modify this file by adding another note. After all, you already know how to use
  82. :dlcmd:`save`, so write a short summary on that as well.
  83. Again, the example uses Unix commands (``cat`` and redirection, this time however
  84. with ``>>`` to *append* new content to the existing file)
  85. to accomplish this, but you can take any editor of your choice.
  86. .. runrecord:: _examples/DL-101-103-104
  87. :language: console
  88. :workdir: dl-101/DataLad-101
  89. :cast: 01_dataset_basics
  90. :notes: Now let's start to modify this text file by adding more notes to it. Think about this being a code file that you add functions to:
  91. $ cat << EOT >> notes.txt
  92. The command "datalad save [-m] PATH" saves the file (modifications) to
  93. history.
  94. Note to self: Always use informative, concise commit messages.
  95. EOT
  96. Let's check the dataset's current state:
  97. .. runrecord:: _examples/DL-101-103-105
  98. :language: console
  99. :workdir: dl-101/DataLad-101
  100. :cast: 01_dataset_basics
  101. $ datalad status
  102. and save the file in DataLad:
  103. .. runrecord:: _examples/DL-101-103-106
  104. :language: console
  105. :workdir: dl-101/DataLad-101
  106. :cast: 01_dataset_basics
  107. :notes: The modification can be saved as well
  108. $ datalad save -m "add note on datalad save"
  109. Let's take another look into our history to see the development of this file.
  110. We are using :gitcmd:`log -p -n 2` to see last two commits and explore
  111. the difference to the previous state of a file within each commit.
  112. .. runrecord:: _examples/DL-101-103-107
  113. :language: console
  114. :workdir: dl-101/DataLad-101
  115. :lines: 1-28
  116. :emphasize-lines: 6, 25
  117. :cast: 01_dataset_basics
  118. :notes: An the history gives an accurate record of what happened to this file
  119. $ git log -p -n 2
  120. We can see that the history can not only show us the commit message attached to
  121. a commit, but also the precise change that occurred in the text file in the commit.
  122. Additions are marked with a ``+``, and deletions would be shown with a leading ``-``.
  123. From the dataset's history, we can therefore also find out *how* the text file
  124. evolved over time. That's quite neat, isn't it?
  125. .. index::
  126. pair: log; Git command
  127. pair: get help; with Git
  128. pair: filter history; with Git
  129. .. find-out-more:: 'git log' has many more useful options
  130. ``git log``, as many other ``Git`` commands, has a good number of options
  131. which you can discover if you run ``git log --help``. Those options could
  132. help to find specific changes (e.g., which added or removed a specific word
  133. with ``-S``), or change how ``git log`` output will look (e.g.,
  134. ``--word-diff`` to highlight individual word changes).
  135. .. only:: adminmode
  136. Add a tag at the section end.
  137. .. runrecord:: _examples/DL-101-103-108
  138. :language: console
  139. :workdir: dl-101/DataLad-101
  140. $ git branch sct_modify_content