-
Notifications
You must be signed in to change notification settings - Fork 14
/
git.rs
641 lines (531 loc) · 17 KB
/
git.rs
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use std::{
collections::HashMap,
hash::Hash,
io::{BufRead, BufReader},
path::{Path, PathBuf},
process::{Command, Stdio},
};
use chrono::{DateTime, Local, TimeDelta};
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CommitHash(String);
impl CommitHash {
pub fn as_short_hash(&self) -> String {
self.0.chars().take(7).collect()
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for CommitHash {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
#[derive(Debug, Default, Clone)]
pub enum CommitType {
#[default]
Commit,
Stash {
parent_commit_committer_date: DateTime<Local>,
},
}
#[derive(Debug, Default, Clone)]
pub struct Commit {
pub commit_hash: CommitHash,
pub author_name: String,
pub author_email: String,
pub author_date: DateTime<Local>,
pub committer_name: String,
pub committer_email: String,
pub committer_date: DateTime<Local>,
pub subject: String,
pub body: String,
pub parent_commit_hashes: Vec<CommitHash>,
pub commit_type: CommitType,
}
impl Commit {
pub fn committer_date_sort_key(&self) -> DateTime<Local> {
match self.commit_type {
CommitType::Commit => self.committer_date,
CommitType::Stash {
parent_commit_committer_date,
} => {
// The unit of committer_date is seconds, so add 1 nanosecond to make sure the stash commit appears after the parent commit
parent_commit_committer_date
.checked_add_signed(TimeDelta::nanoseconds(1))
.unwrap()
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Ref {
Tag {
name: String,
target: CommitHash,
},
Branch {
name: String,
target: CommitHash,
},
RemoteBranch {
name: String,
target: CommitHash,
},
Stash {
name: String,
message: String,
target: CommitHash,
},
}
impl Ref {
pub fn name(&self) -> &str {
match self {
Ref::Tag { name, .. } => name,
Ref::Branch { name, .. } => name,
Ref::RemoteBranch { name, .. } => name,
Ref::Stash { name, .. } => name,
}
}
pub fn target(&self) -> &CommitHash {
match self {
Ref::Tag { target, .. } => target,
Ref::Branch { target, .. } => target,
Ref::RemoteBranch { target, .. } => target,
Ref::Stash { target, .. } => target,
}
}
}
#[derive(Debug, Clone)]
pub enum Head {
Branch { name: String },
Detached { target: CommitHash },
}
type CommitMap = HashMap<CommitHash, Commit>;
type CommitsMap = HashMap<CommitHash, Vec<CommitHash>>;
type RefMap = HashMap<CommitHash, Vec<Ref>>;
#[derive(Debug)]
pub struct Repository {
path: PathBuf,
commit_map: CommitMap,
parents_map: CommitsMap,
children_map: CommitsMap,
ref_map: RefMap,
head: Head,
}
impl Repository {
pub fn load(path: &Path) -> Self {
check_git_repository(path);
let mut commits = load_all_commits(path);
let stashes = load_all_stashes(path, to_commit_ref_map(&commits));
commits.extend(stashes);
let (parents_map, children_map) = build_commits_maps(&commits);
let commit_map = to_commit_map(commits);
let (mut ref_map, head) = load_refs(path);
let stash_ref_map = load_stashes_as_refs(path);
merge_ref_maps(&mut ref_map, stash_ref_map);
Self::new(
path.to_path_buf(),
commit_map,
parents_map,
children_map,
ref_map,
head,
)
}
pub fn new(
path: PathBuf,
commit_map: CommitMap,
parents_map: CommitsMap,
children_map: CommitsMap,
ref_map: RefMap,
head: Head,
) -> Self {
Self {
path,
commit_map,
parents_map,
children_map,
ref_map,
head,
}
}
pub fn commit(&self, commit_hash: &CommitHash) -> Option<&Commit> {
self.commit_map.get(commit_hash)
}
pub fn all_commits(&self) -> Vec<&Commit> {
self.commit_map.values().collect()
}
pub fn parents_hash(&self, commit_hash: &CommitHash) -> Vec<&CommitHash> {
self.parents_map
.get(commit_hash)
.map(|hs| hs.iter().collect::<Vec<&CommitHash>>())
.unwrap_or_default()
}
pub fn children_hash(&self, commit_hash: &CommitHash) -> Vec<&CommitHash> {
self.children_map
.get(commit_hash)
.map(|hs| hs.iter().collect::<Vec<&CommitHash>>())
.unwrap_or_default()
}
pub fn refs(&self, commit_hash: &CommitHash) -> Vec<&Ref> {
self.ref_map
.get(commit_hash)
.map(|refs| refs.iter().collect::<Vec<&Ref>>())
.unwrap_or_default()
}
pub fn all_refs(&self) -> Vec<&Ref> {
self.ref_map.values().flatten().collect()
}
pub fn head(&self) -> &Head {
&self.head
}
pub fn commit_detail(&self, commit_hash: &CommitHash) -> (Commit, Vec<FileChange>) {
let commit = self.commit(commit_hash).unwrap().clone();
let changes = if commit.parent_commit_hashes.is_empty() {
get_initial_commit_additions(&self.path, commit_hash)
} else {
get_diff_summary(&self.path, commit_hash)
};
(commit, changes)
}
}
fn check_git_repository(path: &Path) {
let output = Command::new("git")
.arg("rev-parse")
.arg("--is-inside-work-tree")
.current_dir(path)
.output()
.unwrap();
if !output.status.success() {
panic!("not a git repository (or any of the parent directories)");
}
}
fn load_all_commits(path: &Path) -> Vec<Commit> {
let mut cmd = Command::new("git")
.arg("log")
// exclude stashes and other refs
.arg("--branches")
.arg("--remotes")
.arg("--tags")
.arg(format!("--pretty={}", load_commits_format()))
.arg("--date=iso-strict")
.arg("-z") // use NUL as a delimiter
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let mut commits = Vec::new();
for bytes in reader.split(b'\0') {
let bytes = bytes.unwrap();
let s = String::from_utf8_lossy(&bytes);
let parts: Vec<&str> = s.split('\x1f').collect();
if parts.len() != 10 {
panic!("unexpected number of parts: {} [{}]", parts.len(), s);
}
let commit = Commit {
commit_hash: parts[0].into(),
author_name: parts[1].into(),
author_email: parts[2].into(),
author_date: parse_iso_date(parts[3]),
committer_name: parts[4].into(),
committer_email: parts[5].into(),
committer_date: parse_iso_date(parts[6]),
subject: parts[7].into(),
body: parts[8].into(),
parent_commit_hashes: parse_parent_commit_hashes(parts[9]),
commit_type: CommitType::Commit,
};
commits.push(commit);
}
cmd.wait().unwrap();
commits
}
fn load_all_stashes(path: &Path, commit_map: HashMap<&CommitHash, &Commit>) -> Vec<Commit> {
let mut cmd = Command::new("git")
.arg("stash")
.arg("list")
.arg(format!("--pretty={}", load_commits_format()))
.arg("--date=iso-strict")
.arg("-z") // use NUL as a delimiter
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let mut commits = Vec::new();
for bytes in reader.split(b'\0') {
let bytes = bytes.unwrap();
let s = String::from_utf8_lossy(&bytes);
let parts: Vec<&str> = s.split('\x1f').collect();
if parts.len() != 10 {
panic!("unexpected number of parts: {} [{}]", parts.len(), s);
}
let parent_commit_hashes = parse_parent_commit_hashes(parts[9]);
// Stash commit has multiple parent commits, but the first parent commit is the commit that the stash was created from.
// If the first parent commit is not found, the stash commit is ignored.
if let Some(first_parnet_commit) = commit_map.get(&parent_commit_hashes[0]) {
let commit = Commit {
commit_hash: parts[0].into(),
author_name: parts[1].into(),
author_email: parts[2].into(),
author_date: parse_iso_date(parts[3]),
committer_name: parts[4].into(),
committer_email: parts[5].into(),
committer_date: parse_iso_date(parts[6]),
subject: parts[7].into(),
body: parts[8].into(),
parent_commit_hashes,
commit_type: CommitType::Stash {
parent_commit_committer_date: first_parnet_commit.committer_date,
},
};
commits.push(commit);
}
}
cmd.wait().unwrap();
commits
}
fn load_commits_format() -> String {
[
"%H", "%an", "%ae", "%ad", "%cn", "%ce", "%cd", "%s", "%b", "%P",
]
.join("%x1f") // use Unit Separator as a delimiter
}
fn parse_iso_date(s: &str) -> DateTime<Local> {
DateTime::parse_from_rfc3339(s)
.unwrap()
.with_timezone(&Local)
}
fn parse_parent_commit_hashes(s: &str) -> Vec<CommitHash> {
if s.is_empty() {
return Vec::new();
}
s.split(' ').map(|s| s.into()).collect()
}
fn build_commits_maps(commits: &Vec<Commit>) -> (CommitsMap, CommitsMap) {
let mut parents_map: CommitsMap = HashMap::new();
let mut children_map: CommitsMap = HashMap::new();
for commit in commits {
let hash = &commit.commit_hash;
for parent_hash in &commit.parent_commit_hashes {
parents_map
.entry(hash.clone())
.or_default()
.push(parent_hash.clone());
children_map
.entry(parent_hash.clone())
.or_default()
.push(hash.clone());
}
}
(parents_map, children_map)
}
fn to_commit_ref_map(commits: &[Commit]) -> HashMap<&CommitHash, &Commit> {
commits
.iter()
.map(|commit| (&commit.commit_hash, commit))
.collect()
}
fn to_commit_map(commits: Vec<Commit>) -> CommitMap {
commits
.into_iter()
.map(|commit| (commit.commit_hash.clone(), commit))
.collect()
}
fn load_refs(path: &Path) -> (RefMap, Head) {
let mut cmd = Command::new("git")
.arg("show-ref")
.arg("--head")
.arg("--dereference")
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let mut ref_map = RefMap::new();
let mut tag_map: HashMap<String, Ref> = HashMap::new();
let mut head: Option<Head> = None;
for line in reader.lines() {
let line = line.unwrap();
let parts: Vec<&str> = line.split(' ').collect();
if parts.len() != 2 {
panic!("unexpected number of parts: {} [{}]", parts.len(), line);
}
let hash = parts[0];
let refs = parts[1];
if refs == "HEAD" {
head = if let Some(branch) = get_current_branch(path) {
Some(Head::Branch { name: branch })
} else {
Some(Head::Detached {
target: hash.into(),
})
};
} else if let Some(r) = parse_branch_refs(hash, refs) {
ref_map.entry(hash.into()).or_default().push(r);
} else if let Some(r) = parse_tag_refs(hash, refs) {
// if annotated tag exists, it will be overwritten by the following line of the same tag
// this will make the tag point to the commit that the annotated tag points to
tag_map.insert(r.name().into(), r);
}
}
let head = head.expect("HEAD not found in `git show-ref --head` output");
for tag in tag_map.into_values() {
ref_map.entry(tag.target().clone()).or_default().push(tag);
}
ref_map.values_mut().for_each(|refs| refs.sort());
cmd.wait().unwrap();
(ref_map, head)
}
fn load_stashes_as_refs(path: &Path) -> RefMap {
let format = ["%gd", "%H", "%s"].join("%x1f"); // use Unit Separator as a delimiter
let mut cmd = Command::new("git")
.arg("stash")
.arg("list")
.arg(format!("--format={}", format))
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let mut ref_map = RefMap::new();
for line in reader.lines() {
let line = line.unwrap();
let parts: Vec<&str> = line.split('\x1f').collect();
if parts.len() != 3 {
panic!("unexpected number of parts: {} [{}]", parts.len(), line);
}
let name = parts[0];
let hash = parts[1];
let subject = parts[2];
let r = Ref::Stash {
name: name.into(),
message: subject.into(),
target: hash.into(),
};
ref_map.entry(hash.into()).or_default().push(r);
}
cmd.wait().unwrap();
ref_map
}
fn merge_ref_maps(m1: &mut RefMap, m2: RefMap) {
for (k, v) in m2 {
m1.entry(k).or_default().extend(v);
}
}
fn parse_branch_refs(hash: &str, refs: &str) -> Option<Ref> {
if refs.starts_with("refs/heads/") {
let name = refs.trim_start_matches("refs/heads/");
Some(Ref::Branch {
name: name.into(),
target: hash.into(),
})
} else if refs.starts_with("refs/remotes/") {
let name = refs.trim_start_matches("refs/remotes/");
Some(Ref::RemoteBranch {
name: name.into(),
target: hash.into(),
})
} else {
None
}
}
fn parse_tag_refs(hash: &str, refs: &str) -> Option<Ref> {
if refs.starts_with("refs/tags/") {
let name = refs.trim_start_matches("refs/tags/");
let name = name.trim_end_matches("^{}");
Some(Ref::Tag {
name: name.into(),
target: hash.into(),
})
} else {
None
}
}
fn get_current_branch(path: &Path) -> Option<String> {
let mut cmd = Command::new("git")
.arg("branch")
.arg("--show-current")
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let branch = if let Some(line) = reader.lines().next() {
line.ok()
} else {
None
};
cmd.wait().unwrap();
branch
}
#[derive(Debug)]
pub enum FileChange {
Add { path: String },
Modify { path: String },
Delete { path: String },
Move { from: String, to: String },
}
pub fn get_diff_summary(path: &Path, commit_hash: &CommitHash) -> Vec<FileChange> {
let mut cmd = Command::new("git")
.arg("diff")
.arg("--name-status")
.arg(format!("{}^", commit_hash.0))
.arg(&commit_hash.0)
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let mut changes = Vec::new();
for line in reader.lines() {
let line = line.unwrap();
let parts: Vec<&str> = line.split('\t').collect();
match &parts[0][0..1] {
"A" => changes.push(FileChange::Add {
path: parts[1].into(),
}),
"M" => changes.push(FileChange::Modify {
path: parts[1].into(),
}),
"D" => changes.push(FileChange::Delete {
path: parts[1].into(),
}),
"R" => changes.push(FileChange::Move {
from: parts[1].into(),
to: parts[2].into(),
}),
_ => {}
}
}
cmd.wait().unwrap();
changes
}
pub fn get_initial_commit_additions(path: &Path, commit_hash: &CommitHash) -> Vec<FileChange> {
let mut cmd = Command::new("git")
.arg("ls-tree")
.arg("--name-status")
.arg("-r") // the empty tree hash
.arg(&commit_hash.0)
.current_dir(path)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let stdout = cmd.stdout.take().expect("failed to open stdout");
let reader = BufReader::new(stdout);
let mut changes = Vec::new();
for line in reader.lines() {
let line = line.unwrap();
changes.push(FileChange::Add { path: line });
}
cmd.wait().unwrap();
changes
}