-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstash-push.js
129 lines (113 loc) · 4.56 KB
/
stash-push.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// use isomorphic-git API to clone a github repository
import * as isogit from 'isomorphic-git';
import fs from 'fs';
import { getTreeObjArrayStage, writeStashReflog, getTimezoneOffset, getTreeObjArrayforWorkingDir } from './git-util.js';
const dir = './sandbox';
async function stash_push() {
try {
// Get the current branch name
const branch = await isogit.currentBranch({
fs,
dir,
fullname: false
});
// prepare the stansh commit: first parent is the current branch HEAD
const headCommit = await isogit.resolveRef({
fs,
dir,
ref: 'HEAD' });
const stashCommitParents = [headCommit];
let stashCommitTree = null;
let workDirCompareBase = isogit.TREE({ ref: 'HEAD'});
const timestamp = Math.floor(Date.now() / 1000); // UTC Unix timestamp in seconds
const author = { name: 'stash', email: '[email protected]', timestamp, timezoneOffset: getTimezoneOffset() };
//try to create a tree from the current index if any changes staged
const indexTreeObj = await getTreeObjArrayStage(dir);
if (indexTreeObj.length > 0) {
console.info('indexTreeObj:', indexTreeObj);
// this indexTree will be the tree of the stash commit
const indexTree = await isogit.writeTree({
fs,
dir,
tree: indexTreeObj });
// create a commit from the index tree, which has one parent, the current branch HEAD
const stashCommitOne = await isogit.writeCommit({
fs,
dir,
commit: {
message: `stash-Index: WIP on ${branch} - ${new Date().toISOString()}`,
tree: indexTree, // stashCommitTree
parent: stashCommitParents,
author,
committer: author
}
});
stashCommitParents.push(stashCommitOne);
stashCommitTree = indexTree;
workDirCompareBase = isogit.STAGE();
}
// create a tree from the current working directory
const workingTreeObjects = await getTreeObjArrayforWorkingDir(dir, workDirCompareBase);
if (workingTreeObjects.length> 0) {
console.info('workingTreeObjects:', workingTreeObjects);
const workingTree = await isogit.writeTree({
fs,
dir,
tree: workingTreeObjects });
// create a commit from the working directory tree, which has one parent, the one we just had
const workingHeadCommit = await isogit.writeCommit({
fs,
dir,
commit: {
message: `stash-WorkDir: WIP on ${branch} - ${new Date().toISOString()}`,
tree: workingTree,
parent: [stashCommitParents[stashCommitParents.length-1]],
author,
committer: author
}
});
stashCommitParents.push(workingHeadCommit);
stashCommitTree = workingTree;
}
if (stashCommitTree === null) {
console.info('No changes to stash');
return;
}
//create another commit from the tree, which has three parents: HEAD and the commit we just made:
const stashCommit = await isogit.writeCommit({
fs,
dir,
commit: {
message: `stash: WIP on ${branch} - ${new Date().toISOString()}`,
tree: stashCommitTree,
parent: stashCommitParents,
author,
committer: author
}
});
// next, write this commit into .git/refs/stash:
await isogit.writeRef({
fs,
dir,
ref: 'refs/stash',
value: stashCommit,
force: true
});
// write the stash commit to the logs
await writeStashReflog(dir, stashCommit, `WIP on ${branch}: ${new Date().toISOString()}`);
// finally, go back to a clean working directory
await isogit.checkout({
fs,
dir,
ref: branch,
force: true // Force checkout to discard changes
});
} catch (e) {
console.error(e);
}
}
stash_push();
// const workingTreeObjects = await getTreeObjArrayforWorkingDir(dir);
// console.info('workingTreeObjects:', workingTreeObjects);
// const indexTreeObj = await getTreeObjArrayStage(dir);
// console.info('indexTreeObj:', indexTreeObj);