Repository navigation
feat: recover renamed-cwd sessions via cwd_remap + report missing paths #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System.Runtime.InteropServices; | ||
| using Capacitor.Cli.Core.Config; | ||
|
|
||
| namespace Capacitor.Cli; | ||
|
|
||
| /// <summary> | ||
| /// Applies user-configured path-prefix remaps to a transcript cwd so historic | ||
| /// sessions that recorded a since-renamed local repo path (e.g. | ||
| /// <c>~/dev/kapacitor-cli</c> after rename to <c>~/dev/kcap-cli</c>) can still | ||
| /// be matched to an on-disk git repository during <c>kcap import</c>. | ||
| /// | ||
| /// Matching rules: | ||
| /// - <c>~</c> or <c>~/</c> (or <c>~\</c> on Windows) at the start of | ||
| /// <c>from</c>/<c>to</c> is expanded to the current user's home directory | ||
| /// (transcript cwds are absolute). | ||
| /// - Path-boundary prefix: <c>cwd == from</c> or <c>cwd starts with from + sep</c> | ||
| /// where <c>sep</c> is either <c>/</c> or <c>\</c>; so <c>/dev/kapacitor</c> | ||
| /// does NOT spuriously match <c>/dev/kapacitor-cli</c>. | ||
| /// - Comparisons are case-insensitive on Windows, case-sensitive elsewhere — | ||
| /// matching the host filesystem's behavior. | ||
| /// - Longest <c>from</c> wins when multiple rules could apply. | ||
| /// - At most one remap is applied (no chaining), to keep behavior predictable. | ||
| /// </summary> | ||
| static class CwdRemapper { | ||
| static readonly StringComparison PathComparison = | ||
| RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | ||
| ? StringComparison.OrdinalIgnoreCase | ||
| : StringComparison.Ordinal; | ||
|
|
||
| public static string Apply(string cwd, IReadOnlyList<CwdRemap>? rules) => | ||
| Apply(cwd, rules, Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)); | ||
|
|
||
| // Internal seam so tests can pin the home directory and stay cross-platform. | ||
| internal static string Apply(string cwd, IReadOnlyList<CwdRemap>? rules, string home) => | ||
| Apply(cwd, rules, home, PathComparison); | ||
|
|
||
| // Internal seam for tests that need to pin the comparison policy (so the | ||
| // case-sensitivity behavior can be exercised regardless of host OS). | ||
| internal static string Apply(string cwd, IReadOnlyList<CwdRemap>? rules, string home, StringComparison comparison) { | ||
| if (rules is null or { Count: 0 } || string.IsNullOrEmpty(cwd)) return cwd; | ||
|
|
||
| CwdRemap? best = null; | ||
| string? bestFrom = null; | ||
| string? bestTo = null; | ||
| var bestLen = -1; | ||
|
|
||
| foreach (var rule in rules) { | ||
| if (string.IsNullOrEmpty(rule.From) || rule.To is null) continue; | ||
|
|
||
| var from = ExpandHome(rule.From, home); | ||
|
|
||
| if (!IsPrefixMatch(cwd, from, comparison)) continue; | ||
|
|
||
| if (from.Length > bestLen) { | ||
| best = rule; | ||
| bestFrom = from; | ||
| bestTo = ExpandHome(rule.To, home); | ||
| bestLen = from.Length; | ||
| } | ||
| } | ||
|
|
||
| if (best is null) return cwd; | ||
|
|
||
| // cwd == from → use 'to' verbatim; otherwise replace prefix + keep the | ||
| // tail (the leading separator of the tail was at position from.Length). | ||
| return cwd.Length == bestFrom!.Length | ||
| ? bestTo! | ||
| : bestTo + cwd[bestFrom.Length..]; | ||
| } | ||
|
|
||
| static string ExpandHome(string path, string home) { | ||
| if (path.Length == 0 || path[0] != '~') return path; | ||
| if (path.Length == 1) return home; // "~" | ||
| if (IsSeparator(path[1])) return home + path[1..]; // "~/foo" or "~\foo" | ||
| return path; // "~user" / "~foo" — leave alone | ||
| } | ||
|
|
||
| static bool IsPrefixMatch(string cwd, string from, StringComparison comparison) { | ||
| if (!cwd.StartsWith(from, comparison)) return false; | ||
| if (cwd.Length == from.Length) return true; | ||
| return IsSeparator(cwd[from.Length]); | ||
| } | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
|
|
||
| internal static bool IsSeparator(char c) => c == '/' || c == '\\'; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.