<commit_before>'use strict'

const nfs = require('./nice-fs')
const object = require('./object')
const index = require('./index')
const tree = require('./tree')
const commit = require('./commit')
const utils = require('./utils')
const remote = require('./remote')


const headFile = '.ndr/HEAD'

Error.stackTraceLimit = Infinity

function init () {
	nfs.mkdir('.ndr')

	nfs.mkdir('.ndr/objects')
	nfs.mkdir('.ndr/refs')

	nfs.writeJson(headFile, { type: 'ref', ref: 'refs/master' })
}

function add (file) {
	const contents = nfs.read(file)
	const hash = object.storeBlob(contents)
	index.add(file, hash)
}

function catFile (type, hash) {
	console.log({
		blob: object.retrieveBlob,
		tree: object.retrieveTree,
		commit: object.retrieveCommit,
	}[type](hash))
}

function commitInitial (message) {
	const indexValue = index.retrieve()
	const treeHash = tree.store(indexValue)

	const commitHash = commit.store(message, treeHash, null)

	const head = nfs.readJson(headFile)

	nfs.write(`.ndr/${head.ref}`, commitHash)
}

function getHeadHash (head) {
	return head.type === 'ref'
		? nfs.read(`.ndr/${head.ref}`)
		: head.hash
}

function commitNoninitial (message) {
	const indexValue = index.retrieve()

	const candidateTreeHash = tree.store(indexValue)

	const head = nfs.readJson(headFile)

	const commitHash = getHeadHash(head)

	const commitObject = commit.retrieve(commitHash)

	const currentTreeHash = commitObject.tree

	if (candidateTreeHash === currentTreeHash) {
		console.log('nothing to commit')
		return
	}

	const newCommitHash = commit.store(message, candidateTreeHash, commitHash)

	if (head.type === 'ref') {
		nfs.write(`.ndr/${head.ref}`, newCommitHash)
	} else {
		nfs.writeJson(headFile, { type: 'hash', hash: newCommitHash })
	}
}

function commitCommand (message) {
	const head = nfs.readJson(headFile)

	if (head.type === 'ref' && !nfs.exists(`.ndr/${head.ref}`)) {
		commitInitial(message)
	} else {
		commitNoninitial(message)
	}
}

function log () {
	const head = nfs.readJson(headFile)

	if (head.type === 'ref' && !nfs.exists(`.ndr/${head.ref}`)) {
		console.log('your branch does not have any commits yet')
		return
	}

	function traverse (commitHash) {
		const commitObject = commit.retrieve(commitHash)

		console.log(
			commitHash.slice(0, 7),
			commitHash,
			commitObject.message
		)

		if (commitObject.parent) {
			traverse(commitObject.parent)
		}
	}

	const commitHash = getHeadHash(head)

	traverse(commitHash)
}

function branch (name) {
	const head = nfs.readJson(headFile)

	const commitHash = getHeadHash(head)

	nfs.write(`.ndr/refs/${name}`, commitHash)
}

function branchList () {
	const head = nfs.readJson(headFile)
	const headBranch = head.type !== 'ref'
		? null
		: head.ref.match(/\/(\w+)$/)[1]

	const branches = nfs.readdir(`.ndr/refs`)

	branches.forEach((branch) => {
		console.log(headBranch === branch ? '*' : ' ', branch)
	})
}

function checkout (hashOrBranch) {
	const isHash = /[\da-f]{7}/.test(hashOrBranch)

	const commitHash = isHash
		? hashOrBranch
		: nfs.read(`.ndr/refs/${hashOrBranch}`)

	const commitObject = commit.retrieve(commitHash)

	tree.retrieve(commitObject.tree)

	index.updateFromTree(commitObject.tree)

	if (isHash) {
		nfs.writeJson(headFile, { type: 'hash', hash: commitHash })
	} else {
		nfs.writeJson(headFile, { type: 'ref', ref: `refs/${hashOrBranch}` })
	}
}
<commit_msg>Use path.basename instead of regex<commit_after>'use strict'

const { basename } = require('path')

const nfs = require('./nice-fs')
const object = require('./object')
const index = require('./index')
const tree = require('./tree')
const commit = require('./commit')
const utils = require('./utils')
const remote = require('./remote')


const headFile = '.ndr/HEAD'

Error.stackTraceLimit = Infinity

function init () {
	nfs.mkdir('.ndr')

	nfs.mkdir('.ndr/objects')
	nfs.mkdir('.ndr/refs')

	nfs.writeJson(headFile, { type: 'ref', ref: 'refs/master' })
}

function add (file) {
	const contents = nfs.read(file)
	const hash = object.storeBlob(contents)
	index.add(file, hash)
}

function catFile (type, hash) {
	console.log({
		blob: object.retrieveBlob,
		tree: object.retrieveTree,
		commit: object.retrieveCommit,
	}[type](hash))
}

function commitInitial (message) {
	const indexValue = index.retrieve()
	const treeHash = tree.store(indexValue)

	const commitHash = commit.store(message, treeHash, null)

	const head = nfs.readJson(headFile)

	nfs.write(`.ndr/${head.ref}`, commitHash)
}

function getHeadHash (head) {
	return head.type === 'ref'
		? nfs.read(`.ndr/${head.ref}`)
		: head.hash
}

function commitNoninitial (message) {
	const indexValue = index.retrieve()

	const candidateTreeHash = tree.store(indexValue)

	const head = nfs.readJson(headFile)

	const commitHash = getHeadHash(head)

	const commitObject = commit.retrieve(commitHash)

	const currentTreeHash = commitObject.tree

	if (candidateTreeHash === currentTreeHash) {
		console.log('nothing to commit')
		return
	}

	const newCommitHash = commit.store(message, candidateTreeHash, commitHash)

	if (head.type === 'ref') {
		nfs.write(`.ndr/${head.ref}`, newCommitHash)
	} else {
		nfs.writeJson(headFile, { type: 'hash', hash: newCommitHash })
	}
}

function commitCommand (message) {
	const head = nfs.readJson(headFile)

	if (head.type === 'ref' && !nfs.exists(`.ndr/${head.ref}`)) {
		commitInitial(message)
	} else {
		commitNoninitial(message)
	}
}

function log () {
	const head = nfs.readJson(headFile)

	if (head.type === 'ref' && !nfs.exists(`.ndr/${head.ref}`)) {
		console.log('your branch does not have any commits yet')
		return
	}

	function traverse (commitHash) {
		const commitObject = commit.retrieve(commitHash)

		console.log(
			commitHash.slice(0, 7),
			commitHash,
			commitObject.message
		)

		if (commitObject.parent) {
			traverse(commitObject.parent)
		}
	}

	const commitHash = getHeadHash(head)

	traverse(commitHash)
}

function branch (name) {
	const head = nfs.readJson(headFile)

	const commitHash = getHeadHash(head)

	nfs.write(`.ndr/refs/${name}`, commitHash)
}

function branchList () {
	const head = nfs.readJson(headFile)
	const headBranch = head.type !== 'ref'
		? null
		: basename(head.ref)

	const branches = nfs.readdir(`.ndr/refs`)

	branches.forEach((branch) => {
		console.log(headBranch === branch ? '*' : ' ', branch)
	})
}

function checkout (hashOrBranch) {
	const isHash = /[\da-f]{7}/.test(hashOrBranch)

	const commitHash = isHash
		? hashOrBranch
		: nfs.read(`.ndr/refs/${hashOrBranch}`)

	const commitObject = commit.retrieve(commitHash)

	tree.retrieve(commitObject.tree)

	index.updateFromTree(commitObject.tree)

	if (isHash) {
		nfs.writeJson(headFile, { type: 'hash', hash: commitHash })
	} else {
		nfs.writeJson(headFile, { type: 'ref', ref: `refs/${hashOrBranch}` })
	}
}

{'fraction_non_alphanumeric': 0.15949292452830188, 'fraction_numerical': 0.0010318396226415094, 'mean_word_length': 4.3806502775574945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6044250', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2400, 'n_words': 485}