diff: add an API for deferred freeing

Add a diff_free() function to free anything we may have allocated in
the "diff_options" struct, and the ability to make calling it a noop
by setting "no_free" in "diff_options".

This is required because when e.g. "git diff" is run we'll allocate
things in that struct, use the diff machinery once, and then exit.

But if we run e.g. "git log -p" we're going to re-use what we
allocated across multiple diff_flush() calls, and only want to free
things at the end.

We've thus ended up with features like the recently added "diff -I"[1]
where we'll leak memory. As it turns out it could have simply used the
pattern established in 6ea57703f6 (log: prepare log/log-tree to reuse
the diffopt.close_file attribute, 2016-06-22).

Manually adding more such flags to things log_tree_commit() every time
we need to allocate something would be tedious. Let's instead move
that fclose() code it to a new diff_free(), in anticipation of freeing
more things in that function in follow-up commits.

Some functions such as log_tree_commit() need an idiom of optionally
retaining a previous "no_free", as they may either free the memory
themselves, or their caller may do so. I'm keeping that idiom in
log_show_early() for good measure, even though I don't think it's
currently called in this manner. It also gets passed an existing
"struct rev_info", so future callers may want to set the "no_free"
flag.

This change is a bit hard to read because while the freeing pattern
we're introducing isn't unusual, the "file" member is a special
snowflake. We usually don't want to fclose() it. This is because
"file" is usually stdout, in which case we don't want to fclose()
it. We only want to opt-in to closing it when we e.g. open a file on
the filesystem. Thus the opt-in "close_file" flag.

So the API in general just needs a "no_free" flag to defer freeing,
but the "file" member still needs its "close_file" flag. This is made
more confusing because while refactoring this code we could replace
some "close_file=0" with "no_free=1", whereas others need to set both
flags.

This is because there were some cases where an existing "close_file=0"
meant "let's defer deallocation", and others where it meant "we don't
want to close this file handle at all".

1. 296d4a94e7 (diff: add -I<regex> that ignores matching changes,
   2020-10-20)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
4 files changed
tree: 6b5af494f056424f480f645f56b9facd5d59ac72
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. negotiator/
  14. perl/
  15. po/
  16. ppc/
  17. refs/
  18. sha1dc/
  19. sha256/
  20. t/
  21. templates/
  22. trace2/
  23. vcs-svn/
  24. xdiff/
  25. .cirrus.yml
  26. .clang-format
  27. .editorconfig
  28. .gitattributes
  29. .gitignore
  30. .gitmodules
  31. .mailmap
  32. .travis.yml
  33. .tsan-suppressions
  34. abspath.c
  35. aclocal.m4
  36. add-interactive.c
  37. add-interactive.h
  38. add-patch.c
  39. advice.c
  40. advice.h
  41. alias.c
  42. alias.h
  43. alloc.c
  44. alloc.h
  45. apply.c
  46. apply.h
  47. archive-tar.c
  48. archive-zip.c
  49. archive.c
  50. archive.h
  51. attr.c
  52. attr.h
  53. banned.h
  54. base85.c
  55. bisect.c
  56. bisect.h
  57. blame.c
  58. blame.h
  59. blob.c
  60. blob.h
  61. bloom.c
  62. bloom.h
  63. branch.c
  64. branch.h
  65. builtin.h
  66. bulk-checkin.c
  67. bulk-checkin.h
  68. bundle.c
  69. bundle.h
  70. cache-tree.c
  71. cache-tree.h
  72. cache.h
  73. chdir-notify.c
  74. chdir-notify.h
  75. check-builtins.sh
  76. check_bindir
  77. checkout.c
  78. checkout.h
  79. CODE_OF_CONDUCT.md
  80. color.c
  81. color.h
  82. column.c
  83. column.h
  84. combine-diff.c
  85. command-list.txt
  86. commit-graph.c
  87. commit-graph.h
  88. commit-reach.c
  89. commit-reach.h
  90. commit-slab-decl.h
  91. commit-slab-impl.h
  92. commit-slab.h
  93. commit.c
  94. commit.h
  95. common-main.c
  96. config.c
  97. config.h
  98. config.mak.dev
  99. config.mak.in
  100. config.mak.uname
  101. configure.ac
  102. connect.c
  103. connect.h
  104. connected.c
  105. connected.h
  106. convert.c
  107. convert.h
  108. copy.c
  109. COPYING
  110. credential.c
  111. credential.h
  112. csum-file.c
  113. csum-file.h
  114. ctype.c
  115. daemon.c
  116. date.c
  117. decorate.c
  118. decorate.h
  119. delta-islands.c
  120. delta-islands.h
  121. delta.h
  122. detect-compiler
  123. diff-delta.c
  124. diff-lib.c
  125. diff-merges.c
  126. diff-merges.h
  127. diff-no-index.c
  128. diff.c
  129. diff.h
  130. diffcore-break.c
  131. diffcore-delta.c
  132. diffcore-order.c
  133. diffcore-pickaxe.c
  134. diffcore-rename.c
  135. diffcore.h
  136. dir-iterator.c
  137. dir-iterator.h
  138. dir.c
  139. dir.h
  140. editor.c
  141. entry.c
  142. environment.c
  143. environment.h
  144. exec-cmd.c
  145. exec-cmd.h
  146. fetch-negotiator.c
  147. fetch-negotiator.h
  148. fetch-pack.c
  149. fetch-pack.h
  150. fmt-merge-msg.c
  151. fmt-merge-msg.h
  152. fsck.c
  153. fsck.h
  154. fsmonitor.c
  155. fsmonitor.h
  156. fuzz-commit-graph.c
  157. fuzz-pack-headers.c
  158. fuzz-pack-idx.c
  159. generate-cmdlist.sh
  160. generate-configlist.sh
  161. gettext.c
  162. gettext.h
  163. git-add--interactive.perl
  164. git-archimport.perl
  165. git-bisect.sh
  166. git-compat-util.h
  167. git-cvsexportcommit.perl
  168. git-cvsimport.perl
  169. git-cvsserver.perl
  170. git-difftool--helper.sh
  171. git-filter-branch.sh
  172. git-instaweb.sh
  173. git-merge-octopus.sh
  174. git-merge-one-file.sh
  175. git-merge-resolve.sh
  176. git-mergetool--lib.sh
  177. git-mergetool.sh
  178. git-p4.py
  179. git-quiltimport.sh
  180. git-rebase--preserve-merges.sh
  181. git-request-pull.sh
  182. git-send-email.perl
  183. git-sh-i18n.sh
  184. git-sh-setup.sh
  185. git-submodule.sh
  186. git-svn.perl
  187. GIT-VERSION-GEN
  188. git-web--browse.sh
  189. git.c
  190. git.rc
  191. gpg-interface.c
  192. gpg-interface.h
  193. graph.c
  194. graph.h
  195. grep.c
  196. grep.h
  197. hash-lookup.c
  198. hash-lookup.h
  199. hash.h
  200. hashmap.c
  201. hashmap.h
  202. help.c
  203. help.h
  204. hex.c
  205. http-backend.c
  206. http-fetch.c
  207. http-push.c
  208. http-walker.c
  209. http.c
  210. http.h
  211. ident.c
  212. imap-send.c
  213. INSTALL
  214. iterator.h
  215. json-writer.c
  216. json-writer.h
  217. khash.h
  218. kwset.c
  219. kwset.h
  220. levenshtein.c
  221. levenshtein.h
  222. LGPL-2.1
  223. line-log.c
  224. line-log.h
  225. line-range.c
  226. line-range.h
  227. linear-assignment.c
  228. linear-assignment.h
  229. list-objects-filter-options.c
  230. list-objects-filter-options.h
  231. list-objects-filter.c
  232. list-objects-filter.h
  233. list-objects.c
  234. list-objects.h
  235. list.h
  236. ll-merge.c
  237. ll-merge.h
  238. lockfile.c
  239. lockfile.h
  240. log-tree.c
  241. log-tree.h
  242. ls-refs.c
  243. ls-refs.h
  244. mailinfo.c
  245. mailinfo.h
  246. mailmap.c
  247. mailmap.h
  248. Makefile
  249. match-trees.c
  250. mem-pool.c
  251. mem-pool.h
  252. merge-blobs.c
  253. merge-blobs.h
  254. merge-ort-wrappers.c
  255. merge-ort-wrappers.h
  256. merge-ort.c
  257. merge-ort.h
  258. merge-recursive.c
  259. merge-recursive.h
  260. merge.c
  261. mergesort.c
  262. mergesort.h
  263. midx.c
  264. midx.h
  265. name-hash.c
  266. notes-cache.c
  267. notes-cache.h
  268. notes-merge.c
  269. notes-merge.h
  270. notes-utils.c
  271. notes-utils.h
  272. notes.c
  273. notes.h
  274. object-file.c
  275. object-name.c
  276. object-store.h
  277. object.c
  278. object.h
  279. oid-array.c
  280. oid-array.h
  281. oidmap.c
  282. oidmap.h
  283. oidset.c
  284. oidset.h
  285. pack-bitmap-write.c
  286. pack-bitmap.c
  287. pack-bitmap.h
  288. pack-check.c
  289. pack-objects.c
  290. pack-objects.h
  291. pack-revindex.c
  292. pack-revindex.h
  293. pack-write.c
  294. pack.h
  295. packfile.c
  296. packfile.h
  297. pager.c
  298. parse-options-cb.c
  299. parse-options.c
  300. parse-options.h
  301. patch-delta.c
  302. patch-ids.c
  303. patch-ids.h
  304. path.c
  305. path.h
  306. pathspec.c
  307. pathspec.h
  308. pkt-line.c
  309. pkt-line.h
  310. preload-index.c
  311. pretty.c
  312. pretty.h
  313. prio-queue.c
  314. prio-queue.h
  315. progress.c
  316. progress.h
  317. promisor-remote.c
  318. promisor-remote.h
  319. prompt.c
  320. prompt.h
  321. protocol.c
  322. protocol.h
  323. prune-packed.c
  324. prune-packed.h
  325. quote.c
  326. quote.h
  327. range-diff.c
  328. range-diff.h
  329. reachable.c
  330. reachable.h
  331. read-cache.c
  332. README.md
  333. rebase-interactive.c
  334. rebase-interactive.h
  335. rebase.c
  336. rebase.h
  337. ref-filter.c
  338. ref-filter.h
  339. reflog-walk.c
  340. reflog-walk.h
  341. refs.c
  342. refs.h
  343. refspec.c
  344. refspec.h
  345. remote-curl.c
  346. remote.c
  347. remote.h
  348. replace-object.c
  349. replace-object.h
  350. repo-settings.c
  351. repository.c
  352. repository.h
  353. rerere.c
  354. rerere.h
  355. reset.c
  356. reset.h
  357. resolve-undo.c
  358. resolve-undo.h
  359. revision.c
  360. revision.h
  361. run-command.c
  362. run-command.h
  363. send-pack.c
  364. send-pack.h
  365. sequencer.c
  366. sequencer.h
  367. serve.c
  368. serve.h
  369. server-info.c
  370. setup.c
  371. sh-i18n--envsubst.c
  372. sha1dc_git.c
  373. sha1dc_git.h
  374. shallow.c
  375. shallow.h
  376. shell.c
  377. shortlog.h
  378. sideband.c
  379. sideband.h
  380. sigchain.c
  381. sigchain.h
  382. split-index.c
  383. split-index.h
  384. stable-qsort.c
  385. strbuf.c
  386. strbuf.h
  387. streaming.c
  388. streaming.h
  389. string-list.c
  390. string-list.h
  391. strmap.c
  392. strmap.h
  393. strvec.c
  394. strvec.h
  395. sub-process.c
  396. sub-process.h
  397. submodule-config.c
  398. submodule-config.h
  399. submodule.c
  400. submodule.h
  401. symlinks.c
  402. tag.c
  403. tag.h
  404. tar.h
  405. tempfile.c
  406. tempfile.h
  407. thread-utils.c
  408. thread-utils.h
  409. tmp-objdir.c
  410. tmp-objdir.h
  411. trace.c
  412. trace.h
  413. trace2.c
  414. trace2.h
  415. trailer.c
  416. trailer.h
  417. transport-helper.c
  418. transport-internal.h
  419. transport.c
  420. transport.h
  421. tree-diff.c
  422. tree-walk.c
  423. tree-walk.h
  424. tree.c
  425. tree.h
  426. unicode-width.h
  427. unimplemented.sh
  428. unix-socket.c
  429. unix-socket.h
  430. unpack-trees.c
  431. unpack-trees.h
  432. upload-pack.c
  433. upload-pack.h
  434. url.c
  435. url.h
  436. urlmatch.c
  437. urlmatch.h
  438. usage.c
  439. userdiff.c
  440. userdiff.h
  441. utf8.c
  442. utf8.h
  443. varint.c
  444. varint.h
  445. version.c
  446. version.h
  447. versioncmp.c
  448. walker.c
  449. walker.h
  450. wildmatch.c
  451. wildmatch.h
  452. worktree.c
  453. worktree.h
  454. wrap-for-bin.sh
  455. wrapper.c
  456. write-or-die.c
  457. ws.c
  458. wt-status.c
  459. wt-status.h
  460. xdiff-interface.c
  461. xdiff-interface.h
  462. zlib.c
README.md

Build status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-<commandname>.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission). To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org. The mailing list archives are available at https://lore.kernel.org/git/, http://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks