<commit_before>// Copyright (C) 2014 <NAME> and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

// +build !solaris,!windows,!noupgrade

package main

import (
	"archive/tar"
	"compress/gzip"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"runtime"
	"strings"

	"bitbucket.org/kardianos/osext"
)

var GoArchExtra string // "", "v5", "v6", "v7"

func upgrade() error {
	if runtime.GOOS == "windows" {
		return errors.New("Upgrade currently unsupported on Windows")
	}

	path, err := osext.Executable()
	if err != nil {
		return err
	}

	rel, err := currentRelease()
	if err != nil {
		return err
	}

	switch compareVersions(rel.Tag, Version) {
	case -1:
		l.Okf("Current version %s is newer than latest release %s. Not upgrading.", Version, rel.Tag)
		return nil
	case 0:
		l.Okf("Already running the latest version, %s. Not upgrading.", Version)
		return nil
	default:
		l.Infof("Attempting upgrade to %s...", rel.Tag)
	}

	expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", runtime.GOOS, runtime.GOARCH, GoArchExtra, rel.Tag)
	for _, asset := range rel.Assets {
		if strings.HasPrefix(asset.Name, expectedRelease) {
			if strings.HasSuffix(asset.Name, ".tar.gz") {
				l.Infof("Downloading %s...", asset.Name)
				fname, err := readTarGZ(asset.URL, filepath.Dir(path))
				if err != nil {
					return err
				}

				old := path + "." + Version
				err = os.Rename(path, old)
				if err != nil {
					return err
				}
				err = os.Rename(fname, path)
				if err != nil {
					return err
				}

				l.Okf("Upgraded %q to %s.", path, rel.Tag)
				l.Okf("Previous version saved in %q.", old)

				return nil
			}
		}
	}

	return fmt.Errorf("Found no asset for %q", expectedRelease)
}

func currentRelease() (githubRelease, error) {
	resp, err := http.Get("https://api.github.com/repos/calmh/syncthing/releases?per_page=10")
	if err != nil {
		return githubRelease{}, err
	}

	var rels []githubRelease
	json.NewDecoder(resp.Body).Decode(&rels)
	resp.Body.Close()

	if strings.Contains(Version, "-beta") {
		// We are a beta version. Use whatever we can find that is newer-or-equal than current.
		for _, rel := range rels {
			if compareVersions(rel.Tag, Version) >= 0 {
				return rel, nil
			}
		}
		// We found nothing. Return the latest release and let the next layer decide.
		return rels[0], nil
	} else {
		// We are a regular release. Only consider non-prerelease versions for upgrade.
		for _, rel := range rels {
			if !rel.Prerelease {
				return rel, nil
			}
		}
		return githubRelease{}, errors.New("no suitable release found")
	}
}
<commit_msg>Don't log a panic when there are no releases<commit_after>// Copyright (C) 2014 <NAME> and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

// +build !solaris,!windows,!noupgrade

package main

import (
	"archive/tar"
	"compress/gzip"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"runtime"
	"strings"

	"bitbucket.org/kardianos/osext"
)

var GoArchExtra string // "", "v5", "v6", "v7"

func upgrade() error {
	if runtime.GOOS == "windows" {
		return errors.New("Upgrade currently unsupported on Windows")
	}

	path, err := osext.Executable()
	if err != nil {
		return err
	}

	rel, err := currentRelease()
	if err != nil {
		return err
	}

	switch compareVersions(rel.Tag, Version) {
	case -1:
		l.Okf("Current version %s is newer than latest release %s. Not upgrading.", Version, rel.Tag)
		return nil
	case 0:
		l.Okf("Already running the latest version, %s. Not upgrading.", Version)
		return nil
	default:
		l.Infof("Attempting upgrade to %s...", rel.Tag)
	}

	expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", runtime.GOOS, runtime.GOARCH, GoArchExtra, rel.Tag)
	for _, asset := range rel.Assets {
		if strings.HasPrefix(asset.Name, expectedRelease) {
			if strings.HasSuffix(asset.Name, ".tar.gz") {
				l.Infof("Downloading %s...", asset.Name)
				fname, err := readTarGZ(asset.URL, filepath.Dir(path))
				if err != nil {
					return err
				}

				old := path + "." + Version
				err = os.Rename(path, old)
				if err != nil {
					return err
				}
				err = os.Rename(fname, path)
				if err != nil {
					return err
				}

				l.Okf("Upgraded %q to %s.", path, rel.Tag)
				l.Okf("Previous version saved in %q.", old)

				return nil
			}
		}
	}

	return fmt.Errorf("Found no asset for %q", expectedRelease)
}

func currentRelease() (githubRelease, error) {
	resp, err := http.Get("https://api.github.com/repos/calmh/syncthing/releases?per_page=10")
	if err != nil {
		return githubRelease{}, err
	}

	var rels []githubRelease
	json.NewDecoder(resp.Body).Decode(&rels)
	resp.Body.Close()

	if len(rels) == 0 {
		return githubRelease{}, errors.New("no releases found")
	}

	if strings.Contains(Version, "-beta") {
		// We are a beta version. Use whatever we can find that is newer-or-equal than current.
		for _, rel := range rels {
			if compareVersions(rel.Tag, Version) >= 0 {
				return rel, nil
			}
		}
		// We found nothing. Return the latest release and let the next layer decide.
		return rels[0], nil
	} else {
		// We are a regular release. Only consider non-prerelease versions for upgrade.
		for _, rel := range rels {
			if !rel.Prerelease {
				return rel, nil
			}
		}
		return githubRelease{}, errors.New("no suitable release found")
	}
}

{'fraction_non_alphanumeric': 0.14155005382131325, 'fraction_numerical': 0.004843918191603875, 'mean_word_length': 3.1729041916167664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5196790', 'n_tokens_mistral': 2187, 'n_tokens_neox': 1922, 'n_words': 582}