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
|
use std::collections::HashMap;
use anyhow::{Context, Result};
use client::get_state_from_cache;
use git::RepoActions;
use ngit::{
client,
git::{self},
git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value},
list::{get_ahead_behind, list_from_remotes},
login::get_curent_user,
repo_ref::{self},
utils::{get_all_proposals, get_open_or_draft_proposals, get_short_git_server_name},
};
use repo_ref::RepoRef;
use crate::{
fetch::{fetch_from_git_server, make_commits_for_proposal},
git::Repo,
};
pub async fn run_list(
git_repo: &Repo,
repo_ref: &RepoRef,
for_push: bool,
) -> Result<HashMap<String, (HashMap<String, String>, bool)>> {
let nostr_state = (get_state_from_cache(Some(git_repo.get_path()?), repo_ref).await).ok();
let term = console::Term::stderr();
let remote_states = list_from_remotes(
&term,
git_repo,
&repo_ref.git_server,
&repo_ref.to_nostr_git_url(&None),
&repo_ref.grasp_servers(),
);
let mut state = if let Some(nostr_state) = nostr_state {
for (name, value) in &nostr_state.state {
for (url, (remote_state, _is_grasp_server)) in &remote_states {
let remote_name = get_short_git_server_name(git_repo, url);
if let Some(remote_value) = remote_state.get(name) {
if value.ne(remote_value) {
term.write_line(
format!(
"WARNING: {remote_name} {name} is {} nostr ",
if let Ok((ahead, behind)) =
get_ahead_behind(git_repo, value, remote_value)
{
format!("{} ahead {} behind", ahead.len(), behind.len())
} else {
"out of sync with".to_string()
}
)
.as_str(),
)?;
}
} else {
term.write_line(
format!("WARNING: {remote_name} {name} is missing but tracked on nostr")
.as_str(),
)?;
}
}
}
nostr_state.state
} else {
let (state, _is_grasp_server) = repo_ref
.git_server
.iter()
.filter_map(|server| remote_states.get(server))
.cloned()
.collect::<Vec<(HashMap<String, String>, bool)>>()
.first()
.context("failed to get refs from git server")?
.clone();
state
};
state.retain(|k, _| !k.starts_with("refs/heads/pr/"));
state.extend(
// get as refs/heads/pr/<branch-name>(<shorthand-event-id>)
get_open_and_draft_proposals_state(&term, git_repo, repo_ref, &remote_states).await?,
);
state.extend(
// get as refs/pr/<branch-name>(<shorthand-event-id>) and refs/pr-by-id/<event-id>
get_all_proposals_state(git_repo, repo_ref).await?,
);
// TODO 'for push' should we check with the git servers to see if any of them
// allow push from the user?
for (name, value) in state {
if value.starts_with("ref: ") {
if !for_push {
println!("{} {name}", value.replace("ref: ", "@"));
}
} else {
println!("{value} {name}");
}
}
println!();
Ok(remote_states)
}
/// fetches branches and tags from git servers so patch parent commits can be
/// used to build patches with correct commit ids
async fn get_open_and_draft_proposals_state(
term: &console::Term,
git_repo: &Repo,
repo_ref: &RepoRef,
remote_states: &HashMap<String, (HashMap<String, String>, bool)>,
) -> Result<HashMap<String, String>> {
// we cannot use commit_id in the latest patch in a proposal because:
// 1) the `commit` tag is optional
// 2) if the commit tag is wrong, it will cause errors which stop clone from
// working
// without trusting commit_id we must apply each patch which requires the oid of
// the parent so we much do a fetch
// As we are fetching from git servers we mighgt as well get oids from pull
// request too
// TODO get Pull Request and Pull Request Update Events add these to
// refs/nostr/<event-id>
// TODO prepare PRs and PRS oids to try and fetch from repo servers that are or
// clone urls in PR/update event we are using anyway. TODO after we tried
// and failed to get them from these server we should fallback to fetch them
// from listed clone urls in PR/update but not during list, only during fetch
for (git_server_url, (oids_from_git_servers, is_grasp_server)) in remote_states {
if fetch_from_git_server(
git_repo,
&oids_from_git_servers
.values()
.filter(|v| !v.starts_with("ref: "))
.cloned()
.collect::<Vec<String>>(),
git_server_url,
&repo_ref.to_nostr_git_url(&None),
term,
*is_grasp_server,
)
.is_ok()
{
break;
}
}
let mut state = HashMap::new();
let open_and_draft_proposals = get_open_or_draft_proposals(git_repo, repo_ref).await?;
let current_user = get_curent_user(git_repo)?;
for (_, (proposal, events_to_apply)) in open_and_draft_proposals {
if let Ok(cl) = event_to_cover_letter(&proposal) {
if let Ok(mut branch_name) = cl.get_branch_name_with_pr_prefix_and_shorthand_id() {
branch_name = if let Some(public_key) = current_user {
if proposal.pubkey.eq(&public_key) {
format!("pr/{}", cl.branch_name_without_id_or_prefix)
} else {
branch_name
}
} else {
branch_name
};
// if events_to_apply contains a PR or PR Update event it should be the only
// event in the Vec
if let Some(pr_or_pr_update) = events_to_apply
.iter()
.find(|e| e.kind.eq(&KIND_PULL_REQUEST) || e.kind.eq(&KIND_PULL_REQUEST_UPDATE))
{
match tag_value(pr_or_pr_update, "c") {
Ok(tip) => {
// only list Pull Requests as refs/heads/pr/* if data is on a repo git
// server otherwise the standard `git clone
// nostr://` cmd will fail as it assumes all /refs/heads returned by
// list are accessable
let tip_oid_is_on_a_repo_git_server =
remote_states.iter().any(|(_url, (state, _is_grasp))| {
state.iter().any(|(_, oid)| tip == *oid)
});
if tip_oid_is_on_a_repo_git_server {
state.insert(format!("refs/heads/{branch_name}"), tip);
}
}
Err(_) => {
let _ = term.write_line(
format!(
"WARNING: failed to fetch branch {branch_name} error: {} event poorly formatted",
if pr_or_pr_update.kind.eq(&KIND_PULL_REQUEST) {
"PR"
} else {
"PR update"
}
)
.as_str(),
);
}
}
} else {
match make_commits_for_proposal(git_repo, repo_ref, &events_to_apply) {
Ok(tip) => {
state.insert(format!("refs/heads/{branch_name}"), tip);
}
Err(error) => {
let _ = term.write_line(
format!(
"WARNING: failed to fetch branch {branch_name} error: {error}"
)
.as_str(),
);
}
}
}
}
}
}
Ok(state)
}
/// we assume latest default branch oid has been fetched so patch parent commits
/// are present. doesnt report on proposals failed to recreate
async fn get_all_proposals_state(
git_repo: &Repo,
repo_ref: &RepoRef,
) -> Result<HashMap<String, String>> {
let mut state = HashMap::new();
let all_proposals = get_all_proposals(git_repo, repo_ref).await?;
let current_user = get_curent_user(git_repo)?;
for (_, (proposal, events_to_apply)) in all_proposals {
if let Ok(cl) = event_to_cover_letter(&proposal) {
if let Ok(mut branch_name) = cl.get_branch_name_with_pr_prefix_and_shorthand_id() {
branch_name = if let Some(public_key) = current_user {
if proposal.pubkey.eq(&public_key) {
format!("pr/{}", cl.branch_name_without_id_or_prefix)
} else {
branch_name
}
} else {
branch_name
};
if let Some(pr_or_pr_update) = events_to_apply
.iter()
.find(|e| e.kind.eq(&KIND_PULL_REQUEST) || e.kind.eq(&KIND_PULL_REQUEST_UPDATE))
{
if let Ok(tip) = tag_value(pr_or_pr_update, "c") {
state.insert(format!("refs/{branch_name}"), tip.clone());
state.insert(format!("refs/pr-by-id/{}", proposal.id), tip);
}
} else if let Ok(tip) =
make_commits_for_proposal(git_repo, repo_ref, &events_to_apply)
{
state.insert(format!("refs/{branch_name}"), tip.clone());
state.insert(format!("refs/pr-by-id/{}", proposal.id), tip);
}
}
}
}
Ok(state)
}
|