@mieweb/wikigdrive
Version:
Google Drive to MarkDown synchronization
57 lines (56 loc) • 2 kB
JavaScript
import { sanitize } from './GitExecuter.js';
export class GitStash {
constructor(gitScanner) {
Object.defineProperty(this, "gitScanner", {
enumerable: true,
configurable: true,
writable: true,
value: gitScanner
});
}
get debug() {
return this.gitScanner.debug;
}
async getCount() {
const result = await this.gitScanner.executer.exec('git stash list --format=%gd', { skipLogger: !this.debug });
const lines = result.stdout.trim().split('\n').filter(line => line.length > 0);
return lines.length;
}
async stash(message) {
const beforeCount = await this.getCount();
try {
await this.gitScanner.executer.exec(`git stash push -u -m "${sanitize(message)}"`, {
skipLogger: !this.debug
});
}
catch (err) {
if (err.message && err.message.includes('No local changes to save')) {
return false;
}
throw err;
}
const afterCount = await this.getCount();
return afterCount > beforeCount;
}
async pop() {
try {
await this.gitScanner.executer.exec('git stash pop', { skipLogger: !this.debug });
}
catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (message.includes('No stash entries found')) {
return;
}
if (message.includes('CONFLICT')) {
throw new Error('Stash pop encountered merge conflicts. Please resolve conflicts manually.');
}
throw err;
}
}
async drop(stashNo = 0) {
await this.gitScanner.executer.exec(`git stash drop stash@{${stashNo}`, { skipLogger: !this.debug });
}
async apply(stashNo = 0) {
await this.gitScanner.executer.exec(`git stash apply stash@{${stashNo}`, { skipLogger: !this.debug });
}
}