-
Notifications
You must be signed in to change notification settings - Fork 50
/
diff_util.js
166 lines (155 loc) · 6.58 KB
/
diff_util.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2017, Two Sigma Open Source
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of git-meta nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
const assert = require("chai").assert;
const co = require("co");
const NodeGit = require("nodegit");
const RepoStatus = require("./repo_status");
const SubmoduleConfigUtil = require("./submodule_config_util");
const DELTA = NodeGit.Diff.DELTA;
/**
* Return the `RepoStatus.FILESTATUS` value that corresponds to the specified
* flag. The behavior is undefined unless `flag` represents one of the types
* convertible to `FILESTATUS`.
*
* @param {NodeGit.Diff.DELTA} flag
* @return {RepoStatus.FILESTATUS}
*/
exports.convertDeltaFlag = function (flag) {
const FILESTATUS = RepoStatus.FILESTATUS;
switch (flag) {
case DELTA.MODIFIED: return FILESTATUS.MODIFIED;
case DELTA.ADDED: return FILESTATUS.ADDED;
case DELTA.DELETED: return FILESTATUS.REMOVED;
case DELTA.RENAMED: return FILESTATUS.RENAMED;
case DELTA.TYPECHANGE: return FILESTATUS.TYPECHANGED;
// Status changes in `RepoStatus` objects are separated into `staged`
// and `workdir` maps. Files that are "added" in the workdir are
// implicitly untracked.
case DELTA.UNTRACKED: return FILESTATUS.ADDED;
}
assert(false, `Unrecognized DELTA type: ${flag}.`);
};
function readDiff(diff) {
const result = {};
const FILESTATUS = RepoStatus.FILESTATUS;
const numDeltas = diff.numDeltas();
for (let i = 0; i < numDeltas; ++i) {
const delta = diff.getDelta(i);
const diffStatus = delta.status();
if (DELTA.CONFLICTED === diffStatus) {
continue; // CONTINUE
}
const fileStatus = exports.convertDeltaFlag(diffStatus);
const file = FILESTATUS.REMOVED === fileStatus ?
delta.oldFile() :
delta.newFile();
const path = file.path();
// Skip the .gitmodules file and all submodule changes; they're handled
// separately.
if (SubmoduleConfigUtil.modulesFileName !== path &&
NodeGit.TreeEntry.FILEMODE.COMMIT !== file.mode()) {
result[path] = fileStatus;
}
}
return result;
}
/**
* Do not use this on the meta repo because it uses libgit2 operations
* with bad performance and without the ability to handle sparse checkouts.
*
* Return differences for the specified `paths` in the specified `repo` between
* the current index and working directory, and the specified `tree`, if
* not null. If the specified `allUntracked` is true, include all untracked
* files rather than accumulating them by directory. If `paths` is empty,
* check the entire `repo`. If the specified `ignoreIndex` is true,
* return, in the `workdir` field, the status difference between the workdir
* and `tree`, ignoring the state of the index. Otherwise, return, in the
* `workdir` field, the difference between the workir and the index; and in the
* `staged` field, the difference between the index and `tree`. Note that when
* `ignoreIndex` is true, the returned `staged` field will always be `{}`.
* Note also that conflicts are ignored; we don't have enough information here
* to handle them properly.
*
* @param {NodeGit.Repository} repo
* @param {NodeGit.Tree|null} tree
* @param {String []} paths
* @param {Boolean} ignoreIndex
* @param {Boolean} allUntracked
* @return {Object}
* @return {Object} return.staged path to FILESTATUS of staged changes
* @return {Object} return.workdir path to FILESTATUS of workdir changes
*/
exports.getRepoStatus = co.wrap(function *(repo,
tree,
paths,
ignoreIndex,
allUntracked) {
assert.instanceOf(repo, NodeGit.Repository);
if (null !== tree) {
assert.instanceOf(tree, NodeGit.Tree);
}
assert.isArray(paths);
assert.isBoolean(ignoreIndex);
assert.isBoolean(allUntracked);
const options = {
ignoreSubmodules: 1,
flags: NodeGit.Diff.OPTION.INCLUDE_UNTRACKED |
NodeGit.Diff.OPTION.EXCLUDE_SUBMODULES,
};
if (0 !== paths.length) {
options.pathspec = paths;
}
if (allUntracked) {
options.flags = options.flags |
NodeGit.Diff.OPTION.RECURSE_UNTRACKED_DIRS;
}
if (ignoreIndex) {
const workdirToTreeDiff =
yield NodeGit.Diff.treeToWorkdir(repo, tree, options);
const workdirToTreeStatus = readDiff(workdirToTreeDiff);
return {
staged: {},
workdir: workdirToTreeStatus,
};
}
const index = yield repo.index();
const workdirToIndexDiff =
yield NodeGit.Diff.indexToWorkdir(repo, index, options);
const workdirToIndexStatus = readDiff(workdirToIndexDiff);
const indexToTreeDiff =
yield NodeGit.Diff.treeToIndex(repo, tree, null, options);
const indexToTreeStatus = readDiff(indexToTreeDiff);
return {
staged: indexToTreeStatus,
workdir: workdirToIndexStatus,
};
});