writeObject
This command is overly complicated.
If you know the type of object you are writing, use
writeBlob
,writeCommit
,writeTag
, orwriteTree
.
Write a git object directly
param | type [= default] | description |
---|---|---|
fs | FsClient | a file system client |
dir | string | The working tree directory path |
gitdir | string = join(dir,'.git') | The git directory path |
object | string | Uint8Array | CommitObject | TreeObject | TagObject | The object to write. |
type | 'blob' | 'tree' | 'commit' | 'tag' | The kind of object to write. |
format | 'deflated' | 'wrapped' | 'content' | 'parsed' = 'parsed' | What format the object is in. The possible choices are listed below. |
oid | string | If format is 'deflated' then this param is required. Otherwise it is calculated. |
encoding | string | If type is 'blob' then object will be converted to a Uint8Array using encoding . |
return | Promise<string> | Resolves successfully with the SHA-1 object id of the newly written object. |
format
can have the following values:
param | description |
---|---|
'deflated' | Treat object as the raw deflate-compressed buffer for an object, meaning can be written to .git/objects/** as-is. |
'wrapped' | Treat object as the inflated object buffer wrapped in the git object header. This is the raw buffer used when calculating the SHA-1 object id of a git object. |
'content' | Treat object as the object buffer without the git header. |
'parsed' | Treat object as a parsed representation of the object. |
If format
is 'parsed'
, then object
must match one of the schemas for CommitObject
, TreeObject
, TagObject
, or a string
(for blobs).
A git commit object.
type CommitObject = {
message: string; // Commit message
tree: string; // SHA-1 object id of corresponding file tree
parent: Array<string>; // an array of zero or more SHA-1 object ids
author: {
name: string; // The author's name
email: string; // The author's email
timestamp: number; // UTC Unix timestamp in seconds
timezoneOffset: number; // Timezone difference from UTC in minutes
};
committer: {
name: string; // The committer's name
email: string; // The committer's email
timestamp: number; // UTC Unix timestamp in seconds
timezoneOffset: number; // Timezone difference from UTC in minutes
};
gpgsig?: string; // PGP signature (if present)
}
A git tree object. Trees represent a directory snapshot.
type TreeObject = Array<TreeEntry>;
A git annotated tag object.
type TagObject = {
object: string; // SHA-1 object id of object being tagged
type: 'blob' | 'tree' | 'commit' | 'tag'; // the type of the object being tagged
tag: string; // the tag name
tagger: {
name: string; // the tagger's name
email: string; // the tagger's email
timestamp: number; // UTC Unix timestamp in seconds
timezoneOffset: number; // timezone difference from UTC in minutes
};
message: string; // tag message
gpgsig?: string; // PGP signature (if present)
}
If format
is 'content'
, 'wrapped'
, or 'deflated'
, object
should be a Uint8Array
.
Example Code:
// Manually create an annotated tag.
let sha = await git.resolveRef({ fs, dir: '/tutorial', ref: 'HEAD' })
console.log('commit', sha)
let oid = await git.writeObject({
fs,
dir: '/tutorial',
type: 'tag',
object: {
object: sha,
type: 'commit',
tag: 'my-tag',
tagger: {
name: 'your name',
email: 'email@example.com',
timestamp: Math.floor(Date.now()/1000),
timezoneOffset: new Date().getTimezoneOffset()
},
message: 'Optional message'
}
})
console.log('tag', oid)
Tip: If you need a clean slate, expand and run this snippet to clean up the file system.
window.fs = new LightningFS('fs', { wipe: true })
window.pfs = window.fs.promises
console.log('done')