my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.4 KiB

3 years ago
  1. // Copyright 2019 Roman Perepelitsa.
  2. //
  3. // This file is part of GitStatus.
  4. //
  5. // GitStatus is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GitStatus is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with GitStatus. If not, see <https://www.gnu.org/licenses/>.
  17. #ifndef ROMKATV_GITSTATUS_REPO_H_
  18. #define ROMKATV_GITSTATUS_REPO_H_
  19. #include <stddef.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <git2.h>
  24. #include <algorithm>
  25. #include <atomic>
  26. #include <condition_variable>
  27. #include <cstddef>
  28. #include <cstring>
  29. #include <functional>
  30. #include <future>
  31. #include <memory>
  32. #include <mutex>
  33. #include <string>
  34. #include <utility>
  35. #include <vector>
  36. #include "check.h"
  37. #include "index.h"
  38. #include "options.h"
  39. #include "string_cmp.h"
  40. #include "tag_db.h"
  41. #include "time.h"
  42. namespace gitstatus {
  43. struct IndexStats {
  44. size_t index_size = 0;
  45. size_t num_staged = 0;
  46. size_t num_unstaged = 0;
  47. size_t num_conflicted = 0;
  48. size_t num_untracked = 0;
  49. size_t num_staged_new = 0;
  50. size_t num_staged_deleted = 0;
  51. size_t num_unstaged_deleted = 0;
  52. size_t num_skip_worktree = 0;
  53. size_t num_assume_unchanged = 0;
  54. };
  55. class Repo {
  56. public:
  57. explicit Repo(git_repository* repo, Limits lim);
  58. Repo(Repo&& other) = delete;
  59. ~Repo();
  60. git_repository* repo() const { return repo_; }
  61. // Head can be null, in which case has_staged will be false.
  62. IndexStats GetIndexStats(const git_oid* head, git_config* cfg);
  63. // Returns the last tag in lexicographical order whose target is equal to the given, or an
  64. // empty string. Target can be null, in which case the tag is empty.
  65. std::future<std::string> GetTagName(const git_oid* target);
  66. private:
  67. struct Shard {
  68. bool Contains(Str<> str, StringView path) const;
  69. std::string start_s;
  70. std::string end_s;
  71. size_t start_i;
  72. size_t end_i;
  73. };
  74. void UpdateShards();
  75. int OnDelta(const char* type, const git_diff_delta& d, std::atomic<size_t>& c1, size_t m1,
  76. const std::atomic<size_t>& c2, size_t m2);
  77. void StartStagedScan(const git_oid* head);
  78. void StartDirtyScan(const std::vector<const char*>& paths);
  79. void DecInflight();
  80. void RunAsync(std::function<void()> f);
  81. void Wait();
  82. Limits lim_;
  83. git_repository* const repo_;
  84. git_index* git_index_ = nullptr;
  85. std::vector<Shard> shards_;
  86. git_oid head_ = {};
  87. TagDb tag_db_;
  88. std::unique_ptr<Index> index_;
  89. std::mutex mutex_;
  90. std::condition_variable cv_;
  91. std::atomic<size_t> inflight_{0};
  92. std::atomic<bool> error_{false};
  93. std::atomic<size_t> staged_{0};
  94. std::atomic<size_t> unstaged_{0};
  95. std::atomic<size_t> conflicted_{0};
  96. std::atomic<size_t> untracked_{0};
  97. std::atomic<size_t> staged_new_{0};
  98. std::atomic<size_t> staged_deleted_{0};
  99. std::atomic<size_t> unstaged_deleted_{0};
  100. std::atomic<size_t> skip_worktree_{0};
  101. std::atomic<size_t> assume_unchanged_{0};
  102. std::atomic<Tribool> untracked_cache_{Tribool::kUnknown};
  103. };
  104. } // namespace gitstatus
  105. #endif // ROMKATV_GITSTATUS_REPO_H_