Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 29101c6

Browse files
committed
Simplify git status.
1 parent bb844b5 commit 29101c6

File tree

4 files changed

+34
-107
lines changed

4 files changed

+34
-107
lines changed

src/plugins/GitWatcher.ts

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {watch, FSWatcher} from "fs";
55
import * as Path from "path";
66
import * as _ from "lodash";
77
import {EventEmitter} from "events";
8-
import {executeCommand} from "../PTY";
98
import {debounce} from "../Decorators";
109
import * as Git from "../utils/Git";
10+
import {currentBranchName, GitDirectoryPath, hasUncommittedChanges} from "../utils/Git";
1111

1212
const GIT_WATCHER_EVENT_NAME = "git-data-changed";
1313

@@ -31,7 +31,7 @@ class GitWatcher extends EventEmitter {
3131

3232
watch() {
3333
if (Git.isGitDirectory(this.directory)) {
34-
this.updateGitData();
34+
this.updateGitData(this.directory);
3535
this.watcher = watch(this.directory, <any>{
3636
recursive: true,
3737
});
@@ -42,7 +42,9 @@ class GitWatcher extends EventEmitter {
4242
if (!fileName.startsWith(".git") ||
4343
fileName === this.GIT_HEAD_FILE_NAME ||
4444
fileName.startsWith(this.GIT_HEADS_DIRECTORY_NAME)) {
45-
this.updateGitData();
45+
if (Git.isGitDirectory(this.directory)) {
46+
this.updateGitData(this.directory);
47+
}
4648
}
4749
},
4850
);
@@ -53,88 +55,16 @@ class GitWatcher extends EventEmitter {
5355
}
5456

5557
@debounce(1000 / 60)
56-
private async updateGitData() {
57-
58-
executeCommand("git", ["status", "-b", "--porcelain"], this.directory).then(changes => {
59-
const status: VcsStatus = (changes.split("\n").length > 2) ? "dirty" : "clean";
60-
let head: string = changes.split(" ")[1];
61-
let push: string = "0";
62-
let pull: string = "0";
63-
64-
let secondSplit: Array<string> = changes.split("[");
65-
if (secondSplit.length > 1) {
66-
let rawPushPull: string = secondSplit[1].slice(0, -2);
67-
let separatedPushPull: Array<string> = rawPushPull.split(", ");
68-
69-
70-
if (separatedPushPull.length > 0) {
71-
for (let i in separatedPushPull) {
72-
if (separatedPushPull.hasOwnProperty(i)) {
73-
let splitAgain: Array<string> = separatedPushPull[i].split(" ");
74-
switch (splitAgain[0]) {
75-
case "ahead":
76-
push = splitAgain[1];
77-
break;
78-
case "behind":
79-
pull = splitAgain[1];
80-
break;
81-
default:
82-
break;
83-
}
84-
}
85-
}
86-
}
87-
}
88-
89-
let fileChanges = linesToFileChanges(changes);
90-
91-
const data: VcsData = {
92-
kind: "repository",
93-
branch: head,
94-
push: push,
95-
pull: pull,
96-
status: status,
97-
changes: fileChanges,
98-
};
99-
100-
this.emit(GIT_WATCHER_EVENT_NAME, data);
101-
});
58+
private async updateGitData(directory: GitDirectoryPath) {
59+
const data: VcsData = {
60+
kind: "repository",
61+
branch: await currentBranchName(directory),
62+
status: (await hasUncommittedChanges(directory) ? "dirty" : "clean"),
63+
};
64+
65+
this.emit(GIT_WATCHER_EVENT_NAME, data);
10266
}
10367
}
104-
function linesToFileChanges(lines: string): FileChanges {
105-
let stagedChanges = new Map<string, number>([["+", 0], ["~", 0], ["-", 0], ["!", 0]]);
106-
let unstagedChanges = new Map<string, number>([["+", 0], ["~", 0], ["-", 0], ["!", 0]]);
107-
lines.split("\n").slice(1).forEach((line) => {
108-
switch (line[0]) {
109-
case "A": stagedChanges.set("+", stagedChanges.get("+")! + 1); break;
110-
case "M":
111-
case "R":
112-
case "C": stagedChanges.set("~", stagedChanges.get("~")! + 1); break;
113-
case "D": stagedChanges.set("-", stagedChanges.get("-")! + 1); break;
114-
case "U": stagedChanges.set("!", stagedChanges.get("!")! + 1); break;
115-
default: break;
116-
}
117-
118-
switch (line[1]) {
119-
case "?":
120-
case "A": unstagedChanges.set("+", stagedChanges.get("+")! + 1); break;
121-
case "M": unstagedChanges.set("~", stagedChanges.get("~")! + 1); break;
122-
case "D": unstagedChanges.set("-", stagedChanges.get("-")! + 1); break;
123-
case "U": unstagedChanges.set("!", stagedChanges.get("!")! + 1); break;
124-
default: break;
125-
}
126-
});
127-
let stagedResult: string = [...stagedChanges]
128-
.filter((pair) => (pair[1] !== 0))
129-
.map(([key, v]) => (key + String(v) + " "))
130-
.reduce((left, right) => (left + right), "");
131-
let unstagedResult: string = [...unstagedChanges]
132-
.filter((pair) => (pair[1] !== 0))
133-
.map(([key, v]) => key + String(v) + " ")
134-
.reduce((left, right) => left + right, "");
135-
return {stagedChanges: stagedResult, unstagedChanges: unstagedResult};
136-
}
137-
13868

13969
interface WatchesValue {
14070
listeners: Set<Session>;

src/utils/Git.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,32 @@ type BranchesOptions = {
117117
tags: boolean;
118118
};
119119

120+
export async function currentBranchName(directory: GitDirectoryPath): Promise<string> {
121+
const output = await executeCommand(
122+
"git",
123+
['"symbolic-ref"', '"--short"', '"-q"', '"HEAD"'],
124+
directory,
125+
);
126+
127+
return output.trim();
128+
}
129+
130+
export async function hasUncommittedChanges(directory: GitDirectoryPath): Promise<boolean> {
131+
const output = await executeCommand(
132+
"git",
133+
["status", "--untracked-files=no", "--porcelain"],
134+
directory,
135+
);
136+
137+
return output.trim().length !== 0;
138+
}
139+
120140
export async function branches({
121141
directory,
122142
remotes,
123143
tags,
124144
}: BranchesOptions): Promise<Branch[]> {
125-
const currentBranch = await executeCommand(
126-
"git",
127-
['"symbolic-ref"', '"--short"', '"-q"', '"HEAD"'],
128-
directory)
129-
.then(branchName => branchName.trim());
145+
const currentBranch = await currentBranchName(directory);
130146
const promiseHeadsTags = linedOutputOf(
131147
"git",
132148
["for-each-ref", "refs/heads ", tags ? "refs/tags " : "",

src/views/StatusBarComponent.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@ const VcsDataComponent = ({data}: { data: VcsData }) => {
99
return (
1010
<div style={css.statusBar.vcsData}>
1111
<span style={css.statusBar.status(data.status)}>
12-
{(data.status === "dirty") ?
13-
(<span>
14-
<span style={css.statusBar.stagedFileChanges}>
15-
{data.changes.stagedChanges}
16-
</span>
17-
{(data.changes.stagedChanges === "") ? "" : "| "}
18-
<span style={css.statusBar.unstagedFileChanges}>
19-
{data.changes.unstagedChanges}
20-
</span>
21-
</span>) :
22-
undefined
23-
}
24-
<span style={css.statusBar.icon}>{fontAwesome.longArrowDown}</span>
25-
{data.pull}
26-
<span style={css.statusBar.icon}>{fontAwesome.longArrowUp}</span>
27-
{data.push}
2812
<span style={css.statusBar.icon}>{fontAwesome.codeFork}</span>
2913
{data.branch}
3014
</span>

typings/Interfaces.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ type VcsStatus = "dirty" | "clean";
2323
type VcsData = {
2424
kind: "repository",
2525
branch: string,
26-
push: string,
27-
pull: string,
28-
changes: FileChanges,
2926
status: VcsStatus;
3027
} | { kind: "not-repository"; };
3128

0 commit comments

Comments
 (0)