Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated commit.ts using typesafety,type declaration security enhancement. #302

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Expand Up @@ -7,3 +7,4 @@
- Sean Mitchell https://github.com/dskvr
- Nicola Dardanis <https://github.com/nicdard>
- KOGA Mitsuhiro <https://github.com/shiena>
- Akshat Sharma <https://github.com/Akshat111111>
42 changes: 11 additions & 31 deletions src/commit.ts
Expand Up @@ -6,7 +6,7 @@ import { jsonCompliant } from './common';
/**
* A class that represents the commits of a repository.
* It contains a variety of information like the creation date,
* a human readable message string, that describes the changes, etc.
* a human-readable message string, that describes the changes, etc.
*
* Each commit is distinguishable by its commit hash that can be obtained
* by [[Commit.hash]]
Expand All @@ -15,15 +15,13 @@ export class Commit {
/** Unique commit hash */
hash: string;

tags: string[];

/** Custom commit user data, that was added to [[Repository.createCommit]]. */
userData: any;
userData: Record<string, any> | undefined = {};

/** The repository this commit belongs to. */
repo: Repository;

/** Human readable message string. */
/** Human-readable message string. */
message: string;

/** Creation date of the commit. */
Expand All @@ -35,10 +33,11 @@ export class Commit {
/** Parent commit before this commit. */
parent: string[] | null;

tags: Set<string> = new Set<string>();

constructor(repo: Repository, message: string, creationDate: Date, root: TreeDir, parent: string[] | null) {
this.hash = crypto.createHash('sha256').update(process.hrtime().toString()).digest('hex');
this.tags = [];
this.userData = {};
const uniqueInputValue = `${repo.id}${creationDate.toISOString()}${message}`;
this.hash = crypto.createHash('sha256').update(uniqueInputValue).digest('hex');
this.repo = repo;
this.message = jsonCompliant(message);
this.date = creationDate;
Expand All @@ -50,24 +49,10 @@ export class Commit {
* Return a cloned commit object.
*/
clone(): Commit {
const commit = new Commit(
this.repo,
this.message,
this.date,
this.root,
this.parent ? [...this.parent] : [],
);
const commit = new Commit(this.repo, this.message, new Date(this.date), this.root, this.parent ? [...this.parent] : null);
commit.hash = this.hash;

if (this.tags.length > 0) {
commit.tags = [...this.tags];
}

commit.userData = {};
if (this.userData != null) {
commit.userData = { ...this.userData };
}

commit.tags = new Set(this.tags);
commit.userData = { ...this.userData };
return commit;
}

Expand All @@ -85,13 +70,8 @@ export class Commit {
if (tag.length === 0) {
return;
}

if (this.tags.includes(tag)) {
return;
}

tag = jsonCompliant(tag);
this.tags.push(tag);
this.tags.add(tag);
}

/**
Expand Down