~dricottone/my-utils

ref: b26e1a336ce318b7258b95dd238eb7d0801088e8 my-utils/core/git-sparse.raku -rw-r--r-- 4.4 KiB
b26e1a33Dominic Ricottone git-sparse 1 year, 3 months ago
                                                                                
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
#!/usr/bin/env raku

#TODO: add standard program header
#TODO: add --help message

sub check-environment() {
    my $proc = shell <git --version>, :out, :err;
    if $proc.exitcode != 0 {
        #say $proc.err.slurp(:close);
        #TODO: gate this behind a --verbose feature
        say "$*PROGRAM-NAME: git is not installed";
        exit 1;
    }
}

class Arguments {
    has Str @.options;
    has Str @.positionals;

    #TODO: add .interactive-mode
}

sub parse() {
    if any(@*ARGS) eq '--' {
        my $sep = @*ARGS.first("--", :k);
        return Arguments.new(options => |@*ARGS[0..^$sep], positionals => |@*ARGS[$sep^..*]);
    }
    else {
        return Arguments.new(options => @*ARGS, positionals => ());
    }

    #TODO: set .interactive-mode here
}

sub check-arguments($args) {
    if $args.positionals.elems == 0 {
        say "$*PROGRAM-NAME: no targets specified";
        exit 1;
    }
}

sub git-rev-parse() {
    my $root;

    my $proc = run <git rev-parse --show-toplevel>, :out, :err;
    if $proc.exitcode != 0 {
        #say $proc.err.slurp(:close);
        #TODO: gate this behind a --verbose feature
    }
    else {
        $root = $proc.out.slurp(:close).trim.IO;
    }

    return $root;
}

sub interactive-clone() {
    my $root = $*CWD;

    my $resp = prompt "Create a new repository in $root? [y/N] ";
    given $resp {
        when "y"|"Y" { }
        when "n"|"N" { say "Quiting..."; exit 1 }
        default { say "Invalid response. Quiting..."; exit 1 }
    }

    my $uri = prompt "Repository to clone: ";
    without $uri {
        say "$*PROGRAM-NAME: no repository to clone";
        exit 1;
    }

    git-clone $uri;

    return $root;
}

sub git-clone($uri) {
    my @cmd = $uri, ".";
    @cmd.prepend(<git clone --filter=blob:none --no-checkout -->);

    # Purposefully not capturing STDOUT
    my $proc = run |@cmd, :err;
    if $proc.exitcode != 0 {
        say $proc.err.slurp(:close);
        #TODO: parse error message and print a cleaner version
        exit 1;
    }
}

#TODO: this should not be necessary long term, but no idea how to tell when
sub git-sparse-checkout-legacy() {
    my $proc = run <git sparse-checkout init --cone --sparse-index>, :out, :err;
    if $proc.exitcode != 0 {
        #say $proc.err.slurp(:close);
        #TODO: gate this behind a --verbose feature
        say "$*PROGRAM-NAME: failed to set sparse checkouts";
        exit 1;
    }
}

sub git-sparse-checkout($args) {
    git-sparse-checkout-legacy;

    my @cmd = $args.positionals;
    @cmd.prepend(<git sparse-checkout set -->);
    #@cmd.prepend(<git sparse-checkout set --cone --sparse-index -->);
    #TODO: swap these when legacy code not needed

    my $proc = run @cmd, :err;
    if $proc.exitcode != 0 {
        #say $proc.err.slurp(:close);
        #TODO: gate this behind a --verbose feature
        say "$*PROGRAM-NAME: failed to set sparse checkouts";
        exit 1;
    }
}

sub git-remote-show-origin() {
    my $proc = run <git remote show origin>, :out, :err;
    if $proc.exitcode != 0 {
        #say $proc.err.slurp(:close);
        #TODO: gate this behind a --verbose feature
        say "$*PROGRAM-NAME: failed to set identify default branch";
        exit 1;
    }

    my @lines = $proc.out.slurp(:close).lines;
    my @branch-line = @lines.grep(*.contains: "HEAD branch");
    if @branch-line.elems != 1 {
        say "$*PROGRAM-NAME: failed to set identify default branch";
        exit 1;
    }

    my $branch = @branch-line[0].trim;
    $branch ~~ s/.*\:\s*//;
    return $branch;
}

sub git-checkout($branch) {
    my @cmd = $branch;
    @cmd.prepend(<git checkout>);

    my $proc = run |@cmd, :out, :err;
    if $proc.exitcode != 0 {
        #say $proc.err.slurp(:close);
        #TODO: gate this behind a --verbose feature
        say "$*PROGRAM-NAME: failed to checkout branch $branch";
        exit 1;
    }
}

# setup
check-environment;
my $args = parse;
check-arguments $args;
my $init-mode = any($args.options) eq '-i' | '--init' | '--interactive';   # this is a junction; collapse with .so method
#TODO: move this into Arguments class as .interactive-mode

# git repository
my $root = git-rev-parse;
with $root {
    if $init-mode.so {
        say "$*PROGRAM-NAME: not re-initializing the repository in $root"
    }
}
else {
    unless $init-mode.so {
        say "$*PROGRAM-NAME: not in a git repository";
        exit 1;
    };

    $root = interactive-clone;
};

# sparse checkout
git-sparse-checkout($args);
git-checkout(git-remote-show-origin);