@@ -145,21 +145,21 @@ func WithDetection(config Config) heartbeat.HandleOption {
145
145
for n , h := range hh {
146
146
logger .Debugf ("execute project detection for: %s" , h .Entity )
147
147
148
- // first, use .wakatime-project or [projectmap] section with entity path.
148
+ // First use .wakatime-project or [projectmap] section with entity path.
149
149
// Then, detect with project folder. This tries to use the same project name
150
150
// across all IDEs instead of sometimes using alternate project when file is unsaved
151
151
result , detector := Detect (ctx , config .MapPatterns ,
152
152
DetecterArg {Filepath : h .Entity , ShouldRun : h .EntityType == heartbeat .FileType },
153
153
DetecterArg {Filepath : h .ProjectPathOverride , ShouldRun : true },
154
154
)
155
155
156
- // second, use project override
156
+ // Project override
157
157
if result .Project == "" && h .ProjectOverride != "" {
158
158
result .Project = h .ProjectOverride
159
159
result .Folder = h .ProjectPathOverride
160
160
}
161
161
162
- // third, autodetect with revision control with entity path.
162
+ // Autodetect with revision control from entity path.
163
163
// Then, autodetect with project folder. This tries to use the same project name
164
164
// across all IDEs instead of sometimes using alternate project when file is unsaved
165
165
if result .Project == "" || result .Branch == "" || result .Folder == "" {
@@ -177,30 +177,40 @@ func WithDetection(config Config) heartbeat.HandleOption {
177
177
result .Folder = firstNonEmptyString (result .Folder , revControlResult .Folder )
178
178
}
179
179
180
- // fourth, use alternate project
180
+ folder := h .ProjectPathOverride
181
+ if runtime .GOOS == "windows" {
182
+ folder = windows .FormatFilePath (folder )
183
+ }
184
+
185
+ // Use project folder last part as project name
186
+ if result .Project == "" && h .ProjectPathOverride != "" {
187
+ proj := filepath .Base (folder )
188
+
189
+ if proj != "." && proj != "/" {
190
+ result .Project = proj
191
+ }
192
+ result .Folder = folder
193
+ }
194
+
195
+ // Alternate project if none auto-detected
181
196
if result .Project == "" && h .ProjectAlternate != "" {
182
197
result .Project = h .ProjectAlternate
183
- result .Folder = firstNonEmptyString (h .ProjectPathOverride , result .Folder )
184
198
}
185
199
186
- // fifth, use alternate branch
200
+ // Alternate branch if none detected
187
201
if result .Branch == "" && h .BranchAlternate != "" {
188
202
result .Branch = h .BranchAlternate
189
203
}
190
204
191
- // sixth, use project folder found or entity's path
192
- result .Folder = firstNonEmptyString (result .Folder , h . ProjectPathOverride )
205
+ // Make sure project folder is defined when not found from entity's path
206
+ result .Folder = firstNonEmptyString (result .Folder , folder )
193
207
194
- // seventh, if no folder is found, use entity's directory
208
+ // If no folder found, use entity's directory
195
209
if h .EntityType == heartbeat .FileType && result .Folder == "" {
196
210
result .Folder = filepath .Dir (h .Entity )
197
211
}
198
212
199
- if runtime .GOOS == "windows" && result .Folder != "" {
200
- result .Folder = windows .FormatFilePath (result .Folder )
201
- }
202
-
203
- // finally, obfuscate project name if necessary
213
+ // Obfuscate project name if necessary
204
214
if heartbeat .ShouldSanitize (ctx , heartbeat.SanitizeCheck {
205
215
Entity : h .Entity ,
206
216
ProjectPath : result .Folder ,
@@ -212,7 +222,7 @@ func WithDetection(config Config) heartbeat.HandleOption {
212
222
213
223
result .Folder = FormatProjectFolder (ctx , result .Folder )
214
224
215
- // count total subfolders in project's path
225
+ // Count total subfolders in project's path
216
226
if result .Folder != "" && strings .HasPrefix (h .Entity , result .Folder ) {
217
227
subfolders := CountSlashesInProjectFolder (result .Folder )
218
228
if subfolders > 0 {
@@ -705,13 +715,14 @@ func FormatProjectFolder(ctx context.Context, fp string) string {
705
715
formatted , err := filepath .Abs (fp )
706
716
if err != nil {
707
717
logger .Debugf ("failed to resolve absolute path for %q: %s" , fp , err )
708
- return formatted
718
+ return fp
709
719
}
710
720
711
721
// evaluate any symlinks
712
722
formatted , err = realpath .Realpath (formatted )
713
723
if err != nil {
714
724
logger .Debugf ("failed to resolve real path for %q: %s" , formatted , err )
725
+ return fp
715
726
}
716
727
717
728
return formatted
0 commit comments