Exemplo n.º 1
0
// stashFiles gzips all commited files and stashes the compressed versions
// in a unique folder based on the commit time
func stashFiles(branch string, commit *meta.Commit) error {
	err := stash.Init(branch, commit.Timestamp.String())
	if err != nil {
		return err
	}
	for _, file := range commit.File {
		err = stash.Stash(file.Title)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 2
0
// Revert reverts the state of the working directory to that
// of n commits ago.
func Revert(n int) {
	repo, err := meta.Open()
	if err != nil {
		fmt.Println(err)
		return
	}
	branch, _ := repo.Find(repo.Current)
	if len(branch.Commit) < n {
		fmt.Printf("Only %d commits exist!\n", len(branch.Commit))
		return
	}
	var commit *meta.Commit
	if len(branch.Commit) == n {
		commit = nil
	} else {
		commit = &branch.Commit[(len(branch.Commit)-1)-n]
	}
	files, err := status.CompareStatusToCommit(commit)
	if err != nil {
		fmt.Println(err)
		return
	}
	repoDir, err := dirutils.OpenRepo()
	if err != nil {
		fmt.Println(err)
		return
	}
	// Might have to restore some files if not at initial commit
	if commit != nil {
		err = stash.Init(branch.Title, commit.Timestamp.String())
		if err != nil {
			fmt.Println(err)
			return
		}
	}
	for file, stat := range files {
		if stat == status.NEW {
			fileWithPath := repoDir.Name() + "/" + file
			err = os.Remove(fileWithPath)
			if err != nil {
				fmt.Println(err)
				fmt.Println("Repo may be corrupt.")
				return
			}
			err = dirutils.DeleteIfEmpty(fileWithPath[:strings.LastIndex(fileWithPath, "/")])
			if err != nil {
				fmt.Println(err)
				fmt.Println("Repo may be corrupt.")
				return
			}
		} else if stat == status.REMOVED || stat == status.MODIFIED {
			err = stash.Restore(file)
			if err != nil {
				fmt.Println(err)
				fmt.Println("Repo may be corrupt.")
				return
			}
		}
	}
	err = cleanup(branch, n)
	if err != nil {
		fmt.Println(err)
		fmt.Println("Repo may be corrupt.")
		return
	}
	err = repo.Write()
	if err != nil {
		fmt.Println(err)
		fmt.Println("Repo may be corrupt.")
		return
	}
	if len(branch.Commit) == 0 {
		fmt.Println("Repo reset to intial state")
	} else {
		fmt.Println("Repo reset to commit:")
		fmt.Println("\x1b[32;1m" + branch.Commit[len(branch.Commit)-1].Title + "\x1b[0m")
		fmt.Printf("%v\n", branch.Commit[len(branch.Commit)-1].Timestamp)
	}
}