From c283492fa76e959a528981ae2e9a105a420c6b23 Mon Sep 17 00:00:00 2001 From: Olivier Date: Fri, 2 Dec 2022 20:37:30 +0000 Subject: [PATCH] Support pulling out the original base of the PR body to better preserve history --- src/api/pull_request.rs | 15 +++++++++++++++ src/graph.rs | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/api/pull_request.rs b/src/api/pull_request.rs index 95adfbc..d2979ef 100644 --- a/src/api/pull_request.rs +++ b/src/api/pull_request.rs @@ -1,3 +1,4 @@ +use regex::Regex; use serde::Deserialize; use serde::Serialize; use std::error::Error; @@ -70,6 +71,20 @@ impl PullRequest { &self.base.gitref } + /// Stored version of the base branch in the pull request body. + /// Needed for when the original branch is merged and so it vanishes. + pub fn base_branch_orig(&self) -> Option<&str> { + let regex = Regex::new("git-stack-base-branch:([^ ]+)").unwrap(); + self.body + .as_ref() + .map(|body| { + regex + .captures(&body) + .map(|re_match| re_match.get(1).unwrap().as_str()) + }) + .flatten() + } + pub fn url(&self) -> &str { &self.url } diff --git a/src/graph.rs b/src/graph.rs index 60f8827..ebc7887 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -16,7 +16,8 @@ pub fn build(prs: &[Rc]) -> Graph, usize> { for (i, pr) in prs.iter().enumerate() { let head_handle = handles[i]; - if let Some(&base_handle) = handles_by_head.get(pr.base()) { + let base_to_use = pr.base_branch_orig().unwrap_or(pr.base()); + if let Some(&base_handle) = handles_by_head.get(base_to_use) { tree.add_edge(*base_handle, head_handle, 1); } }