git-branchless-hook 0.10.0

Supporting library for git-branchless
Documentation
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//! Callbacks for Git hooks.
//!
//! Git uses "hooks" to run user-defined scripts after certain events. We
//! extensively use these hooks to track user activity and e.g. decide if a
//! commit should be considered obsolete.
//!
//! The hooks are installed by the `branchless init` command. This module
//! contains the implementations for the hooks.

#![warn(missing_docs)]
#![warn(
    clippy::all,
    clippy::as_conversions,
    clippy::clone_on_ref_ptr,
    clippy::dbg_macro
)]
#![allow(clippy::too_many_arguments, clippy::blocks_in_if_conditions)]

use std::fmt::Write;
use std::fs::File;
use std::io::{stdin, BufRead};
use std::time::SystemTime;

use eyre::Context;
use git_branchless_invoke::CommandContext;
use git_branchless_opts::{HookArgs, HookSubcommand};
use itertools::Itertools;
use lib::core::dag::Dag;
use lib::core::repo_ext::RepoExt;
use lib::core::rewrite::rewrite_hooks::get_deferred_commits_path;
use lib::util::EyreExitOr;
use tracing::{error, instrument, warn};

use lib::core::eventlog::{should_ignore_ref_updates, Event, EventLogDb, EventReplayer};
use lib::core::formatting::{Glyphs, Pluralize};
use lib::core::gc::{gc, mark_commit_reachable};
use lib::git::{CategorizedReferenceName, MaybeZeroOid, NonZeroOid, ReferenceName, Repo};

use lib::core::effects::Effects;
pub use lib::core::rewrite::rewrite_hooks::{
    hook_drop_commit_if_empty, hook_post_rewrite, hook_register_extra_post_rewrite_hook,
    hook_skip_upstream_applied_commit,
};

/// Handle Git's `post-checkout` hook.
///
/// See the man-page for `githooks(5)`.
#[instrument]
fn hook_post_checkout(
    effects: &Effects,
    previous_head_oid: &str,
    current_head_oid: &str,
    is_branch_checkout: isize,
) -> eyre::Result<()> {
    if is_branch_checkout == 0 {
        return Ok(());
    }

    let now = SystemTime::now();
    let timestamp = now.duration_since(SystemTime::UNIX_EPOCH)?;
    writeln!(
        effects.get_output_stream(),
        "branchless: processing checkout"
    )?;

    let repo = Repo::from_current_dir()?;
    let conn = repo.get_db_conn()?;
    let event_log_db = EventLogDb::new(&conn)?;
    let event_tx_id = event_log_db.make_transaction_id(now, "hook-post-checkout")?;
    event_log_db.add_events(vec![Event::RefUpdateEvent {
        timestamp: timestamp.as_secs_f64(),
        event_tx_id,
        old_oid: previous_head_oid.parse()?,
        new_oid: {
            let oid: MaybeZeroOid = current_head_oid.parse()?;
            oid
        },
        ref_name: ReferenceName::from("HEAD"),
        message: None,
    }])?;
    Ok(())
}

fn hook_post_commit_common(effects: &Effects, hook_name: &str) -> eyre::Result<()> {
    let now = SystemTime::now();
    let glyphs = Glyphs::detect();
    let repo = Repo::from_current_dir()?;
    let conn = repo.get_db_conn()?;
    let event_log_db = EventLogDb::new(&conn)?;

    let commit_oid = match repo.get_head_info()?.oid {
        Some(commit_oid) => commit_oid,
        None => {
            // A strange situation, but technically possible.
            warn!(
                "`{}` hook called, but could not determine the OID of `HEAD`",
                hook_name
            );
            return Ok(());
        }
    };

    let commit = repo
        .find_commit_or_fail(commit_oid)
        .wrap_err("Looking up `HEAD` commit")?;
    mark_commit_reachable(&repo, commit_oid)
        .wrap_err("Marking commit as reachable for GC purposes")?;

    let event_replayer = EventReplayer::from_event_log_db(effects, &repo, &event_log_db)?;
    let event_cursor = event_replayer.make_default_cursor();
    let references_snapshot = repo.get_references_snapshot()?;
    Dag::open_and_sync(
        effects,
        &repo,
        &event_replayer,
        event_cursor,
        &references_snapshot,
    )?;

    if repo.is_rebase_underway()? {
        let deferred_commits_path = get_deferred_commits_path(&repo);
        let mut deferred_commits_file = File::options()
            .create(true)
            .append(true)
            .open(&deferred_commits_path)
            .with_context(|| {
                format!("Opening deferred commits file at {deferred_commits_path:?}")
            })?;

        use std::io::Write;
        writeln!(deferred_commits_file, "{commit_oid}")?;
        return Ok(());
    }

    let timestamp = commit.get_time().to_system_time()?;

    // Potentially lossy conversion. The semantics are to round to the nearest
    // possible float:
    // https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics.
    // We don't rely on the timestamp's correctness for anything, so this is
    // okay.
    let timestamp = timestamp
        .duration_since(SystemTime::UNIX_EPOCH)?
        .as_secs_f64();

    let event_tx_id = event_log_db.make_transaction_id(now, hook_name)?;
    event_log_db.add_events(vec![Event::CommitEvent {
        timestamp,
        event_tx_id,
        commit_oid: commit.get_oid(),
    }])?;
    writeln!(
        effects.get_output_stream(),
        "branchless: processed commit: {}",
        glyphs.render(commit.friendly_describe(&glyphs)?)?,
    )?;

    Ok(())
}

/// Handle Git's `post-commit` hook.
///
/// See the man-page for `githooks(5)`.
#[instrument]
fn hook_post_commit(effects: &Effects) -> eyre::Result<()> {
    hook_post_commit_common(effects, "post-commit")
}

/// Handle Git's `post-merge` hook. It seems that Git doesn't invoke the
/// `post-commit` hook after a merge commit, so we need to handle this case
/// explicitly with another hook.
///
/// See the man-page for `githooks(5)`.
#[instrument]
fn hook_post_merge(effects: &Effects, _is_squash_merge: isize) -> eyre::Result<()> {
    hook_post_commit_common(effects, "post-merge")
}

/// Handle Git's `post-applypatch` hook.
///
/// See the man-page for `githooks(5)`.
#[instrument]
fn hook_post_applypatch(effects: &Effects) -> eyre::Result<()> {
    hook_post_commit_common(effects, "post-applypatch")
}

mod reference_transaction {
    use std::collections::HashMap;
    use std::fs::File;
    use std::io::{BufRead, BufReader};
    use std::str::FromStr;

    use eyre::Context;
    use itertools::Itertools;
    use lazy_static::lazy_static;
    use tracing::{instrument, warn};

    use lib::git::{MaybeZeroOid, ReferenceName, Repo};

    /// A reference target parsed from a reference line.
    #[derive(Clone, Debug, PartialEq, Eq)]
    pub enum ReferenceTarget {
        /// A reference target that was in the transaction line as a normal commit hash.
        Direct { oid: MaybeZeroOid },

        /// A reference target that was in the transaction line as a symbolic
        /// reference: `ref:<refname>`.
        Symbolic { name: ReferenceName },
    }

    impl ReferenceTarget {
        /// Attempt to convert the provided reference target into an OID hash value.
        ///
        /// For [ReferenceTarget::Direct] types, this always succeeds, and just
        /// returns the wrapped OID.
        ///
        /// For [ReferenceTarget::Symbolic] types, this attempts to convert the
        /// provided symbolic ref name into an OID hash using the provided
        /// [Repo].
        #[instrument]
        pub fn as_oid(&self, repo: &Repo) -> eyre::Result<MaybeZeroOid> {
            match self {
                ReferenceTarget::Direct { oid } => Ok(*oid),
                ReferenceTarget::Symbolic { name } => Ok(repo.reference_name_to_oid(name)?),
            }
        }
    }

    impl FromStr for ReferenceTarget {
        type Err = eyre::ErrReport;

        /// Attempts to parse a string as a [ReferenceTarget].
        /// The string is expected to be one field from a reference transaction
        /// line, which can be one of the following values:
        ///
        /// ref:<symbolic ref name>
        /// <commit hash>
        fn from_str(value: &str) -> Result<Self, Self::Err> {
            match value.strip_prefix("ref:") {
                Some(refname) => Ok(ReferenceTarget::Symbolic {
                    name: ReferenceName::from(refname),
                }),
                None => Ok(ReferenceTarget::Direct {
                    oid: value.parse()?,
                }),
            }
        }
    }

    #[instrument]
    fn parse_packed_refs_line(line: &str) -> Option<(ReferenceName, MaybeZeroOid)> {
        if line.is_empty() {
            return None;
        }
        if line.starts_with('#') {
            // The leading `# pack-refs with:` pragma.
            return None;
        }
        if line.starts_with('^') {
            // A peeled ref
            // FIXME actually support peeled refs in packed-refs
            return None;
        }
        if !line.starts_with(|c: char| c.is_ascii_hexdigit()) {
            warn!(?line, "Unrecognized pack-refs line starting character");
            return None;
        }

        lazy_static! {
            static ref RE: regex::Regex = regex::Regex::new(r"^([^ ]+) (.+)$").unwrap();
        };
        match RE.captures(line) {
            None => {
                warn!(?line, "No regex match for pack-refs line");
                None
            }

            Some(captures) => {
                let oid = &captures[1];
                let oid = match MaybeZeroOid::from_str(oid) {
                    Ok(oid) => oid,
                    Err(err) => {
                        warn!(?oid, ?err, "Could not parse OID for pack-refs line");
                        return None;
                    }
                };

                let reference_name = &captures[2];
                let reference_name = ReferenceName::from(reference_name);

                Some((reference_name, oid))
            }
        }
    }

    #[cfg(test)]
    #[test]
    fn test_parse_packed_refs_line() {
        use super::*;

        let line = "1234567812345678123456781234567812345678 refs/foo/bar";
        let name = ReferenceName::from("refs/foo/bar");
        let oid = MaybeZeroOid::from_str("1234567812345678123456781234567812345678").unwrap();
        assert_eq!(parse_packed_refs_line(line), Some((name, oid)));
    }

    #[instrument]
    pub fn read_packed_refs_file(
        repo: &Repo,
    ) -> eyre::Result<HashMap<ReferenceName, MaybeZeroOid>> {
        let packed_refs_file_path = repo.get_packed_refs_path();
        let file = match File::open(packed_refs_file_path) {
            Ok(file) => file,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(HashMap::new()),
            Err(err) => return Err(err.into()),
        };

        let reader = BufReader::new(file);
        let mut result = HashMap::new();
        for line in reader.lines() {
            let line = line.wrap_err("Reading line from packed-refs")?;
            if line.is_empty() {
                continue;
            }
            if let Some((k, v)) = parse_packed_refs_line(&line) {
                result.insert(k, v);
            }
        }
        Ok(result)
    }

    #[derive(Debug, PartialEq, Eq)]
    pub struct ParsedReferenceTransactionLine {
        pub ref_name: ReferenceName,
        pub old_value: ReferenceTarget,
        pub new_value: ReferenceTarget,
    }

    #[instrument]
    pub fn parse_reference_transaction_line(
        line: &str,
    ) -> eyre::Result<ParsedReferenceTransactionLine> {
        let fields = line.split(' ').collect_vec();
        match fields.as_slice() {
            [old_value, new_value, ref_name] => Ok(ParsedReferenceTransactionLine {
                ref_name: ReferenceName::from(*ref_name),
                old_value: ReferenceTarget::from_str(old_value)?,
                new_value: ReferenceTarget::from_str(new_value)?,
            }),
            _ => {
                eyre::bail!(
                    "Unexpected number of fields in reference-transaction line: {:?}",
                    &line
                )
            }
        }
    }

    #[cfg(test)]
    #[test]
    fn test_parse_reference_transaction_line() -> eyre::Result<()> {
        use lib::{core::eventlog::should_ignore_ref_updates, testing::make_git};

        let git = make_git()?;
        git.init_repo()?;
        let oid1 = git.commit_file("README", 1)?;
        let oid2 = git.commit_file("README2", 2)?;

        let zero = "0000000000000000000000000000000000000000";
        let branch_ref = "refs/heads/mybranch";
        let orig_head_ref = "ORIG_HEAD";
        let master_tx_ref = "ref:refs/heads/master";
        let master_ref = "refs/heads/master";
        let head_ref = "HEAD";

        let line = format!("{oid1} {oid2} {branch_ref}");
        assert_eq!(
            parse_reference_transaction_line(&line)?,
            ParsedReferenceTransactionLine {
                old_value: ReferenceTarget::Direct { oid: oid1.into() },
                new_value: ReferenceTarget::Direct { oid: oid2.into() },
                ref_name: ReferenceName::from(branch_ref),
            }
        );

        let line = format!("{zero} {master_tx_ref} HEAD");
        assert_eq!(
            parse_reference_transaction_line(&line)?,
            ParsedReferenceTransactionLine {
                old_value: ReferenceTarget::Direct { oid: zero.parse()? },
                new_value: ReferenceTarget::Symbolic {
                    name: ReferenceName::from(master_ref)
                },
                ref_name: ReferenceName::from(head_ref)
            }
        );

        {
            let line = &format!("{oid1} {oid2} ORIG_HEAD");
            let parsed_line = parse_reference_transaction_line(line)?;
            assert_eq!(
                parsed_line,
                ParsedReferenceTransactionLine {
                    old_value: ReferenceTarget::Direct { oid: oid1.into() },
                    new_value: ReferenceTarget::Direct { oid: oid2.into() },
                    ref_name: ReferenceName::from(orig_head_ref)
                }
            );
            assert!(should_ignore_ref_updates(&parsed_line.ref_name));
        }

        let line = "there are not three fields here";
        assert!(parse_reference_transaction_line(line).is_err());

        Ok(())
    }

    fn reftarget_matches_refname(
        reftarget: &ReferenceTarget,
        refname: &ReferenceName,
        packed_references: &HashMap<ReferenceName, MaybeZeroOid>,
    ) -> bool {
        match reftarget {
            ReferenceTarget::Direct { oid } => packed_references.get(refname) == Some(oid),
            ReferenceTarget::Symbolic { name } => name == refname,
        }
    }
    /// As per the discussion at
    /// https://public-inbox.org/git/CAKjfCeBcuYC3OXRVtxxDGWRGOxC38Fb7CNuSh_dMmxpGVip_9Q@mail.gmail.com/,
    /// the OIDs passed to the reference transaction can't actually be trusted
    /// when dealing with packed references, so we need to look up their actual
    /// values on disk again. See https://git-scm.com/docs/git-pack-refs for
    /// details about packed references.
    ///
    /// Supposing we have a ref named `refs/heads/foo` pointing to an OID
    /// `abc123`, when references are packed, we'll first see a transaction like
    /// this:
    ///
    /// ```text
    /// 000000 abc123 refs/heads/foo
    /// ```
    ///
    /// And immediately afterwards see a transaction like this:
    ///
    /// ```text
    /// abc123 000000 refs/heads/foo
    /// ```
    ///
    /// If considered naively, this would suggest that the reference was created
    /// (even though it already exists!) and then deleted (even though it still
    /// exists!).
    #[instrument]
    pub fn fix_packed_reference_oid(
        repo: &Repo,
        packed_references: &HashMap<ReferenceName, MaybeZeroOid>,
        parsed_line: ParsedReferenceTransactionLine,
    ) -> ParsedReferenceTransactionLine {
        match parsed_line {
            ParsedReferenceTransactionLine {
                ref_name,
                old_value:
                    ReferenceTarget::Direct {
                        oid: MaybeZeroOid::Zero,
                    },
                new_value,
            } if reftarget_matches_refname(&new_value, &ref_name, packed_references) => {
                // The reference claims to have been created, but it appears to
                // already be in the `packed-refs` file with that OID. Most
                // likely it was being packed in this operation.
                ParsedReferenceTransactionLine {
                    ref_name,
                    old_value: new_value.clone(),
                    new_value: new_value.clone(),
                }
            }

            ParsedReferenceTransactionLine {
                ref_name,
                old_value,
                new_value:
                    ReferenceTarget::Direct {
                        oid: MaybeZeroOid::Zero,
                    },
            } if reftarget_matches_refname(&old_value, &ref_name, packed_references) => {
                // The reference claims to have been deleted, but it's still in
                // the `packed-refs` file with that OID. Most likely it was
                // being packed in this operation.
                ParsedReferenceTransactionLine {
                    ref_name,
                    old_value: old_value.clone(),
                    new_value: old_value.clone(),
                }
            }

            other => other,
        }
    }
}

/// Handle Git's `reference-transaction` hook.
///
/// See the man-page for `githooks(5)`.
#[instrument]
fn hook_reference_transaction(effects: &Effects, transaction_state: &str) -> eyre::Result<()> {
    use reference_transaction::{
        fix_packed_reference_oid, parse_reference_transaction_line, read_packed_refs_file,
        ParsedReferenceTransactionLine,
    };

    if transaction_state != "committed" {
        return Ok(());
    }
    let now = SystemTime::now();

    let repo = Repo::from_current_dir()?;
    let conn = repo.get_db_conn()?;
    let event_log_db = EventLogDb::new(&conn)?;
    let event_tx_id = event_log_db.make_transaction_id(now, "reference-transaction")?;

    let packed_references = read_packed_refs_file(&repo)?;

    let parsed_lines: Vec<ParsedReferenceTransactionLine> = stdin()
        .lock()
        .split(b'\n')
        .filter_map(|line| {
            let line = match line {
                Ok(line) => line,
                Err(_) => return None,
            };
            let line = match std::str::from_utf8(&line) {
                Ok(line) => line,
                Err(err) => {
                    error!(?err, ?line, "Could not parse reference-transaction line");
                    return None;
                }
            };
            match parse_reference_transaction_line(line) {
                Ok(line) => Some(line),
                Err(err) => {
                    error!(?err, ?line, "Could not parse reference-transaction-line");
                    None
                }
            }
        })
        .filter(
            |ParsedReferenceTransactionLine {
                 ref_name,
                 old_value: _,
                 new_value: _,
             }| !should_ignore_ref_updates(ref_name),
        )
        .map(|parsed_line| fix_packed_reference_oid(&repo, &packed_references, parsed_line))
        .collect();
    if parsed_lines.is_empty() {
        return Ok(());
    }

    let num_reference_updates = Pluralize {
        determiner: None,
        amount: parsed_lines.len(),
        unit: ("update", "updates"),
    };
    writeln!(
        effects.get_output_stream(),
        "branchless: processing {}: {}",
        num_reference_updates,
        parsed_lines
            .iter()
            .map(
                |ParsedReferenceTransactionLine {
                     ref_name,
                     old_value: _,
                     new_value: _,
                 }| { CategorizedReferenceName::new(ref_name).friendly_describe() }
            )
            .map(|description| format!("{}", console::style(description).green()))
            .sorted()
            .collect::<Vec<_>>()
            .join(", ")
    )?;

    let timestamp = now
        .duration_since(SystemTime::UNIX_EPOCH)
        .wrap_err("Calculating timestamp")?
        .as_secs_f64();
    let events: eyre::Result<Vec<Event>> = parsed_lines
        .into_iter()
        .map(
            |ParsedReferenceTransactionLine {
                 ref_name,
                 old_value,
                 new_value,
             }| {
                let old_oid = old_value.as_oid(&repo)?;
                let new_oid = new_value.as_oid(&repo)?;
                Ok(Event::RefUpdateEvent {
                    timestamp,
                    event_tx_id,
                    ref_name,
                    old_oid,
                    new_oid,
                    message: None,
                })
            },
        )
        .collect();
    event_log_db.add_events(events?)?;

    Ok(())
}

/// `hook` subcommand.
#[instrument]
pub fn command_main(ctx: CommandContext, args: HookArgs) -> EyreExitOr<()> {
    let CommandContext {
        effects,
        git_run_info,
    } = ctx;
    let HookArgs { subcommand } = args;

    match subcommand {
        HookSubcommand::DetectEmptyCommit { old_commit_oid } => {
            let old_commit_oid: NonZeroOid = old_commit_oid.parse()?;
            hook_drop_commit_if_empty(&effects, old_commit_oid)?;
        }

        HookSubcommand::PreAutoGc => {
            gc(&effects)?;
        }

        HookSubcommand::PostApplypatch => {
            hook_post_applypatch(&effects)?;
        }

        HookSubcommand::PostCheckout {
            previous_commit,
            current_commit,
            is_branch_checkout,
        } => {
            hook_post_checkout(
                &effects,
                &previous_commit,
                &current_commit,
                is_branch_checkout,
            )?;
        }

        HookSubcommand::PostCommit => {
            hook_post_commit(&effects)?;
        }

        HookSubcommand::PostMerge { is_squash_merge } => {
            hook_post_merge(&effects, is_squash_merge)?;
        }

        HookSubcommand::PostRewrite { rewrite_type } => {
            hook_post_rewrite(&effects, &git_run_info, &rewrite_type)?;
        }

        HookSubcommand::ReferenceTransaction { transaction_state } => {
            hook_reference_transaction(&effects, &transaction_state)?;
        }

        HookSubcommand::RegisterExtraPostRewriteHook => {
            hook_register_extra_post_rewrite_hook()?;
        }

        HookSubcommand::SkipUpstreamAppliedCommit { commit_oid } => {
            let commit_oid: NonZeroOid = commit_oid.parse()?;
            hook_skip_upstream_applied_commit(&effects, commit_oid)?;
        }
    }

    Ok(Ok(()))
}