File size: 5,076 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env ruby

# The "advanced" way to "sync"
# sync with your own fork of the loda-programs repo.
#
# Purpose
# Intended for LODA developers and people that edit LODA programs.
#
# If you are editing LODA programs yourself, and have your own fork of the "loda-programs" repo.
# This script never destroy any local modifications to "loda-programs".
# It commits your own changes, then merges, then pushes the changes to your own fork at github/bitbucket.
#
# The drawback is that it requires several steps of setting up.
# 1. Create a github/bitbucket account.
# 2. Create your own fork of [loda-programs](https://github.com/loda-lang/loda-programs)
# 3. Checkout your repository on your computer.
# 4. In your repository. Add a remote named `official_loda-programs` that tracks the [loda-programs](https://github.com/loda-lang/loda-programs) repository.
#
# The benefit of this approach is that it makes troubleshooting easier.
# being able to see diffs, history of all the programs, reason about what is going on.

require_relative 'config'

LODA_PROGRAMS_OEIS = Config.instance.loda_programs_oeis
REMOTE_NAME = "official_loda-programs"

def git_fetch
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git fetch #{REMOTE_NAME}"
        output = `#{command}`
        output.strip!
        if output.length > 0
            puts "output from command: #{command}"
            puts output
        end
    end
end

# Usage
# found = git_branch_contains("0007b13acb45e91185219114f7e43ecd7ce1a528")
def git_branch_contains(commit_id)
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git branch --contains #{commit_id}"
        output = `#{command}`
        output.strip!
        if output.length == 0
            return false
        end
        if output =~ /\bmain\b/
            return true
        end
        puts "output from command: #{command}"
        puts output
        raise "Expected either no output, or an output containing 'main'"
    end
    raise "could not chdir"
end

def git_latest_commit_id_of_remote_repo
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git ls-remote #{REMOTE_NAME} HEAD"
        output = `#{command}`
        output.strip!
        # extract from the string "7a68a9faec4f0dd7c31a70676dcac3fcb942ed75 HEAD"
        # this commit_id "7a68a9faec4f0dd7c31a70676dcac3fcb942ed75" 
        if output =~ /\b([0-9a-fA-F]{14,})\b/
            return $1
        end
        puts "error: no commit_id found in command output"
        puts "output from command: #{command}"
        puts output
        raise "no commit_id found in command output"
    end
    raise "could not chdir"
end

def determine_if_latest_commit_is_present_in_local_repo
    commit_id = git_latest_commit_id_of_remote_repo
    puts "remote repo latest commit_id: #{commit_id}" 
    is_present_in_local_repo = git_branch_contains(commit_id)
    puts "is_present_in_local_repo: #{is_present_in_local_repo}"
    is_present_in_local_repo
end

def git_add
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git add ."
        output = `#{command}`
        output.strip!
        if output.length > 0
            puts "output from command: #{command}"
            puts output
        end
    end
end

def determine_has_uncommited_changes
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git status --porcelain"
        output = `#{command}`
        output.strip!
        if output.length == 0
            # no output when there isn't anything changed
            return false
        end
        # outputs 1 or more lines with the files that have been changed
        return true
    end
    raise "could not chdir"
end

def git_commit
    if !determine_has_uncommited_changes
        puts "Nothing to commit"
        return
    end
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git commit -m \"Updated programs\""
        output = `#{command}`
        output.strip!
        if output.length > 0
            puts "output from command: #{command}"
            puts output
        end
    end
end

def git_merge
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git merge --strategy-option theirs --no-edit remotes/#{REMOTE_NAME}/main"
        output = `#{command}`
        output.strip!
        if output.length > 0
            puts "output from command: #{command}"
            puts output
        end
    end
end

def git_push
    Dir.chdir(LODA_PROGRAMS_OEIS) do
        command = "git push"
        output = `#{command}`
        output.strip!
        if output.length > 0
            puts "output from command: #{command}"
            puts output
        end
    end
end

def main
    git_fetch
    is_uptodate = determine_if_latest_commit_is_present_in_local_repo
    if is_uptodate
        puts "Already up to date. No changes to the official loda-programs repo."
        puts "status: nochange"
        return
    end
    puts "Obtaining the latest snapshot of the official loda-programs repo."
    git_add
    git_commit
    git_merge
    git_push
    puts "The local repo is now uptodate."
    puts "status: changed"
end

main