@@ -5,9 +5,9 @@ import {watch, FSWatcher} from "fs";
5
5
import * as Path from "path" ;
6
6
import * as _ from "lodash" ;
7
7
import { EventEmitter } from "events" ;
8
- import { executeCommand } from "../PTY" ;
9
8
import { debounce } from "../Decorators" ;
10
9
import * as Git from "../utils/Git" ;
10
+ import { currentBranchName , GitDirectoryPath , hasUncommittedChanges } from "../utils/Git" ;
11
11
12
12
const GIT_WATCHER_EVENT_NAME = "git-data-changed" ;
13
13
@@ -31,7 +31,7 @@ class GitWatcher extends EventEmitter {
31
31
32
32
watch ( ) {
33
33
if ( Git . isGitDirectory ( this . directory ) ) {
34
- this . updateGitData ( ) ;
34
+ this . updateGitData ( this . directory ) ;
35
35
this . watcher = watch ( this . directory , < any > {
36
36
recursive : true ,
37
37
} ) ;
@@ -42,7 +42,9 @@ class GitWatcher extends EventEmitter {
42
42
if ( ! fileName . startsWith ( ".git" ) ||
43
43
fileName === this . GIT_HEAD_FILE_NAME ||
44
44
fileName . startsWith ( this . GIT_HEADS_DIRECTORY_NAME ) ) {
45
- this . updateGitData ( ) ;
45
+ if ( Git . isGitDirectory ( this . directory ) ) {
46
+ this . updateGitData ( this . directory ) ;
47
+ }
46
48
}
47
49
} ,
48
50
) ;
@@ -53,88 +55,16 @@ class GitWatcher extends EventEmitter {
53
55
}
54
56
55
57
@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 ) ;
102
66
}
103
67
}
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
-
138
68
139
69
interface WatchesValue {
140
70
listeners : Set < Session > ;
0 commit comments