|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>JSDoc: Source: commands/catFile.js</title> |
| 6 | + |
| 7 | + <script src="scripts/prettify/prettify.js"> </script> |
| 8 | + <script src="scripts/prettify/lang-css.js"> </script> |
| 9 | + <!--[if lt IE 9]> |
| 10 | + <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> |
| 11 | + <![endif]--> |
| 12 | + <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> |
| 13 | + <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> |
| 14 | +</head> |
| 15 | + |
| 16 | +<body> |
| 17 | + |
| 18 | +<div id="main"> |
| 19 | + |
| 20 | + <h1 class="page-title">Source: commands/catFile.js</h1> |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + <section> |
| 28 | + <article> |
| 29 | + <pre class="prettyprint source linenums"><code>const fs = require('fs'); |
| 30 | +const zlib = require('zlib'); |
| 31 | +const { getObjectPath } = require('@utils/path'); |
| 32 | + |
| 33 | +/** |
| 34 | + * Git 객체 내용을 출력합니다 (blob/tree/commit 지원). |
| 35 | + * @param {string} hash SHA-1 해시 |
| 36 | + * @param {string} gitDir .git 디렉토리 경로 |
| 37 | + */ |
| 38 | +function catFile(hash, gitDir) { |
| 39 | + const { objectPath } = getObjectPath(gitDir, hash); |
| 40 | + |
| 41 | + if (!fs.existsSync(objectPath)) { |
| 42 | + console.error(`fatal: 객체를 찾을 수 없습니다: ${hash}`); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const content = decompress(fs.readFileSync(objectPath)); |
| 47 | + const { type, body } = parseGitObject(content); |
| 48 | + |
| 49 | + const handler = GitObjectPrinter[type]; |
| 50 | + if (!handler) { |
| 51 | + console.error(`fatal: 알 수 없는 객체 타입: '${type}'`); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + handler(body); |
| 56 | +} |
| 57 | + |
| 58 | +function decompress(buffer) { |
| 59 | + return zlib.inflateSync(buffer); |
| 60 | +} |
| 61 | + |
| 62 | +function parseGitObject(buffer) { |
| 63 | + const nullIndex = buffer.indexOf(0); |
| 64 | + const header = buffer.slice(0, nullIndex).toString('utf-8'); |
| 65 | + const type = header.split(' ')[0]; |
| 66 | + const body = buffer.slice(nullIndex + 1); |
| 67 | + return { type, body }; |
| 68 | +} |
| 69 | + |
| 70 | +const GitObjectPrinter = { |
| 71 | + blob: (body) => console.log(body.toString('utf-8')), |
| 72 | + commit: (body) => console.log(body.toString('utf-8')), |
| 73 | + tree: (body) => { |
| 74 | + let i = 0; |
| 75 | + while (i < body.length) { |
| 76 | + const modeEnd = body.indexOf(32, i); |
| 77 | + const mode = body.slice(i, modeEnd).toString(); |
| 78 | + i = modeEnd + 1; |
| 79 | + |
| 80 | + const nameEnd = body.indexOf(0, i); |
| 81 | + const name = body.slice(i, nameEnd).toString(); |
| 82 | + i = nameEnd + 1; |
| 83 | + |
| 84 | + const hash = body.slice(i, i + 20).toString('hex'); |
| 85 | + i += 20; |
| 86 | + |
| 87 | + console.log(`${mode} ${name} ${hash}`); |
| 88 | + } |
| 89 | + }, |
| 90 | +}; |
| 91 | + |
| 92 | +module.exports = catFile; |
| 93 | +</code></pre> |
| 94 | + </article> |
| 95 | + </section> |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +</div> |
| 101 | + |
| 102 | +<nav> |
| 103 | + <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#catFile">catFile</a></li><li><a href="global.html#formatGitDate">formatGitDate</a></li><li><a href="global.html#getCurrentCommitHash">getCurrentCommitHash</a></li><li><a href="global.html#parseCommitObject">parseCommitObject</a></li><li><a href="global.html#readHead">readHead</a></li><li><a href="global.html#readObject">readObject</a></li><li><a href="global.html#writeGitObject">writeGitObject</a></li></ul> |
| 104 | +</nav> |
| 105 | + |
| 106 | +<br class="clear"> |
| 107 | + |
| 108 | +<footer> |
| 109 | + Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Sun Jun 01 2025 11:31:26 GMT+0900 (대한민국 표준시) |
| 110 | +</footer> |
| 111 | + |
| 112 | +<script> prettyPrint(); </script> |
| 113 | +<script src="scripts/linenumber.js"> </script> |
| 114 | +</body> |
| 115 | +</html> |
0 commit comments