gled 2.28.4

gled is an application for creating animations and effects on artnet or dmx installations
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
use crate::{app::persistent_state::PersistentState, ui::action::UiAction};

use super::STORAGE_DIR;
use git2::{
    Cred, Error, ErrorCode, FetchOptions, PushOptions, Reference, RemoteCallbacks, Repository,
    Signature, build::RepoBuilder,
};
use mkdirp::mkdirp;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{
    io::Write,
    path::{Path, PathBuf},
};

pub static SSH_KEY_PASSPHRASE_ENTRY: Lazy<Option<keyring_core::Entry>> = Lazy::new(|| {
    keyring_core::Entry::new("gled2", "ssh_key_passphrase")
        .map_err(|err| UiAction::Error(format!("Could not get keyring entry: {err:?}")).enqueue())
        .ok()
});

pub struct Git {
    url: String,
    repository: Option<Repository>,
}

impl Git {
    pub fn open(url: String) -> Result<Self, Error> {
        let mut db = Self {
            url,
            repository: None,
        };
        db.init()?;
        Ok(db)
    }

    pub fn branches(&self) -> Result<Vec<String>, Error> {
        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;

        repository
            .branches(Some(git2::BranchType::Remote))?
            .map(|branch| {
                Ok(branch?
                    .0
                    .name()?
                    .and_then(|name| name.strip_prefix("origin/"))
                    .map(|name| name.to_owned()))
            })
            .filter_map(|name| name.transpose())
            .filter(|name| name.as_deref() != Ok("HEAD"))
            .collect()
    }

    pub fn current_branch(&self) -> Result<String, Error> {
        Ok(self.branch_shorthand())
    }

    pub fn switch_branch(&self, name: &str) -> Result<(), Error> {
        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;

        if repository
            .find_branch(name, git2::BranchType::Local)
            .is_err()
        {
            let remote_branch =
                repository.find_branch(&format!("origin/{name}"), git2::BranchType::Remote)?;
            let commit = remote_branch.into_reference().peel_to_commit()?;
            repository.branch(name, &commit, false)?;
        }

        let (object, reference) = repository.revparse_ext(name)?;
        repository.checkout_tree(&object, None)?;

        repository.set_head(
            reference
                .ok_or_else(|| Error::from_str("Reference is empty"))?
                .name()?,
        )?;

        Ok(())
    }

    fn push_options(&self) -> PushOptions<'static> {
        let mut push_options = PushOptions::new();
        push_options.remote_callbacks(self.remote_callbacks());
        push_options
    }

    fn fetch_options(&self) -> FetchOptions<'static> {
        let mut fetch_options = FetchOptions::new();
        fetch_options.remote_callbacks(self.remote_callbacks());
        fetch_options
    }

    fn remote_callbacks(&self) -> RemoteCallbacks<'static> {
        let mut callbacks = RemoteCallbacks::new();
        callbacks.credentials(move |_url, username_from_url, _allowed_types| {
            let username = username_from_url
                .map(|username| username.to_owned())
                .unwrap_or(whoami::username().expect("Could not get username"));

            let credentials = PersistentState::default().git_credentials();
            if let Some(private_key) = credentials
                .private_key_path()
                .and_then(|path| std::fs::read_to_string(path).ok())
            {
                Cred::ssh_key_from_memory(
                    &username,
                    None,
                    &private_key,
                    credentials.passphrase().as_deref(),
                )
            } else {
                Err(Error::from_str("No private key set"))
            }
        });
        callbacks
    }

    fn branch_reference(&'_ self) -> Option<Reference<'_>> {
        self.repository
            .as_ref()
            .and_then(|repository| repository.head().ok())
    }

    fn branch_name(&self) -> String {
        self.branch_reference()
            .and_then(|h| h.name().ok().map(|name| name.to_owned()))
            .unwrap_or_else(|| "main".to_owned())
    }

    fn branch_shorthand(&self) -> String {
        self.branch_reference()
            .and_then(|h| h.shorthand().ok().map(|name| name.to_owned()))
            .unwrap_or_else(|| "main".to_owned())
    }

    pub fn init(&mut self) -> Result<(), Error> {
        tracing::debug!("Loading repository at {}", STORAGE_DIR.display());
        match Repository::open(&*STORAGE_DIR) {
            Ok(repository) => {
                tracing::debug!("Repository loaded!");
                self.repository = Some(repository);
                Ok(())
            }
            Err(err) => {
                tracing::warn!("Could not open repository: {err:?}");
                self.clone()
            }
        }
    }

    pub fn pull(&mut self) -> Result<(), Error> {
        self.repository = Some(Repository::open(&*STORAGE_DIR)?);
        let branch = self.branch_shorthand();

        let mut fetch_options = self.fetch_options();
        let repository = self
            .repository
            .as_mut()
            .ok_or_else(|| Error::from_str("no repository set"))?;

        let remote = &mut repository.find_remote("origin")?;

        remote.fetch::<&str>(&[], Some(&mut fetch_options), None)?;

        let fetch_head = repository.find_reference("FETCH_HEAD")?;
        let fetch_commit = repository.reference_to_annotated_commit(&fetch_head)?;

        let analysis = repository.merge_analysis(&[&fetch_commit])?;
        if analysis.0.is_fast_forward() {
            let refname = format!("refs/heads/{branch}");

            match repository.find_reference(&refname) {
                Ok(mut r) => {
                    let name = match r.name() {
                        Ok(s) => s.to_string(),
                        Err(_) => String::from_utf8_lossy(r.name_bytes()).to_string(),
                    };
                    let msg = format!(
                        "Fast-Forward: Setting {} to id: {}",
                        name,
                        fetch_commit.id()
                    );
                    r.set_target(fetch_commit.id(), &msg)?;
                    repository.set_head(&name)?;
                    repository
                        .checkout_head(Some(git2::build::CheckoutBuilder::default().force()))?;
                }
                Err(_) => {
                    repository.reference(
                        &refname,
                        fetch_commit.id(),
                        true,
                        &format!("Setting {branch} to {}", fetch_commit.id()),
                    )?;
                    repository.set_head(&refname)?;
                    repository.checkout_head(Some(
                        git2::build::CheckoutBuilder::default()
                            .allow_conflicts(true)
                            .conflict_style_merge(true)
                            .force(),
                    ))?;
                }
            };
        } else if analysis.0.is_normal() {
            let head_commit = repository.reference_to_annotated_commit(&repository.head()?)?;

            let local_tree = repository.find_commit(head_commit.id())?.tree()?;
            let remote_tree = repository.find_commit(fetch_commit.id())?.tree()?;
            let ancestor = repository
                .find_commit(repository.merge_base(head_commit.id(), fetch_commit.id())?)?
                .tree()?;
            let mut idx = repository.merge_trees(&ancestor, &local_tree, &remote_tree, None)?;

            if idx.has_conflicts() {
                tracing::warn!("Merge conflicts detected");
                repository.checkout_index(Some(&mut idx), None)?;
                return Ok(());
            }
            let result_tree = repository.find_tree(idx.write_tree_to(repository)?)?;
            // now create the merge commit
            let msg = format!("Merge: {} into {}", fetch_commit.id(), head_commit.id());
            let sig = repository.signature()?;
            let local_commit = repository.find_commit(head_commit.id())?;
            let remote_commit = repository.find_commit(fetch_commit.id())?;
            // Do our merge commit and set current branch head to that commit.
            let _merge_commit = repository.commit(
                Some("HEAD"),
                &sig,
                &sig,
                &msg,
                &result_tree,
                &[&local_commit, &remote_commit],
            )?;
            repository.checkout_head(None)?;
        }

        Ok(())
    }

    fn clone(&mut self) -> Result<(), Error> {
        let _ = std::fs::remove_dir_all(&*STORAGE_DIR);

        self.repository = Some(
            RepoBuilder::new()
                .fetch_options(self.fetch_options())
                .clone(&self.url, &STORAGE_DIR)?,
        );

        Ok(())
    }

    pub fn add(&self, file: &Path) -> Result<(), Error> {
        tracing::info!("Adding file: {}", file.display());

        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;
        let file = file
            .strip_prefix(&*STORAGE_DIR)
            .map_err(|_err| Error::from_str("Could not make path relative"))?;

        let mut index = repository.index()?;
        index.add_path(file)?;
        index.write()?;

        Ok(())
    }

    fn delete(&self, file: &Path) -> Result<(), Error> {
        tracing::info!("Deleting file: {}", file.display());

        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;

        let root = repository
            .path()
            .parent()
            .ok_or_else(|| Error::from_str("No parent"))?;
        let file = file
            .strip_prefix(root)
            .map_err(|_err| Error::from_str("Could not make path relative"))?;

        let mut index = repository.index()?;
        index.remove_path(file)?;
        index.write()?;

        Ok(())
    }

    pub fn commit(&self, msg: &str) -> Result<(), Error> {
        tracing::info!("Committing..");

        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;

        let config = git2::Config::open_default()?;
        let name = config.get_string("user.name")?;
        let email = config.get_string("user.email")?;

        let mut index = repository.index()?;
        let tree_oid = index.write_tree()?;
        let tree = repository.find_tree(tree_oid)?;

        let parent_commit = match repository.revparse_single("HEAD") {
            Ok(obj) => Some(obj.into_commit().expect("HEAD must point to a commit")),
            // First commit so no parent commit
            Err(e) if e.code() == ErrorCode::NotFound => None,
            Err(e) => return Err(e),
        };

        let mut parents = Vec::new();
        if let Some(parent_commit) = parent_commit.as_ref() {
            parents.push(parent_commit);
        }

        let signature = Signature::now(&name, &email)?;
        repository.commit(
            Some("HEAD"),
            &signature,
            &signature,
            msg,
            &tree,
            &parents[..],
        )?;

        Ok(())
    }

    pub fn push(&self) -> Result<(), Error> {
        let name = self.branch_name();
        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;

        let remote = &mut repository.find_remote("origin")?;

        remote.push(&[name], Some(&mut self.push_options()))?;

        Ok(())
    }

    pub fn count_staged_files(&self) -> Result<usize, Error> {
        let repository = self
            .repository
            .as_ref()
            .ok_or(Error::from_str("No repository set"))?;

        Ok(repository.statuses(None)?.len())
    }

    pub fn delete_asset(&mut self, path: &Path) -> Result<(), String> {
        if let Err(err) = std::fs::remove_file(path) {
            return Err(format!("Could not remove file: {err:?}"));
        }
        if let Err(err) = self.delete(path) {
            return Err(format!("Could not delete file from git: {err:?}"));
        }

        Ok(())
    }

    pub fn write_asset(&mut self, file: &Path, json: String) -> Result<(), String> {
        let parent = file.parent().expect("Could not get parent of asset path");
        if let Err(err) = mkdirp(parent) {
            return Err(format!(
                "Could not create basedir of {}: {err:?}",
                parent.display()
            ));
        }
        {
            let mut file = match std::fs::File::create(file) {
                Ok(file) => file,
                Err(err) => {
                    return Err(format!("Could not open file {}: {err:?}", file.display()));
                }
            };
            if let Err(err) = file.write_all(json.as_bytes()) {
                return Err(format!("Could not write asset: {err:?}"));
            }
        }
        if let Err(err) = self.add(file) {
            return Err(format!("Could not add file to git: {err:?}"));
        }

        Ok(())
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct GitCredentials {
    private_key_path: Option<PathBuf>,
    use_passphrase: bool,
}

impl GitCredentials {
    pub fn private_key_path(&self) -> Option<&Path> {
        self.private_key_path.as_deref()
    }

    pub fn set_private_key_path(&mut self, private_key_path: PathBuf) {
        self.private_key_path = Some(private_key_path);
    }

    pub fn use_passphrase(&self) -> bool {
        self.use_passphrase
    }

    pub fn set_use_passphrase(&mut self, use_passphrase: bool) {
        self.use_passphrase = use_passphrase;
    }

    pub fn passphrase(&self) -> Option<String> {
        if !self.use_passphrase {
            return None;
        }
        SSH_KEY_PASSPHRASE_ENTRY
            .as_ref()
            .and_then(|entry| entry.get_password().ok())
    }

    pub fn set_passphrase(&mut self, passphrase: String) {
        if let Some(entry) = SSH_KEY_PASSPHRASE_ENTRY.as_ref()
            && let Err(err) = entry.set_password(&passphrase)
        {
            UiAction::Error(format!(
                "Could not set passphrase in system keychain: {err:?}"
            ))
            .enqueue();
        }
    }
}