pub fn publish_tip_by_id(&mut self, id: u64) -> Result<()> {

        // If this isn't enabled, we have no token. Do not try to upload.
        if !cfg!(feature = "support_tip_uploads") {
            return Err(ErrorKind::TipPublishUnsupported.into());
        } else {

            // Open the repo. If it fail's to open, download it.
            let remote_tips_path = get_remote_tips_path()?;
            let mut repo = match Repository::open(&remote_tips_path) {
                Ok(repo) => repo,
                Err(_) => {

                    // If it fails to open, it means either we don't have the .git folder, or we don't have a folder at all.
                    // In either case, recreate it and redownload the messages repo. No more steps are needed here.
                    // On windows, remove the read-only flags before doing anything else, or this will fail.
                    if cfg!(target_os = "windows") {
                        let path = remote_tips_path.to_string_lossy().to_string() + "\\*.*";
                        let _ = SystemCommand::new("attrib").arg("-r").arg(path).arg("/s").output();
                    }
                    let _ = std::fs::remove_dir_all(&remote_tips_path);
                    DirBuilder::new().recursive(true).create(&remote_tips_path)?;
                    match Repository::clone(TIPS_REPO, &remote_tips_path) {
                        Ok(_) => return Ok(()),
                        Err(_) => return Err(ErrorKind::MessagesUpdateError.into()),
                    }
                }
            };

            // Make sure our repo is updated before continuing. This can fail.
            let _ = Self::update_from_repo();

            // TODO: check if the pull request for this new branch already exists and is open. Merged ones will be ignored for the sake of allowing editing tips.

            // Create the new branch for the commit.
            let tip = self.get_local_tips_by_id(id).ok_or_else(|| Error::from(ErrorKind::LocalTipNotFound))?.clone();
            let mut current_branch_name = Reference::normalize_name(repo.head()?.name().unwrap(), ReferenceFormat::ALLOW_ONELEVEL)?.to_lowercase();
            let master_refname = format!("refs/heads/{}", MASTER);
            let id_refname = format!("refs/heads/{}", tip.get_ref_id());

            // Make sure we always start at master.
            if current_branch_name != master_refname {
                let _ = repo.set_head(&master_refname);
                current_branch_name = Reference::normalize_name(repo.head()?.name().unwrap(), ReferenceFormat::ALLOW_ONELEVEL)?.to_lowercase();
            }

            // Stash any changes unrelated with our commit.
            let signature = Signature::now(GH_USER, "-")?;
            let _stash_id = repo.stash_save(&signature, &format!("Stashed changes before update from branch {}", current_branch_name), Some(StashFlags::INCLUDE_UNTRACKED));

            // Make sure our remote is setup properly.
            Self::add_remote(&mut repo);

            // Switch to the new branch.
            if current_branch_name != id_refname {
                let fetch_head = repo.find_reference("FETCH_HEAD")?;
                let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
                repo.branch_from_annotated_commit(&tip.get_ref_id().to_string(), &fetch_commit, true)?;
                repo.set_head(&id_refname)?;
                current_branch_name = Reference::normalize_name(repo.head()?.name().unwrap(), ReferenceFormat::ALLOW_ONELEVEL)?.to_lowercase();
            }

            // Add the tip to the remote tips file.
            self.add_tip_to_remote_tips(tip, true)?;

            // Commit the change and push it.
            let mut index = repo.index()?;
            index.add_path(&PathBuf::from(TIPS_REMOTE_FILE))?;
            let push_result = self.commit_and_push_to_remote(&repo, &mut index, &signature, &id_refname);

            // Reset the repo to master after publish or failure.
            if current_branch_name != master_refname {
                let _stash_id = repo.stash_save(&signature, &format!("Stashed changes before moving back to branch {}", master_refname), Some(StashFlags::INCLUDE_UNTRACKED));
                let _ = repo.set_head(&master_refname);
                let _stash_id = repo.stash_save(&signature, &format!("Stashed changes before moving back to branch {}", master_refname), Some(StashFlags::INCLUDE_UNTRACKED));
            }

            push_result
        }
    }
/// This function tries to publish the local tip with the provided id, in the remote repo.
This function tries to publish the local tip with the provided id, in the remote repo.
This function tries to publish the local tip with the provided id in the remote repo .
pub fn publish_tip_by_id(&mut self, id: u64) -> Result<()> {
        // If this isn't enabled, we have no token. Do not try to upload.
        if !cfg!(feature = "support_tip_uploads") {
            return Err(ErrorKind::TipPublishUnsupported.into());
        } else {
            // Open the repo. If it fail's to open, download it.
            let remote_tips_path = get_remote_tips_path()?;
            let mut repo = match Repository::open(&remote_tips_path) {
                Ok(repo) => repo,
                Err(_) => {
                    // If it fails to open, it means either we don't have the .git folder, or we don't have a folder at all.
                    // In either case, recreate it and redownload the messages repo. No more steps are needed here.
                    // On windows, remove the read-only flags before doing anything else, or this will fail.
                    if cfg!(target_os = "windows") {
                        let path = remote_tips_path.to_string_lossy().to_string() + "\\*.*";
                        let _ = SystemCommand::new("attrib").arg("-r").arg(path).arg("/s").output();
                    }
                    let _ = std::fs::remove_dir_all(&remote_tips_path);
                    DirBuilder::new().recursive(true).create(&remote_tips_path)?;
                    match Repository::clone(TIPS_REPO, &remote_tips_path) {
                        Ok(_) => return Ok(()),
                        Err(_) => return Err(ErrorKind::MessagesUpdateError.into()),
                    }
                }
            };
            // Make sure our repo is updated before continuing. This can fail.
            let _ = Self::update_from_repo();
            // TODO: check if the pull request for this new branch already exists and is open. Merged ones will be ignored for the sake of allowing editing tips.
            // Create the new branch for the commit.
            let tip = self.get_local_tips_by_id(id).ok_or_else(|| Error::from(ErrorKind::LocalTipNotFound))?.clone();
            let mut current_branch_name = Reference::normalize_name(repo.head()?.name().unwrap(), ReferenceFormat::ALLOW_ONELEVEL)?.to_lowercase();
            let master_refname = format!("refs/heads/{}", MASTER);
            let id_refname = format!("refs/heads/{}", tip.get_ref_id());
            // Make sure we always start at master.
            if current_branch_name != master_refname {
                let _ = repo.set_head(&master_refname);
                current_branch_name = Reference::normalize_name(repo.head()?.name().unwrap(), ReferenceFormat::ALLOW_ONELEVEL)?.to_lowercase();
            }
            // Stash any changes unrelated with our commit.
            let signature = Signature::now(GH_USER, "-")?;
            let _stash_id = repo.stash_save(&signature, &format!("Stashed changes before update from branch {}", current_branch_name), Some(StashFlags::INCLUDE_UNTRACKED));
            // Make sure our remote is setup properly.
            Self::add_remote(&mut repo);
            // Switch to the new branch.
            if current_branch_name != id_refname {
                let fetch_head = repo.find_reference("FETCH_HEAD")?;
                let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
                repo.branch_from_annotated_commit(&tip.get_ref_id().to_string(), &fetch_commit, true)?;
                repo.set_head(&id_refname)?;
                current_branch_name = Reference::normalize_name(repo.head()?.name().unwrap(), ReferenceFormat::ALLOW_ONELEVEL)?.to_lowercase();
            }
            // Add the tip to the remote tips file.
            self.add_tip_to_remote_tips(tip, true)?;
            // Commit the change and push it.
            let mut index = repo.index()?;
            index.add_path(&PathBuf::from(TIPS_REMOTE_FILE))?;
            let push_result = self.commit_and_push_to_remote(&repo, &mut index, &signature, &id_refname);
            // Reset the repo to master after publish or failure.
            if current_branch_name != master_refname {
                let _stash_id = repo.stash_save(&signature, &format!("Stashed changes before moving back to branch {}", master_refname), Some(StashFlags::INCLUDE_UNTRACKED));
                let _ = repo.set_head(&master_refname);
                let _stash_id = repo.stash_save(&signature, &format!("Stashed changes before moving back to branch {}", master_refname), Some(StashFlags::INCLUDE_UNTRACKED));
            }
            push_result
        }
    }
pub fn publish_tip_by_id ( & mut self , id : u64 ) -> Result < ( ) > { // If this isn't enabled, we have no token. Do not try to upload. if ! cfg ! ( feature " " ) { return Err ( ErrorKind :: TipPublishUnsupported . into ( ) ) ; } else { // Open the repo. If it fail's to open, download it. let remote_tips_path = get_remote_tips_path ( ) ? ; let mut repo = match Repository :: open ( & remote_tips_path ) { Ok ( repo ) => repo , Err ( _ ) => { // If it fails to open, it means either we don't have the .git folder, or we don't have a folder at all. // In either case, recreate it and redownload the messages repo. No more steps are needed here. // On windows, remove the read-only flags before doing anything else, or this will fail. if cfg ! ( target_os " " ) { let path = remote_tips_path . to_string_lossy ( ) . to_string ( ) + " \\ " ; let _ = SystemCommand :: new ( " " ) . arg ( " " ) . arg ( path ) . arg ( " " ) . output ( ) ; } let _ = std :: fs :: remove_dir_all ( & remote_tips_path ) ; DirBuilder :: new ( ) . recursive ( true ) . create ( & remote_tips_path ) ? ; match Repository :: clone ( TIPS_REPO , & remote_tips_path ) { Ok ( _ ) => return Ok ( ( ) ) , Err ( _ ) => return Err ( ErrorKind :: MessagesUpdateError . into ( ) ) , } } } ; // Make sure our repo is updated before continuing. This can fail. let _ = Self :: update_from_repo ( ) ; // TODO: check if the pull request for this new branch already exists and is open. Merged ones will be ignored for the sake of allowing editing tips. // Create the new branch for the commit. let tip = self . get_local_tips_by_id ( id ) . ok_or_else ( | | Error :: from ( ErrorKind :: LocalTipNotFound ) ) ? . clone ( ) ; let mut current_branch_name = Reference :: normalize_name ( repo . head ( ) ? . name ( ) . unwrap ( ) , ReferenceFormat :: ALLOW_ONELEVEL ) ? . to_lowercase ( ) ; let master_refname = format ! ( " " MASTER ) ; let id_refname = format ! ( " " tip get_ref_id ( ) ) ; // Make sure we always start at master. if current_branch_name != master_refname { let _ = repo . set_head ( & master_refname ) ; current_branch_name = Reference :: normalize_name ( repo . head ( ) ? . name ( ) . unwrap ( ) , ReferenceFormat :: ALLOW_ONELEVEL ) ? . to_lowercase ( ) ; } // Stash any changes unrelated with our commit. let signature = Signature :: now ( GH_USER , " " ) ? ; let _stash_id = repo . stash_save ( & signature , & format ! ( " " current_branch_name ) , Some ( StashFlags :: INCLUDE_UNTRACKED ) ) ; // Make sure our remote is setup properly. Self :: add_remote ( & mut repo ) ; // Switch to the new branch. if current_branch_name != id_refname { let fetch_head = repo . find_reference ( " " ) ? ; let fetch_commit = repo . reference_to_annotated_commit ( & fetch_head ) ? ; repo . branch_from_annotated_commit ( & tip . get_ref_id ( ) . to_string ( ) , & fetch_commit , true ) ? ; repo . set_head ( & id_refname ) ? ; current_branch_name = Reference :: normalize_name ( repo . head ( ) ? . name ( ) . unwrap ( ) , ReferenceFormat :: ALLOW_ONELEVEL ) ? . to_lowercase ( ) ; } // Add the tip to the remote tips file. self . add_tip_to_remote_tips ( tip , true ) ? ; // Commit the change and push it. let mut index = repo . index ( ) ? ; index . add_path ( & PathBuf :: from ( TIPS_REMOTE_FILE ) ) ? ; let push_result = self . commit_and_push_to_remote ( & repo , & mut index , & signature , & id_refname ) ; // Reset the repo to master after publish or failure. if current_branch_name != master_refname { let _stash_id = repo . stash_save ( & signature , & format ! ( " " master_refname ) , Some ( StashFlags :: INCLUDE_UNTRACKED ) ) ; let _ = repo . set_head ( & master_refname ) ; let _stash_id = repo . stash_save ( & signature , & format ! ( " " master_refname ) , Some ( StashFlags :: INCLUDE_UNTRACKED ) ) ; } push_result } }
This function tries to publish the local tip with the provided id, in the remote repo.
This function tries to publish the local tip with the provided id in the remote repo .
&mut self
id
u64
&mut self
id
u64