Skip to content
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

[feature] Added support for negative alt condition #522

Closed
wants to merge 9 commits into from

Conversation

AaronYoung5
Copy link
Contributor

What does this PR do?

Adds negative conditions to alt resolution. For instance, if local.class=test, file##!c.test is an invalid condition. This is useful for me where I want to disable a file on a specific computer.

What issues does this PR fix or reference?

I didn't make an issue, should I?

Previous Behavior

This is a new feature. Before, there was no way (that I know) to accomplish a negative condition.

New Behavior

See test case and comments below. Basically, it's possible to avoid using a specific file if a given condition is met if prefixed with !.

YADM_TEST=1 source {yadm}
score=0
local_class="testclass"
local_classes=("testclass")
# Negative condition with matching value should yield delta -1 and zero the score.
score_file "filename##!class.testclass" "dest"
echo "score: $score"
# Negative condition with non-matching value should yield delta 16.
score=0
score_file "filename##!class.badclass" "dest"
echo "score2: $score"

Have tests been written for this change?

Yes

Have these commits been signed with GnuPG?

No


Please review yadm's Contributing Guide for best practices.

@erijo
Copy link
Collaborator

erijo commented Feb 14, 2025

Hi and thanks for your contribution! This has been requested before (see #365) and I would like to get this merged. But first a few things that would be good if you could look into:

  • The logic could be simplified if we do something like this:
    local delta=
    case "$label" in
    ...
      a | arch)
        [[ "$value" = "$local_arch" ]] && delta=1 || delta=-1
        ;;
    ...
    esac
    if [ -z "$delta" ]; then
      score=0
      return
    fi
    (( negate )) && delta=(( -delta ))
    
  • I'm wondering if it would be better to use .e.g not- instead of !. So that it would be e.g. ##not-class.foobar. What do you think?
  • It would be good if you could add some documentation to yadm.1.

@AaronYoung5
Copy link
Contributor Author

AaronYoung5 commented Feb 14, 2025

Thanks for the quick response!

  1. Nice, much cleaner :)
  2. I agree that ! isn't great, it's hard to see it sometimes in long file names. For not-, would short hands like not-c still be supported? What are your thoughts on ~ instead, so ~class.foobar and ~c.foobar?
  3. I agree. I was also hoping to add exact scoring criteria in the docs (unless that's already there and I just missed it). My interpretation of the contributing doc is that all documentation changes are done in dev-pages, is that correct? Should that be a separate PR then?

@erijo
Copy link
Collaborator

erijo commented Feb 24, 2025

  1. Yes, my idea was that not-c would be handled in the same way as not-class. But I like your suggestion to use ~ so I think we should go with that instead.
  2. I don't think we should document the exact weights for each condition to allow changing the implementation if needed. As for dev-pages, that's the branch used for the next version of https://yadm.io. It should also be updated and if you would like to create a PR I'd be grateful. But we can land this PR first so that the details are set.

Another thing I started thinking about is if the number of negated conditions should contribute to the score in the same way as the number of conditions does today? I.e. should e.g. ~os.WSL,~os.Darwin really be preferred over e.g. class.correct? Or should the extra 1000 only be added for non-negated conditions? What's your opinion?

@AaronYoung5
Copy link
Contributor Author

Thanks for the comments, pushed updated the code.

  1. Sounds good. And happy to make the dev-pages PR once this one lands.

Another thing I started thinking about is if the number of negated conditions should contribute to the score in the same way as the number of conditions does today? I.e. should e.g. ~os.WSL,~os.Darwin really be preferred over e.g. class.correct? Or should the extra 1000 only be added for non-negated conditions? What's your opinion?

That's a good point, I don't really have a strong opinion. I think only adding 1000 for non-negated conditions makes sense. I can't really think of any examples that would be counter-intuitive. I've updated the logic/tests.

Also for the weight documentation, is there another way to determine which alt files will be used? Like getting started with yadm, it was unclear to me which file would be chosen when multiple files had the same number of valid conditions without running yadm alt. I eventually had to just look at the source. The best answer is probably just be more precise with the conditions, but just curious if there's an alternative to guessing and checking.

@erijo
Copy link
Collaborator

erijo commented Feb 27, 2025

Also for the weight documentation, is there another way to determine which alt files will be used? Like getting started with yadm, it was unclear to me which file would be chosen when multiple files had the same number of valid conditions without running yadm alt. I eventually had to just look at the source. The best answer is probably just be more precise with the conditions, but just curious if there's an alternative to guessing and checking.

I was going to say that we could document the relative weights, but when I checked it was already there. From yadm.1:

These are the supported attributes, in the order of the weighted precedence:

Could it be that this is enough, or could it be made clearer in some way?

@AaronYoung5
Copy link
Contributor Author

Could it be that this is enough, or could it be made clearer in some way?

Hm yeah, I think it's clear to me now. Maybe just didn't read through the docs enough the first time around.

And I updated the docs for negated conditions and fixed the mistake you pointed out.

erijo pushed a commit that referenced this pull request Mar 2, 2025
@erijo
Copy link
Collaborator

erijo commented Mar 2, 2025

This has now been merged to develop. Thanks for your contribution!

@erijo erijo closed this Mar 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants