aboutsummaryrefslogtreecommitdiff
path: root/src/repo.h
blob: f243f86e86c75911f1bd04e4aef10ee1f2af40d8 (plain) (blame)
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
// Copyright 2019 Roman Perepelitsa.
//
// This file is part of GitStatus.
//
// GitStatus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GitStatus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GitStatus. If not, see <https://www.gnu.org/licenses/>.

#ifndef ROMKATV_GITSTATUS_REPO_H_
#define ROMKATV_GITSTATUS_REPO_H_

#include <stddef.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <git2.h>

#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstring>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>

#include "check.h"
#include "index.h"
#include "options.h"
#include "string_cmp.h"
#include "tag_db.h"
#include "time.h"

namespace gitstatus {

struct IndexStats {
  size_t index_size = 0;
  size_t num_staged = 0;
  size_t num_unstaged = 0;
  size_t num_conflicted = 0;
  size_t num_untracked = 0;
  size_t num_staged_new = 0;
  size_t num_staged_deleted = 0;
  size_t num_unstaged_deleted = 0;
  size_t num_skip_worktree = 0;
  size_t num_assume_unchanged = 0;
};

class Repo {
 public:
  explicit Repo(git_repository* repo, Limits lim);
  Repo(Repo&& other) = delete;
  ~Repo();

  git_repository* repo() const { return repo_; }

  // Head can be null, in which case has_staged will be false.
  IndexStats GetIndexStats(const git_oid* head, git_config* cfg);

  // Returns the last tag in lexicographical order whose target is equal to the given, or an
  // empty string. Target can be null, in which case the tag is empty.
  std::future<std::string> GetTagName(const git_oid* target);

 private:
  struct Shard {
    bool Contains(Str<> str, StringView path) const;
    std::string start_s;
    std::string end_s;
    size_t start_i;
    size_t end_i;
  };

  void UpdateShards();

  int OnDelta(const char* type, const git_diff_delta& d, std::atomic<size_t>& c1, size_t m1,
              const std::atomic<size_t>& c2, size_t m2);

  void StartStagedScan(const git_oid* head);
  void StartDirtyScan(const std::vector<const char*>& paths);

  void DecInflight();
  void RunAsync(std::function<void()> f);
  void Wait();

  Limits lim_;
  git_repository* const repo_;
  git_index* git_index_ = nullptr;
  std::vector<Shard> shards_;
  git_oid head_ = {};
  TagDb tag_db_;

  std::unique_ptr<Index> index_;

  std::mutex mutex_;
  std::condition_variable cv_;
  std::atomic<size_t> inflight_{0};
  std::atomic<bool> error_{false};
  std::atomic<size_t> staged_{0};
  std::atomic<size_t> unstaged_{0};
  std::atomic<size_t> conflicted_{0};
  std::atomic<size_t> untracked_{0};
  std::atomic<size_t> staged_new_{0};
  std::atomic<size_t> staged_deleted_{0};
  std::atomic<size_t> unstaged_deleted_{0};
  std::atomic<size_t> skip_worktree_{0};
  std::atomic<size_t> assume_unchanged_{0};
  std::atomic<Tribool> untracked_cache_{Tribool::kUnknown};
};

}  // namespace gitstatus

#endif  // ROMKATV_GITSTATUS_REPO_H_