summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/GitStatisticsCollector.kt
blob: 9bdfeb289ffc66637d1b1bf205a53be91997b909 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package git4idea

import com.google.common.collect.HashMultiset
import com.intellij.dvcs.branch.DvcsSyncSettings.Value
import com.intellij.internal.statistic.StructuredIdeActivity
import com.intellij.internal.statistic.beans.MetricEvent
import com.intellij.internal.statistic.beans.addBoolIfDiffers
import com.intellij.internal.statistic.beans.addIfDiffers
import com.intellij.internal.statistic.eventLog.EventLogGroup
import com.intellij.internal.statistic.eventLog.events.EnumEventField
import com.intellij.internal.statistic.eventLog.events.EventFields
import com.intellij.internal.statistic.eventLog.events.VarargEventId
import com.intellij.internal.statistic.service.fus.collectors.ProjectUsagesCollector
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Comparing
import com.intellij.openapi.vcs.VcsException
import com.intellij.util.io.URLUtil
import com.intellij.util.io.exists
import com.intellij.util.io.isDirectory
import com.intellij.util.io.isFile
import com.intellij.vcs.log.impl.VcsLogApplicationSettings
import com.intellij.vcs.log.impl.VcsLogProjectTabsProperties
import com.intellij.vcs.log.impl.VcsLogUiProperties
import com.intellij.vcs.log.impl.VcsProjectLog
import com.intellij.vcs.log.ui.VcsLogUiImpl
import git4idea.config.*
import git4idea.repo.GitCommitTemplateTracker
import git4idea.repo.GitRemote
import git4idea.repo.GitRepository
import git4idea.ui.branch.dashboard.CHANGE_LOG_FILTER_ON_BRANCH_SELECTION_PROPERTY
import git4idea.ui.branch.dashboard.SHOW_GIT_BRANCHES_LOG_PROPERTY
import java.io.File
import java.nio.file.Files
import java.nio.file.Path

class GitStatisticsCollector : ProjectUsagesCollector() {
  override fun getGroup(): EventLogGroup = GROUP

  override fun getMetrics(project: Project): MutableSet<MetricEvent> {
    val set = HashSet<MetricEvent>()

    val repositoryManager = GitUtil.getRepositoryManager(project)
    val repositories = repositoryManager.repositories

    val settings = GitVcsSettings.getInstance(project)
    val defaultSettings = GitVcsSettings()

    addIfDiffers(set, settings, defaultSettings, { it.syncSetting }, REPO_SYNC, REPO_SYNC_VALUE)
    addIfDiffers(set, settings, defaultSettings, { it.updateMethod }, UPDATE_TYPE, UPDATE_TYPE_VALUE)
    addIfDiffers(set, settings, defaultSettings, { it.saveChangesPolicy }, SAVE_POLICY, SAVE_POLICY_VALUE)

    addBoolIfDiffers(set, settings, defaultSettings, { it.autoUpdateIfPushRejected() }, PUSH_AUTO_UPDATE)
    addBoolIfDiffers(set, settings, defaultSettings, { it.warnAboutCrlf() }, WARN_CRLF)
    addBoolIfDiffers(set, settings, defaultSettings, { it.warnAboutDetachedHead() }, WARN_DETACHED)

    val appSettings = GitVcsApplicationSettings.getInstance()
    val defaultAppSettings = GitVcsApplicationSettings()

    addBoolIfDiffers(set, appSettings, defaultAppSettings, { it.isStagingAreaEnabled }, STAGING_AREA)

    val version = GitVcs.getInstance(project).version
    set.add(EXECUTABLE.metric(
      EventFields.Version with version.presentation,
      TYPE with version.type
    ))

    for (repository in repositories) {
      val branches = repository.branches

      val repositoryMetric = REPOSITORY.metric(
        LOCAL_BRANCHES with branches.localBranches.size,
        REMOTE_BRANCHES with branches.remoteBranches.size,
        REMOTES with repository.remotes.size,
        WORKING_COPY_SIZE with repository.workingCopySize(),
        IS_WORKTREE_USED with repository.isWorkTreeUsed(),
        FS_MONITOR with repository.detectFsMonitor(),
      )

      val remoteTypes = HashMultiset.create(repository.remotes.mapNotNull { getRemoteServerType(it) })
      for (remoteType in remoteTypes) {
        repositoryMetric.data.addData("remote_$remoteType", remoteTypes.count(remoteType))
      }

      set.add(repositoryMetric)
    }

    addCommitTemplateMetrics(project, repositories, set)

    addGitLogMetrics(project, set)

    return set
  }

  private fun addCommitTemplateMetrics(project: Project, repositories: List<GitRepository>, set: java.util.HashSet<MetricEvent>) {
    if (repositories.isEmpty()) return

    val templatesCount = project.service<GitCommitTemplateTracker>().templatesCount()
    if (templatesCount == 0) return

    set.add(COMMIT_TEMPLATE.metric(
      TEMPLATES_COUNT with templatesCount,
      TEMPLATES_MULTIPLE_ROOTS with (repositories.size > 1)
    ))
  }

  private fun addGitLogMetrics(project: Project, metrics: MutableSet<MetricEvent>) {
    val projectLog = VcsProjectLog.getInstance(project) ?: return
    val ui = projectLog.mainLogUi ?: return

    addPropertyMetricIfDiffers(metrics, ui, SHOW_GIT_BRANCHES_LOG_PROPERTY, SHOW_GIT_BRANCHES_IN_LOG)
    addPropertyMetricIfDiffers(metrics, ui, CHANGE_LOG_FILTER_ON_BRANCH_SELECTION_PROPERTY, UPDATE_BRANCH_FILTERS_ON_SELECTION)
  }

  private fun addPropertyMetricIfDiffers(metrics: MutableSet<MetricEvent>,
                                         ui: VcsLogUiImpl,
                                         property: VcsLogUiProperties.VcsLogUiProperty<Boolean>,
                                         eventId: VarargEventId) {
    val defaultValue = (property as? VcsLogProjectTabsProperties.CustomBooleanTabProperty)?.defaultValue(ui.id)
                       ?: (property as? VcsLogApplicationSettings.CustomBooleanProperty)?.defaultValue() ?: return
    val properties = ui.properties
    val value = if (properties.exists(property)) properties[property] else defaultValue

    if (!Comparing.equal(value, defaultValue)) {
      metrics.add(eventId.metric(EventFields.Enabled with value))
    }
  }

  companion object {
    private val GROUP = EventLogGroup("git.configuration", 10)

    private val REPO_SYNC_VALUE: EnumEventField<Value> = EventFields.Enum("value", Value::class.java) { it.name.lowercase() }
    private val REPO_SYNC: VarargEventId = GROUP.registerVarargEvent("repo.sync", REPO_SYNC_VALUE)

    private val UPDATE_TYPE_VALUE = EventFields.Enum("value", UpdateMethod::class.java) { it.name.lowercase() }
    private val UPDATE_TYPE = GROUP.registerVarargEvent("update.type", UPDATE_TYPE_VALUE)

    private val SAVE_POLICY_VALUE = EventFields.Enum("value", GitSaveChangesPolicy::class.java) { it.name.lowercase() }
    private val SAVE_POLICY = GROUP.registerVarargEvent("save.policy", SAVE_POLICY_VALUE)

    private val PUSH_AUTO_UPDATE = GROUP.registerVarargEvent("push.autoupdate", EventFields.Enabled)
    private val WARN_CRLF = GROUP.registerVarargEvent("warn.about.crlf", EventFields.Enabled)
    private val WARN_DETACHED = GROUP.registerVarargEvent("warn.about.detached", EventFields.Enabled)
    private val STAGING_AREA = GROUP.registerVarargEvent("staging.area.enabled", EventFields.Enabled)

    private val TYPE = EventFields.Enum("type", GitVersion.Type::class.java) { it.name }
    private val EXECUTABLE = GROUP.registerVarargEvent("executable", EventFields.Version, TYPE)

    private val LOCAL_BRANCHES = EventFields.RoundedInt("local_branches")
    private val REMOTE_BRANCHES = EventFields.RoundedInt("remote_branches")
    private val REMOTES = EventFields.RoundedInt("remotes")
    private val WORKING_COPY_SIZE = EventFields.RoundedLong("working_copy_size")
    private val IS_WORKTREE_USED = EventFields.Boolean("is_worktree_used")
    private val FS_MONITOR = EventFields.Enum<FsMonitor>("fs_monitor")
    private val remoteTypes = setOf("github", "gitlab", "bitbucket",
                                    "github_custom", "gitlab_custom", "bitbucket_custom")

    private val remoteTypesEventIds = remoteTypes.map {
      EventFields.Int("remote_$it")
    }

    private val REPOSITORY = GROUP.registerVarargEvent("repository",
                                                       LOCAL_BRANCHES,
                                                       REMOTE_BRANCHES,
                                                       REMOTES,
                                                       WORKING_COPY_SIZE,
                                                       IS_WORKTREE_USED,
                                                       FS_MONITOR,
                                                       *remoteTypesEventIds.toTypedArray()
    )

    private val TEMPLATES_COUNT = EventFields.Int("count")
    private val TEMPLATES_MULTIPLE_ROOTS = EventFields.Boolean("multiple_root")
    private val COMMIT_TEMPLATE = GROUP.registerVarargEvent("commit_template", TEMPLATES_COUNT, TEMPLATES_MULTIPLE_ROOTS)

    private val SHOW_GIT_BRANCHES_IN_LOG = GROUP.registerVarargEvent("showGitBranchesInLog", EventFields.Enabled)
    private val UPDATE_BRANCH_FILTERS_ON_SELECTION = GROUP.registerVarargEvent("updateBranchesFilterInLogOnSelection", EventFields.Enabled)

    private fun getRemoteServerType(remote: GitRemote): String? {
      val hosts = remote.urls.map(URLUtil::parseHostFromSshUrl).distinct()

      if (hosts.contains("github.com")) return "github"
      if (hosts.contains("gitlab.com")) return "gitlab"
      if (hosts.contains("bitbucket.org")) return "bitbucket"

      if (remote.urls.any { it.contains("github") }) return "github_custom"
      if (remote.urls.any { it.contains("gitlab") }) return "gitlab_custom"
      if (remote.urls.any { it.contains("bitbucket") }) return "bitbucket_custom"

      return null
    }
  }
}

private const val MAX_SIZE: Long = 4L * 1024 * 1024 * 1024 // 4 GB
private const val MAX_TIME: Long = 5 * 1000 * 60 // 5 min

private fun Sequence<Long>.sumWithLimits(): Long {
  var sum = 0L
  val startTime = System.currentTimeMillis()
  for (element in this) {
    if (System.currentTimeMillis() - startTime > MAX_TIME)
      return -1
    sum += element
    if (sum >= MAX_SIZE) return MAX_SIZE
  }
  return sum
}

/**
 * Calculates size of work tree in given [GitRepository]
 *
 * @return size in bytes or -1 if some IO error occurs
 */
private fun GitRepository.workingCopySize(): Long = try {
  val root = this.root.toNioPath().toFile()
  root.walk()
    .onEnter { it.name != GitUtil.DOT_GIT && !isInnerRepo(root, it) }
    .filter { it.isFile }
    .map { it.length() }
    .sumWithLimits() // don't calculate working copy size over 4 gb to reduce CPU usage
}
catch (e: Exception) {
  // if something goes wrong with file system operations
  -1
}

private fun isInnerRepo(root: File, dir: File): Boolean {
  if (root == dir) return false

  return Path.of(dir.toString(), GitUtil.DOT_GIT).exists()
}

/**
 * Checks that worktree is used in [GitRepository]
 *
 * worktree usage will be detected when:
 * repo_root/.git is a file
 * or repo_root/.git/worktrees is not empty
 */
private fun GitRepository.isWorkTreeUsed(): Boolean {
  return try {
    val rootPath = this.root.toNioPath()

    val dotGit = Path.of(rootPath.toString(), GitUtil.DOT_GIT)
    if (dotGit.isFile()) return true

    val worktreesPath = repositoryFiles.worktreesDirFile.toPath()
    if (!worktreesPath.exists()) return false
    if (!worktreesPath.isDirectory()) return false

    Files.list(worktreesPath).count() > 0
  }
  catch (e: Exception) {
    false
  }
}

enum class FsMonitor { NONE, BUILTIN, EXTERNAL_FS_MONITOR }

private fun GitRepository.detectFsMonitor(): FsMonitor {
  try {
    val useBuiltIn = GitConfigUtil.getBooleanValue(GitConfigUtil.getValue(project, root, "core.useBuiltinFSMonitor"))
                     ?: false
    if (useBuiltIn) return FsMonitor.BUILTIN

    val fsMonitorHook = GitConfigUtil.getValue(project, root, "core.fsmonitor")
    if (fsMonitorHook != null && GitConfigUtil.getBooleanValue(fsMonitorHook) != false) {
      return FsMonitor.EXTERNAL_FS_MONITOR
    }
  }
  catch (ignore: VcsException) {
  }

  return FsMonitor.NONE
}