isomorphic-git: Quick Start

node v10.24.1
version: 1.0.1
endpointsharetweet
Here's a whirlwind tour of the main features of `isomorphic-git`. First, let's make a temporary directory we can work in:
const fs = require('fs'); const os = require('os'); const path = require('path'); // for some reason, runkit isn't detecting this dependency // correctly so I'm manually requiring it here. require('ignore'); require('wcwidth'); const git = require("isomorphic-git"); // Make temporary directory const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-')); console.log(dir); // Behold - it is empty! fs.readdirSync(dir);
Now that we've got an empty directory, let's clone a git repository. I'm cloning `isomorphic-git` itself (how meta!). I found that runkit will kill long-running operations, so I'm only cloning a single branch and only to a depth of 10 commits so that it won't time out.
await git.clone({ fs, dir, url: 'https://github.com/isomorphic-git/isomorphic-git', ref: 'master', singleBranch: true, depth: 10 }); // Now it should not be empty... fs.readdirSync(dir);
Great! We've got files. We've also got commits. Let's see what the recent history of this branch looks like. Hint: be sure to expand the objects so you can see all the properties.
await git.log({fs, dir})
Git is used to track files. Let's see what kind of file things we can do! git.status is a major one. That let's us compare the working directory file to the current branch.
await git.status({fs, dir, filepath: 'README.md'})
OK so the status is "unmodified" because we haven't modified it. What if we change the file by writing over it?
fs.writeFileSync(path.join(dir, 'README.md'), 'Very short README', 'utf8') await git.status({fs, dir, filepath: 'README.md'})
The status is "*modified" with a star. Text editors sometimes use a "*" in the title bar to indicate a file has unsaved changes. That's what is going on here - we've made changes to the file but we haven't added those changes to the git "staging area".
await git.add({fs, dir, filepath: 'README.md'}) await git.status({fs, dir, filepath: 'README.md'})
Now that we've done "git add" that little star has gone away and the status is just "modified". What if we write a new file?
fs.writeFileSync(path.join(dir, 'newfile.txt'), 'Hello World', 'utf8') await git.status({fs, dir, filepath: 'newfile.txt'})
"*added" means the file has been added, but not staged. Simple to fix:
await git.add({fs, dir, filepath: 'newfile.txt'}) await git.status({fs, dir, filepath: 'newfile.txt'})
The third and final trick: deleting a file:
fs.unlinkSync(path.join(dir, 'package.json')) await git.status({fs, dir, filepath: 'package.json'})
This last bit has always been unintuitive to me... but you need to tell git you deleted the file.
await git.remove({fs, dir, filepath: 'package.json'}) await git.status({fs, dir, filepath: 'package.json'})
What happens if you tell git you deleted a file but you really didn't?
await git.remove({fs, dir, filepath: 'package-lock.json'}) await git.status({fs, dir, filepath: 'package-lock.json'})
Does that make sense? No? Sorry, naming things is hard. (Git doesn't do a great job of it either. It reports the file as "untracked" and "deleted" at the same time.) OK, enough messing around.
await git.add({fs, dir, filepath: 'package-lock.json'}) await git.status({fs, dir, filepath: 'package-lock.json'})
Cool. So we've deleted package.json and replaced the README with the text "Very short README". A solid day's work - let's commit those changes.
await git.commit({ fs, dir, message: 'Delete package.json and overwrite README.', author: { name: 'Mr. Test', email: 'mrtest@example.com' } })
git.commit returns the shasum of our new commit. Let's examine our handiwork:
commits = await git.log({fs, dir, depth: 1}) commits[0]
Loading…

no comments

    sign in to comment